We frequently see customers ask about adding custom Message headers as well as HTTP headers to outgoing WCF requests. There are multiple approaches of achieving this, but in this blog we are going to discuss the most convenient way of adding these two different type of headers headers to the outgoing WCF method calls.
One of the reasons you may want to add these extra headers could be because you may be having a service that requires you to send either of these headers and the call may fail with an error if you do not add them to the request. This blog just shows how to do it. A MessageHeader is a SOAP Header that gets added to the outgoing SOAP Envelope of the WCF request. A SOAP Envelope is the entity body part of the HTTP request. Depending on whether your service implementation requires a Message Header or a HTTP header, you can change your WCF Client to add that information accordingly.
This blog contains a zip file which implements a WCF Service implementation written in C#, and two client projects in the form of a Windows Store app and a Windows Phone app project type based on C#. The WCF Service is a simple Calculator service that performs the basic Add, Subtract, Divide and Multiply operations. The client side projects only call the “Add” method, but they show how to add the MessageHeader and HTTP headers to the outgoing requests.
We start off by creating a blank Windows Store app and a Windows Phone app project type. Once the project is created, we add a Button and a TextBlock UI element to the MainPage.xaml and add a button click handler to the Button. Then we proceed with adding the required Service Reference to the WCF Service via the “Add Service Reference” option in Visual Studio.
The most important part of adding a MessageHeader or a HTTP Header to the outgoing request is to gain access to the OperationContext of the currently executing request. To summarize the two operations:
a.) To add a MessageHeader, we access OperationContext.Current.OutgoingMessageHeaders property and call the Add(…) method
b.) To add a HTTP Header, we access the OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] property and assign a HttpRequestMessageProperty to it.
The below code section shows how to retrieve the OperationContext and assign these properties…
In the above code, CalculatorServiceClient is the auto-generated class of the Calculator service that was generated when you did “Add Service Reference”.
Once you create the OperationContextScope, you can add a MessageHeader to the outgoing request by accessing the OperationContext.Current.OutgoingMessageHeaders property and then calling the Add method to add the MessageHeader. We are using a custom object of type UserInfo that will automatically get serialized. We are using a custom object to show how you can not only pass in simple types – such as Strings or Integers, but also add complex types to the outgoing MessageHeader. The below code shows this:
Similarly we add a HTTP header to the outgoing request by accessing the OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] property and assigning a HttpRequestMessageProperty. The below code shows you how to do this:
Putting both these things together, we will end up with the below code that adds both – a MessageHeader as well as a HTTP Header to the outgoing HTTP request and then we call the AddAsync(..) method to add two numbers. The below approach shows you the Windows Store app way of calling the asynchronous WCF method.
The Windows Phone way of calling the WCF Service is slightly different, where we assign a Completed event handler that will get triggered when the method completes. The Windows Phone code looks like this:
If you collect a Network/ Fiddler trace of the call, you should see the following HTTP request that shows what the added information looks like. You can reference the following blog: http://blogs.msdn.com/b/wsdevsol/archive/2013/06/05/configure-the-windows-phone-8-emulator-to-work-with-fiddler.aspx that shows how you can configure your Windows Phone 8 Emulator to work with Fiddler.
Notice the <s:Header>…</s:Header> section for the MessageHeader and the MyHttpHeader HTTP header in the request below.
Hopefully this blog will help you understand the differences between adding a MessageHeader and a HTTP header and how you can add them to an outgoing HTTP request when calling the WCF Service.
Please let me know if this helped you out! Be sure to follow my and our team on twitter @prashantphadke and @WSDevSol.
Here are a few MSDN links to the WCF functionality we just used above:
MessageHeader Class: http://msdn.microsoft.com/en-us/library/system.servicemodel.channels.messageheader.aspx
OperationContext.Current Property : http://msdn.microsoft.com/en-us/library/system.servicemodel.operationcontext.current.aspx
OperationContext.OutgoingMessageHeaders Property : http://msdn.microsoft.com/en-us/library/system.servicemodel.operationcontext.outgoingmessageheaders.aspx
OperationContext.OutgoingMessageProperties Property: http://msdn.microsoft.com/en-us/library/system.servicemodel.operationcontext.outgoingmessageproperties.aspx