HTTP headers on SOAP messages for Windows Phone 8

I needed to add HTTP headers to a soap request from a Windows Phone 8 app recently and unfortunately the usual method for doing this with a SOAP proxy isn't supported on Windows Phone.

The typical way of doing this would be to override the GetWebRequest method in the generated proxy, which allows you to get hold of the underlying WebRequest object and do whatever you want with it. Including adding your HTTP headers. Unfortunately, when you generate the proxies in a Windows Phone project the majority of the extensibility points aren't created. This leaves you unable to get to the classes inside the proxy, and manually implementing the proxy wasn't an ideal solution.

Fortunately, there is another way. You can use the OutgoingMessageProperties on the current scope to add the headers.

using (OperationContextScope scope = new OperationContextScope(client.InnerChannel))
    var httpRequestProperty = new HttpRequestMessageProperty();
    httpRequestProperty.Headers["HEADERKEY"] = "HEADERVALUE";

                                                = httpRequestProperty;

    // Send your soap request here...

Simple huh.

Unless otherwise specified all code in this post is made available under the MIT license. Copyright 2013 Simon P Stevens.