{"id":153,"date":"2006-11-03T15:30:32","date_gmt":"2006-11-03T22:30:32","guid":{"rendered":"http:\/\/kennyw.com\/indigo\/153"},"modified":"2006-11-03T15:30:32","modified_gmt":"2006-11-03T22:30:32","slug":"manipulating-http-headers-in-your-wcf-message","status":"publish","type":"post","link":"https:\/\/kennyw.com\/?p=153","title":{"rendered":"Manipulating HTTP Headers in your WCF Message"},"content":{"rendered":"<p>I was going to write a quick note about how to suppress Expect 100-Continue as a result of <a href=\"http:\/\/forums.microsoft.com\/MSDN\/ShowPost.aspx?PostID=888707\">this forum post<\/a>. But it seems no one&#8217;s covered the background of WCF best practices for manipulating HTTP headers in general. So first, this post \ud83d\ude42<\/p>\n<p>Back in Beta 2, we <a href=\"http:\/\/hyperthink.net\/blog\/2005\/07\/15\/The+Indigo+Tent+Is+A+Big+Tent+Indeed.aspx\">stepped up our first class HTTP support<\/a> by allowing WCF users to access and manipulate the HTTP headers that are sent on the wire.  The programming model interface to this feature is through two Message Properties: <a href=\"http:\/\/windowssdk.msdn.microsoft.com\/en-us\/library\/system.servicemodel.channels.httprequestmessageproperty.aspx\">HttpRequestMessageProperty<\/a> (for HTTP requests) and <a href=\"http:\/\/windowssdk.msdn.microsoft.com\/en-us\/library\/system.servicemodel.channels.httpresponsemessageproperty.aspx\">HttpResponseMessageProperty<\/a> (for HTTP responses, as you might expect).  They are represented as Message Properties and not Message Headers since they are transport-specific information that is stored &#8220;outside&#8221; of the Message. Message Headers require an infoset-based representation that is included in serialization over all transports.<\/p>\n<p>Using the HTTP-based Message Properties is very straightforward. First, you setup the contents of your Property. Then you attach it to your outgoing OperationContext or to your outgoing Message (if you are using the Message programming model directly).  For example, if I wanted to send a GET request through WCF, I would do the following:<\/p>\n<div class=\"indent1\">\n            <code class=\"classname\">HttpRequestMessageProperty<\/code><code> httpRequestProperty = <\/code><code class=\"keyword\">new <\/code><code class=\"classname\">HttpRequestMessageProperty<\/code><code>();<br \/>\n            httpRequestProperty.Method = <\/code><code class=\"string\">\"GET\"<\/code><code>;<br \/>\n            httpRequestProperty.SuppressEntityBody = <\/code><code class=\"keyword\">true<\/code>;<br \/>\n            <code>httpRequestProperty.Headers.Add(<\/code><code class=\"classname\">HttpRequestHeader<\/code><code>.UserAgent, <\/code><code class=\"string\">\"Kenny's Client\"<\/code><code>);<\/code><\/p>\n<p>            <code class=\"keyword\">using<\/code><code> (<\/code><code class=\"classname\">OperationContextScope<\/code><code> scope = <\/code><code class=\"keyword\">new <\/code><code class=\"classname\">OperationContextScope<\/code><code>(myClient.InnerChannel))<br \/>\n            {<br \/>\n&nbsp;&nbsp;<\/code><code class=\"classname\">OperationContext<\/code><code>.Current.OutgoingMessageProperties[<\/code><code class=\"classname\">HttpRequestMessageProperty<\/code><code>.Name] = httpRequestProperty;<br \/>\n&nbsp;&nbsp;myClient.CallGetMethod();<br \/>\n            }<\/code>\n<\/div>\n<p>For incoming requests\/responses, you can look up the HTTP-specific information by looking for the Property on your incoming context:<\/p>\n<p><code class=\"classname\">HttpRequestMessageProperty<\/code><code> httpRequestProperty = <\/code><code class=\"keyword\">null<\/code><code>;<\/code><br \/>\n<code class=\"keyword\">object<\/code><code> propertyValue = <\/code><code class=\"keyword\">null<\/code><code>;<\/code><\/p>\n<p><code class=\"keyword\">if<\/code><code> (<\/code><code class=\"classname\">OperationContext<\/code><code>.Current.IncomingMessageProperties.TryGetValue(<\/code><code class=\"classname\">HttpRequestMessageProperty<\/code><code>.Name, <\/code><code class=\"keyword\">out<\/code> <code>propertyValue))<br \/>\n            {<br \/>\n&nbsp;&nbsp;httpRequestProperty = (<\/code><code class=\"classname\">HttpRequestMessageProperty<\/code><code>)propertyValue;<br \/>\n            }<\/code><\/p>\n<p>If this property is non-null (note that you could be accessing a Message that came in over a non-HTTP transport), then it will faithfully represent the information associated with the HTTP-specific data that was associated with this request.  The above approach also applies to HTTP responses.  The difference is that you use the <code class=\"classname\">HttpResponseMessageProperty<\/code> class, and the non-header items that you can manipulate include Status Code and Status Description (rather than Verb and Query String on the request). <\/p>\n","protected":false},"excerpt":{"rendered":"<p>I was going to write a quick note about how to suppress Expect 100-Continue as a result of this forum post. But it seems no one&#8217;s covered the background of WCF best practices for manipulating HTTP headers in general. So first, this post \ud83d\ude42 Back in Beta 2, we stepped up our first class HTTP [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[],"class_list":["post-153","post","type-post","status-publish","format-standard","hentry","category-indigo"],"_links":{"self":[{"href":"https:\/\/kennyw.com\/index.php?rest_route=\/wp\/v2\/posts\/153","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kennyw.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kennyw.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kennyw.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/kennyw.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=153"}],"version-history":[{"count":0,"href":"https:\/\/kennyw.com\/index.php?rest_route=\/wp\/v2\/posts\/153\/revisions"}],"wp:attachment":[{"href":"https:\/\/kennyw.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=153"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kennyw.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=153"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kennyw.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=153"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}