所有操作的 WCF WSDL Soap Header [英] WCF WSDL Soap Header on all operations

查看:19
本文介绍了所有操作的 WCF WSDL Soap Header的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过定义实现 IContactBehavior 和 IWsdlExportExtension 的属性并在您的服务合同中设置该属性,您可以轻松地将 Soap 标头添加到您的 wsdl(请参阅 http://wcfextras.codeplex.com/ 了解更多信息)

By defining an attribute that implements IContactBehavior and IWsdlExportExtension and set that attribute on your service contract, you can easily add Soap Headers to your wsdl (see http://wcfextras.codeplex.com/ for more information)

但是现在我需要在所有 Operationcontracts 的 wsdl 中设置一个 Soap Header 合同,这次我无法设置属性.

But now I need to set a Soap Header contract in the wsdl on all Operationcontracts and this time I cannot set an attribute.

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

Following code (called from IWsdlExportExtension.ExportEndPoint) doesn't work, but does work when called from the SoapHeaderAttributes (that executes an 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 中?

Anyone has an idea how to apply this code on all operations (without using attributes) and by doing this, adding the contract of the header to the wsdl ?

推荐答案

IEndpointBehavior 有如下接口:

The IEndpointBehavior has the following interface:

ApplyDispatchBehavior(ServiceEndpoint endPoint, EndPointDispatcher endpointDispatcher);

您可以通过遍历 ApplyDispatchBehavior 中的端点.Contract.Operations 来将 Soap 标头添加到 wsdl 以进行操作.

You can add Soap Headers to the wsdl for operations by iterating over the endpoint.Contract.Operations in the ApplyDispatchBehavior.

这里有对我有用的完整解决方案:

Here you have the complete solution that worked for me:

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;
}

可以在 WcfExtras 中找到 SoapHeaderHelper.

The SoapHeaderHelper can be found in the WcfExtras.

这篇关于所有操作的 WCF WSDL Soap Header的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆