gpt4 book ai didi

c# - 所有操作的 WCF WSDL Soap Header

转载 作者:太空狗 更新时间:2023-10-30 00:29:11 29 4
gpt4 key购买 nike

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

但现在我需要在 wsdl 中为所有 Operationcontracts 设置一个 Soap Header 契约(Contract),而这次我不能设置属性。

以下代码(从 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;
}

任何人都知道如何将此代码应用于所有操作(不使用属性)并通过这样做,将 header 的契约(Contract)添加到 wsdl 中?

最佳答案

IEndpointBehavior 具有以下接口(interface):

ApplyDispatchBehavior(ServiceEndpoint endPoint, EndPointDispatcher endpointDispatcher);

您可以通过迭代 ApplyDispatchBehavior 中的 endpoint.Contract.Operations 将 Soap Headers 添加到 wsdl 以进行操作。

这里有适合我的完整解决方案:

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 中找到.

关于c# - 所有操作的 WCF WSDL Soap Header,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/986455/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com