所有操作的WCF WSDL Soap Header

通过定义实现IContactBehavior和IWsdlExportExtension的属性并在服务合同上设置该属性,您可以轻松地向您的wsdl添加Soap Headers (有关详细信息,请参阅http://wcfextras.codeplex.com/ )

但现在我需要在所有Operationcontracts的wsdl中设置Soap Header合约,这次我无法设置属性。

以下代码(从IWsdlExportExtension.ExportEndPoint调用)不起作用,但在从SoapHeaderAttributes调用时执行(执行IWsdlExportExtension.ExportContract)

foreach (OperationDescription operationDescription in context.ContractConversionContext.Contract.Operations) { AddSoapHeader(operationDescription, "SomeHeaderObject", typeof(SomeHeaderObject), SoapHeaderDirection.InOut); } internal static void AddSoapHeader(OperationDescription operationDescription, string name, Type type, SoapHeaderDirection direction) { MessageHeaderDescription header = GetMessageHeader(name, type); bool input = ((direction & SoapHeaderDirection.In) == SoapHeaderDirection.In); bool output = ((direction & SoapHeaderDirection.Out) == SoapHeaderDirection.Out); foreach (MessageDescription msgDescription in operationDescription.Messages) { if ((msgDescription.Direction == MessageDirection.Input && input) || (msgDescription.Direction == MessageDirection.Output && output)) msgDescription.Headers.Add(header); } } internal static MessageHeaderDescription GetMessageHeader(string name, Type type) { string headerNamespace = SoapHeaderHelper.GetNamespace(type); MessageHeaderDescription messageHeaderDescription = new MessageHeaderDescription(name, headerNamespace); messageHeaderDescription.Type = type; return messageHeaderDescription; } 

任何人都知道如何在所有操作(不使用属性)上应用此代码,并通过这样做,将标头的合同添加到wsdl?

IEndpointBehavior具有以下接口:

 ApplyDispatchBehavior(ServiceEndpoint endPoint, EndPointDispatcher endpointDispatcher); 

您可以通过遍历ApplyDispatchBehavior中的endpoint.Contract.Operations来向wsdl添加Soap Headers以进行操作。

在这里,您拥有适合我的完整解决方案:

 void IEndpointBehavior.ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher) { foreach (OperationDescription operationDescription in endpoint.Contract.Operations) { foreach (MessageDescription msgDescription in operationDescription.Messages) { AddSoapHeader(operationDescription, "SomeHeaderObject", typeof(SomeHeaderObject), SoapHeaderDirection.InOut); } } } internal static void AddSoapHeader(OperationDescription operationDescription, string name, Type type, SoapHeaderDirection direction) { MessageHeaderDescription header = GetMessageHeader(name, type); bool input = ((direction & SoapHeaderDirection.In) == SoapHeaderDirection.In); bool output = ((direction & SoapHeaderDirection.Out) == SoapHeaderDirection.Out); foreach (MessageDescription msgDescription in operationDescription.Messages) { if ((msgDescription.Direction == MessageDirection.Input && input) || (msgDescription.Direction == MessageDirection.Output && output)) msgDescription.Headers.Add(header); } } internal static MessageHeaderDescription GetMessageHeader(string name, Type type) { string headerNamespace = SoapHeaderHelper.GetNamespace(type); MessageHeaderDescription messageHeaderDescription = new MessageHeaderDescription(name, headerNamespace); messageHeaderDescription.Type = type; return messageHeaderDescription; } 

SoapHeaderHelper可以在WcfExtras中找到。

您可能希望查看CodePlex上的WCFExtras项目 – 它支持自定义SOAP标头和类似的东西。 不是100%确定它是否能满足你的需求,但要检查出来!

更新:您是否考虑过在客户端和服务器端创建WCF扩展,例如消息检查器?

客户端IClientMessageInspector定义了两个方法BeforeSendRequestAfterReceiveReply而服务器端IDispatchMessageInspector具有相反的方法,即AfterReceiveRequestBeforeSendReply

通过这种方式,您可以为每条穿过电线的消息添加标题(或者有选择地只添加一些消息)。

这是来自IClientMessageInspector实现的片段,我们用它来自动地将客户端的语言环境信息(语言和文化信息)传输到服务器 – 应该让您知道如何开始:

 public object BeforeSendRequest(ref Message request, IClientChannel channel) { International intlHeader = new International(); intlHeader.Locale = CultureInfo.CurrentUICulture.TwoLetterISOLanguageName; MessageHeader header = MessageHeader.CreateHeader(WSI18N.ElementNames.International, WSI18N.NamespaceURI, intlHeader); request.Headers.Add(header); return null; } 

最简单的方法是使用WCFExtrasPlus“ https://wcfextrasplus.codeplex.com/wikipage?title=SOAP%20Headers&referringTitle=Documentation ”,只需添加dll作为项目的参考,并以这种方式编辑您的服务:

 [DataContract(Name="MyHeader", Namespace="web")] public class MyHeader { [DataMember(Order=1)] public string UserName {get; set;} [DataMember(Order=2)] public string Password { get; set; } } [SoapHeaders] [ServiceContract] public interface IMyService { [SoapHeader("MyHeader", typeof(MyHeader), Direction = SoapHeaderDirection.In)] [OperationContract] [WebInvoke(BodyStyle = WebMessageBodyStyle.Wrapped)] bool MyMethod(string input); } 

然后Soap请求看起来像:

    ? ?     ?    

如果您在同一服务中需要Soap和JSON,请举例如下: https : //stackoverflow.com/a/23910916/3667714