How do I use WS-Addressing in WCF and set the wsa:

2019-01-25 09:01发布

问题:

I'm calling a BizTalk service using WCF. The service requires the wsa:replyto address to be set in the SOAP header to able to make a 'callback' when the process is done.

We are using a contract-first approch with auto-generated code from svcutil (we cannot 'just' change the contract)...

And it's not possible to do in the config file...

I have seen someone 'overriding' some methods to make their own custom header - but this is not a custom header it's a standard in the SOAP protocol.

How can I add the wsa:replyto in the (SOAP) header?

回答1:

In order to invoke a service that requires WS-Addressing from WCF you'll have to configure the client endpoint to use a binding that supports it, such as the WSHttpBinding.

You can then set the wsa:ReplyTo header to a specific URL in your client code through the OperationContext.OutgoingMessageHeaders property:

using (new OperationContextScope((IContextChannel)channel))
{
    OperationContext.Current.OutgoingMessageHeaders.ReplyTo =
        new EndpointAddress("http://client/callback");

    channel.DoSomething();
}

In this example we are setting the wsa:ReplyTo header to a known URL where the client channel listens for incoming callback messages from the service.

Alternatively, if the service supports it, you could use the WSDualHttpBinding, which has built in support for duplex communication through WS-Addressing. In this case you would set the callback address through the WSDualHttpBinding.ClientBaseAddress property:

<system.serviceModel>
    <bindings>
        <wsDualHttpBinding>
            <binding clientBaseAddress="http://client/callback" />
        </wsDualHttpBinding>
    </bindings>

    <client>
        <endpoint address="http://server/service"
                  binding="wsDualHttpBinding"
                  contract="Namespace.Service" />
    </client>
</system.serviceModel>