<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">
  <channel>
    <title>mob's dev blog</title>
    <link>http://blog.mike-obrien.net/</link>
    <description>Our eyes met across the crowded hat store. I, a customer, and she a coquettish haberdasher.</description>
    <image>
      <url>http://blog.mike-obrien.net/images/rss.png</url>
      <title>mob's dev blog</title>
      <link>http://blog.mike-obrien.net/</link>
    </image>
    <language>en-us</language>
    <copyright>Mike O'Brien</copyright>
    <lastBuildDate>Thu, 11 Jun 2009 13:09:00 GMT</lastBuildDate>
    <generator>newtelligence dasBlog 2.1.8102.813</generator>
    <managingEditor>mob@mike-obrien.net</managingEditor>
    <webMaster>mob@mike-obrien.net</webMaster>
    <atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" href="http://feeds.feedburner.com/MobsDevBlog" type="application/rss+xml" /><item>
      <trackback:ping>http://blog.mike-obrien.net/Trackback.aspx?guid=3799eb5a-d34e-4256-b058-9b9741c70c65</trackback:ping>
      <pingback:server>http://blog.mike-obrien.net/pingback.aspx</pingback:server>
      <pingback:target>http://blog.mike-obrien.net/PermaLink,guid,3799eb5a-d34e-4256-b058-9b9741c70c65.aspx</pingback:target>
      <dc:creator>mob</dc:creator>
      <wfw:comment>http://blog.mike-obrien.net/CommentView,guid,3799eb5a-d34e-4256-b058-9b9741c70c65.aspx</wfw:comment>
      <wfw:commentRss>http://blog.mike-obrien.net/SyndicationService.asmx/GetEntryCommentsRss?guid=3799eb5a-d34e-4256-b058-9b9741c70c65</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
I have an WiX installer project that I wanted to produce the output as the product
name and version number. This is pretty simple to do. First unload/edit your project
file and then update the "AfterBuild" target as follows:
</p>
        <pre class="code">
          <span style="color: blue">&lt;</span>
          <span style="color: #a31515">Target </span>
          <span style="color: red">Name</span>
          <span style="color: blue">=</span>"<span style="color: blue">AfterBuild</span>"<span style="color: blue">&gt;
&lt;</span><span style="color: #a31515">GetAssemblyIdentity </span><span style="color: red">AssemblyFiles</span><span style="color: blue">=</span>"<span style="color: blue">$(SolutionDir)MyApplication\bin\MyApplication.dll</span>"<span style="color: blue">&gt;
&lt;</span><span style="color: #a31515">Output </span><span style="color: red">TaskParameter</span><span style="color: blue">=</span>"<span style="color: blue">Assemblies</span>" <span style="color: red">ItemName</span><span style="color: blue">=</span>"<span style="color: blue">GetVersionAssemblyInfo</span>"<span style="color: blue">/&gt;
&lt;/</span><span style="color: #a31515">GetAssemblyIdentity</span><span style="color: blue">&gt;
&lt;</span><span style="color: #a31515">Copy </span><span style="color: red">SourceFiles</span><span style="color: blue">=</span>"<span style="color: blue">$(TargetPath)</span>" <span style="color: red">DestinationFiles</span><span style="color: blue">=</span>"<span style="color: blue">$(TargetDir)MyApplication%(GetVersionAssemblyInfo.Version).msi</span>" <span style="color: blue">/&gt;
&lt;</span><span style="color: #a31515">Delete </span><span style="color: red">Files</span><span style="color: blue">=</span>"<span style="color: blue">$(TargetPath)</span>" <span style="color: blue">/&gt;
&lt;/</span><span style="color: #a31515">Target</span><span style="color: blue">&gt;</span></pre>
        <a href="http://11011.net/software/vspaste">
        </a>
        <img width="0" height="0" src="http://blog.mike-obrien.net/aggbug.ashx?id=3799eb5a-d34e-4256-b058-9b9741c70c65" />
      <xhtml:img xmlns:xhtml="http://www.w3.org/1999/xhtml" src="http://feeds.feedburner.com/~r/MobsDevBlog/~4/7dyZf13ki6w" height="1" width="1" /></body>
      <title>Renaming Output to Contain the Assembly Version number</title>
      <guid isPermaLink="false">http://blog.mike-obrien.net/PermaLink,guid,3799eb5a-d34e-4256-b058-9b9741c70c65.aspx</guid>
      <link>http://feedproxy.google.com/~r/MobsDevBlog/~3/7dyZf13ki6w/PermaLink,guid,3799eb5a-d34e-4256-b058-9b9741c70c65.aspx</link>
      <pubDate>Thu, 11 Jun 2009 13:09:00 GMT</pubDate>
      <description>&lt;p&gt;
I have an WiX installer project that I wanted to produce the output as the product
name and version number. This is pretty simple to do. First unload/edit your project
file and then update the "AfterBuild" target as follows:
&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;Target &lt;/span&gt;&lt;span style="color: red"&gt;Name&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;"&lt;span style="color: blue"&gt;AfterBuild&lt;/span&gt;"&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;GetAssemblyIdentity &lt;/span&gt;&lt;span style="color: red"&gt;AssemblyFiles&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;"&lt;span style="color: blue"&gt;$(SolutionDir)MyApplication\bin\MyApplication.dll&lt;/span&gt;"&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;Output &lt;/span&gt;&lt;span style="color: red"&gt;TaskParameter&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;"&lt;span style="color: blue"&gt;Assemblies&lt;/span&gt;" &lt;span style="color: red"&gt;ItemName&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;"&lt;span style="color: blue"&gt;GetVersionAssemblyInfo&lt;/span&gt;"&lt;span style="color: blue"&gt;/&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;GetAssemblyIdentity&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;Copy &lt;/span&gt;&lt;span style="color: red"&gt;SourceFiles&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;"&lt;span style="color: blue"&gt;$(TargetPath)&lt;/span&gt;" &lt;span style="color: red"&gt;DestinationFiles&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;"&lt;span style="color: blue"&gt;$(TargetDir)MyApplication%(GetVersionAssemblyInfo.Version).msi&lt;/span&gt;" &lt;span style="color: blue"&gt;/&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;Delete &lt;/span&gt;&lt;span style="color: red"&gt;Files&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;"&lt;span style="color: blue"&gt;$(TargetPath)&lt;/span&gt;" &lt;span style="color: blue"&gt;/&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;Target&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;img width="0" height="0" src="http://blog.mike-obrien.net/aggbug.ashx?id=3799eb5a-d34e-4256-b058-9b9741c70c65" /&gt;</description>
      <comments>http://blog.mike-obrien.net/CommentView,guid,3799eb5a-d34e-4256-b058-9b9741c70c65.aspx</comments>
      <category>MSBuild</category>
      <category>Visual Studio 2008</category>
      <category>WiX</category>
    <feedburner:origLink>http://blog.mike-obrien.net/PermaLink,guid,3799eb5a-d34e-4256-b058-9b9741c70c65.aspx</feedburner:origLink></item>
    <item>
      <trackback:ping>http://blog.mike-obrien.net/Trackback.aspx?guid=220a09d4-d4f6-48e3-a810-129f6aef7b4f</trackback:ping>
      <pingback:server>http://blog.mike-obrien.net/pingback.aspx</pingback:server>
      <pingback:target>http://blog.mike-obrien.net/PermaLink,guid,220a09d4-d4f6-48e3-a810-129f6aef7b4f.aspx</pingback:target>
      <dc:creator>mob</dc:creator>
      <wfw:comment>http://blog.mike-obrien.net/CommentView,guid,220a09d4-d4f6-48e3-a810-129f6aef7b4f.aspx</wfw:comment>
      <wfw:commentRss>http://blog.mike-obrien.net/SyndicationService.asmx/GetEntryCommentsRss?guid=220a09d4-d4f6-48e3-a810-129f6aef7b4f</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Why, why, why? Is it not possible that someone would want to parse a relative uri
that has a querystring? M$, why do you impose these needless limits? Ok, so lets say
we have the following UriTemplate:
</p>
        <p>
/books/?language={language}
</p>
        <p>
And you want to pick this uri apart and grab the querystring portion. You can load
it into a Uri as relative but the Query and Segments properties will not be accessible
and you will get the following exception: "This operation is not supported for a relative
URI." There is a simple workaround but I really don't think we should have to do this.
Basically you pass in a dummy base uri along with your relative one and this does
the trick:
</p>
        <pre class="code">
          <span style="color: #2b91af">Uri </span>myUri = <span style="color: blue">new </span><span style="color: #2b91af">Uri</span>(<span style="color: blue">new </span><span style="color: #2b91af">Uri</span>(<span style="color: #a31515">"dummy:"</span>), <span style="color: #a31515">"/books/?language={language}"</span>);
System.Diagnostics.<span style="color: #2b91af">Debug</span>.Print(myUri.Query);</pre>
        <a href="http://11011.net/software/vspaste">
        </a>
        <p>
If you want to vote on changing this, click <a href="https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=465640">here</a> for
the Microsoft Connect feedback page.
</p>
        <img width="0" height="0" src="http://blog.mike-obrien.net/aggbug.ashx?id=220a09d4-d4f6-48e3-a810-129f6aef7b4f" />
      <xhtml:img xmlns:xhtml="http://www.w3.org/1999/xhtml" src="http://feeds.feedburner.com/~r/MobsDevBlog/~4/y-YIjXvXtpg" height="1" width="1" /></body>
      <title>Uri Query Property Inaccessible when Uri is Relative</title>
      <guid isPermaLink="false">http://blog.mike-obrien.net/PermaLink,guid,220a09d4-d4f6-48e3-a810-129f6aef7b4f.aspx</guid>
      <link>http://feedproxy.google.com/~r/MobsDevBlog/~3/y-YIjXvXtpg/PermaLink,guid,220a09d4-d4f6-48e3-a810-129f6aef7b4f.aspx</link>
      <pubDate>Tue, 09 Jun 2009 15:55:31 GMT</pubDate>
      <description>&lt;p&gt;
Why, why, why? Is it not possible that someone would want to parse a relative uri
that has a querystring? M$, why do you impose these needless limits? Ok, so lets say
we have the following UriTemplate:
&lt;/p&gt;
&lt;p&gt;
/books/?language={language}
&lt;/p&gt;
&lt;p&gt;
And you want to pick this uri apart and grab the querystring portion. You can load
it into a Uri as relative but the Query and Segments properties will not be accessible
and you will get the following exception: "This operation is not supported for a relative
URI." There is a simple workaround but I really don't think we should have to do this.
Basically you pass in a dummy base uri along with your relative one and this does
the trick:
&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: #2b91af"&gt;Uri &lt;/span&gt;myUri = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Uri&lt;/span&gt;(&lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Uri&lt;/span&gt;(&lt;span style="color: #a31515"&gt;"dummy:"&lt;/span&gt;), &lt;span style="color: #a31515"&gt;"/books/?language={language}"&lt;/span&gt;);
System.Diagnostics.&lt;span style="color: #2b91af"&gt;Debug&lt;/span&gt;.Print(myUri.Query);&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; 
&lt;p&gt;
If you want to vote on changing this, click &lt;a href="https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=465640"&gt;here&lt;/a&gt; for
the Microsoft Connect feedback page.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.mike-obrien.net/aggbug.ashx?id=220a09d4-d4f6-48e3-a810-129f6aef7b4f" /&gt;</description>
      <comments>http://blog.mike-obrien.net/CommentView,guid,220a09d4-d4f6-48e3-a810-129f6aef7b4f.aspx</comments>
      <category>.NET 3.5</category>
    <feedburner:origLink>http://blog.mike-obrien.net/PermaLink,guid,220a09d4-d4f6-48e3-a810-129f6aef7b4f.aspx</feedburner:origLink></item>
    <item>
      <trackback:ping>http://blog.mike-obrien.net/Trackback.aspx?guid=898e246f-fd40-4da6-8370-51d80f74c4fb</trackback:ping>
      <pingback:server>http://blog.mike-obrien.net/pingback.aspx</pingback:server>
      <pingback:target>http://blog.mike-obrien.net/PermaLink,guid,898e246f-fd40-4da6-8370-51d80f74c4fb.aspx</pingback:target>
      <dc:creator>mob</dc:creator>
      <wfw:comment>http://blog.mike-obrien.net/CommentView,guid,898e246f-fd40-4da6-8370-51d80f74c4fb.aspx</wfw:comment>
      <wfw:commentRss>http://blog.mike-obrien.net/SyndicationService.asmx/GetEntryCommentsRss?guid=898e246f-fd40-4da6-8370-51d80f74c4fb</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
If you attempt to define a GET operation that accepts an entity body with WCF REST
like this:
</p>
        <pre class="code">[<span style="color: #2b91af">ServiceContract</span>] <span style="color: blue">public
interface </span><span style="color: #2b91af">IService </span>{ [<span style="color: #2b91af">WebGet</span>(UriTemplate
= <span style="color: #a31515">"/"</span>)] [<span style="color: #2b91af">OperationContract</span>] <span style="color: #2b91af">ComplexFilter </span>Search(<span style="color: #2b91af">ComplexFilter </span>filter);
}</pre>
        <a href="http://11011.net/software/vspaste">
        </a>
        <p>
You will encounter the following error:
</p>
        <p>
          <font size="2" face="Courier New">Operation 'xyz' in contract 'abc' uses GET, but
also has body parameter 'lmnop'. GET operations cannot have a body. Either make the
parameter 'lmnop' a UriTemplate parameter, or switch from WebGetAttribute to WebInvokeAttribute.</font>
        </p>
        <p>
Who says that sending an entity body with a GET request is a bad thing?? RFC 2616
sure doesn't (See <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.1">here</a> and <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.3">here</a>).
The spec neither explicitly allows (As it does for POST and PUT) or disallows sending
an entity body. Interestingly M$ does allows sending an entity body with a DELETE
operation even though the the spec for DELETE is just as silent about it as it is
with GET, shouldn't it be the same restriction? All that is said in the spec is that
GET should be used to retrieve an entity and it must be safe and idempotent. It seems
clear that this doesn't rule out sending an entity body. If you have an opinion please
leave a comment as I'm really interested in hearing what others have to say about
this (Or <a href="https://connect.microsoft.com/wcf/feedback/ViewFeedback.aspx?FeedbackID=465416">here</a> regarding
this on Microsoft Connect). I could be totally wrong on this one but it looks like
sending an entity body with a GET request is indeed RESTful. 
</p>
        <p>
Now if you think it's bogus that M$ disallows this don't despair! You can trick the
WCF REST API into allowing this. It takes a bit of jumping through hoops though. First
you need to create a custom WebServiceHost and factory as follows:
</p>
        <pre class="code">
          <span style="color: blue">public class </span>
          <span style="color: #2b91af">WebServiceHostFactory </span>: <span style="color: #2b91af">ServiceHostFactory </span>{ <span style="color: blue">protected
override </span><span style="color: #2b91af">ServiceHost </span>CreateServiceHost(<span style="color: #2b91af">Type </span>serviceType, <span style="color: #2b91af">Uri</span>[]
baseAddresses) { <span style="color: blue">return new </span><span style="color: #2b91af">WebServiceHost</span>(serviceType,
baseAddresses); } }</pre>
        <a href="http://11011.net/software/vspaste">
        </a>
        <pre class="code">
          <span style="color: blue">public
class </span>
          <span style="color: #2b91af">WebServiceHost </span>: System.ServiceModel.Web.<span style="color: #2b91af">WebServiceHost </span>{ <span style="color: blue">public </span>WebServiceHost( <span style="color: #2b91af">Type </span>serviceType, <span style="color: blue">params </span><span style="color: #2b91af">Uri</span>[]
baseAddresses) : <span style="color: blue">base</span>(serviceType, baseAddresses)
{ } <span style="color: blue">public </span>WebServiceHost( <span style="color: blue">object </span>singletonInstance, <span style="color: blue">params </span><span style="color: #2b91af">Uri</span>[]
baseAddresses) : <span style="color: blue">base</span>(singletonInstance, baseAddresses)
{ } <span style="color: blue">protected override void </span>OnOpening() { <span style="color: blue">base</span>.OnOpening();
ReplaceBehaviorOnAllEndpoints(<span style="color: blue">this</span>, <span style="color: blue">typeof</span>(System.ServiceModel.Description.<span style="color: #2b91af">WebHttpBehavior</span>), <span style="color: blue">new </span><span style="color: #2b91af">WebHttpBehavior</span>());
} <span style="color: blue">public void </span>ReplaceBehaviorOnAllEndpoints( <span style="color: #2b91af">ServiceHostBase </span>serviceHost, <span style="color: #2b91af">Type </span>replaceType, <span style="color: #2b91af">IEndpointBehavior </span>behavior)
{ <span style="color: blue">foreach </span>(<span style="color: blue">var </span>endpoint <span style="color: blue">in </span>serviceHost.Description.Endpoints)
{ <span style="color: blue">if </span>(replaceType != <span style="color: blue">null</span>)
{ <span style="color: #2b91af">IEndpointBehavior </span>exisitingBehavior = endpoint.Behaviors.FirstOrDefault(
b =&gt; b.GetType() == replaceType); <span style="color: blue">if </span>(exisitingBehavior
!= <span style="color: blue">null</span>) endpoint.Behaviors.Remove(exisitingBehavior);
} endpoint.Behaviors.Add(behavior); } } }</pre>
        <a href="http://11011.net/software/vspaste">
        </a>
        <p>
Now create the custom WebHttpBehavior. We're basically creating a surrogate behavior
with all the same properties except we set the method to a bogus value. We apply this
behavior before the code executes that imposes the restriction (In GetRequestDispatchFormatter).
Then afterwards we remove the surrogate and reapply the original behavior.
</p>
        <pre class="code">
          <span style="color: blue">public class </span>
          <span style="color: #2b91af">WebHttpBehavior </span>:
System.ServiceModel.Description.<span style="color: #2b91af">WebHttpBehavior </span>{ <span style="color: blue"> #region </span>WebHttpBehavior
Overrides </pre>
        <pre class="code">
          <span style="color: blue">protected override </span>
          <span style="color: #2b91af">IDispatchMessageFormatter </span>GetRequestDispatchFormatter(</pre>
        <pre class="code">
          <span style="color: #2b91af">OperationDescription </span>operationDescription, <span style="color: #2b91af">ServiceEndpoint </span>endpoint)
{ <span style="color: #2b91af">IOperationBehavior </span>originalBehavior = <span style="color: blue">null</span>; <span style="color: #2b91af">IOperationBehavior </span>surrogateBehavior
= <span style="color: blue">null</span>; TryGetSurrogateBehavior(operationDescription, <span style="color: blue">ref </span>originalBehavior, <span style="color: blue">ref </span>surrogateBehavior);
SwapBehaviors(operationDescription, originalBehavior, surrogateBehavior); <span style="color: #2b91af">IDispatchMessageFormatter </span>formatter
= <span style="color: blue">base</span>.GetRequestDispatchFormatter(operationDescription,
endpoint); SwapBehaviors(operationDescription, surrogateBehavior, originalBehavior); <span style="color: blue">return </span>formatter;
} <span style="color: blue">#endregion #region </span>Private Methods <span style="color: blue">private
void </span>SwapBehaviors(<span style="color: #2b91af">OperationDescription </span>operationDescription, </pre>
        <pre class="code">
          <span style="color: #2b91af">IOperationBehavior </span>remove, <span style="color: #2b91af">IOperationBehavior </span>add)
{ <span style="color: blue">if </span>(remove != <span style="color: blue">null </span>&amp;&amp;
add != <span style="color: blue">null</span>) { operationDescription.Behaviors.Remove(remove);
operationDescription.Behaviors.Add(add); } } <span style="color: blue">private void </span>TryGetSurrogateBehavior(<span style="color: #2b91af">OperationDescription </span>operationDescription, </pre>
        <pre class="code">
          <span style="color: blue">ref </span>
          <span style="color: #2b91af">IOperationBehavior </span>original, <span style="color: blue">ref </span><span style="color: #2b91af">IOperationBehavior </span>surrogate)
{ <span style="color: blue">if </span>(!IsUntypedMessage(operationDescription.Messages[0])
&amp;&amp; operationDescription.Messages[0].Body.Parts.Count != 0) { <span style="color: #2b91af">WebGetAttribute </span>webGetAttribute
= </pre>
        <pre class="code">                          operationDescription.Behaviors.Find&lt;<span style="color: #2b91af">WebGetAttribute</span>&gt;(); <span style="color: blue">if </span>(webGetAttribute
!= <span style="color: blue">null</span>) { original = webGetAttribute; surrogate
= <span style="color: blue">new </span><span style="color: #2b91af">WebInvokeAttribute</span>()
{ BodyStyle = webGetAttribute.BodyStyle, Method = <span style="color: #a31515">"NONE"</span>,
RequestFormat = webGetAttribute.RequestFormat, ResponseFormat = webGetAttribute.ResponseFormat,
UriTemplate = webGetAttribute.UriTemplate }; } <span style="color: blue">else </span>{ <span style="color: #2b91af">WebInvokeAttribute </span>webInvokeAttribute
= </pre>
        <pre class="code">                        operationDescription.Behaviors.Find&lt;<span style="color: #2b91af">WebInvokeAttribute</span>&gt;(); <span style="color: blue">if </span>(webInvokeAttribute
!= <span style="color: blue">null </span>&amp;&amp; webInvokeAttribute.Method == <span style="color: #a31515">"GET"</span>)
{ original = webInvokeAttribute; surrogate = <span style="color: blue">new </span><span style="color: #2b91af">WebInvokeAttribute</span>()
{ BodyStyle = webInvokeAttribute.BodyStyle, Method = <span style="color: #a31515">"NONE"</span>,
RequestFormat = webInvokeAttribute.RequestFormat, ResponseFormat = webInvokeAttribute.ResponseFormat,
UriTemplate = webInvokeAttribute.UriTemplate }; } } } } <span style="color: blue">private
bool </span>IsUntypedMessage(<span style="color: #2b91af">MessageDescription </span>message)
{ <span style="color: blue">if </span>(message == <span style="color: blue">null</span>)
{ <span style="color: blue">return false</span>; } <span style="color: blue">return </span>((((message.Body.ReturnValue
!= <span style="color: blue">null</span>) &amp;&amp; (message.Body.Parts.Count ==
0)) &amp;&amp; (message.Body.ReturnValue.Type == <span style="color: blue">typeof</span>(<span style="color: #2b91af">Message</span>)))
|| (((message.Body.ReturnValue == <span style="color: blue">null</span>) &amp;&amp;
(message.Body.Parts.Count == 1)) &amp;&amp; (message.Body.Parts[0].Type == <span style="color: blue">typeof</span>(<span style="color: #2b91af">Message</span>))));
} <span style="color: blue">#endregion </span>}</pre>
        <a href="http://11011.net/software/vspaste">
        </a>
        <img width="0" height="0" src="http://blog.mike-obrien.net/aggbug.ashx?id=898e246f-fd40-4da6-8370-51d80f74c4fb" />
      <xhtml:img xmlns:xhtml="http://www.w3.org/1999/xhtml" src="http://feeds.feedburner.com/~r/MobsDevBlog/~4/_sATi1kBzUM" height="1" width="1" /></body>
      <title>Rigging WCF REST to Accept an Entity Body in a GET Request</title>
      <guid isPermaLink="false">http://blog.mike-obrien.net/PermaLink,guid,898e246f-fd40-4da6-8370-51d80f74c4fb.aspx</guid>
      <link>http://feedproxy.google.com/~r/MobsDevBlog/~3/_sATi1kBzUM/PermaLink,guid,898e246f-fd40-4da6-8370-51d80f74c4fb.aspx</link>
      <pubDate>Tue, 09 Jun 2009 04:09:13 GMT</pubDate>
      <description>&lt;p&gt;
If you attempt to define a GET operation that accepts an entity body with WCF REST
like this:
&lt;/p&gt;
&lt;pre class="code"&gt;[&lt;span style="color: #2b91af"&gt;ServiceContract&lt;/span&gt;] &lt;span style="color: blue"&gt;public
interface &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IService &lt;/span&gt;{ [&lt;span style="color: #2b91af"&gt;WebGet&lt;/span&gt;(UriTemplate
= &lt;span style="color: #a31515"&gt;"/"&lt;/span&gt;)] [&lt;span style="color: #2b91af"&gt;OperationContract&lt;/span&gt;] &lt;span style="color: #2b91af"&gt;ComplexFilter &lt;/span&gt;Search(&lt;span style="color: #2b91af"&gt;ComplexFilter &lt;/span&gt;filter);
}&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; 
&lt;p&gt;
You will encounter the following error:
&lt;/p&gt;
&lt;p&gt;
&lt;font size="2" face="Courier New"&gt;Operation 'xyz' in contract 'abc' uses GET, but
also has body parameter 'lmnop'. GET operations cannot have a body. Either make the
parameter 'lmnop' a UriTemplate parameter, or switch from WebGetAttribute to WebInvokeAttribute.&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
Who says that sending an entity body with a GET request is a bad thing?? RFC 2616
sure doesn't (See &lt;a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.1"&gt;here&lt;/a&gt; and &lt;a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.3"&gt;here&lt;/a&gt;).
The spec neither explicitly allows (As it does for POST and PUT) or disallows sending
an entity body. Interestingly M$ does allows sending an entity body with a DELETE
operation even though the the spec for DELETE is just as silent about it as it is
with GET, shouldn't it be the same restriction? All that is said in the spec is that
GET should be used to retrieve an entity and it must be safe and idempotent. It seems
clear that this doesn't rule out sending an entity body. If you have an opinion please
leave a comment as I'm really interested in hearing what others have to say about
this (Or &lt;a href="https://connect.microsoft.com/wcf/feedback/ViewFeedback.aspx?FeedbackID=465416"&gt;here&lt;/a&gt; regarding
this on Microsoft Connect). I could be totally wrong on this one but it looks like
sending an entity body with a GET request is indeed RESTful. 
&lt;/p&gt;
&lt;p&gt;
Now if you think it's bogus that M$ disallows this don't despair! You can trick the
WCF REST API into allowing this. It takes a bit of jumping through hoops though. First
you need to create a custom WebServiceHost and factory as follows:
&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;WebServiceHostFactory &lt;/span&gt;: &lt;span style="color: #2b91af"&gt;ServiceHostFactory &lt;/span&gt;{ &lt;span style="color: blue"&gt;protected
override &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ServiceHost &lt;/span&gt;CreateServiceHost(&lt;span style="color: #2b91af"&gt;Type &lt;/span&gt;serviceType, &lt;span style="color: #2b91af"&gt;Uri&lt;/span&gt;[]
baseAddresses) { &lt;span style="color: blue"&gt;return new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;WebServiceHost&lt;/span&gt;(serviceType,
baseAddresses); } }&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public
class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;WebServiceHost &lt;/span&gt;: System.ServiceModel.Web.&lt;span style="color: #2b91af"&gt;WebServiceHost &lt;/span&gt;{ &lt;span style="color: blue"&gt;public &lt;/span&gt;WebServiceHost( &lt;span style="color: #2b91af"&gt;Type &lt;/span&gt;serviceType, &lt;span style="color: blue"&gt;params &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Uri&lt;/span&gt;[]
baseAddresses) : &lt;span style="color: blue"&gt;base&lt;/span&gt;(serviceType, baseAddresses)
{ } &lt;span style="color: blue"&gt;public &lt;/span&gt;WebServiceHost( &lt;span style="color: blue"&gt;object &lt;/span&gt;singletonInstance, &lt;span style="color: blue"&gt;params &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Uri&lt;/span&gt;[]
baseAddresses) : &lt;span style="color: blue"&gt;base&lt;/span&gt;(singletonInstance, baseAddresses)
{ } &lt;span style="color: blue"&gt;protected override void &lt;/span&gt;OnOpening() { &lt;span style="color: blue"&gt;base&lt;/span&gt;.OnOpening();
ReplaceBehaviorOnAllEndpoints(&lt;span style="color: blue"&gt;this&lt;/span&gt;, &lt;span style="color: blue"&gt;typeof&lt;/span&gt;(System.ServiceModel.Description.&lt;span style="color: #2b91af"&gt;WebHttpBehavior&lt;/span&gt;), &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;WebHttpBehavior&lt;/span&gt;());
} &lt;span style="color: blue"&gt;public void &lt;/span&gt;ReplaceBehaviorOnAllEndpoints( &lt;span style="color: #2b91af"&gt;ServiceHostBase &lt;/span&gt;serviceHost, &lt;span style="color: #2b91af"&gt;Type &lt;/span&gt;replaceType, &lt;span style="color: #2b91af"&gt;IEndpointBehavior &lt;/span&gt;behavior)
{ &lt;span style="color: blue"&gt;foreach &lt;/span&gt;(&lt;span style="color: blue"&gt;var &lt;/span&gt;endpoint &lt;span style="color: blue"&gt;in &lt;/span&gt;serviceHost.Description.Endpoints)
{ &lt;span style="color: blue"&gt;if &lt;/span&gt;(replaceType != &lt;span style="color: blue"&gt;null&lt;/span&gt;)
{ &lt;span style="color: #2b91af"&gt;IEndpointBehavior &lt;/span&gt;exisitingBehavior = endpoint.Behaviors.FirstOrDefault(
b =&amp;gt; b.GetType() == replaceType); &lt;span style="color: blue"&gt;if &lt;/span&gt;(exisitingBehavior
!= &lt;span style="color: blue"&gt;null&lt;/span&gt;) endpoint.Behaviors.Remove(exisitingBehavior);
} endpoint.Behaviors.Add(behavior); } } }&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; 
&lt;p&gt;
Now create the custom WebHttpBehavior. We're basically creating a surrogate behavior
with all the same properties except we set the method to a bogus value. We apply this
behavior before the code executes that imposes the restriction (In GetRequestDispatchFormatter).
Then afterwards we remove the surrogate and reapply the original behavior.
&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;WebHttpBehavior &lt;/span&gt;:
System.ServiceModel.Description.&lt;span style="color: #2b91af"&gt;WebHttpBehavior &lt;/span&gt;{ &lt;span style="color: blue"&gt; #region &lt;/span&gt;WebHttpBehavior
Overrides &lt;/pre&gt;&lt;pre class="code"&gt;        &lt;span style="color: blue"&gt;protected override &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IDispatchMessageFormatter &lt;/span&gt;GetRequestDispatchFormatter(&lt;/pre&gt;&lt;pre class="code"&gt;              &lt;span style="color: #2b91af"&gt;OperationDescription &lt;/span&gt;operationDescription, &lt;span style="color: #2b91af"&gt;ServiceEndpoint &lt;/span&gt;endpoint)
{ &lt;span style="color: #2b91af"&gt;IOperationBehavior &lt;/span&gt;originalBehavior = &lt;span style="color: blue"&gt;null&lt;/span&gt;; &lt;span style="color: #2b91af"&gt;IOperationBehavior &lt;/span&gt;surrogateBehavior
= &lt;span style="color: blue"&gt;null&lt;/span&gt;; TryGetSurrogateBehavior(operationDescription, &lt;span style="color: blue"&gt;ref &lt;/span&gt;originalBehavior, &lt;span style="color: blue"&gt;ref &lt;/span&gt;surrogateBehavior);
SwapBehaviors(operationDescription, originalBehavior, surrogateBehavior); &lt;span style="color: #2b91af"&gt;IDispatchMessageFormatter &lt;/span&gt;formatter
= &lt;span style="color: blue"&gt;base&lt;/span&gt;.GetRequestDispatchFormatter(operationDescription,
endpoint); SwapBehaviors(operationDescription, surrogateBehavior, originalBehavior); &lt;span style="color: blue"&gt;return &lt;/span&gt;formatter;
} &lt;span style="color: blue"&gt;#endregion #region &lt;/span&gt;Private Methods &lt;span style="color: blue"&gt;private
void &lt;/span&gt;SwapBehaviors(&lt;span style="color: #2b91af"&gt;OperationDescription &lt;/span&gt;operationDescription, &lt;/pre&gt;&lt;pre class="code"&gt;                                   &lt;span style="color: #2b91af"&gt;IOperationBehavior &lt;/span&gt;remove, &lt;span style="color: #2b91af"&gt;IOperationBehavior &lt;/span&gt;add)
{ &lt;span style="color: blue"&gt;if &lt;/span&gt;(remove != &lt;span style="color: blue"&gt;null &lt;/span&gt;&amp;amp;&amp;amp;
add != &lt;span style="color: blue"&gt;null&lt;/span&gt;) { operationDescription.Behaviors.Remove(remove);
operationDescription.Behaviors.Add(add); } } &lt;span style="color: blue"&gt;private void &lt;/span&gt;TryGetSurrogateBehavior(&lt;span style="color: #2b91af"&gt;OperationDescription &lt;/span&gt;operationDescription, &lt;/pre&gt;&lt;pre class="code"&gt;                         &lt;span style="color: blue"&gt;ref &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IOperationBehavior &lt;/span&gt;original, &lt;span style="color: blue"&gt;ref &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IOperationBehavior &lt;/span&gt;surrogate)
{ &lt;span style="color: blue"&gt;if &lt;/span&gt;(!IsUntypedMessage(operationDescription.Messages[0])
&amp;amp;&amp;amp; operationDescription.Messages[0].Body.Parts.Count != 0) { &lt;span style="color: #2b91af"&gt;WebGetAttribute &lt;/span&gt;webGetAttribute
= &lt;/pre&gt;&lt;pre class="code"&gt;                          operationDescription.Behaviors.Find&amp;lt;&lt;span style="color: #2b91af"&gt;WebGetAttribute&lt;/span&gt;&amp;gt;(); &lt;span style="color: blue"&gt;if &lt;/span&gt;(webGetAttribute
!= &lt;span style="color: blue"&gt;null&lt;/span&gt;) { original = webGetAttribute; surrogate
= &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;WebInvokeAttribute&lt;/span&gt;()
{ BodyStyle = webGetAttribute.BodyStyle, Method = &lt;span style="color: #a31515"&gt;"NONE"&lt;/span&gt;,
RequestFormat = webGetAttribute.RequestFormat, ResponseFormat = webGetAttribute.ResponseFormat,
UriTemplate = webGetAttribute.UriTemplate }; } &lt;span style="color: blue"&gt;else &lt;/span&gt;{ &lt;span style="color: #2b91af"&gt;WebInvokeAttribute &lt;/span&gt;webInvokeAttribute
= &lt;/pre&gt;&lt;pre class="code"&gt;                        operationDescription.Behaviors.Find&amp;lt;&lt;span style="color: #2b91af"&gt;WebInvokeAttribute&lt;/span&gt;&amp;gt;(); &lt;span style="color: blue"&gt;if &lt;/span&gt;(webInvokeAttribute
!= &lt;span style="color: blue"&gt;null &lt;/span&gt;&amp;amp;&amp;amp; webInvokeAttribute.Method == &lt;span style="color: #a31515"&gt;"GET"&lt;/span&gt;)
{ original = webInvokeAttribute; surrogate = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;WebInvokeAttribute&lt;/span&gt;()
{ BodyStyle = webInvokeAttribute.BodyStyle, Method = &lt;span style="color: #a31515"&gt;"NONE"&lt;/span&gt;,
RequestFormat = webInvokeAttribute.RequestFormat, ResponseFormat = webInvokeAttribute.ResponseFormat,
UriTemplate = webInvokeAttribute.UriTemplate }; } } } } &lt;span style="color: blue"&gt;private
bool &lt;/span&gt;IsUntypedMessage(&lt;span style="color: #2b91af"&gt;MessageDescription &lt;/span&gt;message)
{ &lt;span style="color: blue"&gt;if &lt;/span&gt;(message == &lt;span style="color: blue"&gt;null&lt;/span&gt;)
{ &lt;span style="color: blue"&gt;return false&lt;/span&gt;; } &lt;span style="color: blue"&gt;return &lt;/span&gt;((((message.Body.ReturnValue
!= &lt;span style="color: blue"&gt;null&lt;/span&gt;) &amp;amp;&amp;amp; (message.Body.Parts.Count ==
0)) &amp;amp;&amp;amp; (message.Body.ReturnValue.Type == &lt;span style="color: blue"&gt;typeof&lt;/span&gt;(&lt;span style="color: #2b91af"&gt;Message&lt;/span&gt;)))
|| (((message.Body.ReturnValue == &lt;span style="color: blue"&gt;null&lt;/span&gt;) &amp;amp;&amp;amp;
(message.Body.Parts.Count == 1)) &amp;amp;&amp;amp; (message.Body.Parts[0].Type == &lt;span style="color: blue"&gt;typeof&lt;/span&gt;(&lt;span style="color: #2b91af"&gt;Message&lt;/span&gt;))));
} &lt;span style="color: blue"&gt;#endregion &lt;/span&gt;}&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;img width="0" height="0" src="http://blog.mike-obrien.net/aggbug.ashx?id=898e246f-fd40-4da6-8370-51d80f74c4fb" /&gt;</description>
      <comments>http://blog.mike-obrien.net/CommentView,guid,898e246f-fd40-4da6-8370-51d80f74c4fb.aspx</comments>
      <category>.NET 3.5</category>
      <category>REST</category>
      <category>WCF</category>
    <feedburner:origLink>http://blog.mike-obrien.net/PermaLink,guid,898e246f-fd40-4da6-8370-51d80f74c4fb.aspx</feedburner:origLink></item>
    <item>
      <trackback:ping>http://blog.mike-obrien.net/Trackback.aspx?guid=24cb015c-f143-44d1-acce-9bf914a10eff</trackback:ping>
      <pingback:server>http://blog.mike-obrien.net/pingback.aspx</pingback:server>
      <pingback:target>http://blog.mike-obrien.net/PermaLink,guid,24cb015c-f143-44d1-acce-9bf914a10eff.aspx</pingback:target>
      <dc:creator>mob</dc:creator>
      <wfw:comment>http://blog.mike-obrien.net/CommentView,guid,24cb015c-f143-44d1-acce-9bf914a10eff.aspx</wfw:comment>
      <wfw:commentRss>http://blog.mike-obrien.net/SyndicationService.asmx/GetEntryCommentsRss?guid=24cb015c-f143-44d1-acce-9bf914a10eff</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Evidentially the request message isn't guaranteed to be around by the time you get
to your response code. I found out this the hard way when I was getting sporadic failures
accessing properties of the IncomingWebRequestContext in response code. To get around
this I just stashed info (While in request code) I was interested in, in the outgoing
message properties. Then I retrieve these properties in my response code. 
</p>
        <p>
Using the outgoing message properties approach is pretty easy. To give you a little
background, I have an error handler behavior that attaches a specified error handler
to all channel dispatchers in a particular service. I also have a custom error handler
that logs exceptions to a specified object that implemented a particular logging interface.
This error handler passes the request http info to the logger. Originally I was directly
accessing the IncomingWebRequestContext in the HandleError method of the error handler
to get this request info and would sporadically get the error in the title. Now in
my error handler behavior I'm adding a message inspector that will add the request
http info to the outgoing message properties (I'm piggy backing off the error handler
behavior to do this because they are so closely related):
</p>
        <pre class="code">
          <span style="color: blue">public class </span>
          <span style="color: #2b91af">ErrorHandlerBehavior </span>: <span style="color: #2b91af">IServiceBehavior</span><span style="color: #2b91af" />{ <span style="color: blue">public
const string </span>HttpRequestInformationProperty = <span style="color: #a31515">"HttpRequestInformation"</span>; <span style="color: blue">public
void </span>ApplyDispatchBehavior( <span style="color: #2b91af">ServiceDescription </span>serviceDescription, <span style="color: #2b91af">ServiceHostBase </span>serviceHostBase)
{ <span style="color: blue">foreach </span>(<span style="color: #2b91af">ChannelDispatcher </span>dispatcher <span style="color: blue">in </span>serviceHostBase.ChannelDispatchers)
{ <font color="#008000"><font color="#008000"><span style="color: green">// Add the
error handler...</span></font><span style="color: green"> // Add the parameter inspector
that gets the request info and</span><span style="color: green"> // stores it in
the outgoing message properties</span></font><span style="color: blue">foreach </span>(<span style="color: #2b91af">EndpointDispatcher </span>endpoint <span style="color: blue">in </span>dispatcher.Endpoints)
endpoint.DispatchRuntime.MessageInspectors.Add( <span style="color: blue">new </span><span style="color: #2b91af">HttpRequestInformationInspector</span>());
} } }</pre>
        <a href="http://11011.net/software/vspaste">
        </a>
        <p>
So now error handlers have this info at their disposal. Then in the message inspector
we grab the http request info and stash it in the outgoing message properties:
</p>
        <pre class="code">
          <span style="color: blue">private class </span>
          <span style="color: #2b91af">HttpRequestInformationInspector </span>: <span style="color: #2b91af">IDispatchMessageInspector </span>{ <span style="color: blue">public
object </span>AfterReceiveRequest(<span style="color: blue">ref </span>System.ServiceModel.Channels.<span style="color: #2b91af">Message </span>request, <span style="color: #2b91af">IClientChannel </span>channel, <span style="color: #2b91af">InstanceContext </span>instanceContext)
{ <span style="color: #2b91af">HttpRequestInformation </span>info = <span style="color: blue">new </span><span style="color: #2b91af">HttpRequestInformation</span>(); <span style="color: blue">string </span>contentLengthHeader
= <span style="color: #2b91af">WebOperationContext</span>.Current.IncomingRequest.Headers[<span style="color: #2b91af">HttpRequestHeader</span>.ContentLength]; <span style="color: blue">long </span>contentLength; <span style="color: blue">if </span>(!<span style="color: blue">string</span>.IsNullOrEmpty(contentLengthHeader)) <span style="color: blue">long</span>.TryParse(contentLengthHeader, <span style="color: blue">out </span>contentLength); <span style="color: blue">else </span>contentLength
= -1; info.ContentLength = contentLength; info.Uri = <span style="color: #2b91af">OperationContext</span>.Current.IncomingMessageHeaders.To;
info.Method = <span style="color: #2b91af">WebOperationContext</span>.Current.IncomingRequest.Method;
info.ContentType = <span style="color: #2b91af">WebOperationContext</span>.Current.IncomingRequest.ContentType;
info.Accept = <span style="color: #2b91af">WebOperationContext</span>.Current.IncomingRequest.Accept;
info.UserAgent = <span style="color: #2b91af">WebOperationContext</span>.Current.IncomingRequest.UserAgent;
info.Headers = <span style="color: #2b91af">WebOperationContext</span>.Current.IncomingRequest.Headers; <span style="color: #2b91af">OperationContext</span>.Current.OutgoingMessageProperties.Add(
HttpRequestInformationProperty, info); <span style="color: blue">return null</span>;
} <span style="color: blue">public void </span>BeforeSendReply(<span style="color: blue">ref </span>System.ServiceModel.Channels.<span style="color: #2b91af">Message </span>reply, <span style="color: blue">object </span>correlationState)
{ } }</pre>
        <pre class="code">
          <span style="color: blue">public class </span>
          <span style="color: #2b91af">HttpRequestInformation </span>{ <span style="color: blue">public </span><span style="color: #2b91af">Uri </span>Uri
{ <span style="color: blue">get</span>; <span style="color: blue">set</span>; } <span style="color: blue">public
string </span>Method { <span style="color: blue">get</span>; <span style="color: blue">set</span>;
} <span style="color: blue">public string </span>ContentType { <span style="color: blue">get</span>; <span style="color: blue">set</span>;
} <span style="color: blue">public long </span>ContentLength { <span style="color: blue">get</span>; <span style="color: blue">set</span>;
} <span style="color: blue">public string </span>Accept { <span style="color: blue">get</span>; <span style="color: blue">set</span>;
} <span style="color: blue">public string </span>UserAgent { <span style="color: blue">get</span>; <span style="color: blue">set</span>;
} <span style="color: blue">public </span><span style="color: #2b91af">WebHeaderCollection </span>Headers
{ <span style="color: blue">get</span>; <span style="color: blue">set</span>; } }</pre>
        <a href="http://11011.net/software/vspaste">
        </a>
        <a href="http://11011.net/software/vspaste">
        </a>
        <p>
Note that I'm manually grabbing the content length from the headers. Interestingly
the content length header may not always be present. And its absence may not mean
that the content length is zero (That's why I return a negative number if it is absent).
The problem is the IncomingWebRequestContext ContentLength property does not check
for the existence of the header before it parses it. So if its not there (Which is
what's going to happen with a GET request) it will throw a null ref exception (Which
can be confusing). I have submitted feedback <a href="https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=462088">here</a> it
you want to read more about this and Microsoft's response. 
</p>
        <p>
So now it's available in response code:
</p>
        <pre class="code">
          <span style="color: blue">public bool </span>HandleError(<span style="color: #2b91af">Exception </span>error)
{ <span style="color: #2b91af">HttpRequestInformation </span>info; <span style="color: blue">if </span>(<span style="color: #2b91af">OperationContext</span>.Current.OutgoingMessageProperties.ContainsKey( <span style="color: #2b91af">ErrorHandlerBehavior</span>.HttpRequestInformationProperty))
info = (<span style="color: #2b91af">RequestInformation</span>)<span style="color: #2b91af">OperationContext</span>.Current.OutgoingMessageProperties[ <span style="color: #2b91af">ErrorHandlerBehavior</span>.HttpRequestInformationProperty]; <span style="color: blue">else </span>info
= <span style="color: blue">new </span><span style="color: #2b91af">RequestInformation</span>();
LogHandler.Write(error, info); <span style="color: blue">return true</span>; }</pre>
        <a href="http://11011.net/software/vspaste">
        </a>
        <p>
Do you know of a better way to handle this? If so, please leave a comment as I'm not
100% satisfied with this approach and would be interested in a simpler alternative. 
</p>
        <img width="0" height="0" src="http://blog.mike-obrien.net/aggbug.ashx?id=24cb015c-f143-44d1-acce-9bf914a10eff" />
      <xhtml:img xmlns:xhtml="http://www.w3.org/1999/xhtml" src="http://feeds.feedburner.com/~r/MobsDevBlog/~4/zZQAON9fKg8" height="1" width="1" /></body>
      <title>"The System.ServiceModel.Channels.MessageProperties object has been disposed." Exception when Accessing IncomingWebRequestContext Properties in Response Code</title>
      <guid isPermaLink="false">http://blog.mike-obrien.net/PermaLink,guid,24cb015c-f143-44d1-acce-9bf914a10eff.aspx</guid>
      <link>http://feedproxy.google.com/~r/MobsDevBlog/~3/zZQAON9fKg8/PermaLink,guid,24cb015c-f143-44d1-acce-9bf914a10eff.aspx</link>
      <pubDate>Mon, 08 Jun 2009 17:51:57 GMT</pubDate>
      <description>&lt;p&gt;
Evidentially the request message isn't guaranteed to be around by the time you get
to your response code. I found out this the hard way when I was getting sporadic failures
accessing properties of the IncomingWebRequestContext in response code. To get around
this I just stashed info (While in request code) I was interested in, in the outgoing
message properties. Then I retrieve these properties in my response code. 
&lt;/p&gt;
&lt;p&gt;
Using the outgoing message properties approach is pretty easy. To give you a little
background, I have an error handler behavior that attaches a specified error handler
to all channel dispatchers in a particular service. I also have a custom error handler
that logs exceptions to a specified object that implemented a particular logging interface.
This error handler passes the request http info to the logger. Originally I was directly
accessing the IncomingWebRequestContext in the HandleError method of the error handler
to get this request info and would sporadically get the error in the title. Now in
my error handler behavior I'm adding a message inspector that will add the request
http info to the outgoing message properties (I'm piggy backing off the error handler
behavior to do this because they are so closely related):
&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ErrorHandlerBehavior &lt;/span&gt;: &lt;span style="color: #2b91af"&gt;IServiceBehavior&lt;/span&gt;&lt;span style="color: #2b91af"&gt; &lt;/span&gt;{ &lt;span style="color: blue"&gt;public
const string &lt;/span&gt;HttpRequestInformationProperty = &lt;span style="color: #a31515"&gt;"HttpRequestInformation"&lt;/span&gt;; &lt;span style="color: blue"&gt;public
void &lt;/span&gt;ApplyDispatchBehavior( &lt;span style="color: #2b91af"&gt;ServiceDescription &lt;/span&gt;serviceDescription, &lt;span style="color: #2b91af"&gt;ServiceHostBase &lt;/span&gt;serviceHostBase)
{ &lt;span style="color: blue"&gt;foreach &lt;/span&gt;(&lt;span style="color: #2b91af"&gt;ChannelDispatcher &lt;/span&gt;dispatcher &lt;span style="color: blue"&gt;in &lt;/span&gt;serviceHostBase.ChannelDispatchers)
{ &lt;font color="#008000"&gt; &lt;font color="#008000"&gt;&lt;span style="color: green"&gt;// Add the
error handler...&lt;/span&gt; &lt;/font&gt;&lt;span style="color: green"&gt; // Add the parameter inspector
that gets the request info and&lt;/span&gt; &lt;span style="color: green"&gt; // stores it in
the outgoing message properties&lt;/span&gt;&lt;/font&gt; &lt;span style="color: blue"&gt;foreach &lt;/span&gt;(&lt;span style="color: #2b91af"&gt;EndpointDispatcher &lt;/span&gt;endpoint &lt;span style="color: blue"&gt;in &lt;/span&gt;dispatcher.Endpoints)
endpoint.DispatchRuntime.MessageInspectors.Add( &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;HttpRequestInformationInspector&lt;/span&gt;());
} } }&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; 
&lt;p&gt;
So now error handlers have this info at their disposal. Then in the message inspector
we grab the http request info and stash it in the outgoing message properties:
&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;private class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;HttpRequestInformationInspector &lt;/span&gt;: &lt;span style="color: #2b91af"&gt;IDispatchMessageInspector &lt;/span&gt;{ &lt;span style="color: blue"&gt;public
object &lt;/span&gt;AfterReceiveRequest(&lt;span style="color: blue"&gt;ref &lt;/span&gt;System.ServiceModel.Channels.&lt;span style="color: #2b91af"&gt;Message &lt;/span&gt;request, &lt;span style="color: #2b91af"&gt;IClientChannel &lt;/span&gt;channel, &lt;span style="color: #2b91af"&gt;InstanceContext &lt;/span&gt;instanceContext)
{ &lt;span style="color: #2b91af"&gt;HttpRequestInformation &lt;/span&gt;info = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;HttpRequestInformation&lt;/span&gt;(); &lt;span style="color: blue"&gt;string &lt;/span&gt;contentLengthHeader
= &lt;span style="color: #2b91af"&gt;WebOperationContext&lt;/span&gt;.Current.IncomingRequest.Headers[&lt;span style="color: #2b91af"&gt;HttpRequestHeader&lt;/span&gt;.ContentLength]; &lt;span style="color: blue"&gt;long &lt;/span&gt;contentLength; &lt;span style="color: blue"&gt;if &lt;/span&gt;(!&lt;span style="color: blue"&gt;string&lt;/span&gt;.IsNullOrEmpty(contentLengthHeader)) &lt;span style="color: blue"&gt;long&lt;/span&gt;.TryParse(contentLengthHeader, &lt;span style="color: blue"&gt;out &lt;/span&gt;contentLength); &lt;span style="color: blue"&gt;else &lt;/span&gt;contentLength
= -1; info.ContentLength = contentLength; info.Uri = &lt;span style="color: #2b91af"&gt;OperationContext&lt;/span&gt;.Current.IncomingMessageHeaders.To;
info.Method = &lt;span style="color: #2b91af"&gt;WebOperationContext&lt;/span&gt;.Current.IncomingRequest.Method;
info.ContentType = &lt;span style="color: #2b91af"&gt;WebOperationContext&lt;/span&gt;.Current.IncomingRequest.ContentType;
info.Accept = &lt;span style="color: #2b91af"&gt;WebOperationContext&lt;/span&gt;.Current.IncomingRequest.Accept;
info.UserAgent = &lt;span style="color: #2b91af"&gt;WebOperationContext&lt;/span&gt;.Current.IncomingRequest.UserAgent;
info.Headers = &lt;span style="color: #2b91af"&gt;WebOperationContext&lt;/span&gt;.Current.IncomingRequest.Headers; &lt;span style="color: #2b91af"&gt;OperationContext&lt;/span&gt;.Current.OutgoingMessageProperties.Add(
HttpRequestInformationProperty, info); &lt;span style="color: blue"&gt;return null&lt;/span&gt;;
} &lt;span style="color: blue"&gt;public void &lt;/span&gt;BeforeSendReply(&lt;span style="color: blue"&gt;ref &lt;/span&gt;System.ServiceModel.Channels.&lt;span style="color: #2b91af"&gt;Message &lt;/span&gt;reply, &lt;span style="color: blue"&gt;object &lt;/span&gt;correlationState)
{ } }&lt;/pre&gt;&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;HttpRequestInformation &lt;/span&gt;{ &lt;span style="color: blue"&gt;public &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Uri &lt;/span&gt;Uri
{ &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; } &lt;span style="color: blue"&gt;public
string &lt;/span&gt;Method { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;;
} &lt;span style="color: blue"&gt;public string &lt;/span&gt;ContentType { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;;
} &lt;span style="color: blue"&gt;public long &lt;/span&gt;ContentLength { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;;
} &lt;span style="color: blue"&gt;public string &lt;/span&gt;Accept { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;;
} &lt;span style="color: blue"&gt;public string &lt;/span&gt;UserAgent { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;;
} &lt;span style="color: blue"&gt;public &lt;/span&gt;&lt;span style="color: #2b91af"&gt;WebHeaderCollection &lt;/span&gt;Headers
{ &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; } }&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; 
&lt;p&gt;
Note that I'm manually grabbing the content length from the headers. Interestingly
the content length header may not always be present. And its absence may not mean
that the content length is zero (That's why I return a negative number if it is absent).
The problem is the IncomingWebRequestContext ContentLength property does not check
for the existence of the header before it parses it. So if its not there (Which is
what's going to happen with a GET request) it will throw a null ref exception (Which
can be confusing). I have submitted feedback &lt;a href="https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=462088"&gt;here&lt;/a&gt; it
you want to read more about this and Microsoft's response. 
&lt;/p&gt;
&lt;p&gt;
So now it's available in response code:
&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public bool &lt;/span&gt;HandleError(&lt;span style="color: #2b91af"&gt;Exception &lt;/span&gt;error)
{ &lt;span style="color: #2b91af"&gt;HttpRequestInformation &lt;/span&gt;info; &lt;span style="color: blue"&gt;if &lt;/span&gt;(&lt;span style="color: #2b91af"&gt;OperationContext&lt;/span&gt;.Current.OutgoingMessageProperties.ContainsKey( &lt;span style="color: #2b91af"&gt;ErrorHandlerBehavior&lt;/span&gt;.HttpRequestInformationProperty))
info = (&lt;span style="color: #2b91af"&gt;RequestInformation&lt;/span&gt;)&lt;span style="color: #2b91af"&gt;OperationContext&lt;/span&gt;.Current.OutgoingMessageProperties[ &lt;span style="color: #2b91af"&gt;ErrorHandlerBehavior&lt;/span&gt;.HttpRequestInformationProperty]; &lt;span style="color: blue"&gt;else &lt;/span&gt;info
= &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;RequestInformation&lt;/span&gt;();
LogHandler.Write(error, info); &lt;span style="color: blue"&gt;return true&lt;/span&gt;; }&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; 
&lt;p&gt;
Do you know of a better way to handle this? If so, please leave a comment as I'm not
100% satisfied with this approach and would be interested in a simpler alternative. 
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.mike-obrien.net/aggbug.ashx?id=24cb015c-f143-44d1-acce-9bf914a10eff" /&gt;</description>
      <comments>http://blog.mike-obrien.net/CommentView,guid,24cb015c-f143-44d1-acce-9bf914a10eff.aspx</comments>
      <category>.NET 3.5</category>
      <category>WCF</category>
    <feedburner:origLink>http://blog.mike-obrien.net/PermaLink,guid,24cb015c-f143-44d1-acce-9bf914a10eff.aspx</feedburner:origLink></item>
    <item>
      <trackback:ping>http://blog.mike-obrien.net/Trackback.aspx?guid=b48125b9-dd73-4b10-8da8-39da27ec501f</trackback:ping>
      <pingback:server>http://blog.mike-obrien.net/pingback.aspx</pingback:server>
      <pingback:target>http://blog.mike-obrien.net/PermaLink,guid,b48125b9-dd73-4b10-8da8-39da27ec501f.aspx</pingback:target>
      <dc:creator>mob</dc:creator>
      <wfw:comment>http://blog.mike-obrien.net/CommentView,guid,b48125b9-dd73-4b10-8da8-39da27ec501f.aspx</wfw:comment>
      <wfw:commentRss>http://blog.mike-obrien.net/SyndicationService.asmx/GetEntryCommentsRss?guid=b48125b9-dd73-4b10-8da8-39da27ec501f</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
I'm now to the point where I have more than one source file in an F# project (Woo
hoo!) and the issue of entry point came up. Here is what the language spec <a href="http://research.microsoft.com/en-us/um/cambridge/projects/fsharp/manual/spec2.aspx#_Toc207785751">says
about it</a> (As of 1.9.6):
</p>
        <p>
          <em>"12.1.4 Explicit “Main” Entry Point</em>
        </p>
        <p>
          <em>The last file specified in the compilation order for a .EXE may additionally contain
an explicit entry point, indicated by annotating a function in a module with EntryPointAttribute.</em>
        </p>
        <p>
          <em>     - The attribute can be applied only to a let-bound function
in a module. It may not be a member.</em>
        </p>
        <p>
          <em>     - Only one function may be given this attribute, and
this must be the last declaration in the last file processed on the command line.
The function may be in a nested module.</em>
        </p>
        <p>
          <em>     - The function is asserted to have type “string[] -&gt;
int” prior to being checked. If this assertion fails an error is reported.</em>
        </p>
        <p>
          <em>     - At runtime the arguments passed on startup are an array
containing the same as entries as System.Environment.GetCommandLineArgs(), minus the
first entry in that array.</em>
        </p>
        <p>
          <em>The function becomes the entry point to the program. It immediately forces the
static initializer for the file in which the function exists. It will then run the
body of the function."</em>
        </p>
        <img width="0" height="0" src="http://blog.mike-obrien.net/aggbug.ashx?id=b48125b9-dd73-4b10-8da8-39da27ec501f" />
      <xhtml:img xmlns:xhtml="http://www.w3.org/1999/xhtml" src="http://feeds.feedburner.com/~r/MobsDevBlog/~4/O9wrnj4TSfk" height="1" width="1" /></body>
      <title>Entry Point in F#</title>
      <guid isPermaLink="false">http://blog.mike-obrien.net/PermaLink,guid,b48125b9-dd73-4b10-8da8-39da27ec501f.aspx</guid>
      <link>http://feedproxy.google.com/~r/MobsDevBlog/~3/O9wrnj4TSfk/PermaLink,guid,b48125b9-dd73-4b10-8da8-39da27ec501f.aspx</link>
      <pubDate>Mon, 08 Jun 2009 13:12:30 GMT</pubDate>
      <description>&lt;p&gt;
I'm now to the point where I have more than one source file in an F# project (Woo
hoo!) and the issue of entry point came up. Here is what the language spec &lt;a href="http://research.microsoft.com/en-us/um/cambridge/projects/fsharp/manual/spec2.aspx#_Toc207785751"&gt;says
about it&lt;/a&gt; (As of 1.9.6):
&lt;/p&gt;
&lt;p&gt;
&lt;em&gt;"12.1.4 Explicit “Main” Entry Point&lt;/em&gt; 
&lt;p&gt;
&lt;em&gt;The last file specified in the compilation order for a .EXE may additionally contain
an explicit entry point, indicated by annotating a function in a module with EntryPointAttribute.&lt;/em&gt; 
&lt;p&gt;
&lt;em&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; - The attribute can be applied only to a let-bound function
in a module. It may not be a member.&lt;/em&gt; 
&lt;p&gt;
&lt;em&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; - Only one function may be given this attribute, and
this must be the last declaration in the last file processed on the command line.
The function may be in a nested module.&lt;/em&gt; 
&lt;p&gt;
&lt;em&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; - The function is asserted to have type “string[] -&amp;gt;
int” prior to being checked. If this assertion fails an error is reported.&lt;/em&gt; 
&lt;p&gt;
&lt;em&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; - At runtime the arguments passed on startup are an array
containing the same as entries as System.Environment.GetCommandLineArgs(), minus the
first entry in that array.&lt;/em&gt; 
&lt;p&gt;
&lt;em&gt;The function becomes the entry point to the program. It immediately forces the
static initializer for the file in which the function exists. It will then run the
body of the function."&lt;/em&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.mike-obrien.net/aggbug.ashx?id=b48125b9-dd73-4b10-8da8-39da27ec501f" /&gt;</description>
      <comments>http://blog.mike-obrien.net/CommentView,guid,b48125b9-dd73-4b10-8da8-39da27ec501f.aspx</comments>
      <category>.NET 3.5</category>
      <category>F#</category>
    <feedburner:origLink>http://blog.mike-obrien.net/PermaLink,guid,b48125b9-dd73-4b10-8da8-39da27ec501f.aspx</feedburner:origLink></item>
    <item>
      <trackback:ping>http://blog.mike-obrien.net/Trackback.aspx?guid=e42c0038-3814-4b52-9f57-fe21e91fd788</trackback:ping>
      <pingback:server>http://blog.mike-obrien.net/pingback.aspx</pingback:server>
      <pingback:target>http://blog.mike-obrien.net/PermaLink,guid,e42c0038-3814-4b52-9f57-fe21e91fd788.aspx</pingback:target>
      <dc:creator>mob</dc:creator>
      <wfw:comment>http://blog.mike-obrien.net/CommentView,guid,e42c0038-3814-4b52-9f57-fe21e91fd788.aspx</wfw:comment>
      <wfw:commentRss>http://blog.mike-obrien.net/SyndicationService.asmx/GetEntryCommentsRss?guid=e42c0038-3814-4b52-9f57-fe21e91fd788</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
One of the things I really wanted with our REST API is per method basic authentication.
So lets say we have a library with a books resource. We want everyone to be able to
read this resource but we want people to authenticate when modifying this resource.
As far as I could tell there is no way to do this OOB with WCF REST without resorting
to breaking your resource up into two parts, read and write and hosting the secure
one in a separate IIS app with basic auth enabled. The only way I could figure out
how to do this properly is implement basic auth in an operation invoker. This way
you could define authentication on a per method basis as follows (With the BasicAuthenticationInvoker)
without breaking the resource up:
</p>
        <pre class="code">[<span style="color: #2b91af">ServiceContract</span>] <span style="color: blue">public
interface </span><span style="color: #2b91af">IBookService </span>{ [<span style="color: #2b91af">WebGet</span>(UriTemplate
= <span style="color: #a31515">"/{isbn}"</span>)] [<span style="color: #2b91af">OperationContract</span>] <span style="color: #2b91af">Book </span>GetBook(<span style="color: blue">string </span>isbn);
[<span style="color: #2b91af">WebInvoke</span>(UriTemplate = <span style="color: #a31515">"/{isbn}"</span>,
Method=<span style="color: #2b91af">Verbs</span>.Delete)] [<span style="color: #2b91af">OperationContract</span>]
[<span style="color: #2b91af">BasicAuthenticationInvoker</span>] <span style="color: blue">void </span>DeleteBook(<span style="color: blue">string </span>isbn);
}</pre>
        <a href="http://11011.net/software/vspaste">
        </a>
        <p>
It's pretty simple to do this and doesn't require integration with IIS. First define
a class that will act as a behavior attribute and an operation invoker:
</p>
        <pre class="code">
          <span style="color: blue">public class </span>
          <span style="color: #2b91af">BasicAuthenticationInvoker </span>: <span style="color: #2b91af">Attribute</span>, <span style="color: #2b91af">IOperationBehavior</span>, <span style="color: #2b91af">IOperationInvoker </span>{
}</pre>
        <a href="http://11011.net/software/vspaste">
        </a>
        <p>
Next, implement the operation behavior. We will store the original invoker to call
if the user successfully authenticates, our invoker will basically act as a proxy.
We can ignore the other three implemented methods.
</p>
        <pre class="code">
          <span style="color: blue">#region </span>Private Fields <span style="color: blue">private </span><span style="color: #2b91af">IOperationInvoker </span>_invoker; <span style="color: blue">#endregion </span><span style="color: blue"> #region </span>IOperationBehavior
Members <span style="color: blue">public void </span>ApplyDispatchBehavior(<span style="color: #2b91af">OperationDescription </span>operationDescription, <span style="color: #2b91af">DispatchOperation </span>dispatchOperation)
{ _invoker = dispatchOperation.Invoker; dispatchOperation.Invoker = <span style="color: blue">this</span>;
} <span style="color: blue">public void </span>ApplyClientBehavior(<span style="color: #2b91af">OperationDescription </span>operationDescription, <span style="color: #2b91af">ClientOperation </span>clientOperation)
{ } <span style="color: blue">public void </span>AddBindingParameters(<span style="color: #2b91af">OperationDescription </span>operationDescription, <span style="color: #2b91af">BindingParameterCollection </span>bindingParameters)
{ } <span style="color: blue">public void </span>Validate(<span style="color: #2b91af">OperationDescription </span>operationDescription)
{ } <span style="color: blue">#endregion</span></pre>
        <a href="http://11011.net/software/vspaste">
        </a>
        <a href="http://11011.net/software/vspaste">
        </a>
        <p>
Now implement the operation invoker. First we call our private authenticate method,
if this is successful we'll call the invoker, otherwise we return nothing.
</p>
        <pre class="code">
          <span style="color: blue">#region </span>IOperationInvoker Members <span style="color: blue">public
object </span>Invoke(<span style="color: blue">object </span>instance, <span style="color: blue">object</span>[]
inputs, <span style="color: blue">out object</span>[] outputs) { <span style="color: blue">if </span>(Authenticate(<span style="color: #a31515">"New
York Public Library"</span>)) <span style="color: blue">return </span>_invoker.Invoke(instance,
inputs, <span style="color: blue">out </span>outputs); <span style="color: blue">else </span>{
outputs = <span style="color: blue">null</span>; <span style="color: blue">return
null</span>; } } <span style="color: blue"> public object</span>[] AllocateInputs()
{ <span style="color: blue">return </span>_invoker.AllocateInputs(); } <span style="color: blue">public </span><span style="color: #2b91af">IAsyncResult </span>InvokeBegin(<span style="color: blue">object </span>instance, <span style="color: blue">object</span>[]
inputs, <span style="color: #2b91af">AsyncCallback </span>callback, <span style="color: blue">object </span>state)
{ <span style="color: blue">throw new </span><span style="color: #2b91af">NotSupportedException</span>();
} <span style="color: blue">public object </span>InvokeEnd(<span style="color: blue">object </span>instance, <span style="color: blue">out
object</span>[] outputs, <span style="color: #2b91af">IAsyncResult </span>result)
{ <span style="color: blue">throw new </span><span style="color: #2b91af">NotSupportedException</span>();
} <span style="color: blue">public bool </span>IsSynchronous { <span style="color: blue">get </span>{ <span style="color: blue">return
true</span>; } } <span style="color: blue">#endregion</span></pre>
        <a href="http://11011.net/software/vspaste">
        </a>
        <p>
The private authentication methods are as follows. The authenticate method checks
the username and password and if successful returns true, otherwise it sets the authenticate
header and the status code to unauthorized. 
</p>
        <pre class="code">
          <span style="color: blue">private bool </span>Authenticate(<span style="color: blue">string </span>realm)
{ <span style="color: blue">string</span>[] credentials = GetCredentials(<span style="color: #2b91af">WebOperationContext</span>.Current.IncomingRequest.Headers); <span style="color: blue">if </span>(credentials
!= <span style="color: blue">null </span>&amp;&amp; credentials[0] == <span style="color: #a31515">"tony" </span>&amp;&amp;
credentials[1] == <span style="color: #a31515">"clifton"</span>) <span style="color: blue">return
true</span>; <span style="color: #2b91af">WebOperationContext</span>.Current.OutgoingResponse.Headers[<span style="color: #a31515">"WWW-Authenticate"</span>]
= <span style="color: blue">string</span>.Format(<span style="color: #a31515">"Basic
realm=\"{0}\""</span>, realm); <span style="color: #2b91af">WebOperationContext</span>.Current.OutgoingResponse.StatusCode
= <span style="color: #2b91af">HttpStatusCode</span>.Unauthorized; <span style="color: blue">return
false</span>; } <span style="color: blue">private string</span>[] GetCredentials(<span style="color: #2b91af">WebHeaderCollection </span>headers)
{ <span style="color: blue">string </span>credentials = <span style="color: #2b91af">WebOperationContext</span>.Current.IncomingRequest.
Headers[<span style="color: #a31515">"Authorization"</span>]; <span style="color: blue">if </span>(credentials
!= <span style="color: blue">null</span>) credentials = credentials.Trim(); <span style="color: blue">if </span>(!<span style="color: blue">string</span>.IsNullOrEmpty(credentials))
{ <span style="color: blue">try </span>{ <span style="color: blue">string</span>[]
credentialParts = credentials.Split(<span style="color: blue">new char</span>[] { <span style="color: #a31515">'
' </span>}); <span style="color: blue">if </span>(credentialParts.Length == 2 &amp;&amp;
credentialParts[0].Equals(<span style="color: #a31515">"basic"</span>, <span style="color: #2b91af">StringComparison</span>.OrdinalIgnoreCase))
{ credentials = <span style="color: #2b91af">ASCIIEncoding</span>.ASCII.GetString( <span style="color: #2b91af">Convert</span>.FromBase64String(credentialParts[1]));
credentialParts = credentials.Split(<span style="color: blue">new char</span>[] { <span style="color: #a31515">':' </span>}); <span style="color: blue">if </span>(credentialParts.Length
== 2) <span style="color: blue">return </span>credentialParts; } } <span style="color: blue">catch </span>{
} } <span style="color: blue">return null</span>; }</pre>
        <a href="http://11011.net/software/vspaste">
        </a>
        <p>
This same approach could also be used to examine parameters as part of the authentication
process (If you are using tokens or the like). 
</p>
        <img width="0" height="0" src="http://blog.mike-obrien.net/aggbug.ashx?id=e42c0038-3814-4b52-9f57-fe21e91fd788" />
      <xhtml:img xmlns:xhtml="http://www.w3.org/1999/xhtml" src="http://feeds.feedburner.com/~r/MobsDevBlog/~4/yav4BRfHY5M" height="1" width="1" /></body>
      <title>WCF REST Per Method Basic Authentication</title>
      <guid isPermaLink="false">http://blog.mike-obrien.net/PermaLink,guid,e42c0038-3814-4b52-9f57-fe21e91fd788.aspx</guid>
      <link>http://feedproxy.google.com/~r/MobsDevBlog/~3/yav4BRfHY5M/PermaLink,guid,e42c0038-3814-4b52-9f57-fe21e91fd788.aspx</link>
      <pubDate>Tue, 26 May 2009 18:47:08 GMT</pubDate>
      <description>&lt;p&gt;
One of the things I really wanted with our REST API is per method basic authentication.
So lets say we have a library with a books resource. We want everyone to be able to
read this resource but we want people to authenticate when modifying this resource.
As far as I could tell there is no way to do this OOB with WCF REST without resorting
to breaking your resource up into two parts, read and write and hosting the secure
one in a separate IIS app with basic auth enabled. The only way I could figure out
how to do this properly is implement basic auth in an operation invoker. This way
you could define authentication on a per method basis as follows (With the BasicAuthenticationInvoker)
without breaking the resource up:
&lt;/p&gt;
&lt;pre class="code"&gt;[&lt;span style="color: #2b91af"&gt;ServiceContract&lt;/span&gt;] &lt;span style="color: blue"&gt;public
interface &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IBookService &lt;/span&gt;{ [&lt;span style="color: #2b91af"&gt;WebGet&lt;/span&gt;(UriTemplate
= &lt;span style="color: #a31515"&gt;"/{isbn}"&lt;/span&gt;)] [&lt;span style="color: #2b91af"&gt;OperationContract&lt;/span&gt;] &lt;span style="color: #2b91af"&gt;Book &lt;/span&gt;GetBook(&lt;span style="color: blue"&gt;string &lt;/span&gt;isbn);
[&lt;span style="color: #2b91af"&gt;WebInvoke&lt;/span&gt;(UriTemplate = &lt;span style="color: #a31515"&gt;"/{isbn}"&lt;/span&gt;,
Method=&lt;span style="color: #2b91af"&gt;Verbs&lt;/span&gt;.Delete)] [&lt;span style="color: #2b91af"&gt;OperationContract&lt;/span&gt;]
[&lt;span style="color: #2b91af"&gt;BasicAuthenticationInvoker&lt;/span&gt;] &lt;span style="color: blue"&gt;void &lt;/span&gt;DeleteBook(&lt;span style="color: blue"&gt;string &lt;/span&gt;isbn);
}&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; 
&lt;p&gt;
It's pretty simple to do this and doesn't require integration with IIS. First define
a class that will act as a behavior attribute and an operation invoker:
&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;BasicAuthenticationInvoker &lt;/span&gt;: &lt;span style="color: #2b91af"&gt;Attribute&lt;/span&gt;, &lt;span style="color: #2b91af"&gt;IOperationBehavior&lt;/span&gt;, &lt;span style="color: #2b91af"&gt;IOperationInvoker &lt;/span&gt;{
}&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; 
&lt;p&gt;
Next, implement the operation behavior. We will store the original invoker to call
if the user successfully authenticates, our invoker will basically act as a proxy.
We can ignore the other three implemented methods.
&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;#region &lt;/span&gt;Private Fields &lt;span style="color: blue"&gt;private &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IOperationInvoker &lt;/span&gt;_invoker; &lt;span style="color: blue"&gt;#endregion &lt;/span&gt;&lt;span style="color: blue"&gt; #region &lt;/span&gt;IOperationBehavior
Members &lt;span style="color: blue"&gt;public void &lt;/span&gt;ApplyDispatchBehavior(&lt;span style="color: #2b91af"&gt;OperationDescription &lt;/span&gt;operationDescription, &lt;span style="color: #2b91af"&gt;DispatchOperation &lt;/span&gt;dispatchOperation)
{ _invoker = dispatchOperation.Invoker; dispatchOperation.Invoker = &lt;span style="color: blue"&gt;this&lt;/span&gt;;
} &lt;span style="color: blue"&gt;public void &lt;/span&gt;ApplyClientBehavior(&lt;span style="color: #2b91af"&gt;OperationDescription &lt;/span&gt;operationDescription, &lt;span style="color: #2b91af"&gt;ClientOperation &lt;/span&gt;clientOperation)
{ } &lt;span style="color: blue"&gt;public void &lt;/span&gt;AddBindingParameters(&lt;span style="color: #2b91af"&gt;OperationDescription &lt;/span&gt;operationDescription, &lt;span style="color: #2b91af"&gt;BindingParameterCollection &lt;/span&gt;bindingParameters)
{ } &lt;span style="color: blue"&gt;public void &lt;/span&gt;Validate(&lt;span style="color: #2b91af"&gt;OperationDescription &lt;/span&gt;operationDescription)
{ } &lt;span style="color: blue"&gt;#endregion&lt;/span&gt;&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; 
&lt;p&gt;
Now implement the operation invoker. First we call our private authenticate method,
if this is successful we'll call the invoker, otherwise we return nothing.
&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;#region &lt;/span&gt;IOperationInvoker Members &lt;span style="color: blue"&gt;public
object &lt;/span&gt;Invoke(&lt;span style="color: blue"&gt;object &lt;/span&gt;instance, &lt;span style="color: blue"&gt;object&lt;/span&gt;[]
inputs, &lt;span style="color: blue"&gt;out object&lt;/span&gt;[] outputs) { &lt;span style="color: blue"&gt;if &lt;/span&gt;(Authenticate(&lt;span style="color: #a31515"&gt;"New
York Public Library"&lt;/span&gt;)) &lt;span style="color: blue"&gt;return &lt;/span&gt;_invoker.Invoke(instance,
inputs, &lt;span style="color: blue"&gt;out &lt;/span&gt;outputs); &lt;span style="color: blue"&gt;else &lt;/span&gt;{
outputs = &lt;span style="color: blue"&gt;null&lt;/span&gt;; &lt;span style="color: blue"&gt;return
null&lt;/span&gt;; } } &lt;span style="color: blue"&gt; public object&lt;/span&gt;[] AllocateInputs()
{ &lt;span style="color: blue"&gt;return &lt;/span&gt;_invoker.AllocateInputs(); } &lt;span style="color: blue"&gt;public &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IAsyncResult &lt;/span&gt;InvokeBegin(&lt;span style="color: blue"&gt;object &lt;/span&gt;instance, &lt;span style="color: blue"&gt;object&lt;/span&gt;[]
inputs, &lt;span style="color: #2b91af"&gt;AsyncCallback &lt;/span&gt;callback, &lt;span style="color: blue"&gt;object &lt;/span&gt;state)
{ &lt;span style="color: blue"&gt;throw new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;NotSupportedException&lt;/span&gt;();
} &lt;span style="color: blue"&gt;public object &lt;/span&gt;InvokeEnd(&lt;span style="color: blue"&gt;object &lt;/span&gt;instance, &lt;span style="color: blue"&gt;out
object&lt;/span&gt;[] outputs, &lt;span style="color: #2b91af"&gt;IAsyncResult &lt;/span&gt;result)
{ &lt;span style="color: blue"&gt;throw new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;NotSupportedException&lt;/span&gt;();
} &lt;span style="color: blue"&gt;public bool &lt;/span&gt;IsSynchronous { &lt;span style="color: blue"&gt;get &lt;/span&gt;{ &lt;span style="color: blue"&gt;return
true&lt;/span&gt;; } } &lt;span style="color: blue"&gt;#endregion&lt;/span&gt;&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; 
&lt;p&gt;
The private authentication methods are as follows. The authenticate method checks
the username and password and if successful returns true, otherwise it sets the authenticate
header and the status code to unauthorized. 
&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;private bool &lt;/span&gt;Authenticate(&lt;span style="color: blue"&gt;string &lt;/span&gt;realm)
{ &lt;span style="color: blue"&gt;string&lt;/span&gt;[] credentials = GetCredentials(&lt;span style="color: #2b91af"&gt;WebOperationContext&lt;/span&gt;.Current.IncomingRequest.Headers); &lt;span style="color: blue"&gt;if &lt;/span&gt;(credentials
!= &lt;span style="color: blue"&gt;null &lt;/span&gt;&amp;amp;&amp;amp; credentials[0] == &lt;span style="color: #a31515"&gt;"tony" &lt;/span&gt;&amp;amp;&amp;amp;
credentials[1] == &lt;span style="color: #a31515"&gt;"clifton"&lt;/span&gt;) &lt;span style="color: blue"&gt;return
true&lt;/span&gt;; &lt;span style="color: #2b91af"&gt;WebOperationContext&lt;/span&gt;.Current.OutgoingResponse.Headers[&lt;span style="color: #a31515"&gt;"WWW-Authenticate"&lt;/span&gt;]
= &lt;span style="color: blue"&gt;string&lt;/span&gt;.Format(&lt;span style="color: #a31515"&gt;"Basic
realm=\"{0}\""&lt;/span&gt;, realm); &lt;span style="color: #2b91af"&gt;WebOperationContext&lt;/span&gt;.Current.OutgoingResponse.StatusCode
= &lt;span style="color: #2b91af"&gt;HttpStatusCode&lt;/span&gt;.Unauthorized; &lt;span style="color: blue"&gt;return
false&lt;/span&gt;; } &lt;span style="color: blue"&gt;private string&lt;/span&gt;[] GetCredentials(&lt;span style="color: #2b91af"&gt;WebHeaderCollection &lt;/span&gt;headers)
{ &lt;span style="color: blue"&gt;string &lt;/span&gt;credentials = &lt;span style="color: #2b91af"&gt;WebOperationContext&lt;/span&gt;.Current.IncomingRequest.
Headers[&lt;span style="color: #a31515"&gt;"Authorization"&lt;/span&gt;]; &lt;span style="color: blue"&gt;if &lt;/span&gt;(credentials
!= &lt;span style="color: blue"&gt;null&lt;/span&gt;) credentials = credentials.Trim(); &lt;span style="color: blue"&gt;if &lt;/span&gt;(!&lt;span style="color: blue"&gt;string&lt;/span&gt;.IsNullOrEmpty(credentials))
{ &lt;span style="color: blue"&gt;try &lt;/span&gt;{ &lt;span style="color: blue"&gt;string&lt;/span&gt;[]
credentialParts = credentials.Split(&lt;span style="color: blue"&gt;new char&lt;/span&gt;[] { &lt;span style="color: #a31515"&gt;'
' &lt;/span&gt;}); &lt;span style="color: blue"&gt;if &lt;/span&gt;(credentialParts.Length == 2 &amp;amp;&amp;amp;
credentialParts[0].Equals(&lt;span style="color: #a31515"&gt;"basic"&lt;/span&gt;, &lt;span style="color: #2b91af"&gt;StringComparison&lt;/span&gt;.OrdinalIgnoreCase))
{ credentials = &lt;span style="color: #2b91af"&gt;ASCIIEncoding&lt;/span&gt;.ASCII.GetString( &lt;span style="color: #2b91af"&gt;Convert&lt;/span&gt;.FromBase64String(credentialParts[1]));
credentialParts = credentials.Split(&lt;span style="color: blue"&gt;new char&lt;/span&gt;[] { &lt;span style="color: #a31515"&gt;':' &lt;/span&gt;}); &lt;span style="color: blue"&gt;if &lt;/span&gt;(credentialParts.Length
== 2) &lt;span style="color: blue"&gt;return &lt;/span&gt;credentialParts; } } &lt;span style="color: blue"&gt;catch &lt;/span&gt;{
} } &lt;span style="color: blue"&gt;return null&lt;/span&gt;; }&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; 
&lt;p&gt;
This same approach could also be used to examine parameters as part of the authentication
process (If you are using tokens or the like). 
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.mike-obrien.net/aggbug.ashx?id=e42c0038-3814-4b52-9f57-fe21e91fd788" /&gt;</description>
      <comments>http://blog.mike-obrien.net/CommentView,guid,e42c0038-3814-4b52-9f57-fe21e91fd788.aspx</comments>
      <category>.NET 3.5</category>
      <category>REST</category>
      <category>WCF</category>
    <feedburner:origLink>http://blog.mike-obrien.net/PermaLink,guid,e42c0038-3814-4b52-9f57-fe21e91fd788.aspx</feedburner:origLink></item>
    <item>
      <trackback:ping>http://blog.mike-obrien.net/Trackback.aspx?guid=068f1438-38ac-473d-935c-6fdb5a4ad5c3</trackback:ping>
      <pingback:server>http://blog.mike-obrien.net/pingback.aspx</pingback:server>
      <pingback:target>http://blog.mike-obrien.net/PermaLink,guid,068f1438-38ac-473d-935c-6fdb5a4ad5c3.aspx</pingback:target>
      <dc:creator>mob</dc:creator>
      <wfw:comment>http://blog.mike-obrien.net/CommentView,guid,068f1438-38ac-473d-935c-6fdb5a4ad5c3.aspx</wfw:comment>
      <wfw:commentRss>http://blog.mike-obrien.net/SyndicationService.asmx/GetEntryCommentsRss?guid=068f1438-38ac-473d-935c-6fdb5a4ad5c3</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
This is really lame. I'm creating a custom service host (That inherits from WebServiceHost)
and I want to be able to programmatically add behaviors from configuration. Since
the WebServiceHost is zero config there are no service elements to define a behavior
config. Now I know I could manually create those service elements with an webHttpBinding
and specify the behavior config, but I really do like the zero configuration feature,
I just want to be able to define behaviors in config. Unfortunately the BehaviorExtensionElement's
CreateBehavior is marked as internal. <a href="http://social.msdn.microsoft.com/forums/en-US/wcf/thread/d00d2a87-4d89-4510-9569-fab73a3ad070">I'm
not sure if M$ will make this public in the future or not but it looks like the only
good way to get around it for now is with reflection (Or create a dummy service element
with the behavior config, have the runtime load the behaviors and then copy them to
your service, boo)</a>. I think in this instance reflection on an internal member
is safe as that method has to be there in order for the behavior to be created by
the runtime. I don't see how it can be changed since that would break custom behaviors.
I snagged this simple extension method from the aforementioned thread, it uses reflection
to invoke the internal CreateBehavior method.
</p>
        <pre class="code">
          <span style="color: blue">public static object </span>CreateBehavior(<span style="color: blue">this </span><span style="color: #2b91af">BehaviorExtensionElement </span>extensionElement)
{ <span style="color: blue">return </span>extensionElement.GetType(). GetMethod( <span style="color: #a31515">"CreateBehavior"</span>,
System.Reflection.<span style="color: #2b91af">BindingFlags</span>.Instance | System.Reflection.<span style="color: #2b91af">BindingFlags</span>.NonPublic).
Invoke(extensionElement, <span style="color: blue">new object</span>[0] { }); }</pre>
        <a href="http://11011.net/software/vspaste">
        </a>
        <p>
I also whipped up a few ServiceHost extension methods that add the behaviors (Adapted
from some reflected framework code):
</p>
        <pre class="code">
          <span style="color: blue">public static void </span>LoadBehaviors( <span style="color: blue">this </span><span style="color: #2b91af">ServiceHost </span>serviceHost, <span style="color: blue">string </span>behaviorConfiguration)
{ <span style="color: #2b91af">ServiceBehaviorElement </span>serviceBehaviors = GetServiceBehaviorElement(serviceHost,
behaviorConfiguration); <span style="color: blue">if </span>(serviceBehaviors != <span style="color: blue">null</span>)
{ <span style="color: blue">foreach </span>(<span style="color: #2b91af">BehaviorExtensionElement </span>behaviorExtension <span style="color: blue">in </span>serviceBehaviors)
{ <span style="color: blue">object </span>extension = behaviorExtension.CreateBehavior(); <span style="color: blue">if </span>(extension
!= <span style="color: blue">null</span>) { <span style="color: #2b91af">Type </span>extensionType
= extension.GetType(); <span style="color: blue">if </span>(<span style="color: blue">typeof</span>(<span style="color: #2b91af">IServiceBehavior</span>).IsAssignableFrom(extensionType))
{ <span style="color: blue">if </span>(serviceHost.Description.Behaviors.Contains(extensionType))
{ serviceHost.Description.Behaviors.Remove(extensionType); } serviceHost.Description.Behaviors.Add((<span style="color: #2b91af">IServiceBehavior</span>)extension);
} } } } } <span style="color: blue">public static </span><span style="color: #2b91af">ServiceBehaviorElement </span>GetServiceBehaviorElement( <span style="color: blue">this </span><span style="color: #2b91af">ServiceHost </span>serviceHost, <span style="color: blue">string </span>behaviorConfiguration)
{ <span style="color: #2b91af">BehaviorsSection </span>behaviorsSection = (<span style="color: #2b91af">BehaviorsSection</span>)<span style="color: #2b91af">ConfigurationManager</span>.
GetSection(<span style="color: #a31515">"system.serviceModel/behaviors"</span>); <span style="color: blue">foreach </span>(<span style="color: #2b91af">ServiceBehaviorElement </span>behavior <span style="color: blue">in </span>behaviorsSection.ServiceBehaviors)
{ <span style="color: blue">if </span>(behavior.Name == behaviorConfiguration) <span style="color: blue">return </span>behavior;
} <span style="color: blue">return null</span>; }</pre>
        <a href="http://11011.net/software/vspaste">
        </a>
        <p>
Then you simply run this on the ServiceHost:
</p>
        <pre class="code">serviceHost.LoadBehaviors(<span style="color: #a31515">"myBehaviorConfig"</span>);</pre>
        <p>
          <a href="http://11011.net/software/vspaste">
          </a>Now if your creating your own service
host you can override the ApplyConfiguration method and add them there. In the following
example I add a check to see if there is a service element for this service. If not
then I apply the behaviors, otherwise I skip it and assume that the service element
has the desired configuration.
</p>
        <pre class="code">
          <span style="color: blue">protected override void </span>ApplyConfiguration()
{ <span style="color: blue">base</span>.ApplyConfiguration(); <span style="color: blue">if </span>(!<span style="color: blue">this</span>.HasServiceElement()) <span style="color: blue">this</span>.LoadBehaviors(<span style="color: #a31515">"myBehaviorConfig"</span>);
}</pre>
        <a href="http://11011.net/software/vspaste">
        </a>The following is the HasServiceElement
&amp; supporting GetServiceElement ServiceHost extension methods:<pre class="code"><span style="color: blue">public
static bool </span>HasServiceElement(<span style="color: blue">this </span><span style="color: #2b91af">ServiceHost </span>serviceHost)
{ <span style="color: blue">return </span>(GetServiceElement(serviceHost) != <span style="color: blue">null</span>);
} <span style="color: blue">public static </span><span style="color: #2b91af">ServiceElement </span>GetServiceElement(<span style="color: blue">this </span><span style="color: #2b91af">ServiceHost </span>serviceHost)
{ <span style="color: #2b91af">ServicesSection </span>servicesSection = (<span style="color: #2b91af">ServicesSection</span>)<span style="color: #2b91af">ConfigurationManager</span>.
GetSection(<span style="color: #a31515">"system.serviceModel/services"</span>); <span style="color: #2b91af">ServiceElementCollection </span>services
= servicesSection.Services; <span style="color: blue">foreach </span>(<span style="color: #2b91af">ServiceElement </span>element <span style="color: blue">in </span>services)
{ <span style="color: blue">if </span>(element.Name == serviceHost.Description.ConfigurationName) <span style="color: blue">return </span>element;
} <span style="color: blue">return null</span>; }</pre><a href="http://11011.net/software/vspaste" /><img width="0" height="0" src="http://blog.mike-obrien.net/aggbug.ashx?id=068f1438-38ac-473d-935c-6fdb5a4ad5c3" /><xhtml:img xmlns:xhtml="http://www.w3.org/1999/xhtml" src="http://feeds.feedburner.com/~r/MobsDevBlog/~4/SD-Z31SlXsY" height="1" width="1" /></body>
      <title>BehaviorExtensionElement's CreateBehavior is Marked Internal</title>
      <guid isPermaLink="false">http://blog.mike-obrien.net/PermaLink,guid,068f1438-38ac-473d-935c-6fdb5a4ad5c3.aspx</guid>
      <link>http://feedproxy.google.com/~r/MobsDevBlog/~3/SD-Z31SlXsY/PermaLink,guid,068f1438-38ac-473d-935c-6fdb5a4ad5c3.aspx</link>
      <pubDate>Thu, 21 May 2009 20:57:40 GMT</pubDate>
      <description>&lt;p&gt;
This is really lame. I'm creating a custom service host (That inherits from WebServiceHost)
and I want to be able to programmatically add behaviors from configuration. Since
the WebServiceHost is zero config there are no service elements to define a behavior
config. Now I know I could manually create those service elements with an webHttpBinding
and specify the behavior config, but I really do like the zero configuration feature,
I just want to be able to define behaviors in config. Unfortunately the BehaviorExtensionElement's
CreateBehavior is marked as internal. &lt;a href="http://social.msdn.microsoft.com/forums/en-US/wcf/thread/d00d2a87-4d89-4510-9569-fab73a3ad070"&gt;I'm
not sure if M$ will make this public in the future or not but it looks like the only
good way to get around it for now is with reflection (Or create a dummy service element
with the behavior config, have the runtime load the behaviors and then copy them to
your service, boo)&lt;/a&gt;. I think in this instance reflection on an internal member
is safe as that method has to be there in order for the behavior to be created by
the runtime. I don't see how it can be changed since that would break custom behaviors.
I snagged this simple extension method from the aforementioned thread, it uses reflection
to invoke the internal CreateBehavior method.
&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public static object &lt;/span&gt;CreateBehavior(&lt;span style="color: blue"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af"&gt;BehaviorExtensionElement &lt;/span&gt;extensionElement)
{ &lt;span style="color: blue"&gt;return &lt;/span&gt;extensionElement.GetType(). GetMethod( &lt;span style="color: #a31515"&gt;"CreateBehavior"&lt;/span&gt;,
System.Reflection.&lt;span style="color: #2b91af"&gt;BindingFlags&lt;/span&gt;.Instance | System.Reflection.&lt;span style="color: #2b91af"&gt;BindingFlags&lt;/span&gt;.NonPublic).
Invoke(extensionElement, &lt;span style="color: blue"&gt;new object&lt;/span&gt;[0] { }); }&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; 
&lt;p&gt;
I also whipped up a few ServiceHost extension methods that add the behaviors (Adapted
from some reflected framework code):
&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public static void &lt;/span&gt;LoadBehaviors( &lt;span style="color: blue"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ServiceHost &lt;/span&gt;serviceHost, &lt;span style="color: blue"&gt;string &lt;/span&gt;behaviorConfiguration)
{ &lt;span style="color: #2b91af"&gt;ServiceBehaviorElement &lt;/span&gt;serviceBehaviors = GetServiceBehaviorElement(serviceHost,
behaviorConfiguration); &lt;span style="color: blue"&gt;if &lt;/span&gt;(serviceBehaviors != &lt;span style="color: blue"&gt;null&lt;/span&gt;)
{ &lt;span style="color: blue"&gt;foreach &lt;/span&gt;(&lt;span style="color: #2b91af"&gt;BehaviorExtensionElement &lt;/span&gt;behaviorExtension &lt;span style="color: blue"&gt;in &lt;/span&gt;serviceBehaviors)
{ &lt;span style="color: blue"&gt;object &lt;/span&gt;extension = behaviorExtension.CreateBehavior(); &lt;span style="color: blue"&gt;if &lt;/span&gt;(extension
!= &lt;span style="color: blue"&gt;null&lt;/span&gt;) { &lt;span style="color: #2b91af"&gt;Type &lt;/span&gt;extensionType
= extension.GetType(); &lt;span style="color: blue"&gt;if &lt;/span&gt;(&lt;span style="color: blue"&gt;typeof&lt;/span&gt;(&lt;span style="color: #2b91af"&gt;IServiceBehavior&lt;/span&gt;).IsAssignableFrom(extensionType))
{ &lt;span style="color: blue"&gt;if &lt;/span&gt;(serviceHost.Description.Behaviors.Contains(extensionType))
{ serviceHost.Description.Behaviors.Remove(extensionType); } serviceHost.Description.Behaviors.Add((&lt;span style="color: #2b91af"&gt;IServiceBehavior&lt;/span&gt;)extension);
} } } } } &lt;span style="color: blue"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ServiceBehaviorElement &lt;/span&gt;GetServiceBehaviorElement( &lt;span style="color: blue"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ServiceHost &lt;/span&gt;serviceHost, &lt;span style="color: blue"&gt;string &lt;/span&gt;behaviorConfiguration)
{ &lt;span style="color: #2b91af"&gt;BehaviorsSection &lt;/span&gt;behaviorsSection = (&lt;span style="color: #2b91af"&gt;BehaviorsSection&lt;/span&gt;)&lt;span style="color: #2b91af"&gt;ConfigurationManager&lt;/span&gt;.
GetSection(&lt;span style="color: #a31515"&gt;"system.serviceModel/behaviors"&lt;/span&gt;); &lt;span style="color: blue"&gt;foreach &lt;/span&gt;(&lt;span style="color: #2b91af"&gt;ServiceBehaviorElement &lt;/span&gt;behavior &lt;span style="color: blue"&gt;in &lt;/span&gt;behaviorsSection.ServiceBehaviors)
{ &lt;span style="color: blue"&gt;if &lt;/span&gt;(behavior.Name == behaviorConfiguration) &lt;span style="color: blue"&gt;return &lt;/span&gt;behavior;
} &lt;span style="color: blue"&gt;return null&lt;/span&gt;; }&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; 
&lt;p&gt;
Then you simply run this on the ServiceHost:
&lt;/p&gt;
&lt;pre class="code"&gt;serviceHost.LoadBehaviors(&lt;span style="color: #a31515"&gt;"myBehaviorConfig"&lt;/span&gt;);&lt;/pre&gt;
&lt;p&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;Now if your creating your own service
host you can override the ApplyConfiguration method and add them there. In the following
example I add a check to see if there is a service element for this service. If not
then I apply the behaviors, otherwise I skip it and assume that the service element
has the desired configuration.
&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;protected override void &lt;/span&gt;ApplyConfiguration()
{ &lt;span style="color: blue"&gt;base&lt;/span&gt;.ApplyConfiguration(); &lt;span style="color: blue"&gt;if &lt;/span&gt;(!&lt;span style="color: blue"&gt;this&lt;/span&gt;.HasServiceElement()) &lt;span style="color: blue"&gt;this&lt;/span&gt;.LoadBehaviors(&lt;span style="color: #a31515"&gt;"myBehaviorConfig"&lt;/span&gt;);
}&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;The following is the HasServiceElement
&amp;amp; supporting GetServiceElement ServiceHost extension methods:&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public
static bool &lt;/span&gt;HasServiceElement(&lt;span style="color: blue"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ServiceHost &lt;/span&gt;serviceHost)
{ &lt;span style="color: blue"&gt;return &lt;/span&gt;(GetServiceElement(serviceHost) != &lt;span style="color: blue"&gt;null&lt;/span&gt;);
} &lt;span style="color: blue"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ServiceElement &lt;/span&gt;GetServiceElement(&lt;span style="color: blue"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ServiceHost &lt;/span&gt;serviceHost)
{ &lt;span style="color: #2b91af"&gt;ServicesSection &lt;/span&gt;servicesSection = (&lt;span style="color: #2b91af"&gt;ServicesSection&lt;/span&gt;)&lt;span style="color: #2b91af"&gt;ConfigurationManager&lt;/span&gt;.
GetSection(&lt;span style="color: #a31515"&gt;"system.serviceModel/services"&lt;/span&gt;); &lt;span style="color: #2b91af"&gt;ServiceElementCollection &lt;/span&gt;services
= servicesSection.Services; &lt;span style="color: blue"&gt;foreach &lt;/span&gt;(&lt;span style="color: #2b91af"&gt;ServiceElement &lt;/span&gt;element &lt;span style="color: blue"&gt;in &lt;/span&gt;services)
{ &lt;span style="color: blue"&gt;if &lt;/span&gt;(element.Name == serviceHost.Description.ConfigurationName) &lt;span style="color: blue"&gt;return &lt;/span&gt;element;
} &lt;span style="color: blue"&gt;return null&lt;/span&gt;; }&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;img width="0" height="0" src="http://blog.mike-obrien.net/aggbug.ashx?id=068f1438-38ac-473d-935c-6fdb5a4ad5c3" /&gt;</description>
      <comments>http://blog.mike-obrien.net/CommentView,guid,068f1438-38ac-473d-935c-6fdb5a4ad5c3.aspx</comments>
      <category>.NET 3.5</category>
      <category>REST</category>
      <category>WCF</category>
    <feedburner:origLink>http://blog.mike-obrien.net/PermaLink,guid,068f1438-38ac-473d-935c-6fdb5a4ad5c3.aspx</feedburner:origLink></item>
    <item>
      <trackback:ping>http://blog.mike-obrien.net/Trackback.aspx?guid=00a2edc7-700d-4e59-94d9-b9fe1927e9a8</trackback:ping>
      <pingback:server>http://blog.mike-obrien.net/pingback.aspx</pingback:server>
      <pingback:target>http://blog.mike-obrien.net/PermaLink,guid,00a2edc7-700d-4e59-94d9-b9fe1927e9a8.aspx</pingback:target>
      <dc:creator>mob</dc:creator>
      <wfw:comment>http://blog.mike-obrien.net/CommentView,guid,00a2edc7-700d-4e59-94d9-b9fe1927e9a8.aspx</wfw:comment>
      <wfw:commentRss>http://blog.mike-obrien.net/SyndicationService.asmx/GetEntryCommentsRss?guid=00a2edc7-700d-4e59-94d9-b9fe1927e9a8</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
I just submitted a few suggestions regarding the WCF/REST implementation. If you feel
the same, please vote on them. If you don't agree with them, please leave a comment,
I'd love to hear some thoughts on these issues.
</p>
        <p>
          <a href="https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=451336">REST/WCF
Automatic Selection of Formatter Based On Accept &amp; Content Type Headers</a>
        </p>
        <p>
          <a href="https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=451312">REST/WCF
UriTemplate Querystring Parameter Name Case Sensitivity</a>
        </p>
        <p>
          <a href="https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=451296">REST/WCF
UriTemplate Optional Querystring Parameters</a>
          <a href="https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=451312">
          </a>
        </p>
        <img width="0" height="0" src="http://blog.mike-obrien.net/aggbug.ashx?id=00a2edc7-700d-4e59-94d9-b9fe1927e9a8" />
      <xhtml:img xmlns:xhtml="http://www.w3.org/1999/xhtml" src="http://feeds.feedburner.com/~r/MobsDevBlog/~4/oSZYgbHa2nU" height="1" width="1" /></body>
      <title>Suggestions for the WCF/REST Implementation</title>
      <guid isPermaLink="false">http://blog.mike-obrien.net/PermaLink,guid,00a2edc7-700d-4e59-94d9-b9fe1927e9a8.aspx</guid>
      <link>http://feedproxy.google.com/~r/MobsDevBlog/~3/oSZYgbHa2nU/PermaLink,guid,00a2edc7-700d-4e59-94d9-b9fe1927e9a8.aspx</link>
      <pubDate>Fri, 15 May 2009 17:06:19 GMT</pubDate>
      <description>&lt;p&gt;
I just submitted a few suggestions regarding the WCF/REST implementation. If you feel
the same, please vote on them. If you don't agree with them, please leave a comment,
I'd love to hear some thoughts on these issues.
&lt;/p&gt;
&lt;p&gt;
&lt;a href="https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=451336"&gt;REST/WCF
Automatic Selection of Formatter Based On Accept &amp;amp; Content Type Headers&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;a href="https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=451312"&gt;REST/WCF
UriTemplate Querystring Parameter Name Case Sensitivity&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;a href="https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=451296"&gt;REST/WCF
UriTemplate Optional Querystring Parameters&lt;/a&gt;&lt;a href="https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=451312"&gt;&lt;/a&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.mike-obrien.net/aggbug.ashx?id=00a2edc7-700d-4e59-94d9-b9fe1927e9a8" /&gt;</description>
      <comments>http://blog.mike-obrien.net/CommentView,guid,00a2edc7-700d-4e59-94d9-b9fe1927e9a8.aspx</comments>
      <category>.NET 3.5</category>
      <category>REST</category>
      <category>WCF</category>
    <feedburner:origLink>http://blog.mike-obrien.net/PermaLink,guid,00a2edc7-700d-4e59-94d9-b9fe1927e9a8.aspx</feedburner:origLink></item>
    <item>
      <trackback:ping>http://blog.mike-obrien.net/Trackback.aspx?guid=b1acadba-6ba3-4012-bd2e-ac775008dea3</trackback:ping>
      <pingback:server>http://blog.mike-obrien.net/pingback.aspx</pingback:server>
      <pingback:target>http://blog.mike-obrien.net/PermaLink,guid,b1acadba-6ba3-4012-bd2e-ac775008dea3.aspx</pingback:target>
      <dc:creator>mob</dc:creator>
      <wfw:comment>http://blog.mike-obrien.net/CommentView,guid,b1acadba-6ba3-4012-bd2e-ac775008dea3.aspx</wfw:comment>
      <wfw:commentRss>http://blog.mike-obrien.net/SyndicationService.asmx/GetEntryCommentsRss?guid=b1acadba-6ba3-4012-bd2e-ac775008dea3</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
This has been done over and over on the web so I'll add my solution to the mix. Basically
it automatically discovers the services in the web application and builds a static
mapping table. Then it examines each request and attempts to find a mapping. If it
finds one it performs the url rewriting, if not it lets it go though as is. Just register
the module and your good to go.
</p>
        <p>
Http Module:
</p>
        <pre class="code">
          <span style="color: blue">public class </span>
          <span style="color: #2b91af">ServiceAnonymityModule </span>: <span style="color: #2b91af">IHttpModule </span>{ <span style="color: blue">#region </span>IHttpModule
Implementation <span style="color: blue">public void </span>Dispose() { } <span style="color: blue">public
void </span>Init(<span style="color: #2b91af">HttpApplication </span>app) { app.BeginRequest
+= (s, e) =&gt; <span style="color: #2b91af">ServiceAnonymityMapper</span>.EnsureServiceMapping();
} <span style="color: blue">#endregion </span>}</pre>
        <a href="http://11011.net/software/vspaste">
        </a>
        <p>
Mapping:
</p>
        <pre class="code">
          <span style="color: blue">public static class </span>
          <span style="color: #2b91af">ServiceAnonymityMapper </span>{ <span style="color: blue">#region </span>Private
Fields <span style="color: blue">private static </span><span style="color: #2b91af">IEnumerable</span>&lt;<span style="color: #2b91af">KeyValuePair</span>&lt;<span style="color: blue">string</span>, <span style="color: blue">string</span>&gt;&gt;
serviceMapping; <span style="color: blue">#endregion #region </span>Static Constructor <span style="color: blue">static </span>ServiceAnonymityMapper()
{ serviceMapping = GetServiceMapping(); } <span style="color: blue">#endregion #region </span>Public
Methods <span style="color: blue">public static void </span>EnsureServiceMapping()
{ <span style="color: blue">string </span>path = <span style="color: #2b91af">HttpContext</span>.Current.Request.AppRelativeCurrentExecutionFilePath; <span style="color: #2b91af">Func</span>&lt;<span style="color: #2b91af">KeyValuePair</span>&lt;<span style="color: blue">string</span>, <span style="color: blue">string</span>&gt;, <span style="color: blue">bool</span>&gt;
serviceMatch = map =&gt; !path.StartsWith(map.Value) &amp;&amp; ( path.StartsWith(map.Key
+ <span style="color: #a31515">'?'</span>, <span style="color: #2b91af">StringComparison</span>.OrdinalIgnoreCase)
|| path.StartsWith(map.Key + <span style="color: #a31515">'/'</span>, <span style="color: #2b91af">StringComparison</span>.OrdinalIgnoreCase)
|| <span style="color: blue">string</span>.Compare(path, map.Key, <span style="color: blue">true</span>)
== 0 ); <span style="color: #2b91af">KeyValuePair</span>&lt;<span style="color: blue">string</span>, <span style="color: blue">string</span>&gt;
mapping = serviceMapping.FirstOrDefault(serviceMatch); <span style="color: blue">if </span>(mapping.Key
!= <span style="color: blue">null </span>&amp;&amp; mapping.Value != <span style="color: blue">null</span>)
{ <span style="color: #2b91af">HttpContext</span>.Current.RewritePath( mapping.Value
+ <span style="color: #a31515">"/"</span>, path.Remove(0, mapping.Key.Length), <span style="color: #2b91af">HttpContext</span>.Current.Request.QueryString.ToString(), <span style="color: blue">false</span>);
} } <span style="color: blue">#endregion #region </span>Private Methods <span style="color: blue">private
static </span><span style="color: #2b91af">IEnumerable</span>&lt;<span style="color: #2b91af">KeyValuePair</span>&lt;<span style="color: blue">string</span>, <span style="color: blue">string</span>&gt;&gt;
GetServiceMapping() { <span style="color: #2b91af">List</span>&lt;<span style="color: #2b91af">KeyValuePair</span>&lt;<span style="color: blue">string</span>, <span style="color: blue">string</span>&gt;&gt;
serviceMapping = <span style="color: blue">new </span><span style="color: #2b91af">List</span>&lt;<span style="color: #2b91af">KeyValuePair</span>&lt;<span style="color: blue">string</span>, <span style="color: blue">string</span>&gt;&gt;(); <span style="color: blue">string </span>webRoot
= <span style="color: #2b91af">HttpContext</span>.Current.Server.MapPath(<span style="color: #a31515">"~/"</span>); <span style="color: blue">string</span>[]
serviceFiles = <span style="color: #2b91af">Directory</span>.GetFiles( webRoot, <span style="color: #a31515">"*.svc"</span>, <span style="color: #2b91af">SearchOption</span>.AllDirectories); <span style="color: #2b91af">Func</span>&lt;<span style="color: blue">string</span>, <span style="color: blue">bool</span>, <span style="color: blue">string</span>&gt;
getRelative = (path, ext) =&gt; <span style="color: #a31515">"~/" </span>+ <span style="color: #2b91af">Path</span>.Combine(<span style="color: #2b91af">Path</span>.GetDirectoryName(path),
ext ? <span style="color: #2b91af">Path</span>.GetFileNameWithoutExtension(path) : <span style="color: #2b91af">Path</span>.GetFileName(path))
.Remove(0, webRoot.Length).Replace(<span style="color: #a31515">'\\'</span>, <span style="color: #a31515">'/'</span>); <span style="color: blue">var </span>servicePaths
= <span style="color: blue">from </span>servicePath <span style="color: blue">in </span>serviceFiles <span style="color: blue">orderby </span>servicePath.Length <span style="color: blue">descending
select new </span><span style="color: #2b91af">KeyValuePair</span>&lt;<span style="color: blue">string</span>, <span style="color: blue">string</span>&gt;(
getRelative(servicePath, <span style="color: blue">true</span>), getRelative(servicePath, <span style="color: blue">false</span>));
serviceMapping.AddRange(servicePaths); <span style="color: blue">return </span>serviceMapping;
} <span style="color: blue">#endregion </span>}</pre>
        <a href="http://11011.net/software/vspaste">
        </a>
        <a href="http://11011.net/software/vspaste">
        </a>
        <a href="http://11011.net/software/vspaste">
        </a>
        <a href="http://11011.net/software/vspaste">
        </a>
        <a href="http://11011.net/software/vspaste">
        </a>
        <a href="http://11011.net/software/vspaste">
        </a>
        <img width="0" height="0" src="http://blog.mike-obrien.net/aggbug.ashx?id=b1acadba-6ba3-4012-bd2e-ac775008dea3" />
      <xhtml:img xmlns:xhtml="http://www.w3.org/1999/xhtml" src="http://feeds.feedburner.com/~r/MobsDevBlog/~4/o1a-RYWmLwY" height="1" width="1" /></body>
      <title>Removing the .svc Extension for RESTful WCF Services</title>
      <guid isPermaLink="false">http://blog.mike-obrien.net/PermaLink,guid,b1acadba-6ba3-4012-bd2e-ac775008dea3.aspx</guid>
      <link>http://feedproxy.google.com/~r/MobsDevBlog/~3/o1a-RYWmLwY/PermaLink,guid,b1acadba-6ba3-4012-bd2e-ac775008dea3.aspx</link>
      <pubDate>Wed, 06 May 2009 22:29:49 GMT</pubDate>
      <description>&lt;p&gt;
This has been done over and over on the web so I'll add my solution to the mix. Basically
it automatically discovers the services in the web application and builds a static
mapping table. Then it examines each request and attempts to find a mapping. If it
finds one it performs the url rewriting, if not it lets it go though as is. Just register
the module and your good to go.
&lt;/p&gt;
&lt;p&gt;
Http Module:
&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ServiceAnonymityModule &lt;/span&gt;: &lt;span style="color: #2b91af"&gt;IHttpModule &lt;/span&gt;{ &lt;span style="color: blue"&gt;#region &lt;/span&gt;IHttpModule
Implementation &lt;span style="color: blue"&gt;public void &lt;/span&gt;Dispose() { } &lt;span style="color: blue"&gt;public
void &lt;/span&gt;Init(&lt;span style="color: #2b91af"&gt;HttpApplication &lt;/span&gt;app) { app.BeginRequest
+= (s, e) =&amp;gt; &lt;span style="color: #2b91af"&gt;ServiceAnonymityMapper&lt;/span&gt;.EnsureServiceMapping();
} &lt;span style="color: blue"&gt;#endregion &lt;/span&gt;}&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; 
&lt;p&gt;
Mapping:
&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public static class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ServiceAnonymityMapper &lt;/span&gt;{ &lt;span style="color: blue"&gt;#region &lt;/span&gt;Private
Fields &lt;span style="color: blue"&gt;private static &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;KeyValuePair&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;string&lt;/span&gt;, &lt;span style="color: blue"&gt;string&lt;/span&gt;&amp;gt;&amp;gt;
serviceMapping; &lt;span style="color: blue"&gt;#endregion #region &lt;/span&gt;Static Constructor &lt;span style="color: blue"&gt;static &lt;/span&gt;ServiceAnonymityMapper()
{ serviceMapping = GetServiceMapping(); } &lt;span style="color: blue"&gt;#endregion #region &lt;/span&gt;Public
Methods &lt;span style="color: blue"&gt;public static void &lt;/span&gt;EnsureServiceMapping()
{ &lt;span style="color: blue"&gt;string &lt;/span&gt;path = &lt;span style="color: #2b91af"&gt;HttpContext&lt;/span&gt;.Current.Request.AppRelativeCurrentExecutionFilePath; &lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;KeyValuePair&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;string&lt;/span&gt;, &lt;span style="color: blue"&gt;string&lt;/span&gt;&amp;gt;, &lt;span style="color: blue"&gt;bool&lt;/span&gt;&amp;gt;
serviceMatch = map =&amp;gt; !path.StartsWith(map.Value) &amp;amp;&amp;amp; ( path.StartsWith(map.Key
+ &lt;span style="color: #a31515"&gt;'?'&lt;/span&gt;, &lt;span style="color: #2b91af"&gt;StringComparison&lt;/span&gt;.OrdinalIgnoreCase)
|| path.StartsWith(map.Key + &lt;span style="color: #a31515"&gt;'/'&lt;/span&gt;, &lt;span style="color: #2b91af"&gt;StringComparison&lt;/span&gt;.OrdinalIgnoreCase)
|| &lt;span style="color: blue"&gt;string&lt;/span&gt;.Compare(path, map.Key, &lt;span style="color: blue"&gt;true&lt;/span&gt;)
== 0 ); &lt;span style="color: #2b91af"&gt;KeyValuePair&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;string&lt;/span&gt;, &lt;span style="color: blue"&gt;string&lt;/span&gt;&amp;gt;
mapping = serviceMapping.FirstOrDefault(serviceMatch); &lt;span style="color: blue"&gt;if &lt;/span&gt;(mapping.Key
!= &lt;span style="color: blue"&gt;null &lt;/span&gt;&amp;amp;&amp;amp; mapping.Value != &lt;span style="color: blue"&gt;null&lt;/span&gt;)
{ &lt;span style="color: #2b91af"&gt;HttpContext&lt;/span&gt;.Current.RewritePath( mapping.Value
+ &lt;span style="color: #a31515"&gt;"/"&lt;/span&gt;, path.Remove(0, mapping.Key.Length), &lt;span style="color: #2b91af"&gt;HttpContext&lt;/span&gt;.Current.Request.QueryString.ToString(), &lt;span style="color: blue"&gt;false&lt;/span&gt;);
} } &lt;span style="color: blue"&gt;#endregion #region &lt;/span&gt;Private Methods &lt;span style="color: blue"&gt;private
static &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;KeyValuePair&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;string&lt;/span&gt;, &lt;span style="color: blue"&gt;string&lt;/span&gt;&amp;gt;&amp;gt;
GetServiceMapping() { &lt;span style="color: #2b91af"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;KeyValuePair&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;string&lt;/span&gt;, &lt;span style="color: blue"&gt;string&lt;/span&gt;&amp;gt;&amp;gt;
serviceMapping = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;KeyValuePair&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;string&lt;/span&gt;, &lt;span style="color: blue"&gt;string&lt;/span&gt;&amp;gt;&amp;gt;(); &lt;span style="color: blue"&gt;string &lt;/span&gt;webRoot
= &lt;span style="color: #2b91af"&gt;HttpContext&lt;/span&gt;.Current.Server.MapPath(&lt;span style="color: #a31515"&gt;"~/"&lt;/span&gt;); &lt;span style="color: blue"&gt;string&lt;/span&gt;[]
serviceFiles = &lt;span style="color: #2b91af"&gt;Directory&lt;/span&gt;.GetFiles( webRoot, &lt;span style="color: #a31515"&gt;"*.svc"&lt;/span&gt;, &lt;span style="color: #2b91af"&gt;SearchOption&lt;/span&gt;.AllDirectories); &lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;string&lt;/span&gt;, &lt;span style="color: blue"&gt;bool&lt;/span&gt;, &lt;span style="color: blue"&gt;string&lt;/span&gt;&amp;gt;
getRelative = (path, ext) =&amp;gt; &lt;span style="color: #a31515"&gt;"~/" &lt;/span&gt;+ &lt;span style="color: #2b91af"&gt;Path&lt;/span&gt;.Combine(&lt;span style="color: #2b91af"&gt;Path&lt;/span&gt;.GetDirectoryName(path),
ext ? &lt;span style="color: #2b91af"&gt;Path&lt;/span&gt;.GetFileNameWithoutExtension(path) : &lt;span style="color: #2b91af"&gt;Path&lt;/span&gt;.GetFileName(path))
.Remove(0, webRoot.Length).Replace(&lt;span style="color: #a31515"&gt;'\\'&lt;/span&gt;, &lt;span style="color: #a31515"&gt;'/'&lt;/span&gt;); &lt;span style="color: blue"&gt;var &lt;/span&gt;servicePaths
= &lt;span style="color: blue"&gt;from &lt;/span&gt;servicePath &lt;span style="color: blue"&gt;in &lt;/span&gt;serviceFiles &lt;span style="color: blue"&gt;orderby &lt;/span&gt;servicePath.Length &lt;span style="color: blue"&gt;descending
select new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;KeyValuePair&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;string&lt;/span&gt;, &lt;span style="color: blue"&gt;string&lt;/span&gt;&amp;gt;(
getRelative(servicePath, &lt;span style="color: blue"&gt;true&lt;/span&gt;), getRelative(servicePath, &lt;span style="color: blue"&gt;false&lt;/span&gt;));
serviceMapping.AddRange(servicePaths); &lt;span style="color: blue"&gt;return &lt;/span&gt;serviceMapping;
} &lt;span style="color: blue"&gt;#endregion &lt;/span&gt;}&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;img width="0" height="0" src="http://blog.mike-obrien.net/aggbug.ashx?id=b1acadba-6ba3-4012-bd2e-ac775008dea3" /&gt;</description>
      <comments>http://blog.mike-obrien.net/CommentView,guid,b1acadba-6ba3-4012-bd2e-ac775008dea3.aspx</comments>
      <category>REST</category>
      <category>WCF</category>
    <feedburner:origLink>http://blog.mike-obrien.net/PermaLink,guid,b1acadba-6ba3-4012-bd2e-ac775008dea3.aspx</feedburner:origLink></item>
    <item>
      <trackback:ping>http://blog.mike-obrien.net/Trackback.aspx?guid=c537e95a-22b5-4806-9493-fb8f9929f61c</trackback:ping>
      <pingback:server>http://blog.mike-obrien.net/pingback.aspx</pingback:server>
      <pingback:target>http://blog.mike-obrien.net/PermaLink,guid,c537e95a-22b5-4806-9493-fb8f9929f61c.aspx</pingback:target>
      <dc:creator>mob</dc:creator>
      <wfw:comment>http://blog.mike-obrien.net/CommentView,guid,c537e95a-22b5-4806-9493-fb8f9929f61c.aspx</wfw:comment>
      <wfw:commentRss>http://blog.mike-obrien.net/SyndicationService.asmx/GetEntryCommentsRss?guid=c537e95a-22b5-4806-9493-fb8f9929f61c</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
I came across a weird issue with proxy's generated by the Axis2 code generator from
a WCF WSDL. Basically there is ambiguity between the String type defined in the Microsoft
WSDL for primitive types which gets generated in the proxy and the java.lang.String.
Supposedly this issue has <a href="https://issues.apache.org/jira/browse/AXIS2-3921">been
fixed</a> but I still had an issue with the code generator plugin that installs with
the Eclipse Ganymede plugin manager. Anyway's, to work around it I just navigated
to the error:
</p>
        <p>
          <a href="http://blog.mike-obrien.net/content/binary/WindowsLiveWriter/Typ.StringErrorinAxis2ClientGeneratedfro_112D9/image_2.png">
            <img style="border-bottom: 0px; border-left: 0px; border-top: 0px; border-right: 0px" border="0" alt="image" src="http://blog.mike-obrien.net/content/binary/WindowsLiveWriter/Typ.StringErrorinAxis2ClientGeneratedfro_112D9/image_thumb.png" width="646" height="236" />
          </a>
        </p>
        <p>
And specified the FQ name for the Java string type and it worked fine:
</p>
        <p>
          <a href="http://blog.mike-obrien.net/content/binary/WindowsLiveWriter/Typ.StringErrorinAxis2ClientGeneratedfro_112D9/image_4.png">
            <img style="border-bottom: 0px; border-left: 0px; border-top: 0px; border-right: 0px" border="0" alt="image" src="http://blog.mike-obrien.net/content/binary/WindowsLiveWriter/Typ.StringErrorinAxis2ClientGeneratedfro_112D9/image_thumb_1.png" width="664" height="168" />
          </a>
        </p>
        <img width="0" height="0" src="http://blog.mike-obrien.net/aggbug.ashx?id=c537e95a-22b5-4806-9493-fb8f9929f61c" />
      <xhtml:img xmlns:xhtml="http://www.w3.org/1999/xhtml" src="http://feeds.feedburner.com/~r/MobsDevBlog/~4/XJ-d6IWuf7Q" height="1" width="1" /></body>
      <title>"Type mismatch: cannot convert from String to &lt;ServiceName&gt;Stub.String" Error in Axis2 Client Generated from WCF WSDL</title>
      <guid isPermaLink="false">http://blog.mike-obrien.net/PermaLink,guid,c537e95a-22b5-4806-9493-fb8f9929f61c.aspx</guid>
      <link>http://feedproxy.google.com/~r/MobsDevBlog/~3/XJ-d6IWuf7Q/PermaLink,guid,c537e95a-22b5-4806-9493-fb8f9929f61c.aspx</link>
      <pubDate>Sun, 03 May 2009 23:32:49 GMT</pubDate>
      <description>&lt;p&gt;
I came across a weird issue with proxy's generated by the Axis2 code generator from
a WCF WSDL. Basically there is ambiguity between the String type defined in the Microsoft
WSDL for primitive types which gets generated in the proxy and the java.lang.String.
Supposedly this issue has &lt;a href="https://issues.apache.org/jira/browse/AXIS2-3921"&gt;been
fixed&lt;/a&gt; but I still had an issue with the code generator plugin that installs with
the Eclipse Ganymede plugin manager. Anyway's, to work around it I just navigated
to the error:
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://blog.mike-obrien.net/content/binary/WindowsLiveWriter/Typ.StringErrorinAxis2ClientGeneratedfro_112D9/image_2.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; border-top: 0px; border-right: 0px" border="0" alt="image" src="http://blog.mike-obrien.net/content/binary/WindowsLiveWriter/Typ.StringErrorinAxis2ClientGeneratedfro_112D9/image_thumb.png" width="646" height="236"&gt;&lt;/a&gt; 
&lt;/p&gt;
&lt;p&gt;
And specified the FQ name for the Java string type and it worked fine:
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://blog.mike-obrien.net/content/binary/WindowsLiveWriter/Typ.StringErrorinAxis2ClientGeneratedfro_112D9/image_4.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; border-top: 0px; border-right: 0px" border="0" alt="image" src="http://blog.mike-obrien.net/content/binary/WindowsLiveWriter/Typ.StringErrorinAxis2ClientGeneratedfro_112D9/image_thumb_1.png" width="664" height="168"&gt;&lt;/a&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.mike-obrien.net/aggbug.ashx?id=c537e95a-22b5-4806-9493-fb8f9929f61c" /&gt;</description>
      <comments>http://blog.mike-obrien.net/CommentView,guid,c537e95a-22b5-4806-9493-fb8f9929f61c.aspx</comments>
      <category>Apache Axis2</category>
      <category>Interoperability</category>
      <category>Java</category>
      <category>SOAP</category>
      <category>WCF</category>
    <feedburner:origLink>http://blog.mike-obrien.net/PermaLink,guid,c537e95a-22b5-4806-9493-fb8f9929f61c.aspx</feedburner:origLink></item>
    <item>
      <trackback:ping>http://blog.mike-obrien.net/Trackback.aspx?guid=14d80002-a485-41c2-ac03-e62268b758c8</trackback:ping>
      <pingback:server>http://blog.mike-obrien.net/pingback.aspx</pingback:server>
      <pingback:target>http://blog.mike-obrien.net/PermaLink,guid,14d80002-a485-41c2-ac03-e62268b758c8.aspx</pingback:target>
      <dc:creator>mob</dc:creator>
      <wfw:comment>http://blog.mike-obrien.net/CommentView,guid,14d80002-a485-41c2-ac03-e62268b758c8.aspx</wfw:comment>
      <wfw:commentRss>http://blog.mike-obrien.net/SyndicationService.asmx/GetEntryCommentsRss?guid=14d80002-a485-41c2-ac03-e62268b758c8</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Kevin Rohling is my hero; he created a library to add the WSS UsernameToken headers
in SilverLight 2.0! <a href="http://geekswithblogs.net/SunnyCoder/archive/2009/03/15/username-password-amp-ws-security-with-silverlight.aspx">Check
it out</a>.
</p>
        <img width="0" height="0" src="http://blog.mike-obrien.net/aggbug.ashx?id=14d80002-a485-41c2-ac03-e62268b758c8" />
      <xhtml:img xmlns:xhtml="http://www.w3.org/1999/xhtml" src="http://feeds.feedburner.com/~r/MobsDevBlog/~4/JqatNMEA88k" height="1" width="1" /></body>
      <title>WSS UsernameToken with Silverlight 2.0!</title>
      <guid isPermaLink="false">http://blog.mike-obrien.net/PermaLink,guid,14d80002-a485-41c2-ac03-e62268b758c8.aspx</guid>
      <link>http://feedproxy.google.com/~r/MobsDevBlog/~3/JqatNMEA88k/PermaLink,guid,14d80002-a485-41c2-ac03-e62268b758c8.aspx</link>
      <pubDate>Sat, 25 Apr 2009 06:15:03 GMT</pubDate>
      <description>&lt;p&gt;
Kevin Rohling is my hero; he created a library to add the WSS UsernameToken headers
in SilverLight 2.0! &lt;a href="http://geekswithblogs.net/SunnyCoder/archive/2009/03/15/username-password-amp-ws-security-with-silverlight.aspx"&gt;Check
it out&lt;/a&gt;.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.mike-obrien.net/aggbug.ashx?id=14d80002-a485-41c2-ac03-e62268b758c8" /&gt;</description>
      <comments>http://blog.mike-obrien.net/CommentView,guid,14d80002-a485-41c2-ac03-e62268b758c8.aspx</comments>
      <category>Interoperability</category>
      <category>Silverlight</category>
      <category>WCF</category>
      <category>WS-Security</category>
    <feedburner:origLink>http://blog.mike-obrien.net/PermaLink,guid,14d80002-a485-41c2-ac03-e62268b758c8.aspx</feedburner:origLink></item>
    <item>
      <trackback:ping>http://blog.mike-obrien.net/Trackback.aspx?guid=aaf32b5b-eb60-41f6-8b16-55777a62ee30</trackback:ping>
      <pingback:server>http://blog.mike-obrien.net/pingback.aspx</pingback:server>
      <pingback:target>http://blog.mike-obrien.net/PermaLink,guid,aaf32b5b-eb60-41f6-8b16-55777a62ee30.aspx</pingback:target>
      <dc:creator>mob</dc:creator>
      <wfw:comment>http://blog.mike-obrien.net/CommentView,guid,aaf32b5b-eb60-41f6-8b16-55777a62ee30.aspx</wfw:comment>
      <wfw:commentRss>http://blog.mike-obrien.net/SyndicationService.asmx/GetEntryCommentsRss?guid=aaf32b5b-eb60-41f6-8b16-55777a62ee30</wfw:commentRss>
      <slash:comments>1</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Donn Felker has the real answer <a href="http://blog.donnfelker.com/post/Silverlight-Cross-Domain-Issue.aspx">here</a>!
</p>
        <img width="0" height="0" src="http://blog.mike-obrien.net/aggbug.ashx?id=aaf32b5b-eb60-41f6-8b16-55777a62ee30" />
      <xhtml:img xmlns:xhtml="http://www.w3.org/1999/xhtml" src="http://feeds.feedburner.com/~r/MobsDevBlog/~4/HxlCao6Ef_4" height="1" width="1" /></body>
      <title>Silverlight "This could be due to attempting to access a service in a cross-domain..." Error</title>
      <guid isPermaLink="false">http://blog.mike-obrien.net/PermaLink,guid,aaf32b5b-eb60-41f6-8b16-55777a62ee30.aspx</guid>
      <link>http://feedproxy.google.com/~r/MobsDevBlog/~3/HxlCao6Ef_4/PermaLink,guid,aaf32b5b-eb60-41f6-8b16-55777a62ee30.aspx</link>
      <pubDate>Sat, 25 Apr 2009 04:56:17 GMT</pubDate>
      <description>&lt;p&gt;
Donn Felker has the real answer &lt;a href="http://blog.donnfelker.com/post/Silverlight-Cross-Domain-Issue.aspx"&gt;here&lt;/a&gt;!
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.mike-obrien.net/aggbug.ashx?id=aaf32b5b-eb60-41f6-8b16-55777a62ee30" /&gt;</description>
      <comments>http://blog.mike-obrien.net/CommentView,guid,aaf32b5b-eb60-41f6-8b16-55777a62ee30.aspx</comments>
      <category>.NET 3.0</category>
      <category>Silverlight</category>
      <category>SOAP</category>
      <category>WCF</category>
    <feedburner:origLink>http://blog.mike-obrien.net/PermaLink,guid,aaf32b5b-eb60-41f6-8b16-55777a62ee30.aspx</feedburner:origLink></item>
    <item>
      <trackback:ping>http://blog.mike-obrien.net/Trackback.aspx?guid=f6e9d1b9-bc98-4a19-9d69-a9873059f592</trackback:ping>
      <pingback:server>http://blog.mike-obrien.net/pingback.aspx</pingback:server>
      <pingback:target>http://blog.mike-obrien.net/PermaLink,guid,f6e9d1b9-bc98-4a19-9d69-a9873059f592.aspx</pingback:target>
      <dc:creator>mob</dc:creator>
      <wfw:comment>http://blog.mike-obrien.net/CommentView,guid,f6e9d1b9-bc98-4a19-9d69-a9873059f592.aspx</wfw:comment>
      <wfw:commentRss>http://blog.mike-obrien.net/SyndicationService.asmx/GetEntryCommentsRss?guid=f6e9d1b9-bc98-4a19-9d69-a9873059f592</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
I have run into a couple of instances where I've had to manually add a WSS UsernameToken
to consume a WCF service with the basicHttpBinding and a UsernameToken. The client
could not understand the WSS timestamp returned by WCF (Notably ColdFusion/Axis1.2
w/ no wss4j). If you are trying to achieve improbability and want to just turn this
off there isn't a way to disable the timestamp in the basicHttpBinding or wsHttpBinding
through configuration but you can use the customBinding. To emulate basicHttpBinding
with transport security and message credentials but with the timestamp off you can
define the custom binding as follows:
</p>
        <pre class="code">
          <span style="color: blue">...</span>
        </pre>
        <pre class="code">
          <span style="color: blue">&lt;</span>
          <span style="color: #a31515">bindings</span>
          <span style="color: blue">&gt;
&lt;</span>
          <span style="color: #a31515">customBinding</span>
          <span style="color: blue">&gt;
&lt;</span>
          <span style="color: #a31515">binding </span>
          <span style="color: red">name</span>
          <span style="color: blue">=</span>"<span style="color: blue">MyBinding</span>"<span style="color: blue">&gt;
&lt;</span><span style="color: #a31515">security </span><span style="color: red">authenticationMode</span><span style="color: blue">=</span>"<span style="color: blue">UserNameOverTransport</span>" <span style="color: red">includeTimestamp</span><span style="color: blue">=</span>"<span style="color: blue">false</span>" <span style="color: blue">/&gt;
&lt;</span><span style="color: #a31515">textMessageEncoding </span><span style="color: red">messageVersion</span><span style="color: blue">=</span>"<span style="color: blue">Soap11</span>" <span style="color: blue">/&gt;
&lt;</span><span style="color: #a31515">httpsTransport </span><span style="color: blue">/&gt;
&lt;/</span><span style="color: #a31515">binding</span><span style="color: blue">&gt;
&lt;/</span><span style="color: #a31515">customBinding</span><span style="color: blue">&gt;
&lt;/</span><span style="color: #a31515">bindings</span><span style="color: blue">&gt;</span></pre>
        <pre class="code">
          <span style="color: blue">...</span>
        </pre>
        <a href="http://11011.net/software/vspaste">
        </a>
        <img width="0" height="0" src="http://blog.mike-obrien.net/aggbug.ashx?id=f6e9d1b9-bc98-4a19-9d69-a9873059f592" />
      <xhtml:img xmlns:xhtml="http://www.w3.org/1999/xhtml" src="http://feeds.feedburner.com/~r/MobsDevBlog/~4/cbnS-pr1dns" height="1" width="1" /></body>
      <title>Removing the WSS Timestamp from WCF Responses</title>
      <guid isPermaLink="false">http://blog.mike-obrien.net/PermaLink,guid,f6e9d1b9-bc98-4a19-9d69-a9873059f592.aspx</guid>
      <link>http://feedproxy.google.com/~r/MobsDevBlog/~3/cbnS-pr1dns/PermaLink,guid,f6e9d1b9-bc98-4a19-9d69-a9873059f592.aspx</link>
      <pubDate>Sat, 25 Apr 2009 02:28:30 GMT</pubDate>
      <description>&lt;p&gt;
I have run into a couple of instances where I've had to manually add a WSS UsernameToken
to consume a WCF service with the basicHttpBinding and a UsernameToken. The client
could not understand the WSS timestamp returned by WCF (Notably ColdFusion/Axis1.2
w/ no wss4j). If you are trying to achieve improbability and want to just turn this
off there isn't a way to disable the timestamp in the basicHttpBinding or wsHttpBinding
through configuration but you can use the customBinding. To emulate basicHttpBinding
with transport security and message credentials but with the timestamp off you can
define the custom binding as follows:
&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;...&lt;/span&gt;&lt;/pre&gt;&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;bindings&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;customBinding&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;binding &lt;/span&gt;&lt;span style="color: red"&gt;name&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;"&lt;span style="color: blue"&gt;MyBinding&lt;/span&gt;"&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;security &lt;/span&gt;&lt;span style="color: red"&gt;authenticationMode&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;"&lt;span style="color: blue"&gt;UserNameOverTransport&lt;/span&gt;" &lt;span style="color: red"&gt;includeTimestamp&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;"&lt;span style="color: blue"&gt;false&lt;/span&gt;" &lt;span style="color: blue"&gt;/&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;textMessageEncoding &lt;/span&gt;&lt;span style="color: red"&gt;messageVersion&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;"&lt;span style="color: blue"&gt;Soap11&lt;/span&gt;" &lt;span style="color: blue"&gt;/&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;httpsTransport &lt;/span&gt;&lt;span style="color: blue"&gt;/&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;binding&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;customBinding&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;bindings&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;...&lt;/span&gt;&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;img width="0" height="0" src="http://blog.mike-obrien.net/aggbug.ashx?id=f6e9d1b9-bc98-4a19-9d69-a9873059f592" /&gt;</description>
      <comments>http://blog.mike-obrien.net/CommentView,guid,f6e9d1b9-bc98-4a19-9d69-a9873059f592.aspx</comments>
      <category>Apache Axis</category>
      <category>ColdFusion</category>
      <category>Interoperability</category>
      <category>SOAP</category>
      <category>WCF</category>
      <category>WS-Security</category>
    <feedburner:origLink>http://blog.mike-obrien.net/PermaLink,guid,f6e9d1b9-bc98-4a19-9d69-a9873059f592.aspx</feedburner:origLink></item>
    <item>
      <trackback:ping>http://blog.mike-obrien.net/Trackback.aspx?guid=91eb24ba-5c92-42c9-89a6-0477701ff143</trackback:ping>
      <pingback:server>http://blog.mike-obrien.net/pingback.aspx</pingback:server>
      <pingback:target>http://blog.mike-obrien.net/PermaLink,guid,91eb24ba-5c92-42c9-89a6-0477701ff143.aspx</pingback:target>
      <dc:creator>mob</dc:creator>
      <wfw:comment>http://blog.mike-obrien.net/CommentView,guid,91eb24ba-5c92-42c9-89a6-0477701ff143.aspx</wfw:comment>
      <wfw:commentRss>http://blog.mike-obrien.net/SyndicationService.asmx/GetEntryCommentsRss?guid=91eb24ba-5c92-42c9-89a6-0477701ff143</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
The following demonstrates how to consume a web service that requires a WSS UsernameToke
with ColdFusion:
</p>
        <pre class="code">
          <span style="color: blue">&lt;</span>
          <span style="color: #a31515">html</span>
          <span style="color: blue">&gt;
&lt;</span>
          <span style="color: #a31515">head</span>
          <span style="color: blue">&gt;&lt;</span>
          <span style="color: #a31515">title</span>
          <span style="color: blue">&gt;</span>My
Web Services<span style="color: blue">&lt;/</span><span style="color: #a31515">title</span><span style="color: blue">&gt;&lt;/</span><span style="color: #a31515">head</span><span style="color: blue">&gt;
&lt;</span><span style="color: #a31515">body</span><span style="color: blue">&gt;
&lt;</span><span style="color: #a31515">h2</span><span style="color: blue">&gt;</span>My
Web Services<span style="color: blue">&lt;/</span><span style="color: #a31515">h2</span><span style="color: blue">&gt;
&lt;</span><span style="color: #a31515">cfscript</span><span style="color: blue">&gt; </span>service
= createObject("webservice", "https://services.nsa.gov/NOCList.svc?wsdl"); AddCredentials(service,
"username", "p@$$w0rd"); result = service.DoSomething(); writeoutput("Result=" &amp;
result); function AddCredentials(service, username, password) { wssNamespace = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd";
header = createObject("java", "org.apache.axis.message.SOAPHeaderElement"); header.init(wssNamespace,
"wsse:Security"); header.addChildElement("wsse:UsernameToken"); header.getFirstChild().addChildElement("wsse:Username").setValue(username);
header.getFirstChild().addChildElement("wsse:Password").setValue(password); header.setMustUnderstand(1);
service.setHeader(header); } <span style="color: blue">&lt;/</span><span style="color: #a31515">cfscript</span><span style="color: blue">&gt;
&lt;/</span><span style="color: #a31515">body</span><span style="color: blue">&gt;
&lt;/</span><span style="color: #a31515">html</span><span style="color: blue">&gt;</span></pre>
        <a href="http://11011.net/software/vspaste">
        </a>
        <img width="0" height="0" src="http://blog.mike-obrien.net/aggbug.ashx?id=91eb24ba-5c92-42c9-89a6-0477701ff143" />
      <xhtml:img xmlns:xhtml="http://www.w3.org/1999/xhtml" src="http://feeds.feedburner.com/~r/MobsDevBlog/~4/IZUAOutAonk" height="1" width="1" /></body>
      <title>Consuming a Web Service that Requires a WSS UsernameToken with ColdFusion</title>
      <guid isPermaLink="false">http://blog.mike-obrien.net/PermaLink,guid,91eb24ba-5c92-42c9-89a6-0477701ff143.aspx</guid>
      <link>http://feedproxy.google.com/~r/MobsDevBlog/~3/IZUAOutAonk/PermaLink,guid,91eb24ba-5c92-42c9-89a6-0477701ff143.aspx</link>
      <pubDate>Sat, 25 Apr 2009 02:09:03 GMT</pubDate>
      <description>&lt;p&gt;
The following demonstrates how to consume a web service that requires a WSS UsernameToke
with ColdFusion:
&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;html&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;head&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;title&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;My
Web Services&lt;span style="color: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;title&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;head&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;body&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;h2&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;My
Web Services&lt;span style="color: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;h2&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;cfscript&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt; &lt;/span&gt;service
= createObject("webservice", "https://services.nsa.gov/NOCList.svc?wsdl"); AddCredentials(service,
"username", "p@$$w0rd"); result = service.DoSomething(); writeoutput("Result=" &amp;amp;
result); function AddCredentials(service, username, password) { wssNamespace = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd";
header = createObject("java", "org.apache.axis.message.SOAPHeaderElement"); header.init(wssNamespace,
"wsse:Security"); header.addChildElement("wsse:UsernameToken"); header.getFirstChild().addChildElement("wsse:Username").setValue(username);
header.getFirstChild().addChildElement("wsse:Password").setValue(password); header.setMustUnderstand(1);
service.setHeader(header); } &lt;span style="color: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;cfscript&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;body&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;html&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;img width="0" height="0" src="http://blog.mike-obrien.net/aggbug.ashx?id=91eb24ba-5c92-42c9-89a6-0477701ff143" /&gt;</description>
      <comments>http://blog.mike-obrien.net/CommentView,guid,91eb24ba-5c92-42c9-89a6-0477701ff143.aspx</comments>
      <category>ColdFusion</category>
      <category>Interoperability</category>
      <category>SOAP</category>
      <category>WS-Security</category>
    <feedburner:origLink>http://blog.mike-obrien.net/PermaLink,guid,91eb24ba-5c92-42c9-89a6-0477701ff143.aspx</feedburner:origLink></item>
    <item>
      <trackback:ping>http://blog.mike-obrien.net/Trackback.aspx?guid=c7a7fc0e-9d57-4622-9e80-1d0eeb796eaa</trackback:ping>
      <pingback:server>http://blog.mike-obrien.net/pingback.aspx</pingback:server>
      <pingback:target>http://blog.mike-obrien.net/PermaLink,guid,c7a7fc0e-9d57-4622-9e80-1d0eeb796eaa.aspx</pingback:target>
      <dc:creator>mob</dc:creator>
      <wfw:comment>http://blog.mike-obrien.net/CommentView,guid,c7a7fc0e-9d57-4622-9e80-1d0eeb796eaa.aspx</wfw:comment>
      <wfw:commentRss>http://blog.mike-obrien.net/SyndicationService.asmx/GetEntryCommentsRss?guid=c7a7fc0e-9d57-4622-9e80-1d0eeb796eaa</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
The following are a couple of things I've discovered about web service namespaces
while working on interoperability:
</p>
        <p>
1) If your not going to use a URL for your namespace you need to prepend your namespace
with a <a href="http://en.wikipedia.org/wiki/URI_scheme">uri scheme</a> (IE: "myscheme:MyNamespace")
to signify that it is absolute. The <a href="http://tools.ietf.org/html/rfc2141">urn
scheme</a> is an appropriate choice but this isn't required as the namespace must
simply be a URI. The Microsoft stacks don't seem to mind if the scheme is not present
but the Apache Xml Security library is finicky about this (Thinking its a relative
namespace if its not there), you will see the following error on the client side if
you don't:
</p>
        <p>
          <font color="#ff0000" size="2" face="Courier New">org.apache.xml.security.c14n.CanonicalizationException:
Element ns1:doSomething has a relative namespace: xmlns:ns1="MyNamespace"</font>
        </p>
        <p>
2) Use the same namespace for your binding namespace, behavior namespace, and all
service and data contract namespaces. WCF will break out WSDL artifacts from different
namespaces into different files and some proxy generators don't like this (Notably
ColdFusion).
</p>
        <img width="0" height="0" src="http://blog.mike-obrien.net/aggbug.ashx?id=c7a7fc0e-9d57-4622-9e80-1d0eeb796eaa" />
      <xhtml:img xmlns:xhtml="http://www.w3.org/1999/xhtml" src="http://feeds.feedburner.com/~r/MobsDevBlog/~4/pZAKXg8OUa8" height="1" width="1" /></body>
      <title>A Couple of WCF Namespace Interoperability Tips</title>
      <guid isPermaLink="false">http://blog.mike-obrien.net/PermaLink,guid,c7a7fc0e-9d57-4622-9e80-1d0eeb796eaa.aspx</guid>
      <link>http://feedproxy.google.com/~r/MobsDevBlog/~3/pZAKXg8OUa8/PermaLink,guid,c7a7fc0e-9d57-4622-9e80-1d0eeb796eaa.aspx</link>
      <pubDate>Fri, 24 Apr 2009 19:09:06 GMT</pubDate>
      <description>&lt;p&gt;
The following are a couple of things I've discovered about web service namespaces
while working on interoperability:
&lt;/p&gt;
&lt;p&gt;
1) If your not going to use a URL for your namespace you need to prepend your namespace
with a &lt;a href="http://en.wikipedia.org/wiki/URI_scheme"&gt;uri scheme&lt;/a&gt; (IE: "myscheme:MyNamespace")
to signify that it is absolute. The &lt;a href="http://tools.ietf.org/html/rfc2141"&gt;urn
scheme&lt;/a&gt; is an appropriate choice but this isn't required as the namespace must
simply be a URI. The Microsoft stacks don't seem to mind if the scheme is not present
but the Apache Xml Security library is finicky about this (Thinking its a relative
namespace if its not there), you will see the following error on the client side if
you don't:
&lt;/p&gt;
&lt;p&gt;
&lt;font color="#ff0000" size="2" face="Courier New"&gt;org.apache.xml.security.c14n.CanonicalizationException:
Element ns1:doSomething has a relative namespace: xmlns:ns1="MyNamespace"&lt;/font&gt; 
&lt;p&gt;
2) Use the same namespace for your binding namespace, behavior namespace, and all
service and data contract namespaces. WCF will break out WSDL artifacts from different
namespaces into different files and some proxy generators don't like this (Notably
ColdFusion).
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.mike-obrien.net/aggbug.ashx?id=c7a7fc0e-9d57-4622-9e80-1d0eeb796eaa" /&gt;</description>
      <comments>http://blog.mike-obrien.net/CommentView,guid,c7a7fc0e-9d57-4622-9e80-1d0eeb796eaa.aspx</comments>
      <category>Apache Axis</category>
      <category>ColdFusion</category>
      <category>Interoperability</category>
      <category>SOAP</category>
      <category>WCF</category>
    <feedburner:origLink>http://blog.mike-obrien.net/PermaLink,guid,c7a7fc0e-9d57-4622-9e80-1d0eeb796eaa.aspx</feedburner:origLink></item>
  </channel>
</rss>
