为了显示我的问题,我创建了一个简单的 WCF 服务,其操作使用带有[MessageContract]
属性的消息类。此消息包含MyHeader
属性并使用属性进行注释[MessageHeader]
。
[MessageContract] public class HeaderedMessage
{
[MessageHeader] public string MyHeader { get; set; }
}
[ServiceContract] public interface IService
{
[OperationContract] void GetDataUsingDataContract(HeaderedMessage headered);
}
public class Service : IService
{
public void GetDataUsingDataContract(HeaderedMessage headered) { }
}
然后我尝试使用以下命令为 Silverlight 4(或 5)生成代理类SlSvcUtil.exe
:
slsvcutil.exe http://localhost:8732/Design_Time_Addresses/MessageHeaderedService/?wsdl
没有任何警告,消息头在生成的类中被完全忽略。因此,HeaderedMessage
根本不包含任何属性。
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
[System.ServiceModel.MessageContractAttribute(WrapperName="HeaderedMessage", WrapperNamespace="http://tempuri.org/", IsWrapped=true)]
public partial class HeaderedMessage
{
public HeaderedMessage()
{
}
}
我没有在 MSDN 或通过 Google 搜索找到任何关于这种奇怪行为的信息。还有其他人有这个问题吗?
我还尝试使用可移植类库扩展直接通过Silverlight 应用程序使用IService
合同。ChannelFactory
由于System.ServiceModel
Silverlight 的程序集不包含MessageHeaderAttribute
无法编译的类。
更新:缺少生成的 WSDL 和 WCFSystem.ServiceModel
部分:
<wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:wsx="http://schemas.xmlsoap.org/ws/2004/09/mex" xmlns:wsa10="http://www.w3.org/2005/08/addressing" xmlns:tns="http://tempuri.org/" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:wsap="http://schemas.xmlsoap.org/ws/2004/08/addressing/policy" xmlns:msc="http://schemas.microsoft.com/ws/2005/12/wsdl/contract" xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing" xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata" xmlns:wsaw="http://www.w3.org/2006/05/addressing/wsdl" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" name="Service" targetNamespace="http://tempuri.org/">
<wsdl:types>
<xsd:schema targetNamespace="http://tempuri.org/Imports">
<xsd:import schemaLocation="http://localhost:8732/Design_Time_Addresses/MessageHeaderedService/?xsd=xsd0" namespace="http://tempuri.org/"/>
<xsd:import schemaLocation="http://localhost:8732/Design_Time_Addresses/MessageHeaderedService/?xsd=xsd1" namespace="http://schemas.microsoft.com/2003/10/Serialization/"/>
</xsd:schema>
</wsdl:types>
<wsdl:message name="HeaderedMessage">
<wsdl:part name="parameters" element="tns:HeaderedMessage"/>
</wsdl:message>
<wsdl:message name="HeaderedMessage_Headers">
<wsdl:part name="MyHeader" element="tns:MyHeader"/>
</wsdl:message>
<wsdl:message name="IService_GetDataUsingDataContract_OutputMessage"/>
<wsdl:portType name="IService">
<wsdl:operation name="GetDataUsingDataContract">
<wsdl:input wsaw:Action="http://tempuri.org/IService/GetDataUsingDataContract" name="HeaderedMessage" message="tns:HeaderedMessage"/>
<wsdl:output wsaw:Action="http://tempuri.org/IService/GetDataUsingDataContractResponse" message="tns:IService_GetDataUsingDataContract_OutputMessage"/>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="BasicHttpBinding_IService" type="tns:IService">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="GetDataUsingDataContract">
<soap:operation soapAction="http://tempuri.org/IService/GetDataUsingDataContract" style="document"/>
<wsdl:input name="HeaderedMessage">
<soap:header message="tns:HeaderedMessage_Headers" part="MyHeader" use="literal"/>
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="Service">
<wsdl:port name="BasicHttpBinding_IService" binding="tns:BasicHttpBinding_IService">
<soap:address location="http://localhost:8732/Design_Time_Addresses/MessageHeaderedService/"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
WCF 配置:
<system.serviceModel>
<services>
<service name="MessageHeaderedService.Service">
<host>
<baseAddresses>
<add baseAddress = "http://localhost:8732/Design_Time_Addresses/MessageHeaderedService/" />
</baseAddresses>
</host>
<endpoint address ="" binding="basicHttpBinding" contract="MessageHeaderedService.IService">
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="True"/>
<serviceDebug includeExceptionDetailInFaults="False" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>