WCF: How to append header to HTTP Request?

Damir Dobric Posts

Next talks:

 

Damir@Phone    

Follow me on Twitter: #ddobric



 

 

Archives

Long time ago I have described how to inject the message header in WCF message by using of Operation Context. After some time I also have posted how to do the same thing in Silverlight.

Now in the time of AppFabric and Access Control Service there is a new requirement caused by REST design of some ACS endpoints. In such cases (REST) services do not relay on SOAP. Because of this appending of header can only make a sense it it is added to HTTP header list.

It is interesting that there are also some cases when you have a SOAP service, but wants to append the header to HTTP. This makes a sense when you work HTTP hosted service

Following example shows how to do this:

/// <summary>

/// This method shows how to append the header to HTTP request.

/// </summary>

private static string InvokeOperation()
{
     MyServiceClient proxy = new MyServiceClient();

     using (var factory = new WebChannelFactory<IMyService>())
     {

        using (new OperationContextScope((IClientChannel)proxy.InnerChannel))
        {

             WebOperationContext.Current.OutgoingRequest.Headers.Add("MYHEADER"
             “BLABLA);

             
             return
proxy.DoWork(“hello”);
        }

    }

}

       

And this is the message, as you see, with regular SOAP message.

POST http://servicehost/ServiceApp/MyService.svc HTTP/1.1
Content-Type: text/xml; charset=utf-8
MYHEADER: BLABLA
SOAPAction: "http://tempuri.org/IMyService/DoWork"
Host: servicehost
Content-Length: 155
Expect: 100-continue
Accept-Encoding: gzip, deflate

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"><s:Body>
    <DoWork xmlns="hellohttp://tempuri.org/"><msg>hello</msg></DoWork></s:Body>
</s:Envelope
>


Posted Jun 04 2011, 12:25 AM by Damir Dobric
Filed under:
developers.de is a .Net Community Blog powered by daenet GmbH.