【问题标题】:WCF Failed to add a service. Service metadata may not be accessible. Make sure your service is running and exposing metadataWCF 未能添加服务。服务元数据可能无法访问。确保您的服务正在运行并公开元数据
【发布时间】:2014-07-30 08:14:39
【问题描述】:

我有一个打算将文件上传到服务器的 WebService,但是当我想从我的 VS2010 运行它时,标题中不断出现错误。

我已经搜索过这个网站,但解决方案对我没有帮助,除非我做错了什么。

这是我得到示例的网站:Example

这是我的界面

namespace FUWcf
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
public class FileUploadService : IFileUploadService
{
    public bool UploadFileData(FileData fileData)
    {
        bool result = false;
        try
        {
            //Set the location where you want to save your file
            string FilePath = Path.Combine(ConfigurationManager.AppSettings["Path"], fileData.FileName);

            //If fileposition sent as 0 then create an empty file
            if (fileData.FilePosition == 0)
            {
                File.Create(FilePath).Close();
            }

            //Open the created file to write the buffer data starting at the given file position
            using (FileStream fileStream = new FileStream(FilePath, FileMode.Open, FileAccess.ReadWrite, FileShare.Read))
            {
                fileStream.Seek(fileData.FilePosition, SeekOrigin.Begin);
                fileStream.Write(fileData.BufferData, 0, fileData.BufferData.Length);
            }
        }
        catch (Exception ex)
        {
            ErrorDetails ed = new ErrorDetails();
            ed.ErrorCode = 1001;
            ed.ErrorMessage = ex.Message;
            throw new FaultException<ErrorDetails>(ed);
        }

        return result;
    }
}
}

这里是服务:

namespace FUWcf
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
[ServiceContract]
public interface IFileUploadService
{

    [OperationContract]
    [FaultContract(typeof(ErrorDetails))]
    bool UploadFileData(FileData fileData);
}


[DataContract]
public class FileData
{
    [DataMember]
    public string FileName { get; set; }

    [DataMember]
    public byte[] BufferData { get; set; }

    [DataMember]
    public int FilePosition { get; set; }
}

[DataContract]
public class ErrorDetails
{
    [DataMember]
    public int ErrorCode { get; set; }

    [DataMember]
    public string ErrorMessage { get; set; }
}
}

这是我的 web.config

<?xml version="1.0"?>
<configuration>

  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <appSettings>
    <add key="Path" value="C:\Users\c.asacha\Documents\Proyectos\Generales\FUWcf\FUWcf\temp\"/>
  </appSettings>
  <system.serviceModel>
    <bindings>
      <wsHttpBinding>
        <binding name="WSHBBinding" />
      </wsHttpBinding>
    </bindings>
    <services>
      <service behaviorConfiguration="fus" name="FUWcd.FileUploadService">
        <endpoint address=""
          binding="wsHttpBinding" bindingConfiguration="WSHBBinding" name="FileUploadService"
          contract="FUWcf.IFileUploadService" />

        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="fus">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="false" multipleSiteBindingsEnabled="false" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>

</configuration>

我希望有人可以帮助我解决这个问题,或者一个更好的例子的链接。

提前致谢。

【问题讨论】:

  • 您可以在浏览器中导航到您的服务吗?

标签: c# web-services wcf file-upload


【解决方案1】:

你的配置错误(服务名是name="FUWcd.FileUploadService"应该是name="FUWcf.FileUploadService"

无论如何- 创建网站并添加这样的 Web 服务(您不需要在 Web 配置中使用 Mex 端点,因为它已经打开,URL 将成为服务名称 http://{localServerName}:{port}/FileUploadService.svc):

 public class FileUploadService : IFileUploadService
{
    public bool UploadFileData(FileData fileData)
    {
        bool result = false;
        try
        {
            //Set the location where you want to save your file
            string FilePath = Path.Combine(ConfigurationManager.AppSettings["Path"], fileData.FileName);

            //If fileposition sent as 0 then create an empty file
            if (fileData.FilePosition == 0)
            {
                File.Create(FilePath).Close();
            }

            //Open the created file to write the buffer data starting at the given file position
            using (FileStream fileStream = new FileStream(FilePath, FileMode.Open, FileAccess.ReadWrite, FileShare.Read))
            {
                fileStream.Seek(fileData.FilePosition, SeekOrigin.Begin);
                fileStream.Write(fileData.BufferData, 0, fileData.BufferData.Length);
            }
        }
        catch (Exception ex)
        {
            ErrorDetails ed = new ErrorDetails();
            ed.ErrorCode = 1001;
            ed.ErrorMessage = ex.Message;
            throw new FaultException<ErrorDetails>(ed);
        }

        return result;
    }
}

数据合同也一样。 然后在 Web.Config 文件中像这样添加它,它工作正常:

 <system.serviceModel>
<services>
  <service name="WebApplication1.FileUploadService">
    <endpoint address="fileService" binding="wsHttpBinding" bindingConfiguration=""
      contract="WebApplication1.IFileUploadService" />
  </service>
</services>
<behaviors>
  <serviceBehaviors>
    <behavior name="">
      <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="false" />
    </behavior>
  </serviceBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"
  multipleSiteBindingsEnabled="true" />

【讨论】:

  • 不错的选择,总是很难发现这样的错字。
  • 谢谢!这解决了我的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-11-07
  • 1970-01-01
  • 2011-12-14
  • 1970-01-01
  • 1970-01-01
  • 2015-09-09
  • 1970-01-01
相关资源
最近更新 更多