【问题标题】:How to add WS-Addressing fields to SOAP message如何将 WS-Addressing 字段添加到 SOAP 消息
【发布时间】:2013-12-22 16:57:38
【问题描述】:

我在我的项目中创建了一个指向 Web 服务的链接(即添加服务引用 -> 高级 -> 添加 Web 服务引用)。

VS生成了一个代理类:System.Web.Services.Protocols.SoapHttpClientProtocol

WSDL 看起来像:

<definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:wsaw="http://www.w3.org/2006/05/addressing/wsdl" 

<types>
<xsd:schema ... >
<xsd:include schemaLocation="https://server.com/path/path/Serv?xsd=../path/path/path/name.xsd"/>
... 
</xsd:schema>
</types>
<message name="Mes1_Message">
<part element="..." name="body"></part>
</message>
... 
<message>...</message>

<portType name="name">

<operation name="Method1">
<input message="name" wsaw:Action="name"></input>
<output message="name" wsaw:Action="name"></output>
</operation>
... 
</operation>


<binding name="name" type="type">
<soap12:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsaw:UsingAddressing xmlns:ns3="http://schemas.xmlsoap.org/wsdl/" required="true"/>
<wsaw:Anonymous>required</wsaw:Anonymous>
<operation name="Method1">...</operation>
<operation name="Method2">...</operation>

<service name="name">
<port binding="tns:name" name="name">
<soap12:address location="https://server.com/Serv"/>
</port>
</service>
</definitions>

如何向 SOAP 请求添加一些强制性数据?

<wsa:MessageID> ...</wsa:MessageID>

<wsa:ReplyTo> <wsa:Address>http://www.w3.org/2005/08/addressing/anonymous</wsa:Address></wsa:ReplyTo>
<wsa:Action>...</wsa:Action>
<wsa:To>...</wsa:To>
<wsa:RelatesTo></wsa:RelatesTo>

【问题讨论】:

  • 欢迎来到 Stack Overflow! ASMX 是一项遗留技术,不应用于新开发。 WCF 或 ASP.NET Web API 应该用于 Web 服务客户端和服务器的所有新开发。一个提示:Microsoft 已停用 MSDN 上的 ASMX Forum
  • 您使用旧的“网络参考”有什么原因吗?他们不支持 WS-anything。

标签: c# visual-studio-2010 web-services soap asmx


【解决方案1】:

您的解决方案应包括以下四个步骤:

a) 为每个 WS-Addressing 字段定义类,例如:

public class AddressingHeader : SoapHeader
{
    public AddressingHeader()
        : base() { }

    [XmlElement("Action")]
    public string Action
    {
        get; set;
    }
}

b) 定义 Soap Extension 以将这些标题添加到 Soap Envelope:

public class AddressingExtension : SoapExtension
{
    public AddressingExtension()
        : base() { }

    public override object GetInitializer(Type serviceType)
    {
        return null;
    }

    public override object GetInitializer(LogicalMethodInfo methodInfo, SoapExtensionAttribute attribute)
    {
        return null;
    }

    public override void Initialize(object initializer)
    {
    }

    public override void ProcessMessage(SoapMessage message)
    {

        switch (message.Stage)
        {
            case SoapMessageStage.BeforeSerialize:
                AddAddressingHeaders(message);
                break;
            default:
                break;
        }
    }

    private void AddAddressingHeaders(SoapMessage message)
    {
        message.Headers.Add(new AddressingHeader());           
    }
}

c) 定义 Soap Extension Attribute 来标记您选择的 Web 方法:

[AttributeUsage(AttributeTargets.Method)]
public class AddressingExtensionAttribute : SoapExtensionAttribute
{
    private string action;
    private int priority;

    public AddressingExtensionAttribute()
        : base()
    {
        this.action = "defaultaction";
    }

    public override Type ExtensionType
    {
        get
        {
            return typeof(AddressingExtension);
        }
    }

    public override int Priority
    {
        get
        {
            return priority;
        }
        set
        {
            priority = value;
        }
    }

    public string Action
    {
        get
        {
            return action;
        }
        set
        {
            action = value;
        }
    }
}

d) 不幸的是,您必须修改自动生成的代理类,才能使用上述属性,例如:

    [System.Web.Services.Protocols.SoapDocumentMethodAttribute(...)]
    [WebApplication1.AddressingExtension(Action = "http://some.example/DoWork")]
    public void DoWork(...) {
        ...
    }

【讨论】:

  • 谢谢。你能告诉更多 - 正如在 部分中所写的那样,将数据准备为字符串?
  • @SoapNewbie 恐怕我不明白。
猜你喜欢
  • 2015-03-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-19
  • 2017-08-14
  • 2010-10-11
  • 1970-01-01
相关资源
最近更新 更多