Question

I created a link in my project to a web service (Ie Add Service Reference -> Advanced -> Add Web Service Reference).

VS generated a proxy class: System.Web.Services.Protocols.SoapHttpClientProtocol

The WSDL looks like:

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

How can I add some mandatory data to the SOAP request?

<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>
Was it helpful?

Solution

Your solution should consist of those four steps:

a) Define classes for each WS-Addressing field, like:

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

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

b) Define a Soap Extension to add those headers to the 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) define a Soap Extension Attribute to mark web methods of your choice:

[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) Unfortunatelly, you will have to modify auto-generated proxy class, to use above attribute, for example:

    [System.Web.Services.Protocols.SoapDocumentMethodAttribute(...)]
    [WebApplication1.AddressingExtension(Action = "http://some.example/DoWork")]
    public void DoWork(...) {
        ...
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top