Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/visual-studio-2010/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将WS-Addressing字段添加到SOAP消息_C#_Visual Studio 2010_Web Services_Soap_Asmx - Fatal编程技术网

C# 如何将WS-Addressing字段添加到SOAP消息

C# 如何将WS-Addressing字段添加到SOAP消息,c#,visual-studio-2010,web-services,soap,asmx,C#,Visual Studio 2010,Web Services,Soap,Asmx,我在项目中创建了一个指向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="htt

我在项目中创建了一个指向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>

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

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

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

    [XmlElement("Action")]
    public string Action
    {
        get; set;
    }
}
b) 定义将这些头添加到Soap信封的方法:

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) 定义标记所选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(...) {
        ...
    }

欢迎来到堆栈溢出!ASMX是一种遗留技术,不应用于新的开发。WCF或ASP.NET Web API应用于Web服务客户端和服务器的所有新开发。一个提示:微软已经退出了MSDN。你使用旧的“网络参考”有什么原因吗?他们不支持WS-anything。谢谢。你能告诉我更多吗?就像在ready data一节中以字符串形式写的那样?@SoapNewbie我恐怕不明白。
    [System.Web.Services.Protocols.SoapDocumentMethodAttribute(...)]
    [WebApplication1.AddressingExtension(Action = "http://some.example/DoWork")]
    public void DoWork(...) {
        ...
    }