WCF如何启用元数据?

Mar*_*ijn 12 c# wcf metadata

我试图让我的svc文件在IIS下工作.在我的项目中,当我按F5时,我得到了svc工作.所以我知道一切都好,对吧?除了IIS.

我正在使用Windows XP Pro计算机,在IIS中我添加了一个虚拟目录.

这是我的代码:IcarePlanActions(项目:A)

namespace WcfServiceLibrary
{
    [ServiceContract]
    public interface ICarePlanActions
    {
        [OperationContract]
        List<string> GetAllClients();
    }
}
Run Code Online (Sandbox Code Playgroud)

客户:(项目:A)

namespace WcfServiceLibrary
{
    public class Client : ICarePlanActions
    {
        public List<string> GetAllClients()
        {
            List<string> clients = new List<string>();
            clients.Add("Hendrik de Jong");
            clients.Add("Miep de Berg");
            clients.Add("Jaap Jongeneel");
            clients.Add("Joop Prakman");
            clients.Add("Pieter Schaakman");

            return clients;

        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Web.config(项目:B)

  <configuration>
    <system.serviceModel>
      <services>
        <service behaviorConfiguration="CarePlanService.Service1Behavior"
          name="WcfServiceLibrary.Client">
          <endpoint address="" binding="wsHttpBinding" contract="WcfServiceLibrary.ICarePlanActions">
            <identity>
              <dns value="localhost" />
            </identity>
          </endpoint>
          <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
        </service>
      </services>
      <behaviors>
        <serviceBehaviors>
          <behavior name="CarePlanService.Service1Behavior">
            <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
            <serviceMetadata httpGetEnabled="true"/>
            <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
            <serviceDebug includeExceptionDetailInFaults="false"/>
          </behavior>
        </serviceBehaviors>
      </behaviors>
    </system.serviceModel>
  </configuration>
Run Code Online (Sandbox Code Playgroud)

CarePlan.svc

<%@ ServiceHost Language="C#" Debug="true" Service="WcfServiceLibrary.Client" %>
Run Code Online (Sandbox Code Playgroud)

当我使用wfctestclient运行此服务(在IIS上)时,我收到此错误

错误:无法从http://localhost/CarePlanService/CarePlan.svc获取元数据 如果这是您有权访问的Windows(R)Communication Foundation服务,请检查您是否已在指定地址启用元数据发布.

我究竟做错了什么?

我没有让IIS下的服务工作.首先,我手动创建一个虚拟目录,并指向svc所在的directiry.这没用.我不知道为什么.

然后我去了Visual Studio并更改了服务器设置(右键鼠标在项目,属性,选项卡Web上,单击Use local IIS Web Server并单击Create Virtual Directory.当我这样做时,它在IIS下使用上面的代码工作.

小智 7

首先,删除客户端中的[DataContract]属性.

然后,重新编译并确保您的虚拟目录的/ bin目录中包含WcfServiceLibrary.dll.

您的配置应使用此端点进行metadataexchange:

<endpoint address="mex" 
    binding="mexHttpBinding" 
    contract="IMetadataExchange"/>
Run Code Online (Sandbox Code Playgroud)


Mar*_*ijn 5

我没有让IIS下的服务工作.首先,我手动创建一个虚拟目录,并指向svc所在的directiry.这没用.我不知道为什么.

然后我去了Visual Studio并更改了服务器设置(右键鼠标在项目,属性,选项卡Web上,单击使用本地IIS Web服务器,然后单击创建虚拟目录.当我这样做时,它在IIS下使用上面的代码工作.