Search code examples
c#wcfws-addressing

How can I manually override the WSA To header sent by a WCF client


I am currently writing a WCF client for a Java web service that is outside my control. WCF seems to populate the WSA To header with the endpoint address, but this web service requires a different value.

I am currently trying to set the value manually as follows:

var binding = new CustomBinding();
binding.Elements.Add(GetSecurityElement());
binding.Elements.Add
(
    new TextMessageEncodingBindingElement
    (
        MessageVersion.Soap11WSAddressing10,
        Encoding.UTF8
    )
);
binding.Elements.Add(new HttpsTransportBindingElement());

var endpoint = new EndpointAddress
(
    new Uri("endpoint address"),
    new DnsEndpointIdentity("endpoint identity"),
    new AddressHeaderCollection()
);

var client = new Client(binding, endpoint);
client.Open();

using (new OperationContextScope(client.InnerChannel))
{
    OperationContext.Current.OutgoingMessageHeaders.To = new Uri("some other address");
    OperationContext.Current.OutgoingMessageHeaders.MessageId = new UniqueId("message id");
    var response = client.doSomething();
}

Inspecting the request that is generated and sent using Fiddler, I can see that the MessageID header is successfully being set to "message id" rather than the default urn:uuid:[some uuid], but the To header is still being set to "endpoint address" rather than "some other address".

Is there some other way to overwrite the header value?


Solution

  • I have managd to resolve this using the approach oulined here. In code, the solution was to use :

            var endpoint = new EndpointAddress
            (
                new Uri("wsa to address"),
                new DnsEndpointIdentity("endpoint identity"),
                new AddressHeaderCollection()
            );
    

    To set the value of the WSA To header. Then use:

            client.Endpoint.Behaviors.Add(new ClientViaBehavior(new Uri("address")));
    

    To control the address the request is actually sent to.