如何为WCF服务库创建wsdl文件?

时间:2012-06-04 21:07:18

标签: wcf wcf-binding

我有一个WCF服务库项目。我试图通过在Visual Studio中运行WCF测试客户端来生成一个wsdl文件(推送F5)。它启动了WCF测试客户端,但它说“无法添加服务。可能无法访问服务元数据。请确保您的服务正在运行并公开元数据。”。它还给出了以下错误消息。

c:\ Users \ xxx \ AppData \ Local \ Temp \ Test Client Projects \ 10.0 \ 354421b1-b65e-45fc-8d98-ac87254a5903 \ Client.cs(911,26):error CS0644:'System.ComponentModel.PropertyChangedEventHandler '不能从特殊类'System.MulticastDelegate'

派生出来

我添加了servive行为以公开Metadata,如下所示。我不知道我还缺少什么才能生成wsdl文件。谢谢你的帮助!

<services>
  <service name="CU.Customer" behaviorConfiguration="Metadata">
    <endpoint address="" binding="wsHttpBinding" contract="CU.ICustomer">
      <identity>
        <dns value="localhost"/>
      </identity>
    </endpoint>
    <endpoint name="mex" address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
    <host>
      <baseAddresses>
        <add baseAddress="http://localhost:8732/Design_Time_Addresses/CustomerService/Service1/"/>
      </baseAddresses>
    </host>
  </service>
</services>
<behaviors>
  <serviceBehaviors>
    <behavior name="Metadata">
      <!-- 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>

1 个答案:

答案 0 :(得分:2)

您的元数据绑定没有任何问题,但您的服务中存在编译器错误。这阻止了WCF构建服务类,这是暴露元数据端点所必需的。

首先修复此错误:

  

'System.ComponentModel.PropertyChangedEventHandler'无法从特殊类'System.MulticastDelegate'派生

当WCF尝试将服务契约本地编译为可用于访问服务的类时,会发生错误(请注意它位于临时文件中)。这意味着您正在遇到C#中合法的内容,但在WCF中不合法。最有可能的是,鉴于错误,您有一个实现INotifyPropertyChanged的类被用作操作合同中的数据联系人。

请注意,通过WCF通道序列化的每个类都是数据协定。通常,您会使用DataContract和每个具有DataMember属性的字段来装饰您的课程,这会指示序列化工具如何处理您的课程。但是如果你不这样做,并且你将你的类作为参数或在OperationContract中返回值,那么WCF只是假装,就像你将这些属性放在每个上一样你班上的公共领域。

在这种情况下,我的猜测是你有一个课程,你正在进入或退出服务电话,它有:

 public event PropertyChangedEventHandler PropertyChanged;

这是一个公共字段,因此除非您告诉WCF,否则它将尝试将其序列化为隐式数据协定的一部分。但是有些类型无法以这种方式序列化,MulticastDelegate就是其中之一。

要修复此问题,始终明确地使用DataContractDataMember来装饰用于服务的类型。将这些属性放在任何类上是完全安全的 - 如果你从不尝试序列化它,那么属性就会被忽略。

相关问题