WCF添加服务失败.服务元数据可能无法访问.确保您的服务正在运行并公开元数据

本文关键字:服务 元数据 运行 访问 失败 添加 WCF 确保 | 更新日期: 2023-09-27 18:07: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>

我希望有人能帮助我这个,或者一个更好的例子的链接。

WCF添加服务失败.服务元数据可能无法访问.确保您的服务正在运行并公开元数据

您的配置错误(服务名称是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。像这样添加配置文件,它工作得很好:

 <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" />