<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:atom="https://www.w3.org/2005/Atom" xmlns:sy="https://purl.org/rss/1.0/modules/syndication/" xmlns:dc="https://purl.org/dc/elements/1.1/" xmlns:content="https://purl.org/rss/1.0/modules/content/" xmlns:wfw="https://wellformedweb.org/CommentAPI/" xmlns:slash="https://purl.org/rss/1.0/modules/slash/"
  version="2.0">
  <channel>
    <title>The .NET Endpoint</title>
    <atom:link
      href="https://docs.microsoft.com/archive/blogs/endpoint/feed.xml"
      rel="self"
      type="application/rss+xml" />
    <link>https://docs.microsoft.com/archive/blogs/endpoint/feed.xml</link>
    <description>Blog by the .NET and AppFabric teams about WCF and WF development, deployment, and management</description>
    <lastBuildDate>Wed, 29 Jul 2015 17:58:23 GMT</lastBuildDate>
    <language>en-US</language>
    <sy:updatePeriod>hourly</sy:updatePeriod>
    <sy:updateFrequency>1</sy:updateFrequency>
    <item>
      <title>Goodbye .NET Endpoint, Hello AppFabric Team Blog</title>
      <link>https://docs.microsoft.com/archive/blogs/endpoint/goodbye-net-endpoint-hello-appfabric-team-blog</link>
      <pubDate>Mon, 16 May 2011 06:09:00 GMT</pubDate>
      <dc:creator><![CDATA[Ron Jacobs]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/endpoint/2011/05/16/goodbye-net-endpoint-hello-appfabric-team-blog/</guid>
      <description><![CDATA[Over the last year you&rsquo;ve heard us talking about both Windows Azure AppFabric and Windows...]]></description>
      <content:encoded><![CDATA[<p>Over the last year you&rsquo;ve heard us talking about both Windows Azure AppFabric and Windows Server AppFabric.&nbsp; Today we are adding our final post to the .NET endpoint team blog and announcing the new <a href="https://blogs.msdn.com/appfabric">AppFabric Team blog</a>.&nbsp; This blog will be the home of content about all things AppFabric from Service Bus to Caching to WCF and WF.</p>
<p>If you&rsquo;ve been following us here on the .NET Endpoint please be sure to follow the <a href="https://blogs.msdn.com/appfabric">AppFabric Team Blog</a>.</p>
<h3>endpoint.tv becomes AppFabric.tv</h3>
<p>As part of this change, endpoint.tv will now become <a href="https://channel9.msdn.com/shows/AppFabric-TV">AppFabric.tv</a>.&nbsp; We will continue to bring you the best content about WF and WCF as well as the rest of the AppFabric world.&nbsp;</p>
<p><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/03/60/metablogapi/4426.MS_AppFabricTV-Banner-220_46663A82.png" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-03-60-metablogapi/4426.MS_5F00_AppFabricTV_2D00_Banner_2D00_220_5F00_46663A82.png"><img height="164" width="224" src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/03/60/metablogapi/2843.MS_AppFabricTV-Banner-220_thumb_77091C60.png" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-03-60-metablogapi/2843.MS_5F00_AppFabricTV_2D00_Banner_2D00_220_5F00_thumb_5F00_77091C60.png" alt="MS_AppFabricTV-Banner-220" border="0" title="MS_AppFabricTV-Banner-220" style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" /></a></p>]]></content:encoded>
    </item>
    <item>
      <title>Using DataContracts with WCF Web API</title>
      <link>https://docs.microsoft.com/archive/blogs/endpoint/using-datacontracts-with-wcf-web-api</link>
      <pubDate>Sat, 14 May 2011 19:55:34 GMT</pubDate>
      <dc:creator><![CDATA[The Real Glenn Block]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/endpoint/2011/05/14/using-datacontracts-with-wcf-web-api/</guid>
      <description><![CDATA[A few folks have been asking if it is possible to serialize/deserialize using the...]]></description>
      <content:encoded><![CDATA[<p>A few folks have been <a href="https://wcf.codeplex.com/discussions/248365">asking</a> if it is possible to serialize/deserialize using the DataContractSerializer. Rest assured, yes it is possible. Now whether or not it is the easiest/most intuitive model well that it is a different question. <img style="border-bottom-style: none; border-left-style: none; border-top-style: none; border-right-style: none" class="wlEmoticon wlEmoticon-winkingsmile" alt="Winking smile" src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/03/60/metablogapi/7558.wlEmoticon-winkingsmile_2.png" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-03-60-metablogapi/7558.wlEmoticon_2D00_winkingsmile_5F00_2.png" /></p>  <p>If you read to the end the post you will see a bunch of extensions which have been pushed to webapicontrib and make this all MUCH easier (at least I think). If you don’t jump you’ll read below what you need to do if you do it yourself.</p>  <p>It stars with the SetSerializer&lt;T&gt; method which is expose on the XmlMediaTypeFormatter. You can call this method and pass a DataContractSerializer for T. That tells the formatter use this serializer not the default. You need to give it the exact information, for example if you are returning List&lt;Contact&gt; then make T List&lt;Contact&gt; NOT T. If you want to support both, you need to register both.</p>  <p>To allow you to get to the formatter, the MediaTypeFormatterCollection exposes an XmlFormatter property. You might now be thinking, “Oh that’s easy, but how do I get a hold of the formatter collection?”</p>  <p>Good question. It depends on whether you are on the server or on the client.</p>  <h1>On the Server</h1>  <p>The easiest way to configure things on the server is to annotate your service/resource with a [DataContractFormat] attribute. Once you do then we will automatically use the DataContractSerializer. </p>  <p>If you do not want to annotate your service, the alternative option is to do it in code. If you are using HttpHostConfiguration you can get to the formatters by accessing the OperationHandlerFactory property on the config class. If you are using the fluent API, you’ll want to cast to HttpHostConfiguration in order to access it. For example see the code below</p>  <pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">var config = <span style="color: #0000ff">new</span> HttpHostConfiguration();
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">var formatter = OperationHandlerFactory.XmlFormatter;
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">formatter.SetSerializer&lt;Contact&gt;(
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">  <span style="color: #0000ff">new</span> DataContractSerializer(<span style="color: #0000ff">typeof</span>(Contact)));
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">formatter.SetSerializer&lt;List&lt;Contact&gt;&gt;(
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">  <span style="color: #0000ff">new</span> DataContractSerializer(<span style="color: #0000ff">typeof</span>(List&lt;Contact&gt;)));</pre></pre>

<p>The code above is setting the formatter to use the DCS for Contact and List&lt;Contact&gt; both for reading and writing.</p>

<h1>On the client</h1>

<p>On the client side how you configure depends on if you are reading or writing. In either case both steps involve calling SetSerializer&lt;T&gt; on the XmlMediaTypeFormatter. </p>

<p>For reading you will create the formatter and pass it to the ReadAs&lt;T&gt; method of the HttpContent which is accessed off of the Content property on HttpResponseMessage.</p>

<pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">var formatter = <span style="color: #0000ff">new</span> XmlMediaTypeFormatter();
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">formatter.SetSerializer&lt;List&lt;Contact&gt;&gt;&gt;(
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">  <span style="color: #0000ff">new</span> DataContractSerializer(<span style="color: #0000ff">typeof</span>(List&lt;Contact&gt;))));
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">var contact = response.Content.ReadAs&lt;T&gt;(
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">  <span style="color: #0000ff">new</span> List&lt;MediaTypeFormatter&gt; { formatter });
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px"></pre></pre>

<p>If you are writing to the request, you will access the formatters collection on the ObjectContent&lt;T&gt; instance.</p>

<pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">var content = <span style="color: #0000ff">new</span> ObjectContent&lt;Contact&gt;();
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">content.Formatters.SetSerializer&lt;Contact&gt;(
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">  <span style="color: #0000ff">new</span> DataContractSerializer(<span style="color: #0000ff">typeof</span>(Contact)));
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">client.Post(content);</pre></pre>

<h1></h1>

<h1>New extension methods in webapicontrib to make life easier</h1>

<p>Now that we’ve seen how you can do it, we can wrap this up in a bunch of helper extension methods and life gets a whole lot easier. I did that and <a href="https://webapicontrib.codeplex.com/SourceControl/changeset/view/2da8381982b9#source%2fFormatters%2fDataContractExtensions.cs">pushed</a> them to webapicontrib <img style="border-bottom-style: none; border-left-style: none; border-top-style: none; border-right-style: none" class="wlEmoticon wlEmoticon-winkingsmile" alt="Winking smile" src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/03/60/metablogapi/7558.wlEmoticon-winkingsmile_2.png" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-03-60-metablogapi/7558.wlEmoticon_2D00_winkingsmile_5F00_2.png" /></p>

<p>Check the code below from the new DataContractExample in webapicontrib.</p>

<pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">var config = HttpHostConfiguration.Create();
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px"><font style="background-color: #ffff00">config.UseDataContractSerializer&lt;Contact&gt;();</font>
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">var host = <span style="color: #0000ff">new</span> HttpConfigurableServiceHost&lt;ContactsResource&gt;(
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">  config, <span style="color: #0000ff">new</span> Uri(&quot;<span style="color: #8b0000">https://localhost:8080/</span>&quot;));
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">host.Open();
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px"></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">var client = <span style="color: #0000ff">new</span> HttpClient(&quot;<span style="color: #8b0000">https://localhost:8080/</span>&quot;);
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">var newContact = <span style="color: #0000ff">new</span> Contact {FirstName = &quot;<span style="color: #8b0000">Brad</span>&quot;, LastName = &quot;<span style="color: #8b0000">Abrams</span>&quot;};
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">var content = <span style="color: #0000ff">new</span> ObjectContent&lt;Contact&gt;(newContact,
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">  &quot;<span style="color: #8b0000">application/xml</span>&quot;).<font style="background-color: #ffff00">UseDataContractSerializer();</font>
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">var response = client.Post(&quot;<span style="color: #8b0000">contacts</span>&quot;, content);
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">response = client.Get(&quot;<span style="color: #8b0000">contacts</span>&quot;);
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px"></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">var contacts = response.Content.<font style="background-color: #ffff00">ReadAsDataContract&lt;Contact[]&gt;();</font>
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px"><span style="color: #0000ff">foreach</span> (var contact <span style="color: #0000ff">in</span> contacts)
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">{
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">  Console.WriteLine(<span style="color: #0000ff">string</span>.Format(
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">    &quot;<span style="color: #8b0000">Contact: FirstName={0}, LastName={1}</span>&quot;, 
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">    contact.FirstName, contact.LastName));
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">}
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">Console.ReadLine();
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px"></pre></pre>

<p>I’ve highlighted the important parts</p>

<ol>
  <li>UseDataContractSerializer&lt;T&gt; on the config will set the formatter to serialize T using DCS. In order to make life easier the extension method also registers for List&lt;T&gt; and T[]. </li>

  <li>For reading on the client call the ReadAsDataContract&lt;T&gt; extension method on HttpContent. </li>

  <li>For writing on the client create an ObjectContent&lt;T&gt; instance and call the UseDataContractSerializer() extension metho </li>
</ol>

<p>Download the bits in wcfwebapi contrib <a href="https://webapicontrib.codeplex.com/SourceControl/list/changesets#">here</a></p>]]></content:encoded>
    </item>
    <item>
      <title>How to eliminate tempuri.org from your service WSDL</title>
      <link>https://docs.microsoft.com/archive/blogs/endpoint/how-to-eliminate-tempuri-org-from-your-service-wsdl</link>
      <pubDate>Thu, 12 May 2011 12:34:45 GMT</pubDate>
      <dc:creator><![CDATA[Ron Jacobs]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/endpoint/2011/05/12/how-to-eliminate-tempuri-org-from-your-service-wsdl/</guid>
      <description><![CDATA[tempuri.org is the default namespace applied to WCF Services and Workflow Services.&#160; You can...]]></description>
      <content:encoded><![CDATA[<p>tempuri.org is the default namespace applied to WCF Services and Workflow Services.&#160; You can and should specify your service namespace.</p>  <blockquote>   <p><em>It is recommended that you explicitly specify a name and namespace for the service contract, and an action for each operation to avoid using &quot;</em><a href="https://tempuri.org&quot;"><em>https://tempuri.org&quot;</em></a><em> and to prevent interface and method names from being exposed in the service’s contract        <br /></em><a href="https://msdn.microsoft.com/en-us/library/ms731060.aspx">MSDN Library - Service Contract Versioning</a></p> </blockquote>  <h1>Where is tempuri.org in the WSDL?</h1>  <p>If you browse the service WSDL you will see tempuri.org all over the place.&#160; Here is a default WCF service WSDL.</p>  <div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">   <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet">     <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #0000ff">&lt;</span><span style="color: #800000">wsdl:definitions</span> <span style="color: #ff0000">name</span><span style="color: #0000ff">=&quot;Service1&quot;</span> <span style="color: #ff0000">targetNamespace</span><span style="color: #0000ff">=&quot;https://tempuri.org/&quot;</span> <span style="color: #ff0000">xmlns:wsdl</span><span style="color: #0000ff">=&quot;https://schemas.xmlsoap.org/wsdl/&quot;</span> <span style="color: #ff0000">xmlns:soap</span><span style="color: #0000ff">=&quot;https://schemas.xmlsoap.org/wsdl/soap/&quot;</span> <span style="color: #ff0000">xmlns:soapenc</span><span style="color: #0000ff">=&quot;https://schemas.xmlsoap.org/soap/encoding/&quot;</span> <span style="color: #ff0000">xmlns:wsu</span><span style="color: #0000ff">=&quot;https://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd&quot;</span> <span style="color: #ff0000">xmlns:xsd</span><span style="color: #0000ff">=&quot;https://www.w3.org/2001/XMLSchema&quot;</span> <span style="color: #ff0000">xmlns:soap12</span><span style="color: #0000ff">=&quot;https://schemas.xmlsoap.org/wsdl/soap12/&quot;</span> <span style="color: #ff0000">xmlns:tns</span><span style="color: #0000ff">=&quot;https://tempuri.org/&quot;</span> <span style="color: #ff0000">xmlns:wsa</span><span style="color: #0000ff">=&quot;https://schemas.xmlsoap.org/ws/2004/08/addressing&quot;</span> <span style="color: #ff0000">xmlns:wsp</span><span style="color: #0000ff">=&quot;https://schemas.xmlsoap.org/ws/2004/09/policy&quot;</span> <span style="color: #ff0000">xmlns:wsap</span><span style="color: #0000ff">=&quot;https://schemas.xmlsoap.org/ws/2004/08/addressing/policy&quot;</span> <span style="color: #ff0000">xmlns:wsaw</span><span style="color: #0000ff">=&quot;https://www.w3.org/2006/05/addressing/wsdl&quot;</span> <span style="color: #ff0000">xmlns:msc</span><span style="color: #0000ff">=&quot;https://schemas.microsoft.com/ws/2005/12/wsdl/contract&quot;</span> <span style="color: #ff0000">xmlns:wsa10</span><span style="color: #0000ff">=&quot;https://www.w3.org/2005/08/addressing&quot;</span> <span style="color: #ff0000">xmlns:wsx</span><span style="color: #0000ff">=&quot;https://schemas.xmlsoap.org/ws/2004/09/mex&quot;</span> <span style="color: #ff0000">xmlns:wsam</span><span style="color: #0000ff">=&quot;https://www.w3.org/2007/05/addressing/metadata&quot;</span><span style="color: #0000ff">&gt;</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">  <span style="color: #0000ff">&lt;</span><span style="color: #800000">wsdl:types</span><span style="color: #0000ff">&gt;</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">&lt;</span><span style="color: #800000">xsd:schema</span> <span style="color: #ff0000">targetNamespace</span><span style="color: #0000ff">=&quot;https://tempuri.org/Imports&quot;</span><span style="color: #0000ff">&gt;</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">      <span style="color: #0000ff">&lt;</span><span style="color: #800000">xsd:import</span> <span style="color: #ff0000">schemaLocation</span><span style="color: #0000ff">=&quot;https://localhost:63720/Service1.svc?xsd=xsd0&quot;</span> <span style="color: #ff0000">namespace</span><span style="color: #0000ff">=&quot;https://tempuri.org/&quot;</span><span style="color: #0000ff">/&gt;</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">&lt;/</span><span style="color: #800000">xsd:schema</span><span style="color: #0000ff">&gt;</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">  <span style="color: #0000ff">&lt;/</span><span style="color: #800000">wsdl:types</span><span style="color: #0000ff">&gt;</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">  <span style="color: #0000ff">&lt;</span><span style="color: #800000">wsdl:message</span> <span style="color: #ff0000">name</span><span style="color: #0000ff">=&quot;IService1_DoWork_InputMessage&quot;</span><span style="color: #0000ff">&gt;</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">&lt;</span><span style="color: #800000">wsdl:part</span> <span style="color: #ff0000">name</span><span style="color: #0000ff">=&quot;parameters&quot;</span> <span style="color: #ff0000">element</span><span style="color: #0000ff">=&quot;tns:DoWork&quot;</span><span style="color: #0000ff">/&gt;</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">  <span style="color: #0000ff">&lt;/</span><span style="color: #800000">wsdl:message</span><span style="color: #0000ff">&gt;</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">  <span style="color: #0000ff">&lt;</span><span style="color: #800000">wsdl:message</span> <span style="color: #ff0000">name</span><span style="color: #0000ff">=&quot;IService1_DoWork_OutputMessage&quot;</span><span style="color: #0000ff">&gt;</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">&lt;</span><span style="color: #800000">wsdl:part</span> <span style="color: #ff0000">name</span><span style="color: #0000ff">=&quot;parameters&quot;</span> <span style="color: #ff0000">element</span><span style="color: #0000ff">=&quot;tns:DoWorkResponse&quot;</span><span style="color: #0000ff">/&gt;</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">  <span style="color: #0000ff">&lt;/</span><span style="color: #800000">wsdl:message</span><span style="color: #0000ff">&gt;</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">  <span style="color: #0000ff">&lt;</span><span style="color: #800000">wsdl:portType</span> <span style="color: #ff0000">name</span><span style="color: #0000ff">=&quot;IService1&quot;</span><span style="color: #0000ff">&gt;</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">&lt;</span><span style="color: #800000">wsdl:operation</span> <span style="color: #ff0000">name</span><span style="color: #0000ff">=&quot;DoWork&quot;</span><span style="color: #0000ff">&gt;</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">      <span style="color: #0000ff">&lt;</span><span style="color: #800000">wsdl:input</span> <span style="color: #ff0000">wsaw:Action</span><span style="color: #0000ff">=&quot;https://tempuri.org/IService1/DoWork&quot;</span> <span style="color: #ff0000">message</span><span style="color: #0000ff">=&quot;tns:IService1_DoWork_InputMessage&quot;</span><span style="color: #0000ff">/&gt;</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">      <span style="color: #0000ff">&lt;</span><span style="color: #800000">wsdl:output</span> <span style="color: #ff0000">wsaw:Action</span><span style="color: #0000ff">=&quot;https://tempuri.org/IService1/DoWorkResponse&quot;</span> <span style="color: #ff0000">message</span><span style="color: #0000ff">=&quot;tns:IService1_DoWork_OutputMessage&quot;</span><span style="color: #0000ff">/&gt;</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">&lt;/</span><span style="color: #800000">wsdl:operation</span><span style="color: #0000ff">&gt;</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">  <span style="color: #0000ff">&lt;/</span><span style="color: #800000">wsdl:portType</span><span style="color: #0000ff">&gt;</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">  <span style="color: #0000ff">&lt;</span><span style="color: #800000">wsdl:binding</span> <span style="color: #ff0000">name</span><span style="color: #0000ff">=&quot;BasicHttpBinding_IService1&quot;</span> <span style="color: #ff0000">type</span><span style="color: #0000ff">=&quot;tns:IService1&quot;</span><span style="color: #0000ff">&gt;</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">&lt;</span><span style="color: #800000">soap:binding</span> <span style="color: #ff0000">transport</span><span style="color: #0000ff">=&quot;https://schemas.xmlsoap.org/soap/http&quot;</span><span style="color: #0000ff">/&gt;</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">&lt;</span><span style="color: #800000">wsdl:operation</span> <span style="color: #ff0000">name</span><span style="color: #0000ff">=&quot;DoWork&quot;</span><span style="color: #0000ff">&gt;</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">      <span style="color: #0000ff">&lt;</span><span style="color: #800000">soap:operation</span> <span style="color: #ff0000">soapAction</span><span style="color: #0000ff">=&quot;https://tempuri.org/IService1/DoWork&quot;</span> <span style="color: #ff0000">style</span><span style="color: #0000ff">=&quot;document&quot;</span><span style="color: #0000ff">/&gt;</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">      <span style="color: #0000ff">&lt;</span><span style="color: #800000">wsdl:input</span><span style="color: #0000ff">&gt;</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">        <span style="color: #0000ff">&lt;</span><span style="color: #800000">soap:body</span> <span style="color: #ff0000">use</span><span style="color: #0000ff">=&quot;literal&quot;</span><span style="color: #0000ff">/&gt;</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">      <span style="color: #0000ff">&lt;/</span><span style="color: #800000">wsdl:input</span><span style="color: #0000ff">&gt;</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">      <span style="color: #0000ff">&lt;</span><span style="color: #800000">wsdl:output</span><span style="color: #0000ff">&gt;</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">        <span style="color: #0000ff">&lt;</span><span style="color: #800000">soap:body</span> <span style="color: #ff0000">use</span><span style="color: #0000ff">=&quot;literal&quot;</span><span style="color: #0000ff">/&gt;</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">      <span style="color: #0000ff">&lt;/</span><span style="color: #800000">wsdl:output</span><span style="color: #0000ff">&gt;</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">&lt;/</span><span style="color: #800000">wsdl:operation</span><span style="color: #0000ff">&gt;</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">  <span style="color: #0000ff">&lt;/</span><span style="color: #800000">wsdl:binding</span><span style="color: #0000ff">&gt;</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">  <span style="color: #0000ff">&lt;</span><span style="color: #800000">wsdl:service</span> <span style="color: #ff0000">name</span><span style="color: #0000ff">=&quot;Service1&quot;</span><span style="color: #0000ff">&gt;</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">&lt;</span><span style="color: #800000">wsdl:port</span> <span style="color: #ff0000">name</span><span style="color: #0000ff">=&quot;BasicHttpBinding_IService1&quot;</span> <span style="color: #ff0000">binding</span><span style="color: #0000ff">=&quot;tns:BasicHttpBinding_IService1&quot;</span><span style="color: #0000ff">&gt;</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">      <span style="color: #0000ff">&lt;</span><span style="color: #800000">soap:address</span> <span style="color: #ff0000">location</span><span style="color: #0000ff">=&quot;https://localhost:63720/Service1.svc&quot;</span><span style="color: #0000ff">/&gt;</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">&lt;/</span><span style="color: #800000">wsdl:port</span><span style="color: #0000ff">&gt;</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">  <span style="color: #0000ff">&lt;/</span><span style="color: #800000">wsdl:service</span><span style="color: #0000ff">&gt;</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #0000ff">&lt;/</span><span style="color: #800000">wsdl:definitions</span><span style="color: #0000ff">&gt;</span></pre>
<!--CRLF--></div>
</div>

<h1>Tip: Use a constant to ensure consistency</h1>

<p>I like to use a constant to provide a consistent namespace across my services</p>

<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
  <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet">
    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> Constants</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">{</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">    <span style="color: #008000">// Ensures consistency in the namespace declarations across services</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">const</span> <span style="color: #0000ff">string</span> Namespace = <span style="color: #006080">&quot;https://contoso.com/services/&quot;</span>;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">}</pre>
<!--CRLF--></div>
</div>

<h1>How to eliminate tempuri.org from WCF Services WSDL</h1>

<h3>Step 1: Declare a namespace on your service contract</h3>

<p>The namespace can be anything.&#160; People typically use a URI of some form but it does not have to point to an actual web page.&#160; Often people will use a version identifier in the namsepace but there are no rules about what you should do.</p>

<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
  <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet">
    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #008000">// Eliminate tempuri.org from the contract</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #008000">// If you don't want to us a constant, put the URI here</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #008000">// [ServiceContract(Namespace = &quot;https://contoso.com/services&quot;)]</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">[ServiceContract(Namespace = Constants.Namespace)]</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #0000ff">public</span> <span style="color: #0000ff">interface</span> IService1</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">{</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">    [OperationContract]</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">void</span> DoWork();</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">}</pre>
<!--CRLF--></div>
</div>

<h3>Step 2: Declare a namespace on your service</h3>

<p>The service namespace is added with a ServiceBehavior attribute.&#160; Using the constant ensures that the namespace is the same for the contract and the service.</p>

<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
  <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet">
    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #008000">// If you don't want to us a constant, put the URI here</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #008000">// [ServiceBehavior(Namespace = &quot;https://contoso.com/services&quot;)]</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">[ServiceBehavior(Namespace = Constants.Namespace)]</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> Service1 : IService1</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">{</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> DoWork()</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">    {</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">    }</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">}</pre>
<!--CRLF--></div>
</div>

<h3>Step 3: Set the binding namespace</h3>

<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
  <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet">
    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #0000ff">&lt;</span><span style="color: #800000">system.serviceModel</span><span style="color: #0000ff">&gt;</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">&lt;</span><span style="color: #800000">services</span><span style="color: #0000ff">&gt;</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">      <span style="color: #0000ff">&lt;</span><span style="color: #800000">service</span> <span style="color: #ff0000">name</span><span style="color: #0000ff">=&quot;EliminateTempUri.Service1&quot;</span><span style="color: #0000ff">&gt;</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">        <span style="color: #008000">&lt;!-- Use a bindingNamespace to eliminate tempuri.org --&gt;</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">        <span style="color: #0000ff">&lt;</span><span style="color: #800000">endpoint</span> <span style="color: #ff0000">address</span><span style="color: #0000ff">=&quot;&quot;</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">                  <span style="color: #ff0000">binding</span> <span style="color: #0000ff">=&quot;basicHttpBinding&quot;</span> </pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">                  <span style="color: #ff0000">bindingNamespace</span><span style="color: #0000ff">=&quot;https://contoso.com/services&quot;</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">                  <span style="color: #ff0000">contract</span><span style="color: #0000ff">=&quot;EliminateTempUri.IService1&quot;</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">        <span style="color: #0000ff">/&gt;</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">      <span style="color: #0000ff">&lt;/</span><span style="color: #800000">service</span><span style="color: #0000ff">&gt;</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">&lt;/</span><span style="color: #800000">services</span><span style="color: #0000ff">&gt;</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">&lt;</span><span style="color: #800000">behaviors</span><span style="color: #0000ff">&gt;</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">      <span style="color: #0000ff">&lt;</span><span style="color: #800000">serviceBehaviors</span><span style="color: #0000ff">&gt;</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">        <span style="color: #0000ff">&lt;</span><span style="color: #800000">behavior</span> <span style="color: #ff0000">name</span><span style="color: #0000ff">=&quot;&quot;</span><span style="color: #0000ff">&gt;</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">          <span style="color: #0000ff">&lt;</span><span style="color: #800000">serviceMetadata</span> <span style="color: #ff0000">httpGetEnabled</span><span style="color: #0000ff">=&quot;true&quot;</span> <span style="color: #0000ff">/&gt;</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">          <span style="color: #0000ff">&lt;</span><span style="color: #800000">serviceDebug</span> <span style="color: #ff0000">includeExceptionDetailInFaults</span><span style="color: #0000ff">=&quot;false&quot;</span> <span style="color: #0000ff">/&gt;</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">        <span style="color: #0000ff">&lt;/</span><span style="color: #800000">behavior</span><span style="color: #0000ff">&gt;</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">      <span style="color: #0000ff">&lt;/</span><span style="color: #800000">serviceBehaviors</span><span style="color: #0000ff">&gt;</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">&lt;/</span><span style="color: #800000">behaviors</span><span style="color: #0000ff">&gt;</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">&lt;</span><span style="color: #800000">serviceHostingEnvironment</span> <span style="color: #ff0000">multipleSiteBindingsEnabled</span><span style="color: #0000ff">=&quot;true&quot;</span> <span style="color: #0000ff">/&gt;</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">  <span style="color: #0000ff">&lt;/</span><span style="color: #800000">system.serviceModel</span><span style="color: #0000ff">&gt;</span></pre>
<!--CRLF--></div>
</div>

<h1>&#160;</h1>

<h1>How to eliminate tempuri.org from Workflow Services WSDL</h1>

<p>For workflow services the process is slightly different.&#160; Workflow services do not have a contract (it is inferred from the activities) so here is the process.</p>

<h3>Step 1: Declare a namespace on each receive activity</h3>

<p>I wish it were possible to use a constant here but sadly it is not.&#160; <strong><em>For each receive activity</em></strong> you must declare the namespace and contract name using a special syntax of <strong>{namespace}IContractName</strong>.&#160; In this example I’ve set the <strong>ServiceContractName</strong> property to <strong>{https://contoso.com/services}IService</strong></p>

<p><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/03/60/metablogapi/4682.image_263690B6.png" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-03-60-metablogapi/4682.image_5F00_263690B6.png"><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/03/60/metablogapi/8311.image_thumb_6600773B.png" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-03-60-metablogapi/8311.image_5F00_thumb_5F00_6600773B.png" width="525" height="406" /></a></p>

<h3>Step 2: Declare a namespace for the Workflow Service</h3>

<ol>
  <li>Open the Workflow Service and click in the designer outside of an activity.&#160; This will allow you to set the properties of the service itself.&#160; </li>

  <li>Set the Name property to use the same namespace as you see below. </li>
</ol>

<p><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/03/60/metablogapi/0871.image_5EE13AC3.png" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-03-60-metablogapi/0871.image_5F00_5EE13AC3.png"><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/03/60/metablogapi/6170.image_thumb_6CB380BE.png" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-03-60-metablogapi/6170.image_5F00_thumb_5F00_6CB380BE.png" width="525" height="156" /></a></p>

<h3>Step 3: Set the binding namespace</h3>

<p>Many people do not declare a &lt;service&gt; tag for their Workflow Services but if you want to eliminate tempuri.org you will need to declare it.</p>

<p>For a Workflow Service the A,B,Cs of WCF configuration are a little different.</p>

<ul>
  <li>Service Name – use the ConfigurationName property from the WorkflowService </li>

  <li>A – Address – should be blank when hosted in IIS / AppFabric </li>

  <li>B – Binding – use the binding of your choice.&#160; Typically basicHttpBinding </li>

  <li>C – Contract – use the interface name from the ServiceContractName property (without the namespace) </li>
</ul>

<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
  <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet">
    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #008000">&lt;!-- For Workflow Services name is the ConfigurationName property --&gt;</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #0000ff">&lt;</span><span style="color: #800000">service</span> <span style="color: #ff0000">name</span><span style="color: #0000ff">=&quot;Service2&quot;</span><span style="color: #0000ff">&gt;</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">  <span style="color: #008000">&lt;!-- contract must match the receive activity --&gt;</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">  <span style="color: #008000">&lt;!-- Use a bindingNamespace to eliminate tempuri.org --&gt;</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">  <span style="color: #0000ff">&lt;</span><span style="color: #800000">endpoint</span> <span style="color: #ff0000">address</span><span style="color: #0000ff">=&quot;&quot;</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">            <span style="color: #ff0000">binding</span> <span style="color: #0000ff">=&quot;basicHttpBinding&quot;</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">            <span style="color: #ff0000">bindingNamespace</span><span style="color: #0000ff">=&quot;https://contoso.com/services&quot;</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">            <span style="color: #ff0000">contract</span><span style="color: #0000ff">=&quot;IService&quot;</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">  <span style="color: #0000ff">/&gt;</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #0000ff">&lt;/</span><span style="color: #800000">service</span><span style="color: #0000ff">&gt;</span></pre>
<!--CRLF--></div>
</div>

<h1>Test It Out</h1>

<ol>
  <li>Right click on your WCF or Workflow Service in Visual Studio and select View In Browser. </li>

  <li>Click on the link to view the WSDL </li>

  <li>Search in the browser for tempuri.org – there should be no matches </li>
</ol>

<p><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/03/60/metablogapi/8814.image_65944446.png" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-03-60-metablogapi/8814.image_5F00_65944446.png"><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/03/60/metablogapi/1563.image_thumb_5E08D4D9.png" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-03-60-metablogapi/1563.image_5F00_thumb_5F00_5E08D4D9.png" width="644" height="135" /></a></p>

<p>Happy Coding!</p>

<p>Ron Jacobs 
  <br /><a href="https://blogs.msdn.com/rjacobs">https://blogs.msdn.com/rjacobs</a> 

  <br />Twitter: @ronljacobs <a href="https://twitter.com/ronljacobs">https://twitter.com/ronljacobs</a></p>]]></content:encoded>
    </item>
    <item>
      <title>WCF Extensibility – Operation Selectors</title>
      <link>https://docs.microsoft.com/archive/blogs/endpoint/wcf-extensibility-operation-selectors</link>
      <pubDate>Tue, 10 May 2011 09:48:38 GMT</pubDate>
      <dc:creator><![CDATA[CarlosFigueira]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/endpoint/2011/05/10/wcf-extensibility-operation-selectors/</guid>
      <description><![CDATA[This post is part of a series about WCF extensibility points. For a list of all previous posts and...]]></description>
      <content:encoded><![CDATA[<p><em>This post is part of a series about WCF extensibility points. For a list of all previous posts and planned future ones, go to the </em><a href="https://blogs.msdn.com/b/carlosfigueira/archive/2011/03/14/wcf-extensibility.aspx"><em>index page</em></a><em>.</em></p>  <p>We’re now entering the realm of the less used extensibility points for the WCF runtime. Operation selectors could well be left as an internal implementation detail for WCF, but for some reason the designers decided to make them public (I think the guideline back then was if anyone can come up with a scenario where a user would use it, then WCF would expose a hook for that). The server selector (<a href="https://msdn.microsoft.com/en-us/library/system.servicemodel.dispatcher.idispatchoperationselector.aspx">IDispatchOperationSelector</a>) is actually interesting to have as a public hook, as the whole UriTemplate model for REST services was built on top of it – basically, the selector knows about the HTTP method and URI templates for each service operation, and based on those properties of incoming messages it can call the appropriate operation. For “normal” (i.e., SOAP) endpoints, the operation selector implementation (which happens to be internal) is based on the addressing of the binding – in most of the cases it simply matches the Action header of the message with a map of action to the operation names.</p>  <p>The client operation selector (<a href="https://msdn.microsoft.com/en-us/library/system.servicemodel.dispatcher.iclientoperationselector.aspx">IClientOperationSelector</a>) is another story. The client selector receives the method invoked by the client (the <a href="https://msdn.microsoft.com/en-us/library/system.reflection.methodbase.aspx">System.Reflection.MethodBase</a> object) and it must return an operation name which will be used by the WCF runtime to retrieve the actual client operation from the client runtime. I’ve tried for a while to think of any scenario where one would actually want to call one method in the proxy and have another operation be executed (besides some practical joke scenario where one would swap Add for Subtract in a calculator), but I just can’t think of any. Even searching on Bing or Google doesn’t show any real scenarios where a client selector is used. But well, WCF is extensible, so let’s let people choose their client operations as well <img style="border-bottom-style: none; border-left-style: none; border-top-style: none; border-right-style: none" class="wlEmoticon wlEmoticon-smile" alt="Smile" src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/03/60/metablogapi/4431.wlEmoticon-smile_0C608874.png" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-03-60-metablogapi/4431.wlEmoticon_2D00_smile_5F00_0C608874.png" />.</p>  <h3>Public implementations in WCF</h3>  <ul>   <li><a href="https://msdn.microsoft.com/en-us/library/system.servicemodel.dispatcher.webhttpdispatchoperationselector.aspx">WebHttpDispatchOperationSelector</a> (server): The dispatch implementation where the UriTemplate magic happens for WCF REST endpoints. </li> </ul>  <p>There are no public implementations for <a href="https://msdn.microsoft.com/en-us/library/system.servicemodel.dispatcher.iclientoperationselector.aspx">IClientOperationSelector</a>, only an internal one which simply returns the operation name of the method being called.</p>  <h3>Interface declaration</h3>  <div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:45359482-7044-4731-87cc-a3be49e0cb5b" class="wlWriterEditableSmartContent"> <div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"> <div style="background: #fff; max-height: 400px; overflow: auto"> <ol style="background: #ffffff; margin: 0; padding: 0 0 0 5px; white-space: nowrap"> <li><span style="background:#ffffff;color:#000000"></span><span style="background:#ffffff;color:#0000ff">public</span><span style="background:#ffffff;color:#000000"> </span><span style="background:#ffffff;color:#0000ff">interface</span><span style="background:#ffffff;color:#000000"> </span><span style="background:#ffffff;color:#2b91af">IDispatchOperationSelector</span></li> <li style="background: #f3f3f3"><span style="background:#ffffff;color:#000000">{</span></li> <li>    <span style="background:#ffffff;color:#000000"></span><span style="background:#ffffff;color:#0000ff">string</span><span style="background:#ffffff;color:#000000"> SelectOperation(</span><span style="background:#ffffff;color:#0000ff">ref</span><span style="background:#ffffff;color:#000000"> </span><span style="background:#ffffff;color:#2b91af">Message</span><span style="background:#ffffff;color:#000000"> message);</span></li> <li style="background: #f3f3f3"><span style="background:#ffffff;color:#000000">}</span></li> <li>&nbsp;</li> <li style="background: #f3f3f3"><span style="background:#ffffff;color:#000000"></span><span style="background:#ffffff;color:#0000ff">public</span><span style="background:#ffffff;color:#000000"> </span><span style="background:#ffffff;color:#0000ff">interface</span><span style="background:#ffffff;color:#000000"> </span><span style="background:#ffffff;color:#2b91af">IClientOperationSelector</span></li> <li><span style="background:#ffffff;color:#000000">{</span></li> <li style="background: #f3f3f3">    <span style="background:#ffffff;color:#000000"></span><span style="background:#ffffff;color:#0000ff">bool</span><span style="background:#ffffff;color:#000000"> AreParametersRequiredForSelection { </span><span style="background:#ffffff;color:#0000ff">get</span><span style="background:#ffffff;color:#000000">; }</span></li> <li>    <span style="background:#ffffff;color:#000000"></span><span style="background:#ffffff;color:#0000ff">string</span><span style="background:#ffffff;color:#000000"> SelectOperation(</span><span style="background:#ffffff;color:#2b91af">MethodBase</span><span style="background:#ffffff;color:#000000"> method, </span><span style="background:#ffffff;color:#0000ff">object</span><span style="background:#ffffff;color:#000000">[] parameters);</span></li> <li style="background: #f3f3f3"><span style="background:#ffffff;color:#000000">}</span></li> </ol> </div> </div> </div>  <p>The dispatch operation selector is quite simple – on <a href="https://msdn.microsoft.com/en-us/library/system.servicemodel.dispatcher.idispatchoperationselector.selectoperation.aspx">SelectOperation</a> you get the message, you return the operation name. In most cases the operation can be selected based on information on the message header (e.g., the Action header for SOAP endpoints) or the message properties (e.g., the HTTP request property attached to the message). There are scenarios, however, where the routing to the operation needs to be done based on the message contents, so the message is passed by reference – if it needs to be consumed by the operation selector, it can recreated it to hand it back to the WCF pipeline.</p>  <p>On the client side the <a href="https://msdn.microsoft.com/en-us/library/system.servicemodel.dispatcher.iclientoperationselector.areparametersrequiredforselection.aspx">AreParametersRequiredForSelection</a> is used to determine whether the response to <a href="https://msdn.microsoft.com/en-us/library/system.servicemodel.dispatcher.iclientoperationselector.selectoperation.aspx">SelectOperation</a> can be cached or not – if the parameters are not required, the WCF runtime will only call <a href="https://msdn.microsoft.com/en-us/library/system.servicemodel.dispatcher.iclientoperationselector.selectoperation.aspx">SelectOperation</a> once per method in the proxy, otherwise every time a proxy call is made the selector will be invoked again.</p>  <h3>How to add operation selectors</h3>  <p><strong>At the server side</strong>: the operation selector is a property of the <a href="https://msdn.microsoft.com/en-us/library/system.servicemodel.dispatcher.dispatchruntime.aspx">DispatchRuntime</a> object. It’s typically accessed via the endpoint dispatcher in a call to <a href="https://msdn.microsoft.com/en-us/library/system.servicemodel.description.iendpointbehavior.applydispatchbehavior.aspx">IEndpointBehavior.ApplyDispatchBehavior</a>.</p>  <div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:1b4cc170-2428-417a-a7e8-c372d02bbb98" class="wlWriterEditableSmartContent"> <div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"> <div style="background: #fff; max-height: 400px; overflow: auto"> <ol style="background: #ffffff; margin: 0; padding: 0 0 0 5px; white-space: nowrap"> <li><span style="color:#0000ff">public</span> <span style="color:#0000ff">class</span> <span style="color:#2b91af">MyEndpointBehavior</span> : <span style="color:#2b91af">IEndpointBehavior</span></li> <li style="background: #f3f3f3">{</li> <li>    <span style="color:#0000ff">public</span> <span style="color:#0000ff">void</span> ApplyDispatchBehavior(<span style="color:#2b91af">ServiceEndpoint</span> endpoint, <span style="color:#2b91af">EndpointDispatcher</span> endpointDispatcher)</li> <li style="background: #f3f3f3">    {</li> <li>        endpointDispatcher.DispatchRuntime.OperationSelector = <span style="color:#0000ff">new</span> <span style="color:#2b91af">MyOperationSelector</span>();</li> <li style="background: #f3f3f3">    }</li> <li>}</li> <li style="background: #f3f3f3">&nbsp;</li> <li><span style="color:#0000ff">public</span> <span style="color:#0000ff">class</span> <span style="color:#2b91af">MyOperationSelector</span> : <span style="color:#2b91af">IDispatchOperationSelector</span></li> <li style="background: #f3f3f3">{</li> <li>    <span style="color:#0000ff">public</span> <span style="color:#0000ff">string</span> SelectOperation(<span style="color:#0000ff">ref</span> <span style="color:#2b91af">Message</span> message)</li> <li style="background: #f3f3f3">    {</li> <li>        <span style="color:#0000ff">string</span> action = message.Headers.Action;</li> <li style="background: #f3f3f3">        <span style="color:#0000ff">return</span> action.Substring(action.LastIndexOf(<span style="color:#a31515">&#39;/&#39;</span>) + 1);</li> <li>    }</li> <li style="background: #f3f3f3">}</li> </ol> </div> </div> </div>  <p>At the client side: the operation selector is a property of the <a href="https://msdn.microsoft.com/en-us/library/system.servicemodel.dispatcher.clientruntime.aspx">ClientRuntime</a> object, and is typically accessed via the implementation of <a href="https://msdn.microsoft.com/en-us/library/system.servicemodel.description.iendpointbehavior.applyclientbehavior.aspx">IEndpointBehavior.ApplyClientBehavior</a>.</p>  <div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:bd12c499-9d72-426a-9ef7-31795b3e1d1d" class="wlWriterEditableSmartContent"> <div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"> <div style="background: #fff; max-height: 400px; overflow: auto"> <ol style="background: #ffffff; margin: 0; padding: 0 0 0 5px; white-space: nowrap"> <li><span style="color:#0000ff">public</span> <span style="color:#0000ff">class</span> <span style="color:#2b91af">MyEndpointBehavior</span> : <span style="color:#2b91af">IEndpointBehavior</span></li> <li style="background: #f3f3f3">{</li> <li>    <span style="color:#0000ff">public</span> <span style="color:#0000ff">void</span> ApplyClientBehavior(<span style="color:#2b91af">ServiceEndpoint</span> endpoint, <span style="color:#2b91af">ClientRuntime</span> clientRuntime)</li> <li style="background: #f3f3f3">    {</li> <li>        clientRuntime.OperationSelector = <span style="color:#0000ff">new</span> <span style="color:#2b91af">MyClientOperationSelector</span>();</li> <li style="background: #f3f3f3">    }</li> <li>}</li> <li style="background: #f3f3f3">&nbsp;</li> <li><span style="color:#0000ff">public</span> <span style="color:#0000ff">class</span> <span style="color:#2b91af">MyClientOperationSelector</span> : <span style="color:#2b91af">IClientOperationSelector</span></li> <li style="background: #f3f3f3">{</li> <li>    <span style="color:#0000ff">public</span> <span style="color:#0000ff">bool</span> AreParametersRequiredForSelection</li> <li style="background: #f3f3f3">    {</li> <li>        <span style="color:#0000ff">get</span> { <span style="color:#0000ff">return</span> <span style="color:#0000ff">false</span>; }</li> <li style="background: #f3f3f3">    }</li> <li>&nbsp;</li> <li style="background: #f3f3f3">    <span style="color:#0000ff">public</span> <span style="color:#0000ff">string</span> SelectOperation(<span style="color:#2b91af">MethodBase</span> method, <span style="color:#0000ff">object</span>[] parameters)</li> <li>    {</li> <li style="background: #f3f3f3">        <span style="color:#0000ff">return</span> method.Name;</li> <li>    }</li> <li style="background: #f3f3f3">}</li> </ol> </div> </div> </div>  <p>&#160;</p>  <h3>Real world scenario: Support for X-HTTP-Method-Override in REST endpoints</h3>  <p>This issue came from the <a href="https://social.msdn.microsoft.com/Forums/en-US/wcf/thread/1dc19d08-71b0-4f4d-9d8a-698d7af877d8">forum</a>. REST services we use HTTP verbs (methods) to determine the action to be applied to the resource. For example, a GET request to /Contacts/1 would return the contact with id 1, while DELETE /Contacts/1 would remove that same contact from the repository. This is a great – and simple – model, but there are some browsers, proxies, firewalls or even some web service platforms which don’t support the full set of verbs (rejecting anything other than GET and POST). In order to overcome this problem, some platforms (such as <a href="https://msdn.microsoft.com/en-ca/library/system.web.mvc.htmlhelper.httpmethodoverride">ASP.NET MVC</a> and <a href="https://code.google.com/apis/gdata/docs/2.0/basics.html#UpdatingEntry">Google Data</a>) started supporting a new HTTP header or field, called X-HTTP-Method-Override, which contain the actual method which should be used by the service to access the resource. The WCF REST API doesn’t support this override natively, but that doesn’t preclude us from using an extensibility point to enable it.</p>  <p>Implementing the support for X-HTTP-Method-Override is nothing but a matter of replacing the operation selector (based on methods and URI templates) used by REST endpoints with our own, which is aware of this new header. When the <a href="https://msdn.microsoft.com/en-us/library/system.servicemodel.dispatcher.idispatchoperationselector.selectoperation.aspx">IDispatchOperationSelector.SelectOperation</a> method is called, we can check if the incoming request contained the override header, and if so, we can update the incoming message properties to let the original selector chose the appropriate operation.</p>  <p>For this example I’ll reuse the contact manager service from the <a href="https://blogs.msdn.com/b/carlosfigueira/archive/2011/04/19/wcf-extensibility-message-inspectors.aspx">message inspectors post</a> – it contains the different operations which differ based on the HTTP verb of the incoming request, so it’s perfect for this issue. And before I start, the usual disclaimer – this is a sample for illustrating the topic of this post, this is not production-ready code. I tested it for a few contracts and it worked, but I cannot guarantee that it will work for all scenarios (please let me know if you find a bug or something missing). Also, for simplicity sake it doesn’t have a lot of error handling which a production-level code would. Also, the contact manager is all stored in memory, a “real” one would have a backing database or something more “persistent”.</p>  <p>One small parenthesis: the link below for the code in this post contains the version shown here in C# and also a version in VB.NET which does the same thing; if you’re a VB developer, you can check the MSDN code samples out.</p>  <p>The contracts and service implementation are exactly the same as the one from before (I removed the version for Text, as it didn’t add any value to this topic):</p>  <div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:3c983fcd-9aee-48f5-93cb-bf7554b42763" class="wlWriterEditableSmartContent"> <div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"> <div style="background: #fff; max-height: 400px; overflow: auto"> <ol style="background: #ffffff; margin: 0; padding: 0 0 0 5px; white-space: nowrap"> <li>[<span style="color:#2b91af">DataContract</span>]</li> <li style="background: #f3f3f3"><span style="color:#0000ff">public</span> <span style="color:#0000ff">class</span> <span style="color:#2b91af">Contact</span></li> <li>{</li> <li style="background: #f3f3f3">    [<span style="color:#2b91af">DataMember</span>]</li> <li>    <span style="color:#0000ff">public</span> <span style="color:#0000ff">string</span> Id { <span style="color:#0000ff">get</span>; <span style="color:#0000ff">set</span>; }</li> <li style="background: #f3f3f3">    [<span style="color:#2b91af">DataMember</span>]</li> <li>    <span style="color:#0000ff">public</span> <span style="color:#0000ff">string</span> Name { <span style="color:#0000ff">get</span>; <span style="color:#0000ff">set</span>; }</li> <li style="background: #f3f3f3">    [<span style="color:#2b91af">DataMember</span>]</li> <li>    <span style="color:#0000ff">public</span> <span style="color:#0000ff">string</span> Email { <span style="color:#0000ff">get</span>; <span style="color:#0000ff">set</span>; }</li> <li style="background: #f3f3f3">    [<span style="color:#2b91af">DataMember</span>]</li> <li>    <span style="color:#0000ff">public</span> <span style="color:#0000ff">string</span>[] Telephones { <span style="color:#0000ff">get</span>; <span style="color:#0000ff">set</span>; }</li> <li style="background: #f3f3f3">}</li> <li>&nbsp;</li> <li style="background: #f3f3f3">[<span style="color:#2b91af">ServiceContract</span>]</li> <li><span style="color:#0000ff">public</span> <span style="color:#0000ff">interface</span> <span style="color:#2b91af">IContactManager</span></li> <li style="background: #f3f3f3">{</li> <li>    [<span style="color:#2b91af">WebInvoke</span>(</li> <li style="background: #f3f3f3">        Method = <span style="color:#a31515">&quot;POST&quot;</span>,</li> <li>        UriTemplate = <span style="color:#a31515">&quot;/Contacts&quot;</span>,</li> <li style="background: #f3f3f3">        ResponseFormat = <span style="color:#2b91af">WebMessageFormat</span>.Json)]</li> <li>    <span style="color:#0000ff">string</span> AddContact(<span style="color:#2b91af">Contact</span> contact);</li> <li style="background: #f3f3f3">&nbsp;</li> <li>    [<span style="color:#2b91af">WebInvoke</span>(</li> <li style="background: #f3f3f3">        Method = <span style="color:#a31515">&quot;PUT&quot;</span>,</li> <li>        UriTemplate = <span style="color:#a31515">&quot;/Contacts/{id}&quot;</span>,</li> <li style="background: #f3f3f3">        ResponseFormat = <span style="color:#2b91af">WebMessageFormat</span>.Json)]</li> <li>    <span style="color:#0000ff">void</span> UpdateContact(<span style="color:#0000ff">string</span> id, <span style="color:#2b91af">Contact</span> contact);</li> <li style="background: #f3f3f3">&nbsp;</li> <li>    [<span style="color:#2b91af">WebInvoke</span>(</li> <li style="background: #f3f3f3">        Method = <span style="color:#a31515">&quot;DELETE&quot;</span>,</li> <li>        UriTemplate = <span style="color:#a31515">&quot;/Contacts/{id}&quot;</span>,</li> <li style="background: #f3f3f3">        ResponseFormat = <span style="color:#2b91af">WebMessageFormat</span>.Json)]</li> <li>    <span style="color:#0000ff">void</span> DeleteContact(<span style="color:#0000ff">string</span> id);</li> <li style="background: #f3f3f3">&nbsp;</li> <li>    [<span style="color:#2b91af">WebGet</span>(UriTemplate = <span style="color:#a31515">&quot;/Contacts&quot;</span>, ResponseFormat = <span style="color:#2b91af">WebMessageFormat</span>.Json)]</li> <li style="background: #f3f3f3">    <span style="color:#2b91af">List</span>&lt;<span style="color:#2b91af">Contact</span>&gt; GetAllContacts();</li> <li>&nbsp;</li> <li style="background: #f3f3f3">    [<span style="color:#2b91af">WebGet</span>(UriTemplate = <span style="color:#a31515">&quot;/Contacts/{id}&quot;</span>, ResponseFormat = <span style="color:#2b91af">WebMessageFormat</span>.Json)]</li> <li>    <span style="color:#2b91af">Contact</span> GetContact(<span style="color:#0000ff">string</span> id);</li> <li style="background: #f3f3f3">}</li> <li>&nbsp;</li> <li style="background: #f3f3f3"><span style="color:#0000ff">public</span> <span style="color:#0000ff">class</span> <span style="color:#2b91af">ContactManagerService</span> : <span style="color:#2b91af">IContactManager</span></li> <li>{</li> <li style="background: #f3f3f3">    <span style="color:#0000ff">static</span> <span style="color:#2b91af">List</span>&lt;<span style="color:#2b91af">Contact</span>&gt; AllContacts = <span style="color:#0000ff">new</span> <span style="color:#2b91af">List</span>&lt;<span style="color:#2b91af">Contact</span>&gt;();</li> <li>    <span style="color:#0000ff">static</span> <span style="color:#0000ff">int</span> currentId = 0;</li> <li style="background: #f3f3f3">    <span style="color:#0000ff">static</span> <span style="color:#0000ff">object</span> syncRoot = <span style="color:#0000ff">new</span> <span style="color:#0000ff">object</span>();</li> <li>&nbsp;</li> <li style="background: #f3f3f3">    <span style="color:#0000ff">public</span> <span style="color:#0000ff">string</span> AddContact(<span style="color:#2b91af">Contact</span> contact)</li> <li>    {</li> <li style="background: #f3f3f3">        <span style="color:#0000ff">int</span> contactId = <span style="color:#2b91af">Interlocked</span>.Increment(<span style="color:#0000ff">ref</span> currentId);</li> <li>        contact.Id = contactId.ToString(<span style="color:#2b91af">CultureInfo</span>.InvariantCulture);</li> <li style="background: #f3f3f3">        <span style="color:#0000ff">lock</span> (syncRoot)</li> <li>        {</li> <li style="background: #f3f3f3">            AllContacts.Add(contact);</li> <li>            <span style="color:#2b91af">WebOperationContext</span>.Current.OutgoingResponse.StatusCode = <span style="color:#2b91af">HttpStatusCode</span>.Created;</li> <li style="background: #f3f3f3">        }</li> <li>&nbsp;</li> <li style="background: #f3f3f3">        <span style="color:#0000ff">return</span> contact.Id;</li> <li>    }</li> <li style="background: #f3f3f3">&nbsp;</li> <li>    <span style="color:#0000ff">public</span> <span style="color:#0000ff">void</span> UpdateContact(<span style="color:#0000ff">string</span> id, <span style="color:#2b91af">Contact</span> contact)</li> <li style="background: #f3f3f3">    {</li> <li>        contact.Id = id;</li> <li style="background: #f3f3f3">        <span style="color:#0000ff">lock</span> (syncRoot)</li> <li>        {</li> <li style="background: #f3f3f3">            <span style="color:#0000ff">int</span> index = <span style="color:#0000ff">this</span>.FetchContact(id);</li> <li>            <span style="color:#0000ff">if</span> (index &gt;= 0)</li> <li style="background: #f3f3f3">            {</li> <li>                AllContacts[index] = contact;</li> <li style="background: #f3f3f3">            }</li> <li>        }</li> <li style="background: #f3f3f3">    }</li> <li>&nbsp;</li> <li style="background: #f3f3f3">    <span style="color:#0000ff">public</span> <span style="color:#0000ff">void</span> DeleteContact(<span style="color:#0000ff">string</span> id)</li> <li>    {</li> <li style="background: #f3f3f3">        <span style="color:#0000ff">lock</span> (syncRoot)</li> <li>        {</li> <li style="background: #f3f3f3">            <span style="color:#0000ff">int</span> index = <span style="color:#0000ff">this</span>.FetchContact(id);</li> <li>            <span style="color:#0000ff">if</span> (index &gt;= 0)</li> <li style="background: #f3f3f3">            {</li> <li>                AllContacts.RemoveAt(index);</li> <li style="background: #f3f3f3">            }</li> <li>        }</li> <li style="background: #f3f3f3">    }</li> <li>&nbsp;</li> <li style="background: #f3f3f3">    <span style="color:#0000ff">public</span> <span style="color:#2b91af">List</span>&lt;<span style="color:#2b91af">Contact</span>&gt; GetAllContacts()</li> <li>    {</li> <li style="background: #f3f3f3">        <span style="color:#2b91af">List</span>&lt;<span style="color:#2b91af">Contact</span>&gt; result;</li> <li>        <span style="color:#0000ff">lock</span> (syncRoot)</li> <li style="background: #f3f3f3">        {</li> <li>            result = AllContacts.ToList();</li> <li style="background: #f3f3f3">        }</li> <li>&nbsp;</li> <li style="background: #f3f3f3">        <span style="color:#0000ff">return</span> result;</li> <li>    }</li> <li style="background: #f3f3f3">&nbsp;</li> <li>    <span style="color:#0000ff">public</span> <span style="color:#2b91af">Contact</span> GetContact(<span style="color:#0000ff">string</span> id)</li> <li style="background: #f3f3f3">    {</li> <li>        <span style="color:#2b91af">Contact</span> result;</li> <li style="background: #f3f3f3">        <span style="color:#0000ff">lock</span> (syncRoot)</li> <li>        {</li> <li style="background: #f3f3f3">            <span style="color:#0000ff">int</span> index = <span style="color:#0000ff">this</span>.FetchContact(id);</li> <li>            result = index &lt; 0 ? <span style="color:#0000ff">null</span> : AllContacts[index];</li> <li style="background: #f3f3f3">        }</li> <li>&nbsp;</li> <li style="background: #f3f3f3">        <span style="color:#0000ff">return</span> result;</li> <li>    }</li> <li style="background: #f3f3f3">&nbsp;</li> <li>    <span style="color:#0000ff">private</span> <span style="color:#0000ff">int</span> FetchContact(<span style="color:#0000ff">string</span> id)</li> <li style="background: #f3f3f3">    {</li> <li>        <span style="color:#0000ff">int</span> result = -1;</li> <li style="background: #f3f3f3">        <span style="color:#0000ff">for</span> (<span style="color:#0000ff">int</span> i = 0; i &lt; AllContacts.Count; i++)</li> <li>        {</li> <li style="background: #f3f3f3">            <span style="color:#0000ff">if</span> (AllContacts[i].Id == id)</li> <li>            {</li> <li style="background: #f3f3f3">                result = i;</li> <li>                <span style="color:#0000ff">break</span>;</li> <li style="background: #f3f3f3">            }</li> <li>        }</li> <li style="background: #f3f3f3">&nbsp;</li> <li>        <span style="color:#0000ff">if</span> (result &lt; 0)</li> <li style="background: #f3f3f3">        {</li> <li>            <span style="color:#2b91af">WebOperationContext</span>.Current.OutgoingResponse.StatusCode = <span style="color:#2b91af">HttpStatusCode</span>.NotFound;</li> <li style="background: #f3f3f3">        }</li> <li>&nbsp;</li> <li style="background: #f3f3f3">        <span style="color:#0000ff">return</span> result;</li> <li>    }</li> <li style="background: #f3f3f3">}</li> </ol> </div> </div> </div>  <p>Now for the behavior used to set up the operation selector. The behavior wraps the original operation selector from the endpoint, so it needs to be added <em>after</em> the <a href="https://msdn.microsoft.com/en-us/library/system.servicemodel.description.webhttpbehavior.aspx">WebHttpBehavior</a> (which will add the first selector to the runtime) – see the discussion about wrapping internal objects at the <a href="https://blogs.msdn.com/b/carlosfigueira/archive/2011/05/03/wcf-extensibility-message-formatters.aspx">post about formatters</a>).</p>  <div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:fe94dcf2-414b-4197-a8a2-1e95ebe5e3fd" class="wlWriterEditableSmartContent"> <div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"> <div style="background: #fff; max-height: 400px; overflow: auto"> <ol style="background: #ffffff; margin: 0; padding: 0 0 0 5px; white-space: nowrap"> <li><span style="color:#0000ff">public</span> <span style="color:#0000ff">class</span> <span style="color:#2b91af">HttpOverrideBehavior</span> : <span style="color:#2b91af">IEndpointBehavior</span></li> <li style="background: #f3f3f3">{</li> <li>    <span style="color:#0000ff">public</span> <span style="color:#0000ff">const</span> <span style="color:#0000ff">string</span> HttpMethodOverrideHeaderName = <span style="color:#a31515">&quot;X-HTTP-Method-Override&quot;</span>;</li> <li style="background: #f3f3f3">    <span style="color:#0000ff">public</span> <span style="color:#0000ff">const</span> <span style="color:#0000ff">string</span> OriginalHttpMethodPropertyName = <span style="color:#a31515">&quot;OriginalHttpMethod&quot;</span>;</li> <li>&nbsp;</li> <li style="background: #f3f3f3">    <span style="color:#0000ff">public</span> <span style="color:#0000ff">void</span> AddBindingParameters(<span style="color:#2b91af">ServiceEndpoint</span> endpoint, <span style="color:#2b91af">BindingParameterCollection</span> bindingParameters)</li> <li>    {</li> <li style="background: #f3f3f3">    }</li> <li>&nbsp;</li> <li style="background: #f3f3f3">    <span style="color:#0000ff">public</span> <span style="color:#0000ff">void</span> ApplyClientBehavior(<span style="color:#2b91af">ServiceEndpoint</span> endpoint, <span style="color:#2b91af">ClientRuntime</span> clientRuntime)</li> <li>    {</li> <li style="background: #f3f3f3">    }</li> <li>&nbsp;</li> <li style="background: #f3f3f3">    <span style="color:#0000ff">public</span> <span style="color:#0000ff">void</span> ApplyDispatchBehavior(<span style="color:#2b91af">ServiceEndpoint</span> endpoint, <span style="color:#2b91af">EndpointDispatcher</span> endpointDispatcher)</li> <li>    {</li> <li style="background: #f3f3f3">        endpointDispatcher.DispatchRuntime.OperationSelector = <span style="color:#0000ff">new</span> <span style="color:#2b91af">HttpOverrideOperationSelector</span>(endpointDispatcher.DispatchRuntime.OperationSelector);</li> <li>    }</li> <li style="background: #f3f3f3">&nbsp;</li> <li>    <span style="color:#0000ff">public</span> <span style="color:#0000ff">void</span> Validate(<span style="color:#2b91af">ServiceEndpoint</span> endpoint)</li> <li style="background: #f3f3f3">    {</li> <li>    }</li> <li style="background: #f3f3f3">}</li> </ol> </div> </div> </div>  <p>And the selector itself. As I mentioned before, The first thing the selector will do is to try to fetch the <a href="https://msdn.microsoft.com/en-us/library/system.servicemodel.channels.httprequestmessageproperty.aspx">HttpRequestMessageProperty</a> from the message properties – if the message was received using HTTP, it will contain information about the HTTP verb and headers in the request. It will then try to find the X-HTTP-Method-Override header, and if it’s present, it will update the message property with the method override. For completeness sake, this selector also adds a new property to the message with the original HTTP verb from the request (if the service needs this information somehow). Finally, the selector delegates the selection to the original selector, which will then use that information to route the incoming request to the appropriate method.</p>  <div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:4645797e-4cd2-484d-bf95-4eb3725ac5c3" class="wlWriterEditableSmartContent"> <div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"> <div style="background: #fff; max-height: 400px; overflow: auto"> <ol style="background: #ffffff; margin: 0; padding: 0 0 0 5px; white-space: nowrap"> <li><span style="color:#0000ff">class</span> <span style="color:#2b91af">HttpOverrideOperationSelector</span> : <span style="color:#2b91af">IDispatchOperationSelector</span></li> <li style="background: #f3f3f3">{</li> <li>    <span style="color:#0000ff">private</span> <span style="color:#2b91af">IDispatchOperationSelector</span> originalSelector;</li> <li style="background: #f3f3f3">&nbsp;</li> <li>    <span style="color:#0000ff">public</span> HttpOverrideOperationSelector(<span style="color:#2b91af">IDispatchOperationSelector</span> originalSelector)</li> <li style="background: #f3f3f3">    {</li> <li>        <span style="color:#0000ff">this</span>.originalSelector = originalSelector;</li> <li style="background: #f3f3f3">    }</li> <li>&nbsp;</li> <li style="background: #f3f3f3">    <span style="color:#0000ff">public</span> <span style="color:#0000ff">string</span> SelectOperation(<span style="color:#0000ff">ref</span> <span style="color:#2b91af">Message</span> message)</li> <li>    {</li> <li style="background: #f3f3f3">        <span style="color:#0000ff">if</span> (message.Properties.ContainsKey(<span style="color:#2b91af">HttpRequestMessageProperty</span>.Name))</li> <li>        {</li> <li style="background: #f3f3f3">            <span style="color:#2b91af">HttpRequestMessageProperty</span> reqProp;</li> <li>            reqProp = (<span style="color:#2b91af">HttpRequestMessageProperty</span>)message.Properties[<span style="color:#2b91af">HttpRequestMessageProperty</span>.Name];</li> <li style="background: #f3f3f3">            <span style="color:#0000ff">string</span> httpMethodOverride = reqProp.Headers[<span style="color:#2b91af">HttpOverrideBehavior</span>.HttpMethodOverrideHeaderName];</li> <li>            <span style="color:#0000ff">if</span> (!<span style="color:#2b91af">String</span>.IsNullOrEmpty(httpMethodOverride))</li> <li style="background: #f3f3f3">            {</li> <li>                message.Properties[<span style="color:#2b91af">HttpOverrideBehavior</span>.OriginalHttpMethodPropertyName] = reqProp.Method;</li> <li style="background: #f3f3f3">                reqProp.Method = httpMethodOverride;</li> <li>            }</li> <li style="background: #f3f3f3">        }</li> <li>&nbsp;</li> <li style="background: #f3f3f3">        <span style="color:#0000ff">return</span> <span style="color:#0000ff">this</span>.originalSelector.SelectOperation(<span style="color:#0000ff">ref</span> message);</li> <li>    }</li> <li style="background: #f3f3f3">}</li> </ol> </div> </div> </div>  <p>Finally, the test code to wrap it all together. The code is very similar to the one for the message inspectors post, with a few differences: the new behavior (HttpOverrideBehavior) is added <em>after</em> the REST ((WebHttp) behavior, and after sending requests with the “real” HTTP verb it will send some additional requests using POST, but using the HTTP override method to route it to different operations.</p>  <div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:23248aa9-e4b2-4b07-a779-442a25ecdd3a" class="wlWriterEditableSmartContent"> <div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"> <div style="background: #fff; max-height: 400px; overflow: auto"> <ol style="background: #ffffff; margin: 0; padding: 0 0 0 5px; white-space: nowrap"> <li><span style="color:#0000ff">static</span> <span style="color:#0000ff">void</span> Main(<span style="color:#0000ff">string</span>[] args)</li> <li style="background: #f3f3f3">{</li> <li>    <span style="color:#0000ff">string</span> baseAddress = <span style="color:#a31515">&quot;https://&quot;</span> + <span style="color:#2b91af">Environment</span>.MachineName + <span style="color:#a31515">&quot;:8000/Service&quot;</span>;</li> <li style="background: #f3f3f3">    <span style="color:#2b91af">ServiceHost</span> host = <span style="color:#0000ff">new</span> <span style="color:#2b91af">ServiceHost</span>(<span style="color:#0000ff">typeof</span>(<span style="color:#2b91af">ContactManagerService</span>), <span style="color:#0000ff">new</span> <span style="color:#2b91af">Uri</span>(baseAddress));</li> <li>    <span style="color:#2b91af">ServiceEndpoint</span> endpoint = host.AddServiceEndpoint(<span style="color:#0000ff">typeof</span>(<span style="color:#2b91af">IContactManager</span>), <span style="color:#0000ff">new</span> <span style="color:#2b91af">WebHttpBinding</span>(), <span style="color:#a31515">&quot;&quot;</span>);</li> <li style="background: #f3f3f3">    endpoint.Behaviors.Add(<span style="color:#0000ff">new</span> <span style="color:#2b91af">WebHttpBehavior</span>());</li> <li>    endpoint.Behaviors.Add(<span style="color:#0000ff">new</span> <span style="color:#2b91af">HttpOverrideBehavior</span>());</li> <li style="background: #f3f3f3">    host.Open();</li> <li>    <span style="color:#2b91af">Console</span>.WriteLine(<span style="color:#a31515">&quot;Host opened&quot;</span>);</li> <li style="background: #f3f3f3">&nbsp;</li> <li>    <span style="color:#0000ff">string</span> johnId = SendRequest(</li> <li style="background: #f3f3f3">        <span style="color:#a31515">&quot;POST&quot;</span>,</li> <li>        baseAddress + <span style="color:#a31515">&quot;/Contacts&quot;</span>,</li> <li style="background: #f3f3f3">        <span style="color:#a31515">&quot;application/json&quot;</span>,</li> <li>        CreateJsonContact(<span style="color:#0000ff">null</span>, <span style="color:#a31515">&quot;John Doe&quot;</span>, <span style="color:#a31515">&quot;john@doe.com&quot;</span>, <span style="color:#a31515">&quot;206-555-3333&quot;</span>));</li> <li style="background: #f3f3f3">    <span style="color:#0000ff">string</span> janeId = SendRequest(</li> <li>        <span style="color:#a31515">&quot;POST&quot;</span>,</li> <li style="background: #f3f3f3">        baseAddress + <span style="color:#a31515">&quot;/Contacts&quot;</span>,</li> <li>        <span style="color:#a31515">&quot;application/json&quot;</span>,</li> <li style="background: #f3f3f3">        CreateJsonContact(<span style="color:#0000ff">null</span>, <span style="color:#a31515">&quot;Jane Roe&quot;</span>, <span style="color:#a31515">&quot;jane@roe.com&quot;</span>, <span style="color:#a31515">&quot;202-555-4444 202-555-8888&quot;</span>));</li> <li>&nbsp;</li> <li style="background: #f3f3f3">    <span style="color:#2b91af">Console</span>.WriteLine(<span style="color:#a31515">&quot;All contacts&quot;</span>);</li> <li>    SendRequest(<span style="color:#a31515">&quot;GET&quot;</span>, baseAddress + <span style="color:#a31515">&quot;/Contacts&quot;</span>, <span style="color:#0000ff">null</span>, <span style="color:#0000ff">null</span>);</li> <li style="background: #f3f3f3">&nbsp;</li> <li>    <span style="color:#2b91af">Console</span>.WriteLine(<span style="color:#a31515">&quot;Updating Jane&quot;</span>);</li> <li style="background: #f3f3f3">    SendRequest(</li> <li>        <span style="color:#a31515">&quot;PUT&quot;</span>,</li> <li style="background: #f3f3f3">        baseAddress + <span style="color:#a31515">&quot;/Contacts/&quot;</span> + janeId,</li> <li>        <span style="color:#a31515">&quot;application/json&quot;</span>,</li> <li style="background: #f3f3f3">        CreateJsonContact(janeId, <span style="color:#a31515">&quot;Jane Roe&quot;</span>, <span style="color:#a31515">&quot;jane@roe.org&quot;</span>, <span style="color:#a31515">&quot;202-555-4444 202-555-8888&quot;</span>));</li> <li>&nbsp;</li> <li style="background: #f3f3f3">    <span style="color:#2b91af">Console</span>.WriteLine(<span style="color:#a31515">&quot;All contacts&quot;</span>);</li> <li>    SendRequest(<span style="color:#a31515">&quot;GET&quot;</span>, baseAddress + <span style="color:#a31515">&quot;/Contacts&quot;</span>, <span style="color:#0000ff">null</span>, <span style="color:#0000ff">null</span>);</li> <li style="background: #f3f3f3">&nbsp;</li> <li>    <span style="color:#2b91af">Console</span>.WriteLine(<span style="color:#a31515">&quot;Deleting John&quot;</span>);</li> <li style="background: #f3f3f3">    SendRequest(<span style="color:#a31515">&quot;DELETE&quot;</span>, baseAddress + <span style="color:#a31515">&quot;/Contacts/&quot;</span> + johnId, <span style="color:#0000ff">null</span>, <span style="color:#0000ff">null</span>);</li> <li>&nbsp;</li> <li style="background: #f3f3f3">    <span style="color:#2b91af">Console</span>.WriteLine(<span style="color:#a31515">&quot;Is John still here?&quot;</span>);</li> <li>    SendRequest(<span style="color:#a31515">&quot;GET&quot;</span>, baseAddress + <span style="color:#a31515">&quot;/Contacts/&quot;</span> + johnId, <span style="color:#0000ff">null</span>, <span style="color:#0000ff">null</span>);</li> <li style="background: #f3f3f3">&nbsp;</li> <li>    <span style="color:#2b91af">Console</span>.WriteLine(<span style="color:#a31515">&quot;Adding John again&quot;</span>);</li> <li style="background: #f3f3f3">    johnId = SendRequest(</li> <li>        <span style="color:#a31515">&quot;POST&quot;</span>,</li> <li style="background: #f3f3f3">        baseAddress + <span style="color:#a31515">&quot;/Contacts&quot;</span>,</li> <li>        <span style="color:#a31515">&quot;application/json&quot;</span>,</li> <li style="background: #f3f3f3">        CreateJsonContact(<span style="color:#0000ff">null</span>, <span style="color:#a31515">&quot;John Doe&quot;</span>, <span style="color:#a31515">&quot;john@doe.com&quot;</span>, <span style="color:#a31515">&quot;206-555-3333&quot;</span>));</li> <li>&nbsp;</li> <li style="background: #f3f3f3">    <span style="color:#2b91af">Console</span>.WriteLine(<span style="color:#a31515">&quot;Updating John, now using X-HTTP-Method-Override&quot;</span>);</li> <li>    <span style="color:#2b91af">Dictionary</span>&lt;<span style="color:#0000ff">string</span>, <span style="color:#0000ff">string</span>&gt; overrideWithPut = <span style="color:#0000ff">new</span> <span style="color:#2b91af">Dictionary</span>&lt;<span style="color:#0000ff">string</span>, <span style="color:#0000ff">string</span>&gt;();</li> <li style="background: #f3f3f3">    overrideWithPut.Add(<span style="color:#a31515">&quot;X-HTTP-Method-Override&quot;</span>, <span style="color:#a31515">&quot;PUT&quot;</span>);</li> <li>    SendRequest(</li> <li style="background: #f3f3f3">        <span style="color:#a31515">&quot;POST&quot;</span>,</li> <li>        baseAddress + <span style="color:#a31515">&quot;/Contacts/&quot;</span> + johnId,</li> <li style="background: #f3f3f3">        <span style="color:#a31515">&quot;application/json&quot;</span>,</li> <li>        CreateJsonContact(johnId, <span style="color:#a31515">&quot;John Doe Updated&quot;</span>, <span style="color:#a31515">&quot;john@doe.com&quot;</span>, <span style="color:#a31515">&quot;206-555-3333&quot;</span>),</li> <li style="background: #f3f3f3">        overrideWithPut);</li> <li>&nbsp;</li> <li style="background: #f3f3f3">    <span style="color:#2b91af">Console</span>.WriteLine(<span style="color:#a31515">&quot;All contacts&quot;</span>);</li> <li>    SendRequest(<span style="color:#a31515">&quot;GET&quot;</span>, baseAddress + <span style="color:#a31515">&quot;/Contacts&quot;</span>, <span style="color:#0000ff">null</span>, <span style="color:#0000ff">null</span>);</li> <li style="background: #f3f3f3">&nbsp;</li> <li>    <span style="color:#2b91af">Console</span>.WriteLine(<span style="color:#a31515">&quot;Deleting Jane, using X-HTTP-Method-Override&quot;</span>);</li> <li style="background: #f3f3f3">    <span style="color:#2b91af">Dictionary</span>&lt;<span style="color:#0000ff">string</span>, <span style="color:#0000ff">string</span>&gt; overrideWithDelete = <span style="color:#0000ff">new</span> <span style="color:#2b91af">Dictionary</span>&lt;<span style="color:#0000ff">string</span>, <span style="color:#0000ff">string</span>&gt;();</li> <li>    overrideWithDelete.Add(<span style="color:#a31515">&quot;X-HTTP-Method-Override&quot;</span>, <span style="color:#a31515">&quot;DELETE&quot;</span>);</li> <li style="background: #f3f3f3">    SendRequest(<span style="color:#a31515">&quot;POST&quot;</span>, baseAddress + <span style="color:#a31515">&quot;/Contacts/&quot;</span> + janeId, <span style="color:#a31515">&quot;application/json&quot;</span>, <span style="color:#a31515">&quot;&quot;</span>, overrideWithDelete);</li> <li>&nbsp;</li> <li style="background: #f3f3f3">    <span style="color:#2b91af">Console</span>.WriteLine(<span style="color:#a31515">&quot;All contacts&quot;</span>);</li> <li>    SendRequest(<span style="color:#a31515">&quot;GET&quot;</span>, baseAddress + <span style="color:#a31515">&quot;/Contacts&quot;</span>, <span style="color:#0000ff">null</span>, <span style="color:#0000ff">null</span>);</li> <li style="background: #f3f3f3">&nbsp;</li> <li>    <span style="color:#2b91af">Console</span>.WriteLine(<span style="color:#a31515">&quot;Press ENTER to close&quot;</span>);</li> <li style="background: #f3f3f3">    <span style="color:#2b91af">Console</span>.ReadLine();</li> <li>    host.Close();</li> <li style="background: #f3f3f3">}</li> </ol> </div> </div> </div>  <h3>Coming up</h3>  <p>Operation invokers, for both synchronous and asynchronous operations.</p>  <p>[<a href="https://code.msdn.microsoft.com/Support-for-X-HTTP-Method-9f71ceb1">Code in this post</a>]</p>  <p>[<a href="https://blogs.msdn.com/b/carlosfigueira/archive/2011/03/14/wcf-extensibility.aspx">Back to the index</a>]</p>  <p>Carlos Figueira   <br /><a href="https://blogs.msdn.com/carlosfigueira">https://blogs.msdn.com/carlosfigueira</a>    <br />Twitter: @carlos_figueira <a href="https://twitter.com/carlos_figueira">https://twitter.com/carlos_figuei</a></p>]]></content:encoded>
    </item>
    <item>
      <title>Constraining what activities can be dropped on a custom activity designer in Windows Workflow Foundation 4</title>
      <link>https://docs.microsoft.com/archive/blogs/endpoint/constraining-what-activities-can-be-dropped-on-a-custom-activity-designer-in-windows-workflow-foundation-4</link>
      <pubDate>Mon, 09 May 2011 15:34:00 GMT</pubDate>
      <dc:creator><![CDATA[Patrick D. Fletcher [MSFT]]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/endpoint/2011/05/09/constraining-what-activities-can-be-dropped-on-a-custom-activity-designer-in-windows-workflow-foundation-4/</guid>
      <description><![CDATA[When designing a custom activity that can be used as a container for other activities, you may want...]]></description>
      <content:encoded><![CDATA[<p>When designing a custom activity that can be used as a container for other activities, you may want to restrict the activity user to a certain set of activities.</p>
<p>If the custom activity can only accept a single type, this can be implemented quite simply by setting the WorkflowItemPresenter.AllowedItemType or WorkflowItemsPresenter.AllowedItemType property. In this example, I allow only a WriteLine activity to be dropped:</p>
<p><code>
<pre class="scroll"><code class="cplusplus">&lt;sap:WorkflowItemsPresenter HintText="Drop Activities Here" <br />
Margin="5" MinHeight="100"<br />
Items="{Binding Path=ModelItem.Activities}"<br />
AllowedItemType="{x:Type TypeName=sa:WriteLine}"&gt;</code></pre>
</code></p>
<p>Note that to use the above code, you'll need to add a namespace declaration to your designer:</p>
<p><code>
<pre class="scroll"><code class="cplusplus">xmlns:sa="clr-namespace:System.Activities.Statements;assembly=System.Activities"</code></pre>
</code></p>
<p>If you need to restrict to a set of allowed activity types, rather than a single type, this can be done by adding a validation error to the validation errors collection during the CacheMetadata phase. Note that this will create a runtime&nbsp;error (though technically the user will experience it in the design phase), which will&nbsp;cause an error icon to be displayed, rather than preventing the activity to be dropped in the first place.&nbsp;In this example, I allow only a WriteLine or Assign activity to be dropped:</p>
<p><code>
<pre class="scroll"><code class="cplusplus">protected override void CacheMetadata(NativeActivityMetadata metadata)<br />
{<br />
    base.CacheMetadata(metadata);<br />
    foreach (Activity a in Activities)<br />
    {<br />
        if ((a.GetType() != typeof(System.Activities.Statements.WriteLine)) &amp;&amp; (a.GetType() != typeof(System.Activities.Statements.Assign)))<br />
        {<br />
            metadata.AddValidationError("Child activity is not of type WriteLine or Assign");<br />
        }<br />
    }<br />
}<br />
</code></pre>
</code></p>]]></content:encoded>
    </item>
    <item>
      <title>Microsoft.Activities and Microsoft.Activities.UnitTesting now available via NuGet</title>
      <link>https://docs.microsoft.com/archive/blogs/endpoint/microsoft-activities-and-microsoft-activities-unittesting-now-available-via-nuget</link>
      <pubDate>Mon, 09 May 2011 10:31:48 GMT</pubDate>
      <dc:creator><![CDATA[Ron Jacobs]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/endpoint/2011/05/09/microsoft-activities-and-microsoft-activities-unittesting-now-available-via-nuget/</guid>
      <description><![CDATA[When I first saw NuGet I knew I had to find a way to take advantage of this great new way to deliver...]]></description>
      <content:encoded><![CDATA[<p>When I first saw <a href="https://nuget.org">NuGet</a> I knew I had to find a way to take advantage of this great new way to deliver code to you.&#160; It took a few months but finally the other day I decided to spend an afternoon working on a NuGet package for Microsoft.Activities.&#160; Two and a half days later (isn’t that how it always goes) I produced packages for <a href="https://nuget.org/List/Packages/Microsoft.Activities">Microsoft.Activities</a> and <a href="https://nuget.org/List/Packages/Microsoft.Activities.UnitTesting">Microsoft.Activities.UnitTesting</a></p>  <p><object type="application/x-silverlight-2" data="data:application/x-silverlight-2," width="512" height="288"><param name="minRuntimeVersion" value="4.0.50401.0" /><param name="source" value="https://channel9.msdn.com/scripts/Channel9.xap?v=1.5" /><param name="initParams" value="mediaurl=https://files.ch9.ms/ch9/5bc8/ed310d62-f9a3-4368-b613-9edc000c5bc8/endpointWFNuGet.ism/manifest,thumbnail=https://media.ch9.ms/ch9/5bc8/ed310d62-f9a3-4368-b613-9edc000c5bc8/endpointWFNuGet_512_ch9.jpg,deliverymethod=adaptivestreaming,autoplay=false,entryid=ed310d62f9a34368b6139edc000c5bc8" /></object></p>  <h3>How To Get Microsoft.Activities or Microsoft.Activities.UnitTesting with NuGet</h3>  <br />  <h4>Step 0: Install NuGet</h4>  <p>If you don’t already have <a href="https://nuget.org/">NuGet</a>, install it <a href="https://visualstudiogallery.msdn.microsoft.com/en-us/27077b70-9dad-4c64-adcf-c7cf6bc9970c/file/37502/5/NuGet.Tools.signed.vsix">now</a> (you will have to restart Visual Studio if it is already running)</p>  <h4>Step 1: Create a project</h4>  <p>Your project can be any kind of project.&#160; NuGet will automatically add references that are required.&#160; For this example, I’m going to create a Class Library project.</p>  <h4>Step 2a: Install the Microsoft.Activities Package (Using the Package Manager Console)</h4>  <ol>   <li>Select <strong>View / Other Windows / Package Manager Console</strong> </li>    <li>Type <strong>Install-Package Microsoft.Activities</strong> </li> </ol>  <p><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/03/60/metablogapi/4035.image_5D33391D.png" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-03-60-metablogapi/4035.image_5F00_5D33391D.png"><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/03/60/metablogapi/2772.image_thumb_4AEA725B.png" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-03-60-metablogapi/2772.image_5F00_thumb_5F00_4AEA725B.png" width="517" height="176" /></a></p>  <h4>Step 2b: Install the Microsoft.Activities Package (Using Library Package Reference)</h4>  <p>If you would rather have a UI to browse packages, </p>  <ol>   <li>Right click on the references folder </li>    <li>Select Add Library Package Reference… </li>    <li>In the search box type <strong>Microsoft.Activities</strong> </li>    <li>Click the <strong>Install</strong> button to add a library package reference </li> </ol>  <p><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/03/60/metablogapi/7673.SNAGHTML4b4210_0AB458E1.png" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-03-60-metablogapi/7673.SNAGHTML4b4210_5F00_0AB458E1.png"><img style="background-image: none; border-right-width: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="SNAGHTML4b4210" border="0" alt="SNAGHTML4b4210" src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/03/60/metablogapi/3056.SNAGHTML4b4210_thumb_6E376700.png" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-03-60-metablogapi/3056.SNAGHTML4b4210_5F00_thumb_5F00_6E376700.png" width="244" height="139" /></a></p>  <h4></h4>  <h2>What Happens to my Project / Visual Studio when I install this?</h2>  <h4>Your Project </h4>  <ul>   <li>Add reference to <strong>System.Activities</strong> </li>    <li>Add reference to ($SolutionDir)packages\Microsoft.Activities.1.8.1.506\lib\Net40\Microsoft.Activities.dll) with copy local = true so the assembly will be in your bin directory when you build. </li> </ul>  <h4>Visual Studio</h4>  <ul>   <li>Added toolbox icons for the activities in Microsoft.Activities to your toolbox pointing to the last installation location for Microsoft.Activities.dll </li>    <li>Add a new Workflow to your project to see the two groups (Dictionary and Microsoft.Activities) </li> </ul>  <p><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/03/60/metablogapi/2474.image_3BD39381.png" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-03-60-metablogapi/2474.image_5F00_3BD39381.png"><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/03/60/metablogapi/4848.image_thumb_298ACCBF.png" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-03-60-metablogapi/4848.image_5F00_thumb_5F00_298ACCBF.png" width="230" height="316" /></a></p>  <h2>What if I want to use .NET 4 Platform Update 1 so I can create a State Machine workflow?</h2>  <p>The Microsoft.Activities package includes a version built for .NET 4 Platform Update 1.&#160; As you see in the folder structure there are Net40 and Net401 folders.&#160; NuGet automatically detects which version of the .NET Framework your project is targeting and will add a reference to the appropriate version </p>  <p><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/03/60/metablogapi/8233.image_0250838A.png" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-03-60-metablogapi/8233.image_5F00_0250838A.png"><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/03/60/metablogapi/0726.image_thumb_7B314711.png" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-03-60-metablogapi/0726.image_5F00_thumb_5F00_7B314711.png" width="204" height="110" /></a></p>  <h2>What if I change my target framework to .NET 4 Platform Update 1 after installing the Microsoft.Activities Package?</h2>  <p>If you change your target framework after installing your project will continue to reference the .NET 4 version of Microsoft.Activities.&#160; You can update the reference manually by browsing the packages folder and adding a reference or you can uninstall / reinstall the package.</p>  <h2>What if I create a new project and drag an activity from Microsoft.Activities onto a workflow without first installing Microsoft.Activities?</h2>  <p>Toolbox activities remain in the toolbox across projects.&#160; When you create a new project and open the workflow designer you will see the Microsoft.Activities and Dictionary tabs.&#160; When you drag one of these activities onto the workflow designer, Visual Studio will add a reference to the assembly that the toolbox icon is associated with.</p>  <p>If you installed the Microsoft.Activities package to a project named ClassLibary1 and then created another project named WorkflowConsoleApplication1 as soon as you start dragging an activity onto a workflow from WorkflowConsoleApplication1 you will see an assembly reference added to the Microsoft.Activities package installed under the ClassLibrary1 solution.</p>  <h2>What if I delete the solution where Microsoft.Activities was installed?</h2>  <p>If you try to delete it while Visual Studio is open you will get an error because Visual Studio has the assembly loaded.&#160; If you close Visual Studio and delete the solution, the next time you run Visual Studio and open the Workflow Designer you will see the icons are still in the toolbox.&#160; However when you try to drop an activity from the missing assembly, Visual Studio won’t allow it because it is unable to locate the assembly to add a reference.</p>  <p><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/03/60/metablogapi/7266.image_53F6FDDC.png" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-03-60-metablogapi/7266.image_5F00_53F6FDDC.png"><img style="background-image: none; border-right-width: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/03/60/metablogapi/3377.image_thumb_687C4D5A.png" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-03-60-metablogapi/3377.image_5F00_thumb_5F00_687C4D5A.png" width="244" height="197" /></a></p>  <h2>How do I fix the toolbox icons if they refer to an assembly that no longer exists?</h2>  <p>Each time install the Microsoft.Activities package, if the toolbox icons exist, they are removed and replaced with a toolbox icon that refers to the Microsoft.Activities assembly that is installed with the package in current project.</p>  <h2>What if I want to store the Microsoft.Activities assembly in some other location?&#160; How do I keep the NuGet package from updating my toolbox icons?</h2>  <p>If you change the name of the Tab group from Microsoft.Activities to something else, the package installation script won’t find the activities and won’t update them.&#160; However it will add a new activity group.</p>  <h2>How do I uninstall the Microsoft.Activities package?</h2>  <ol>   <li>Open the Package Manager console </li>    <li>Type <strong>Uninstall-Package Microsoft.Activities</strong> </li>    <li>In the toolbox, right click on the Microsoft.Activities tab group and select Delete Tab </li>    <li>Repeat for the Dictionary Group </li> </ol>  <p>Sometimes you will get an error when trying to uninstall because Visual Studio has one of the assemblies open.&#160; In this case you will have to manually delete the package.&#160; </p>  <pre>PM&gt; Uninstall-Package Microsoft.Activities
Successfully removed 'Microsoft.Activities 1.8.1.506' from WorkflowConsoleApplication10. 
Access to the path 'd:\ngt\WorkflowConsoleApplication10\packages\Microsoft.Activities.1.8.1.506\lib\Net40\Microsoft.Activities.dll' is denied. 
Access to the path 'd:\ngt\WorkflowConsoleApplication10\packages\Microsoft.Activities.1.8.1.506\tools\Microsoft.Activities.NuGet.dll' is denied. 
The directory is not empty.</pre>

<br />

<h2>What if I install Microsoft.Activities.UnitTesting and I don’t have Microsoft.Activities?</h2>

<p>Not to worry.&#160; Microsoft.Activities.UnitTesting has a dependency on the Microsoft.Activities package so NuGet will automatically deploy it as well.</p>

<pre>PM&gt; Install-Package Microsoft.Activities.UnitTesting 
'Microsoft.Activities (≥ 1.8.1)' not installed. Attempting to retrieve dependency from source... 
Done. 
Successfully installed 'Microsoft.Activities 1.8.1.509'. 
Successfully installed 'Microsoft.Activities.UnitTesting 1.8.1.509'. 
Successfully added 'Microsoft.Activities 1.8.1.509' to WorkflowConsoleApplication14. 
Successfully added 'Microsoft.Activities.UnitTesting 1.8.1.509' to WorkflowConsoleApplication14.</pre>

<h1>What if I install Microsoft.Activities.UnitTesting and then try to Uninstall Microsoft.Activities</h1>

<pre>PM&gt; Uninstall-Package Microsoft.Activities
Uninstall-Package : Unable to uninstall 'Microsoft.Activities 1.8.1.509' because 'Microsoft.Activities.UnitTesting 1.8.1.509' depends on it.
At line:1 char:18
+ Uninstall-Package &lt;&lt;&lt;&lt;  Microsoft.Activities
    + CategoryInfo          : NotSpecified: (:) [Uninstall-Package], InvalidOperationException
    + FullyQualifiedErrorId : NuGetCmdletUnhandledException,NuGet.PowerShell.Commands.UninstallPackageCommand</pre>

<h1>What do you think?</h1>

<p>I’m really happy with NuGet so far.&#160; I just updated the uninstall scripts so that they will remove the toolbox tabs (Microsoft.Activities, Dictionary and Unit Testing) created by the install scripts.&#160; Of all the things these packages do the interaction with the toolbox is the one area that I’m not sure about.&#160; Hopefully what I’ve done here will work for you.&#160; If not, tell me how we can do it better.</p>

<p>Ron Jacobs 
  <br /><a href="https://blogs.msdn.com/rjacobs">https://blogs.msdn.com/rjacobs</a> 

  <br />Twitter: @ronljacobs <a href="https://twitter.com/ronljacobs">https://twitter.com/ronljacobs</a></p>]]></content:encoded>
    </item>
    <item>
      <title>WCF scales up slowly with bursts of work</title>
      <link>https://docs.microsoft.com/archive/blogs/endpoint/wcf-scales-up-slowly-with-bursts-of-work</link>
      <pubDate>Wed, 04 May 2011 18:30:00 GMT</pubDate>
      <dc:creator><![CDATA[Dustin Metzgar]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/endpoint/2011/05/04/wcf-scales-up-slowly-with-bursts-of-work/</guid>
      <description><![CDATA[A few customers have&nbsp;noticed an issue with WCF scaling up when handling a burst of requests....]]></description>
      <content:encoded><![CDATA[<p>A few customers have&nbsp;noticed an issue with WCF scaling up when handling a burst of requests. Fortunately, there is a very simple workaround for this problem that is covered in <a href="https://support.microsoft.com/kb/2538826" title="KB2538826">KB2538826</a>&nbsp;(thanks to David Lamb for the investigation and write up). The KB article provides a lot of good information about when this would apply to your application and what to do to fix it. In this post, I want to explore exactly what's happening.</p>
<p>
<object height="288" type="application/x-silverlight-2" width="512" data="data:application/x-oleobject;base64,QfXq3+HzJEysrJnDBxUISgAJAADqNAAAxB0AAAAAAAAAAAAAAAAAAAAAAAACAAAAAAA8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAgAAAAAAAAAAAAAAAAABAAAAAQAAAAAAAAAAAAAAAAAAABgAAAA0AC4AMAAuADUAMAA0ADAAMQAuADAAAAAAAAAAAAAAAAAAAAAAAAAA">
<param value="4.0.50401.0" name="minRuntimeVersion" />
<param name="source" />
<param name="initParams" />
</object>
</p>
<p>The best part is that there is an easy way to reproduce the problem. To do this, create a new WCF service application with the .NET 4 framework:</p>
<p><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/41/84/2402.IOThreadPoolBug7.png" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-41-84/2402.IOThreadPoolBug7.png"><img src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/41/84/2402.IOThreadPoolBug7.png" original-url="https://blogs.msdn.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-01-41-84/2402.IOThreadPoolBug7.png" border="0" /></a></p>
<p>Then trim the default service contract down to only the GetData method:</p>
<pre><span><span class="net_key">using</span> System.ServiceModel;

[ServiceContract]
<span class="net_key">public</span> <span class="net_key">interface</span> IService1
{
    [OperationContract]
    <span class="net_key">string</span> GetData(<span class="net_key">int</span> value);
}</span></pre>
<p>And fill out the GetData method in the service code:</p>
<pre><span><span class="net_key">using</span> System;
<span class="net_key">using</span> System.Threading;

<span class="net_key">public</span> <span class="net_key">class</span> Service1 : IService1
{
    <span class="net_key">public</span> <span class="net_key">string</span> GetData(<span class="net_key">int</span> value)
    {
        <span class="yh">Thread.Sleep(TimeSpan.FromSeconds(2));</span>
        <span class="net_key">return</span> <span class="net_key">string</span>.Format(<span class="net_string">"You entered: {0}"</span>, value);
    }
}</span></pre>
<p>The two second sleep is the most important part of this repro. We'll explore why after we create the client and observe the effects. But first, make sure to set the throttle in the web.config:</p>
<pre>&lt;configuration&gt;
  &lt;system.web&gt;
    &lt;compilation debug="true" targetFramework="4.0" /&gt;
  &lt;/system.web&gt;
  &lt;system.serviceModel&gt;
    &lt;behaviors&gt;
      &lt;serviceBehaviors&gt;
        &lt;behavior&gt;
          &lt;serviceMetadata httpGetEnabled="true"/&gt;
          &lt;serviceDebug includeExceptionDetailInFaults="false"/&gt;
          <span class="yh">&lt;serviceThrottling maxConcurrentCalls="100"/&gt;</span>
        &lt;/behavior&gt;
      &lt;/serviceBehaviors&gt;
    &lt;/behaviors&gt;
    &lt;serviceHostingEnvironment multipleSiteBindingsEnabled="true" /&gt;
  &lt;/system.serviceModel&gt;
  &lt;system.webServer&gt;
    &lt;modules runAllManagedModulesForAllRequests="true"/&gt;
  &lt;/system.webServer&gt;
&lt;/configuration&gt;</pre>
<p>The Visual Studio web server would probably suffice, but I changed my web project to use IIS instead. This makes it more of a realistic situation. To do this go to the project properties, select the Web tab, and switch to IIS.</p>
<p><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/41/84/8750.IOThreadPoolBug8.png" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-41-84/8750.IOThreadPoolBug8.png"><img src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/41/84/8750.IOThreadPoolBug8.png" original-url="https://blogs.msdn.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-01-41-84/8750.IOThreadPoolBug8.png" border="0" /></a></p>
<p>Be sure to hit the Create Virtual Directory button. In my case, I have SharePoint installed and it's taking up port 80, so I put the default web server on port 8080. Once this is done, build all and browse to the service to make sure it's working correctly.</p>
<p>To create the test client, add a new command prompt executable to the solution and add a service reference to Service1. Change the client code to the following:</p>
<pre><span><span class="net_key">using</span> System;
<span class="net_key">using</span> System.Threading;
<span class="net_key">using</span> ServiceReference1;

<span class="net_key">class</span> Program
{
    <span class="net_key">private</span> <span class="net_key">const</span> <span class="net_key">int</span> numThreads = 100;
    <span class="net_key">private</span> <span class="net_key">static</span> CountdownEvent countdown;
    <span class="net_key">private</span> <span class="net_key">static</span> ManualResetEvent mre = <span class="net_key">new</span> ManualResetEvent(<span class="net_key">false</span>);

    <span class="net_key">static</span> <span class="net_key">void</span> Main(<span class="net_key">string</span>[] args)
    {
        <span class="net_key">string</span> s = <span class="net_key">null</span>;
        Console.WriteLine(<span class="net_string">"Press enter to start test."</span>);
        <span class="net_key">while</span> ((s = Console.ReadLine()) == <span class="net_key">string</span>.Empty)
        {
            RunTest();
            Console.WriteLine(<span class="net_string">"Allow a few seconds for threads to die."</span>);
            Console.WriteLine(<span class="net_string">"Press enter to run again."</span>);
            Console.WriteLine();
        }
    }

    <span class="net_key">static</span> <span class="net_key">void</span> RunTest()
    {
        mre.Reset();
        Console.WriteLine(<span class="net_string">"Starting {0} threads."</span>, numThreads);
        countdown = <span class="net_key">new</span> CountdownEvent(numThreads);
        <span class="net_key">for</span> (<span class="net_key">int</span> i = 0; i &lt; numThreads; i++)
        {
            Thread t = <span class="net_key">new</span> Thread(ThreadProc);
            t.Name = <span class="net_string">"Thread_"</span> + i;
            t.Start();
        }
        <span class="net_comment">// Wait for all threads to signal.</span>
        countdown.Wait();
        Console.Write(<span class="net_string">"Press enter to release threads."</span>);
        Console.ReadLine();
        Console.WriteLine(<span class="net_string">"Releasing threads."</span>);
        DateTime startTime = DateTime.Now;
        countdown = <span class="net_key">new</span> CountdownEvent(numThreads);
        <span class="net_comment">// Release all the threads.</span>
        mre.Set();
        <span class="net_comment">// Wait for all the threads to finish.</span>
        countdown.Wait();
        TimeSpan ts = DateTime.Now - startTime;
        Console.WriteLine(<span class="net_string">"Total time to run threads: {0}.{1:0}s."</span>,
            ts.Seconds, ts.Milliseconds / 100);
    }

    <span class="net_key">private</span> <span class="net_key">static</span> <span class="net_key">void</span> ThreadProc()
    {
        Service1Client client = <span class="net_key">new</span> Service1Client();
        client.Open();
        <span class="net_comment">// Signal that this thread is ready.</span>
        countdown.Signal();
        <span class="net_comment">// Wait for all the other threads before making service call.</span>
        mre.WaitOne();
        client.GetData(1337);
        <span class="net_comment">// Signal that this thread is finished.</span>
        countdown.Signal();
        client.Close();
    }
}</span></pre>
<p>The code above uses a combination of a ManualResetEvent and a CountdownEvent to coordinate the calls to all happen at the same time. It then waits for all the calls to finish and determines how long it took. When I run this on a dual core machine, I get the following:</p>
<pre><code>Press enter to start test.

Starting 100 threads.
Releasing threads.
Total time to run threads: 14.0s.
Allow a few seconds for threads to die.
Press enter to run again.</code></pre>
<p>If each individual request only sleeps for 2 seconds, why would it take 100 simultaneous requests&nbsp;14 seconds to complete? It should only take 2 seconds if they're all executing at the same time. To understand what's happening, let's look at the thread count in perfmon. First, open Administrative Tools -&gt; Performance Monitor. In the tree view on the left, pick Monitoring Tools -&gt; Performance Monitor. It should have the % Processor counter in there by default. We don't need that counter so select it in the bottom and press the delete key. Now click the add button to add a new counter:</p>
<p><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/41/84/8765.IOThreadPoolBug14.png" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-41-84/8765.IOThreadPoolBug14.png"><img src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/41/84/8765.IOThreadPoolBug14.png" original-url="https://blogs.msdn.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-01-41-84/8765.IOThreadPoolBug14.png" border="0" /></a></p>
<p>In the Add Counters dialog, expand the Process node:</p>
<p><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/41/84/7384.IOThreadPoolBug15.png" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-41-84/7384.IOThreadPoolBug15.png"><img height="333" width="414" src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/41/84/7384.IOThreadPoolBug15.png" original-url="https://blogs.msdn.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-01-41-84/7384.IOThreadPoolBug15.png" style="border: 0px;" /></a></p>
<p>Under this locate the Thread Count counter and then find the w3wp instance and press the&nbsp;Add &gt;&gt; button.</p>
<p><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/41/84/2804.IOThreadPoolBug16.png" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-41-84/2804.IOThreadPoolBug16.png"><img src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/41/84/2804.IOThreadPoolBug16.png" original-url="https://blogs.msdn.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-01-41-84/2804.IOThreadPoolBug16.png" border="0" /></a></p>
<p>If you don't see the w3wp instance, it is most likely because it wasn't running when you opened the Add Counters dialog. To correct this, close the dialog, ping the Service1.svc service, and then click the Add button again in perfmon.</p>
<p>Now you can run the test and you should see results similar to the following:</p>
<p><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/41/84/3386.IOThreadPoolBug1.png" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-41-84/3386.IOThreadPoolBug1.png"><img src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/41/84/3386.IOThreadPoolBug1.png" original-url="https://blogs.msdn.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-01-41-84/3386.IOThreadPoolBug1.png" border="0" /></a></p>
<p>In this test, I had pinged the service to get the metadata to warm everything up. When the test ran, you can see it took several seconds to add another 21 threads. The reason these threads are added is because when WCF runs your service code, it uses a thread from the IO thread pool. Your service code is then expected to do some work that is either quick or CPU intensive or takes up a database connection. But in the case of the test, the Thread.Sleep means that no work is being done and the thread is being held. A real world scenario where this pattern could occur is if you have a WCF middle tier that has to make calls into lower layers and then return the results.</p>
<p>For the most part, server load is assumed to change slowly. This means you would have a fairly constant load and the thread pool manager would pick the appropriate size for the thread pool to keep the CPU load high but without too much context switching. However, in the case of a sudden burst of requests with long-running, non-CPU-intensive&nbsp;work like this, the thread pool adapts too slowly. You can see in the graph above that the number of threads drops back down to 30 after some time. The thread pool has recognized that a thread has not been used for 15 seconds and therefore kills it because it is not needed.</p>
<p>To understand the thread pool a bit more, you can profile this and see what's happening. To get a profile, go to the Performance Explorer, usually pinned on the left side in Visual Studio, and click the button to create a new performance session:</p>
<p><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/41/84/8182.IOThreadPoolBug9.png" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-41-84/8182.IOThreadPoolBug9.png"><img height="174" width="264" src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/41/84/8182.IOThreadPoolBug9.png" original-url="https://blogs.msdn.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-01-41-84/8182.IOThreadPoolBug9.png" style="border: 0px;" /></a></p>
<p>Right click on the new performance session and select properties. In the properties window change the profiling type to Concurrency and uncheck the "Collect resource contention data" option.</p>
<p><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/41/84/0830.IOThreadPoolBug10.png" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-41-84/0830.IOThreadPoolBug10.png"><img src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/41/84/0830.IOThreadPoolBug10.png" original-url="https://blogs.msdn.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-01-41-84/0830.IOThreadPoolBug10.png" border="0" /></a></p>
<p>Before we start profiling, let's first go to the Tools-&gt;Options menu in Visual Studio. Under Debugging, enable the Microsoft public symbol server.</p>
<p><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/41/84/0841.IOThreadPoolBug11.png" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-41-84/0841.IOThreadPoolBug11.png"><img src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/41/84/0841.IOThreadPoolBug11.png" original-url="https://blogs.msdn.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-01-41-84/0841.IOThreadPoolBug11.png" border="0" /></a></p>
<p>Also, turn off the Just My Code option under Performance Tools:</p>
<p><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/41/84/3817.IOThreadPoolBug12.png" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-41-84/3817.IOThreadPoolBug12.png"><img src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/41/84/3817.IOThreadPoolBug12.png" original-url="https://blogs.msdn.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-01-41-84/3817.IOThreadPoolBug12.png" border="0" /></a></p>
<p>The next step is to make the test client the startup project. You may need to ping the Service1.svc service again to make sure the w3wp process is running.&nbsp;Now, attach to the w3wp process with the profiler. There is an attach button at the top of the performance explorer window or you can right click on the performance session and choose attach. </p>
<p><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/41/84/5554.IOThreadPoolBug13.png" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-41-84/5554.IOThreadPoolBug13.png"><img src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/41/84/5554.IOThreadPoolBug13.png" original-url="https://blogs.msdn.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-01-41-84/5554.IOThreadPoolBug13.png" border="0" /></a></p>
<p>Give the profiler a few seconds to warmup and then hit ctrl+F5 to execute the test client. After the test client finishes a single run pause and detach the profiler from the w3wp process. You can also hit the "Stop profiling" option but it will kill the process.</p>
<p>After you've finished profiling, it will take some time to process the data. Once it's up, switch to the thread view. In the figure below, I've rearranged the threads to make it easier to see the relationships:</p>
<p><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/41/84/6114.IOThreadPoolBug2.png" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-41-84/6114.IOThreadPoolBug2.png"><img src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/41/84/6114.IOThreadPoolBug2.png" original-url="https://blogs.msdn.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-01-41-84/6114.IOThreadPoolBug2.png" border="0" /></a></p>
<p>Blue represents sleep time. This matches what we expect to see, which is a lot of new threads all sleeping for 2 seconds. At the top of the sleeping threads is the gate thread. It checks every 500ms to see if the number of threads in the thread pool is appropriate to handle the outstanding work. If not, it creates a new thread. You can see in the beginning that it creates 2 threads and then 1 thread at a time in 500ms increments. This is why it takes 14 seconds to process all 100 requests. So why are there two threads that look like they're created at the same time? If you look more closely, that's actually not the case. Let's zoom in on that part:</p>
<p><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/41/84/6507.IOThreadPoolBug3.png" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-41-84/6507.IOThreadPoolBug3.png"><img src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/41/84/6507.IOThreadPoolBug3.png" original-url="https://blogs.msdn.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-01-41-84/6507.IOThreadPoolBug3.png" border="0" /></a></p>
<p>Here again the highlighted thread is the gate thread. Before it finishes its 500ms sleep, a new thread is created to handle one of the WCF service calls which sleeps for 2 seconds. Then when the gate thread does its check, it realizes that there is a lot of work currently backed up and creates another new thread. On a dual core machine like this, the default minimum IO thread pool setting is 2: 1 per core. One of those threads is always taken up by WCF and functions as the timer thread. You can see that there are other threads created up top. This is most likely ASP.Net creating some worker threads to handle the incoming requests. You can see that they don't sleep because they're passing off work to WCF and getting ready to handle the next batch of work.</p>
<p>The most obvious thing to do then would be to increase the minimum number of IO threads. You can do this in two ways: use ThreadPool.SetMinThreads or use the &lt;processModel&gt; tag in the machine.config. Here is how to do the latter:</p>
<pre>&lt;system.web&gt;
  &lt;!--&lt;processModel autoConfig="true"/&gt;--&gt;
  &lt;processModel 
    autoConfig="false" 
    minIoThreads="101" 
    minWorkerThreads="2" 
    maxIoThreads="200" 
    maxWorkerThreads="40" 
    /&gt;</pre>
<p>Be sure to turn off the autoConfig setting or the other options will be ignored. If we run this test again, we get a much better result. Compare the previous snapshot of permon with this one:</p>
<p><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/41/84/5670.IOThreadPoolBug4.png" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-41-84/5670.IOThreadPoolBug4.png"><img src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/41/84/5670.IOThreadPoolBug4.png" original-url="https://blogs.msdn.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-01-41-84/5670.IOThreadPoolBug4.png" border="0" /></a></p>
<p>And the resulting output of the run is:</p>
<pre><code>Starting 100 threads.
Press enter to release threads.
Releasing threads.
Total time to run threads: <span class="yh">2.2s</span>.
Allow a few seconds for threads to die.
Press enter to run again.</code></pre>
<p>This is an excellent result. It is exactly what you would want to happen if a sudden burst of work comes in. But customers were saying that this wasn't happening for them. David Lamb told me that if you run this for a long time, like 2 hours, it would eventually stop quickly adding threads and behave as if the min IO threads was not set.</p>
<p>One of the things we can do is modify the test code to give enough time for the threads to have their 15 second timeout and just take out all the Console.ReadLine calls:</p>
<pre><span><span class="net_key">using</span> System;
<span class="net_key">using</span> System.Threading;
<span class="net_key">using</span> ServiceReference1;

<span class="net_key">class</span> Program
{
    <span class="net_key">private</span> <span class="net_key">const</span> <span class="net_key">int</span> numThreads = 100;
    <span class="net_key">private</span> <span class="net_key">static</span> CountdownEvent countdown;
    <span class="net_key">private</span> <span class="net_key">static</span> ManualResetEvent mre = <span class="net_key">new</span> ManualResetEvent(<span class="net_key">false</span>);

    <span class="net_key">static</span> <span class="net_key">void</span> Main(<span class="net_key">string</span>[] args)
    {
        <span class="net_key">while</span> (<span class="net_key">true</span>)
        {
            RunTest();
            Thread.Sleep(TimeSpan.FromSeconds(25));
            Console.WriteLine();
        }
    }

    <span class="net_key">static</span> <span class="net_key">void</span> RunTest()
    {
        mre.Reset();
        Console.WriteLine(<span class="net_string">"Starting {0} threads."</span>, numThreads);
        countdown = <span class="net_key">new</span> CountdownEvent(numThreads);
        <span class="net_key">for</span> (<span class="net_key">int</span> i = 0; i &lt; numThreads; i++)
        {
            Thread t = <span class="net_key">new</span> Thread(ThreadProc);
            t.Name = <span class="net_string">"Thread_"</span> + i;
            t.Start();
        }
        <span class="net_comment">// Wait for all threads to signal.</span>
        countdown.Wait();
        Console.WriteLine(<span class="net_string">"Releasing threads."</span>);
        DateTime startTime = DateTime.Now;
        countdown = <span class="net_key">new</span> CountdownEvent(numThreads);
        <span class="net_comment">// Release all the threads.</span>
        mre.Set();
        <span class="net_comment">// Wait for all the threads to finish.</span>
        countdown.Wait();
        TimeSpan ts = DateTime.Now - startTime;
        Console.WriteLine(<span class="net_string">"Total time to run threads: {0}.{1:0}s."</span>,
            ts.Seconds, ts.Milliseconds / 100);
    }

    <span class="net_key">private</span> <span class="net_key">static</span> <span class="net_key">void</span> ThreadProc()
    {
        Service1Client client = <span class="net_key">new</span> Service1Client();
        client.Open();
        <span class="net_comment">// Signal that this thread is ready.</span>
        countdown.Signal();
        <span class="net_comment">// Wait for all the other threads before making service call.</span>
        mre.WaitOne();
        client.GetData(1337);
        <span class="net_comment">// Signal that this thread is finished.</span>
        countdown.Signal();
        client.Close();
    }
}</span></pre>
<p>Then just let it run for a couple hours and see what happens. Luckily, it doesn't take 2 hours and we can reproduce the weird behavior pretty quickly. Here is a perfmon graph showing several runs:</p>
<p><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/41/84/8865.IOThreadPoolBug5.png" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-41-84/8865.IOThreadPoolBug5.png"><img src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/41/84/8865.IOThreadPoolBug5.png" original-url="https://blogs.msdn.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-01-41-84/8865.IOThreadPoolBug5.png" border="0" /></a></p>
<p>You can see that the first two bursts had quick scale up with the thread count. After that, it went back to being slow. After working with the CLR team, they determined that there is a bug in the IO thread pool. This bug causes the internal count of IO threads to get out-of-sync with reality. Some of you may be asking the question as to what min really means in terms of the thread pool. Because if you specify you want a minimum of 100 threads, why wouldn't there just always be 100 threads? For the purposes of the thread pool, the min is supposed to be the threshold that the pool can scale up to before it starts to be metered. The default working set cost for one thread is a half MB. So that is one reason to keep the thread count down.</p>
<p>Some customers have found that Juval Lowy's thread pool doesn't have the same problem and handles these bursts of work much better. Here is a link to his article: <a href="https://msdn.microsoft.com/en-us/magazine/cc163321.aspx">https://msdn.microsoft.com/en-us/magazine/cc163321.aspx</a></p>
<p>Notice that Juval has created a custom thread pool and relies on the SynchronizationContext to switch from the IO thread that WCF uses to the custom thread pool. Using a custom thread pool in your production environment is not advisable. Most developers do not have the resources to properly test a custom thread pool for their applications. Thankfully, there is an alternative and that is the recommendation made in the KB article. The alternative is to use the worker thread pool. Juval has some code for a custom attribute that you can put on your service to set the SynchronizationContext to use his custom thread pool and we can just modify that to put the work into the worker thread pool. The code for this change is all in the KB article, but I'll reiterate here along with with the changes to the service itself.</p>
<pre><span><span class="net_key">using</span> System;
<span class="net_key">using</span> System.Threading;
<span class="net_key">using</span> System.ServiceModel.Description;
<span class="net_key">using</span> System.ServiceModel.Channels;
<span class="net_key">using</span> System.ServiceModel.Dispatcher;

<span class="yh">[WorkerThreadPoolBehavior]</span>
<span class="net_key">public</span> <span class="net_key">class</span> Service1 : IService1
{
    <span class="net_key">public</span> <span class="net_key">string</span> GetData(<span class="net_key">int</span> value)
    {
        Thread.Sleep(TimeSpan.FromSeconds(2));
        <span class="net_key">return</span> <span class="net_key">string</span>.Format(<span class="net_string">"You entered: {0}"</span>, value);
    }
}

<span class="net_key">public</span> <span class="net_key">class</span> WorkerThreadPoolSynchronizer : SynchronizationContext
{
    <span class="net_key">public</span> <span class="net_key">override</span> <span class="net_key">void</span> Post(SendOrPostCallback d, <span class="net_key">object</span> state)
    {
        <span class="net_comment">// WCF almost always uses Post</span>
        <span class="yh">ThreadPool.QueueUserWorkItem(<span class="net_key">new</span> WaitCallback(d), state);</span>
    }

    <span class="net_key">public</span> <span class="net_key">override</span> <span class="net_key">void</span> Send(SendOrPostCallback d, <span class="net_key">object</span> state)
    {
        <span class="net_comment">// Only the peer channel in WCF uses Send</span>
        d(state);
    }
}

[AttributeUsage(AttributeTargets.Class)]
<span class="net_key">public</span> <span class="net_key">class</span> WorkerThreadPoolBehaviorAttribute : Attribute, IContractBehavior
{
    <span class="net_key">private</span> <span class="net_key">static</span> WorkerThreadPoolSynchronizer synchronizer = <span class="net_key">new</span> WorkerThreadPoolSynchronizer();

    <span class="net_key">void</span> IContractBehavior.AddBindingParameters(
        ContractDescription contractDescription,
        ServiceEndpoint endpoint,
        BindingParameterCollection bindingParameters)
    {
    }

    <span class="net_key">void</span> IContractBehavior.ApplyClientBehavior(
        ContractDescription contractDescription,
        ServiceEndpoint endpoint,
        ClientRuntime clientRuntime)
    {
    }

    <span class="net_key">void</span> IContractBehavior.ApplyDispatchBehavior(
        ContractDescription contractDescription,
        ServiceEndpoint endpoint,
        DispatchRuntime dispatchRuntime)
    {
        <span class="yh">dispatchRuntime.SynchronizationContext = synchronizer;</span>
    }

    <span class="net_key">void</span> IContractBehavior.Validate(
        ContractDescription contractDescription,
        ServiceEndpoint endpoint)
    {
    }
}</span></pre>
<p>The areas highlighted above show the most important bits for this change. There really isn't that much code to this. With this change essentially what is happening is that WCF recognizes that when it's time to execute the service code, there is an ambient SynchronizationContext. That context then pushes the work into the worker thread pool with QueueUserWorkItem. The only other change to make is to the &lt;processModel&gt; node in the machine.config to configure the worker thread pool for a higher min and max.</p>
<pre>&lt;system.web&gt;
  &lt;!--&lt;processModel autoConfig="true"/&gt;--&gt;
  &lt;processModel 
    autoConfig="false" 
    minWorkerThreads="100" 
    maxWorkerThreads="200" 
    /&gt;</pre>
<p>Running the test again, you will see a much better result:</p>
<p><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/41/84/6237.IOThreadPoolBug6.png" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-41-84/6237.IOThreadPoolBug6.png"><img src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/41/84/6237.IOThreadPoolBug6.png" original-url="https://blogs.msdn.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-01-41-84/6237.IOThreadPoolBug6.png" border="0" /></a></p>
<p>All of the test runs finished in under 2.5 seconds.</p>
<p>We cannot make a recommendation on this alone though. There are other questions like how does this affect performance and does it continue to work this way for hours, days, weeks, etc. Our testing showed that for at least 72 hours, this worked without a problem. The performance runs showed some caveats though. These are also pointed out in the KB article. There is a cost for switching from one thread to another. This would be the same with the worker thread pool or a custom thread pool. That overhead can be negligible for large amounts of work. In the case that applies here with 2 seconds of blocking work, the context switch is definitely not a factor. But if you've got small messages and fast work,&nbsp;then it's likely to&nbsp;hurt your performance.</p>
<p>The test project is included with this post.</p>
<p>Originally posted on Dustin Metzgar's blog</p>
<p><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Components.PostAttachments/00/10/16/12/24/IOThreadPoolBug.zip" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-components-postattachments/00-10-16-12-24/IOThreadPoolBug.zip">IOThreadPoolBug.zip</a></p>]]></content:encoded>
    </item>
    <item>
      <title>WCF Extensibility – Message Formatters</title>
      <link>https://docs.microsoft.com/archive/blogs/endpoint/wcf-extensibility-message-formatters</link>
      <pubDate>Tue, 03 May 2011 10:38:00 GMT</pubDate>
      <dc:creator><![CDATA[CarlosFigueira]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/endpoint/2011/05/03/wcf-extensibility-message-formatters/</guid>
      <description><![CDATA[This post is part of a series about WCF extensibility points. For a list of all previous posts and...]]></description>
      <content:encoded><![CDATA[<p><em>This post is part of a series about WCF extensibility points. For a list of all previous posts and planned future ones, go to the </em><a href="https://blogs.msdn.com/b/carlosfigueira/archive/2011/03/14/wcf-extensibility.aspx"><em>index page</em></a><em>.</em></p>
<p>Message formatters are the component which do the translation between CLR operations and the WCF <a href="https://msdn.microsoft.com/en-us/library/system.servicemodel.channels.message.aspx">Message</a> object &ndash; their role is to convert all the operation parameters and return values (possibly via serialization) into a Message on output, and deconstruct the message into parameter and return values on input. Anytime a new format is to be used (be it a new serialization format for currently supported CLR types, or supporting a new CLR type altogether), a message formatter is the interface you&rsquo;d implement to enable this scenario. For example, even though the type System.IO.Stream is not serializable (it&rsquo;s even abstract), WCF allows it to be used as the input or return values for methods (for example, in the <a href="https://blogs.msdn.com/b/carlosfigueira/archive/2008/04/17/wcf-raw-programming-model-web.aspx">WCF REST Raw programming model</a>) and a formatter deals with converting it into messages. Like the message inspectors, there are two versions, one for the server side (<a href="https://msdn.microsoft.com/en-us/library/system.servicemodel.dispatcher.idispatchmessageformatter.aspx">IDispatchMessageFormatter</a>), and one for the client side (<a href="https://msdn.microsoft.com/en-us/library/system.servicemodel.dispatcher.iclientmessageformatter.aspx">IClientMessageFormatter</a>).</p>
<p>Among the extensibility points listed in this series, the message formatters are the first kind to be <em>required</em> in the WCF pipeline &ndash; a service does not need to have any service / endpoint / contract / operation behavior, nor any message / parameter inspector. If the user doesn&rsquo;t add any of those, the client / service will just work (some behaviors are automatically added to the operations when one adds an endpoint, but that&rsquo;s an implementation detail for WCF &ndash; they aren&rsquo;t strictly necessary). Formatters, on the other hand, are required (to bridge the gap between the message world and the operations using CLR types). If we don&rsquo;t add any behaviors to the service / endpoint / contract / operation that sets a formatter, WCF will add one formatter to the operation (usually via the <a href="https://msdn.microsoft.com/en-us/library/system.servicemodel.description.datacontractserializeroperationbehavior.aspx">DataContractSerializerOperationBehavior</a>, which is added by default in all operation contracts).</p>
<h3>Public implementations in WCF</h3>
<p>None. As with most of the runtime extensibility points, there are no public implementations of either the dispatch or the client message formatter. There are a lot of internal implementations, though, such as a formatter which converts operation parameters into a message (both using the DataContractSerializer and the XmlSerializer), formatters which map parameters to the HTTP URI (used in REST operations with UriTemplate), among others.</p>
<h3>Interface declaration</h3>
<div style="margin: 0px; display: inline; float: none; padding: 0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:e68f9267-8f0b-45e3-87cc-e02d35c5450b" class="wlWriterEditableSmartContent">
<div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt;">
<div style="background: #fff; max-height: 400px; overflow: auto;"> <ol style="background: #ffffff; margin: 0; padding: 0 0 0 5px; white-space: nowrap;">
<li><span style="background: #ffffff; color: #000000;"></span><span style="background: #ffffff; color: #0000ff;">public</span><span style="background: #ffffff; color: #000000;"> </span><span style="background: #ffffff; color: #0000ff;">interface</span><span style="background: #ffffff; color: #000000;"> </span><span style="background: #ffffff; color: #2b91af;">IDispatchMessageFormatter</span></li>
<li style="background: #f3f3f3;"><span style="background: #ffffff; color: #000000;">{</span></li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="background: #ffffff; color: #000000;"></span><span style="background: #ffffff; color: #0000ff;">void</span><span style="background: #ffffff; color: #000000;"> DeserializeRequest(</span><span style="background: #ffffff; color: #2b91af;">Message</span><span style="background: #ffffff; color: #000000;"> message, </span><span style="background: #ffffff; color: #0000ff;">object</span><span style="background: #ffffff; color: #000000;">[] parameters);</span></li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;<span style="background: #ffffff; color: #000000;"></span><span style="background: #ffffff; color: #2b91af;">Message</span><span style="background: #ffffff; color: #000000;"> SerializeReply(</span><span style="background: #ffffff; color: #2b91af;">MessageVersion</span><span style="background: #ffffff; color: #000000;"> messageVersion, </span><span style="background: #ffffff; color: #0000ff;">object</span><span style="background: #ffffff; color: #000000;">[] parameters, </span><span style="background: #ffffff; color: #0000ff;">object</span><span style="background: #ffffff; color: #000000;"> result);</span></li>
<li><span style="background: #ffffff; color: #000000;">}</span></li>
<li style="background: #f3f3f3;">&nbsp;</li>
<li><span style="background: #ffffff; color: #000000;"></span><span style="background: #ffffff; color: #0000ff;">public</span><span style="background: #ffffff; color: #000000;"> </span><span style="background: #ffffff; color: #0000ff;">interface</span><span style="background: #ffffff; color: #000000;"> </span><span style="background: #ffffff; color: #2b91af;">IClientMessageFormatter</span></li>
<li style="background: #f3f3f3;"><span style="background: #ffffff; color: #000000;">{</span></li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="background: #ffffff; color: #000000;"></span><span style="background: #ffffff; color: #2b91af;">Message</span><span style="background: #ffffff; color: #000000;"> SerializeRequest(</span><span style="background: #ffffff; color: #2b91af;">MessageVersion</span><span style="background: #ffffff; color: #000000;"> messageVersion, </span><span style="background: #ffffff; color: #0000ff;">object</span><span style="background: #ffffff; color: #000000;">[] parameters);</span></li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;<span style="background: #ffffff; color: #000000;"></span><span style="background: #ffffff; color: #0000ff;">object</span><span style="background: #ffffff; color: #000000;"> DeserializeReply(</span><span style="background: #ffffff; color: #2b91af;">Message</span><span style="background: #ffffff; color: #000000;"> message, </span><span style="background: #ffffff; color: #0000ff;">object</span><span style="background: #ffffff; color: #000000;">[] parameters);</span></li>
<li><span style="background: #ffffff; color: #000000;">}</span></li>
</ol> </div>
</div>
</div>
<p>The formatter interfaces each have two methods, one to convert between input outgoing CLR objects into the <a href="https://msdn.microsoft.com/en-us/library/system.servicemodel.channels.message.aspx">Message</a> object (serialize), and one to take the incoming message object apart and break it down into incoming CLR objects (deserialize).</p>
<p>At the server side, after the the message was decoded, passed through the channel stack and other extensibility points and is about to be dispatched to the service operation, the method <a href="https://msdn.microsoft.com/en-us/library/system.servicemodel.dispatcher.idispatchmessageformatter.deserializerequest.aspx">DeserializeRequest</a> is called to deserialize the message object into an array of parameters to be passed to the operation. The object argument &ldquo;parameters&rdquo; will have been allocated, so the implementation of the formatter only needs to read the message and set the operation parameters in their correct order in the array passed to it. After the operation is invoked (and any parameter inspector has a chance to change the operation outputs), <a href="https://msdn.microsoft.com/en-us/library/system.servicemodel.dispatcher.idispatchmessageformatter.serializereply.aspx">SerializeReply</a> is called, to take the operation return, plus the final value of any out/ref parameters which the operation may have, and package them into a Message object to be sent down the WCF channel stack.</p>
<p>At the client side the mirror happens: when the operation is called (and after the parameter inspector has a chance to change the inputs), <a href="https://msdn.microsoft.com/en-us/library/system.servicemodel.dispatcher.iclientmessageformatter.serializerequest.aspx">SerializeRequest</a> is called to package all the input parameters into the outgoing message object. After the message is sent to the server and the response is decoded by the client&rsquo;s encoder, <a href="https://msdn.microsoft.com/en-us/library/system.servicemodel.dispatcher.iclientmessageformatter.deserializereply.aspx">DeserializeReply</a> is called to open the message object and populate the return value and any possible out/ref parameters.</p>
<p>One small parenthesis about out/ref parameters: all parameters in WCF calls are passed by value &ndash; a copy of the value is sent across the wire. If an operation declares a parameter with a ref modifier (ByRef in Visual Basic), the service will receive a copy (i.e., by value) of that parameter, and then return another copy of the value of the parameter at the end of the method. This second copy will be deserialized at the client into a new instance of that type. So even if you pass an instance of your type by reference to a WCF service, and the service doesn&rsquo;t change it at all, the value at the end of the call at the client side the reference will point to a different object.</p>
<div style="margin: 0px; display: inline; float: none; padding: 0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:e30d41d2-fb53-4602-9d5d-f1a2ee804c32" class="wlWriterEditableSmartContent">
<div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt;">
<div style="background: #fff; max-height: 400px; overflow: auto;"> <ol style="background: #ffffff; margin: 0; padding: 0 0 0 5px; white-space: nowrap;">
<li>[<span style="color: #2b91af;">ServiceContract</span>]</li>
<li style="background: #f3f3f3;"><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">interface</span> <span style="color: #2b91af;">ITest</span></li>
<li>{</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;[<span style="color: #2b91af;">OperationContract</span>]</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">void</span> UpdatePerson(<span style="color: #0000ff;">ref</span> <span style="color: #2b91af;">Person</span> person);</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;[<span style="color: #2b91af;">OperationContract</span>]</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">void</span> InOutRef(<span style="color: #0000ff;">int</span> x, <span style="color: #0000ff;">ref</span> <span style="color: #0000ff;">int</span> y, <span style="color: #0000ff;">out</span> <span style="color: #0000ff;">int</span> z, <span style="color: #0000ff;">out</span> <span style="color: #0000ff;">int</span> w);</li>
<li style="background: #f3f3f3;">}</li>
<li><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span> <span style="color: #2b91af;">Client</span></li>
<li style="background: #f3f3f3;">{</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span> Main()</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;{</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #2b91af;">ChannelFactory</span>&lt;<span style="color: #2b91af;">ITest</span>&gt; factory = <span style="color: #0000ff;">new</span> <span style="color: #2b91af;">ChannelFactory</span>&lt;<span style="color: #2b91af;">ITest</span>&gt;();</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #2b91af;">ITest</span> proxy = factory.CreateChannel();</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #2b91af;">Person</span> p1 = <span style="color: #0000ff;">new</span> <span style="color: #2b91af;">Person</span>();</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #2b91af;">Person</span> p2 = p1;</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #2b91af;">Console</span>.WriteLine(<span style="color: #0000ff;">object</span>.ReferenceEquals(p1, p2)); <span style="color: #008000;">// true</span></li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;proxy.UpdatePerson(<span style="color: #0000ff;">ref</span> p2);</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #2b91af;">Console</span>.WriteLine(<span style="color: #0000ff;">object</span>.ReferenceEquals(p1, p2)); <span style="color: #008000;">// false, even if service doesn't change reference</span></li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;}</li>
<li>}</li>
</ol> </div>
</div>
</div>
<p>Going back to the formatters, the list of &ldquo;input&rdquo; parameters (those in the *Request) operations are either the input (ByVal) parameters or reference (ByRef without &lt;Out&gt;). The list of &ldquo;output&rdquo; parameters are ones marked with ref (ByRef) and out (&lt;Out&gt; ByRef). In the operation InOutRef above, the parameters x and y are considered to be input parameters, so the parameters array in the *Request operations will have two objects). The parameters y, z and w are considered to be output parameters, so the parameters array in the *Reply operations will have three elements.</p>
<h3>How to add message formatters</h3>
<p><strong>At the server side</strong>: the formatter is a property of the operation runtime, so at the server side it&rsquo;s available at the DispatchOperation object. This object is typically accessed via an operation behavior, or by traversing the operation list from an endpoint behavior. both in the ApplyDispatchBehavior method. Another way is if you&rsquo;re creating a REST endpoints (i.e., one with a behavior which inherits from <a href="https://msdn.microsoft.com/en-us/library/system.servicemodel.description.webhttpbehavior.aspx">WebHttpBehavior</a>). WebHttpBehavior exposes two protected methods, <a href="https://msdn.microsoft.com/en-us/library/system.servicemodel.description.webhttpbehavior.getrequestdispatchformatter.aspx">GetRequestDispatchFormatter</a> and <a href="https://msdn.microsoft.com/en-us/library/system.servicemodel.description.webhttpbehavior.getreplydispatchformatter.aspx">GetReplyDispatchFormatter</a>, which can be overridden to to return the formatter to be used by the operation.</p>
<div style="margin: 0px; display: inline; float: none; padding: 0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:4355aa99-a540-440a-8e6d-6d404dd83289" class="wlWriterEditableSmartContent">
<div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt;">
<div style="background: #fff; max-height: 400px; overflow: auto;"> <ol style="background: #ffffff; margin: 0; padding: 0 0 0 5px; white-space: nowrap;">
<li><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span> <span style="color: #2b91af;">MyDispatchMessageFormatter</span> : <span style="color: #2b91af;">IDispatchMessageFormatter</span></li>
<li style="background: #f3f3f3;">{</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #2b91af;">OperationDescription</span> operation;</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">public</span> MyDispatchMessageFormatter(<span style="color: #2b91af;">OperationDescription</span> operation) { ... }</li>
<li>}</li>
<li style="background: #f3f3f3;"><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span> <span style="color: #2b91af;">MyRestBehavior</span> : <span style="color: #2b91af;">WebHttpBehavior</span></li>
<li>{</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">protected</span> <span style="color: #0000ff;">override</span> <span style="color: #2b91af;">IDispatchMessageFormatter</span> GetReplyDispatchFormatter(<span style="color: #2b91af;">OperationDescription</span> operationDescription, <span style="color: #2b91af;">ServiceEndpoint</span> endpoint)</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;{</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">return</span> <span style="color: #0000ff;">new</span> <span style="color: #2b91af;">MyDispatchMessageFormatter</span>(operationDescription);</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;}</li>
<li style="background: #f3f3f3;">}</li>
</ol> </div>
</div>
</div>
<p><strong>At the client side</strong>: Similarly, the operation formatter at the client side is available at the ClientOperation object, typically accessed via an operation or endpoint behavior. And for behaviors inheriting from <a href="https://msdn.microsoft.com/en-us/library/system.servicemodel.description.webhttpbehavior.aspx">WebHttpBehavior</a>, there are two protected methods, <a href="https://msdn.microsoft.com/en-us/library/system.servicemodel.description.webhttpbehavior.getrequestclientformatter.aspx">GetRequestClientFormatter</a> and <a href="https://msdn.microsoft.com/en-us/library/system.servicemodel.description.webhttpbehavior.getreplyclientformatter.aspx">GetReplyClientFormatter</a>, which can be used to return the operation formatter as well.</p>
<div style="margin: 0px; display: inline; float: none; padding: 0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:4ba56ef0-968f-4a87-912a-0f505a6fb3a9" class="wlWriterEditableSmartContent">
<div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt;">
<div style="background: #fff; max-height: 400px; overflow: auto;"> <ol style="background: #ffffff; margin: 0; padding: 0 0 0 5px; white-space: nowrap;">
<li><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span> <span style="color: #2b91af;">MyClientFormatter</span> : <span style="color: #2b91af;">IClientMessageFormatter</span></li>
<li style="background: #f3f3f3;">{</li>
<li>}</li>
<li style="background: #f3f3f3;"><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span> <span style="color: #2b91af;">MyOperationBehavior</span> : <span style="color: #2b91af;">IOperationBehavior</span></li>
<li>{</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> ApplyClientBehavior(<span style="color: #2b91af;">OperationDescription</span> operationDescription, <span style="color: #2b91af;">ClientOperation</span> clientOperation)</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;{</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;clientOperation.Formatter = <span style="color: #0000ff;">new</span> <span style="color: #2b91af;">MyClientFormatter</span>();</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;}</li>
<li style="background: #f3f3f3;">}</li>
</ol> </div>
</div>
</div>
<p>A quick note about wrapping the default WCF formatter. As I mentioned before, the default formatter is usually added by the DataContractSerializerOperationBehavior when it&rsquo;s executed. If we want to use that default formatter, and wrap it in our own formatter, we need to make sure that the DataContractSerializerOperationBehavior is a position in the operation behavior list which is executed <em>before</em> our own operation behavior is. Take this seemingly problem-free example:</p>
<div style="margin: 0px; display: inline; float: none; padding: 0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:224d3c51-c154-404c-b5ed-87b915b5cef6" class="wlWriterEditableSmartContent">
<div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt;">
<div style="background: #fff; max-height: 400px; overflow: auto;"> <ol style="background: #ffffff; margin: 0; padding: 0 0 0 5px; white-space: nowrap;">
<li>[<span style="color: #2b91af;">ServiceContract</span>]</li>
<li style="background: #f3f3f3;"><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">interface</span> <span style="color: #2b91af;">ITest</span></li>
<li>{</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;[<span style="color: #2b91af;">OperationContract</span>]</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;[<span style="color: #2b91af;">MyOperationBehavior</span>]</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">int</span> Add(<span style="color: #0000ff;">int</span> x, <span style="color: #0000ff;">int</span> y);</li>
<li>}</li>
<li style="background: #f3f3f3;"><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span> <span style="color: #2b91af;">Service</span> : <span style="color: #2b91af;">ITest</span></li>
<li>{</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">public</span> <span style="color: #0000ff;">int</span> Add(<span style="color: #0000ff;">int</span> x, <span style="color: #0000ff;">int</span> y) { <span style="color: #0000ff;">return</span> x + y; }</li>
<li>}</li>
<li style="background: #f3f3f3;"><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span> <span style="color: #2b91af;">MyClientFormatter</span> : <span style="color: #2b91af;">IClientMessageFormatter</span></li>
<li>{</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #2b91af;">IClientMessageFormatter</span> inner;</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">public</span> MyClientFormatter(<span style="color: #2b91af;">IClientMessageFormatter</span> inner)</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;{</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">if</span> (inner == <span style="color: #0000ff;">null</span>) <span style="color: #0000ff;">throw</span> <span style="color: #0000ff;">new</span> <span style="color: #2b91af;">ArgumentNullException</span>(<span style="color: #a31515;">"inner"</span>);</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">this</span>.inner = inner;</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;}</li>
<li style="background: #f3f3f3;">&nbsp;</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">public</span> <span style="color: #0000ff;">object</span> DeserializeReply(<span style="color: #2b91af;">Message</span> message, <span style="color: #0000ff;">object</span>[] parameters)</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;{</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">return</span> <span style="color: #0000ff;">this</span>.inner.DeserializeReply(message, parameters);</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;}</li>
<li>&nbsp;</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">public</span> <span style="color: #2b91af;">Message</span> SerializeRequest(<span style="color: #2b91af;">MessageVersion</span> messageVersion, <span style="color: #0000ff;">object</span>[] parameters)</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;{</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">return</span> <span style="color: #0000ff;">this</span>.inner.SerializeRequest(messageVersion, parameters);</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;}</li>
<li style="background: #f3f3f3;">}</li>
<li><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span> <span style="color: #2b91af;">MyOperationBehaviorAttribute</span> : <span style="color: #2b91af;">Attribute</span>, <span style="color: #2b91af;">IOperationBehavior</span></li>
<li style="background: #f3f3f3;">{</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> AddBindingParameters(<span style="color: #2b91af;">OperationDescription</span> operationDescription, <span style="color: #2b91af;">BindingParameterCollection</span> bindingParameters)</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;{</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;}</li>
<li style="background: #f3f3f3;">&nbsp;</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> ApplyClientBehavior(<span style="color: #2b91af;">OperationDescription</span> operationDescription, <span style="color: #2b91af;">ClientOperation</span> clientOperation)</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;{</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;clientOperation.Formatter = <span style="color: #0000ff;">new</span> <span style="color: #2b91af;">MyClientFormatter</span>(clientOperation.Formatter);</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;}</li>
<li>&nbsp;</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> ApplyDispatchBehavior(<span style="color: #2b91af;">OperationDescription</span> operationDescription, <span style="color: #2b91af;">DispatchOperation</span> dispatchOperation)</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;{</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;}</li>
<li>&nbsp;</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> Validate(<span style="color: #2b91af;">OperationDescription</span> operationDescription)</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;{</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;}</li>
<li>}</li>
<li style="background: #f3f3f3;">&nbsp;</li>
<li><span style="color: #0000ff;">class</span> <span style="color: #2b91af;">Program</span></li>
<li style="background: #f3f3f3;">{</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span> Main(<span style="color: #0000ff;">string</span>[] args)</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;{</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">string</span> baseAddress = <span style="color: #a31515;">"https://"</span> + <span style="color: #2b91af;">Environment</span>.MachineName + <span style="color: #a31515;">":8000/Service"</span>;</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #2b91af;">ServiceHost</span> host = <span style="color: #0000ff;">new</span> <span style="color: #2b91af;">ServiceHost</span>(<span style="color: #0000ff;">typeof</span>(<span style="color: #2b91af;">Service</span>), <span style="color: #0000ff;">new</span> <span style="color: #2b91af;">Uri</span>(baseAddress));</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;host.AddServiceEndpoint(<span style="color: #0000ff;">typeof</span>(<span style="color: #2b91af;">ITest</span>), <span style="color: #0000ff;">new</span> <span style="color: #2b91af;">BasicHttpBinding</span>(), <span style="color: #a31515;">""</span>);</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;host.Open();</li>
<li>&nbsp;</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #2b91af;">ChannelFactory</span>&lt;<span style="color: #2b91af;">ITest</span>&gt; factory = <span style="color: #0000ff;">new</span> <span style="color: #2b91af;">ChannelFactory</span>&lt;<span style="color: #2b91af;">ITest</span>&gt;(<span style="color: #0000ff;">new</span> <span style="color: #2b91af;">BasicHttpBinding</span>(), <span style="color: #0000ff;">new</span> <span style="color: #2b91af;">EndpointAddress</span>(baseAddress));</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">foreach</span> (<span style="color: #0000ff;">var</span> opBehavior <span style="color: #0000ff;">in</span> factory.Endpoint.Contract.Operations[0].Behaviors)</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #2b91af;">Console</span>.WriteLine(opBehavior);</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</li>
<li>&nbsp;</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #2b91af;">ITest</span> proxy = factory.CreateChannel();</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #2b91af;">Console</span>.WriteLine(proxy.Add(3, 4));</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;}</li>
<li>}</li>
</ol> </div>
</div>
</div>
<p>The call to CreateChannel in the channel factory will fail &ndash; the constructor of MyClientFormatter will throw. That&rsquo;s because the order of the operation behaviors (printed in the code) is the following:</p>
<blockquote>
<p>System.ServiceModel.Dispatcher.OperationInvokerBehavior      <br /><strong>MyOperationBehaviorAttribute</strong>       <br />System.ServiceModel.OperationBehaviorAttribute       <br />System.ServiceModel.Description.DataContractSerializerOperationBehavior       <br />System.ServiceModel.Description.DataContractSerializerOperationGenerator</p>
</blockquote>
<p>I have no idea why attribute-based operation behaviors are added in this position, but the fact is that this doesn&rsquo;t work. So for this scenario we cannot use attribute-based behaviors, we need to add it &ldquo;manually&rdquo;, as shown below.</p>
<div style="margin: 0px; display: inline; float: none; padding: 0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:6345efb9-a0a1-4e7b-956d-7783ecce5fe1" class="wlWriterEditableSmartContent">
<div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt;">
<div style="background: #fff; max-height: 400px; overflow: auto;"> <ol style="background: #ffffff; margin: 0; padding: 0 0 0 5px; white-space: nowrap;">
<li>[<span style="color: #2b91af;">ServiceContract</span>]</li>
<li style="background: #f3f3f3;"><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">interface</span> <span style="color: #2b91af;">ITest</span></li>
<li>{</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;[<span style="color: #2b91af;">OperationContract</span>]</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #008000;">//[MyOperationBehavior]</span></li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">int</span> Add(<span style="color: #0000ff;">int</span> x, <span style="color: #0000ff;">int</span> y);</li>
<li>}</li>
<li style="background: #f3f3f3;">&nbsp;</li>
<li><span style="color: #0000ff;">class</span> <span style="color: #2b91af;">Program</span></li>
<li style="background: #f3f3f3;">{</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span> Main(<span style="color: #0000ff;">string</span>[] args)</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;{</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">string</span> baseAddress = <span style="color: #a31515;">"https://"</span> + <span style="color: #2b91af;">Environment</span>.MachineName + <span style="color: #a31515;">":8000/Service"</span>;</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #2b91af;">ServiceHost</span> host = <span style="color: #0000ff;">new</span> <span style="color: #2b91af;">ServiceHost</span>(<span style="color: #0000ff;">typeof</span>(<span style="color: #2b91af;">Service</span>), <span style="color: #0000ff;">new</span> <span style="color: #2b91af;">Uri</span>(baseAddress));</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #2b91af;">ServiceEndpoint</span> endpoint = host.AddServiceEndpoint(<span style="color: #0000ff;">typeof</span>(<span style="color: #2b91af;">ITest</span>), <span style="color: #0000ff;">new</span> <span style="color: #2b91af;">BasicHttpBinding</span>(), <span style="color: #a31515;">""</span>);</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;endpoint.Contract.Operations.Find(<span style="color: #a31515;">"Add"</span>).Behaviors.Add(<span style="color: #0000ff;">new</span> <span style="color: #2b91af;">MyOperationBehaviorAttribute</span>());</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;host.Open();</li>
<li style="background: #f3f3f3;">&nbsp;</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #2b91af;">ChannelFactory</span>&lt;<span style="color: #2b91af;">ITest</span>&gt; factory = <span style="color: #0000ff;">new</span> <span style="color: #2b91af;">ChannelFactory</span>&lt;<span style="color: #2b91af;">ITest</span>&gt;(<span style="color: #0000ff;">new</span> <span style="color: #2b91af;">BasicHttpBinding</span>(), <span style="color: #0000ff;">new</span> <span style="color: #2b91af;">EndpointAddress</span>(baseAddress));</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #2b91af;">ITest</span> proxy = factory.CreateChannel();</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #2b91af;">Console</span>.WriteLine(proxy.Add(3, 4));</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;}</li>
<li>}</li>
</ol> </div>
</div>
</div>
<p>After doing that, the code runs and works fine.</p>
<h3>Real world scenario: supporting different data and serialization formats</h3>
<p>The first version of WCF (.NET Framework 3.0) supported essentially SOAP endpoints &ndash; an XML payload following some rules defined in the SOAP specification (you actually could do POX/REST, but it was awfully hard). On the next version (.NET Framework 3.5), the REST programming model was added and that started supporting a whole new set of formats, mostly notably POX (plain-old XML, no SOAP involved), JSON (based on the <a href="https://msdn.microsoft.com/en-us/library/system.runtime.serialization.json.datacontractjsonserializer.aspx">DataContractJsonSerializer</a>) and a special &ldquo;raw&rdquo; mode, where one could declare a single <a href="https://msdn.microsoft.com/en-us/library/system.io.stream.aspx">Stream</a> parameter in an operation for <a href="https://blogs.msdn.com/b/carlosfigueira/archive/2008/04/17/wcf-raw-programming-model-receiving-arbitrary-data.aspx">receiving arbitrary data</a>, or returning a Stream value for <a href="https://blogs.msdn.com/b/carlosfigueira/archive/2008/04/17/wcf-raw-programming-model-web.aspx">returning arbitrary data</a>. The raw mode can be used to support essentially all data and serialization formats, but that comes at a price that the operation itself has to deal with serializing / deserializing the parameters. Also, there are <a href="https://social.msdn.microsoft.com/Forums/en-US/wcf/thread/220d3cec-2ea6-42a9-b9e9-dbd78fb26b83">scenarios</a> (from the forums) where the user needs to expose the same operation in non-REST endpoints (or even non-HTTP, as in the scenario described in the forum post), where the Stream trick won&rsquo;t work. This example will expand on the answer to the forum question and provide formatters which will use a custom serializer (in this case, the JSON.NET one, available from codeplex at <a href="https://json.codeplex.com">https://json.codeplex.com</a>).</p>
<p>And the usual disclaimer: this is a sample for illustrating the topic of this post, this is not production-ready code. I tested it for a few contracts and it worked, but I cannot guarantee that it will work for all scenarios (please let me know if you find a bug or something missing). Also, for simplicity sake it doesn&rsquo;t have a lot of error handling which a production-level code would, and it isn&rsquo;t fully integrated with the WCF REST pipeline (i.e., it doesn&rsquo;t support UriTemplates, it doesn&rsquo;t integrate with the REST help page, doesn&rsquo;t support out/ref parameters,, etc.).</p>
<p>First, some data contracts to set up the scenario. The service will process people and their pets. The data contracts are decorated both with WCF serialization attributes (<a href="https://msdn.microsoft.com/en-us/library/system.runtime.serialization.datacontractattribute.aspx">DataContract</a>, <a href="https://msdn.microsoft.com/en-us/library/system.runtime.serialization.datamemberattribute.aspx">DataMember</a>), and with the JSON.NET serialization attributes (<a href="https://james.newtonking.com/projects/json/help/html/T_Newtonsoft_Json_JsonObjectAttribute.htm">JsonObejct</a>, <a href="https://james.newtonking.com/projects/json/help/html/T_Newtonsoft_Json_JsonPropertyAttribute.htm">JsonProperty</a>, <a href="https://james.newtonking.com/projects/json/help/html/T_Newtonsoft_Json_JsonConverterAttribute.htm">JsonConverter</a>) so that it can be mapped according to both formats.</p>
<div style="margin: 0px; display: inline; float: none; padding: 0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:df72f693-8bec-4d85-885f-9f03825d5186" class="wlWriterEditableSmartContent">
<div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt;">
<div style="background: #fff; overflow: auto;"> <ol style="background: #ffffff; margin: 0; padding: 0 0 0 5px; white-space: nowrap;">
<li>[<span style="color: #2b91af;">DataContract</span>]</li>
<li style="background: #f3f3f3;">[Newtonsoft.Json.<span style="color: #2b91af;">JsonObject</span>(MemberSerialization = Newtonsoft.Json.<span style="color: #2b91af;">MemberSerialization</span>.OptIn)]</li>
<li><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span> <span style="color: #2b91af;">Person</span></li>
<li style="background: #f3f3f3;">{</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;[<span style="color: #2b91af;">DataMember</span>(Order = 1), Newtonsoft.Json.<span style="color: #2b91af;">JsonProperty</span>]</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">public</span> <span style="color: #0000ff;">string</span> FirstName;</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;[<span style="color: #2b91af;">DataMember</span>(Order = 2), Newtonsoft.Json.<span style="color: #2b91af;">JsonProperty</span>]</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">public</span> <span style="color: #0000ff;">string</span> LastName;</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;[<span style="color: #2b91af;">DataMember</span>(Order = 3),</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Newtonsoft.Json.<span style="color: #2b91af;">JsonProperty</span>,</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Newtonsoft.Json.<span style="color: #2b91af;">JsonConverter</span>(<span style="color: #0000ff;">typeof</span>(Newtonsoft.Json.Converters.<span style="color: #2b91af;">IsoDateTimeConverter</span>))]</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">public</span> <span style="color: #2b91af;">DateTime</span> BirthDate;</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;[<span style="color: #2b91af;">DataMember</span>(Order = 4), Newtonsoft.Json.<span style="color: #2b91af;">JsonProperty</span>]</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">public</span> <span style="color: #2b91af;">List</span>&lt;<span style="color: #2b91af;">Pet</span>&gt; Pets;</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;[<span style="color: #2b91af;">DataMember</span>(Order = 5), Newtonsoft.Json.<span style="color: #2b91af;">JsonProperty</span>]</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">public</span> <span style="color: #0000ff;">int</span> Id;</li>
<li>}</li>
<li style="background: #f3f3f3;">&nbsp;</li>
<li>[<span style="color: #2b91af;">DataContract</span>, Newtonsoft.Json.<span style="color: #2b91af;">JsonObject</span>(MemberSerialization = Newtonsoft.Json.<span style="color: #2b91af;">MemberSerialization</span>.OptIn)]</li>
<li style="background: #f3f3f3;"><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span> <span style="color: #2b91af;">Pet</span></li>
<li>{</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;[<span style="color: #2b91af;">DataMember</span>(Order = 1), Newtonsoft.Json.<span style="color: #2b91af;">JsonProperty</span>]</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">public</span> <span style="color: #0000ff;">string</span> Name;</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;[<span style="color: #2b91af;">DataMember</span>(Order = 2), Newtonsoft.Json.<span style="color: #2b91af;">JsonProperty</span>]</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">public</span> <span style="color: #0000ff;">string</span> Color;</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;[<span style="color: #2b91af;">DataMember</span>(Order = 3), Newtonsoft.Json.<span style="color: #2b91af;">JsonProperty</span>]</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">public</span> <span style="color: #0000ff;">string</span> Markings;</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;[<span style="color: #2b91af;">DataMember</span>(Order = 4), Newtonsoft.Json.<span style="color: #2b91af;">JsonProperty</span>]</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">public</span> <span style="color: #0000ff;">int</span> Id;</li>
<li style="background: #f3f3f3;">}</li>
</ol> </div>
</div>
</div>
<p>Next the interface definition, and the service implementation. It&rsquo;s not very interesting, but I added operations using both GET and POST methods, and operations with single and multiple parameters (the goal of this sample is to demonstrate the formatter, not a specific service, so I think I can slack off on the service example a little <img style="border-style: none;" class="wlEmoticon wlEmoticon-smile" alt="Smile" src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/00/95/96/metablogapi/5810.wlEmoticon-smile_4F5C1381.png" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/CommunityServer-Blogs-Components-WeblogFiles/00-00-00-95-96-metablogapi/5810.wlEmoticon_2D00_smile_5F00_4F5C1381.png" />).</p>
<div style="margin: 0px; display: inline; float: none; padding: 0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:1ab7133b-4ce4-442a-8be7-190f7b2883a3" class="wlWriterEditableSmartContent">
<div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt;">
<div style="background: #fff; max-height: 400px; overflow: auto;"> <ol style="background: #ffffff; margin: 0; padding: 0 0 0 5px; white-space: nowrap;">
<li>[<span style="color: #2b91af;">ServiceContract</span>]</li>
<li style="background: #f3f3f3;"><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">interface</span> <span style="color: #2b91af;">ITestService</span></li>
<li>{</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;[<span style="color: #2b91af;">WebGet</span>, <span style="color: #2b91af;">OperationContract</span>]</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #2b91af;">Person</span> GetPerson();</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;[<span style="color: #2b91af;">WebInvoke</span>, <span style="color: #2b91af;">OperationContract</span>]</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #2b91af;">Pet</span> EchoPet(<span style="color: #2b91af;">Pet</span> pet);</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;[<span style="color: #2b91af;">WebInvoke</span>(BodyStyle = <span style="color: #2b91af;">WebMessageBodyStyle</span>.WrappedRequest), <span style="color: #2b91af;">OperationContract</span>]</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">int</span> Add(<span style="color: #0000ff;">int</span> x, <span style="color: #0000ff;">int</span> y);</li>
<li style="background: #f3f3f3;">}</li>
<li>&nbsp;</li>
<li style="background: #f3f3f3;"><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span> <span style="color: #2b91af;">Service</span> : <span style="color: #2b91af;">ITestService</span></li>
<li>{</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">public</span> <span style="color: #2b91af;">Person</span> GetPerson()</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;{</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">return</span> <span style="color: #0000ff;">new</span> <span style="color: #2b91af;">Person</span></li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;FirstName = <span style="color: #a31515;">"First"</span>,</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;LastName = <span style="color: #a31515;">"Last"</span>,</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;BirthDate = <span style="color: #0000ff;">new</span> DateTime(1993, 4, 17, 2, 51, 37, 47, DateTimeKind.Local),</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Id = 0,</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Pets = <span style="color: #0000ff;">new</span> List&lt;<span style="color: #2b91af;">Pet</span>&gt;</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">new</span> <span style="color: #2b91af;">Pet</span> { Name= <span style="color: #a31515;">"Generic Pet 1"</span>, Color = <span style="color: #a31515;">"Beige"</span>, Id = 0, Markings = <span style="color: #a31515;">"Some markings"</span> },</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">new</span> <span style="color: #2b91af;">Pet</span> { Name= <span style="color: #a31515;">"Generic Pet 2"</span>, Color = <span style="color: #a31515;">"Gold"</span>, Id = 0, Markings = <span style="color: #a31515;">"Other markings"</span> },</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;},</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;};</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;}</li>
<li>&nbsp;</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">public</span> <span style="color: #2b91af;">Pet</span> EchoPet(<span style="color: #2b91af;">Pet</span> pet)</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;{</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">return</span> pet;</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;}</li>
<li style="background: #f3f3f3;">&nbsp;</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">public</span> <span style="color: #0000ff;">int</span> Add(<span style="color: #0000ff;">int</span> x, <span style="color: #0000ff;">int</span> y)</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;{</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">return</span> x + y;</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;}</li>
<li>}</li>
</ol> </div>
</div>
</div>
<p>Now the behavior which will set everything together. Since this is a REST endpoint, instead of writing a new endpoint, I&rsquo;ll simply inherit from <a href="https://msdn.microsoft.com/en-us/library/system.servicemodel.description.webhttpbehavior.aspx">WebHttpBehavior</a>, and override the protected methods to return the client and server formatters. Notice that in some cases when there are no parameters or for GET requests (where the parameters are in the query string, not in the request body), so in those cases I&rsquo;ll simply reuse the default formatter from the base class. Another logic which is implemented at the behavior is validation &ndash; we know what our formatter doesn&rsquo;t support, so we&rsquo;ll simply throw while the service (or the client channel) is being opened, giving the user a clear message explaining why the behavior failed the validation (some of the validation code is not in the snippet below, but it can be found in the source code for this post).</p>
<div style="margin: 0px; display: inline; float: none; padding: 0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:888eb198-1581-425c-854e-848882962da6" class="wlWriterEditableSmartContent">
<div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt;">
<div style="background: #fff; max-height: 400px; overflow: auto;"> <ol style="background: #ffffff; margin: 0; padding: 0 0 0 5px; white-space: nowrap;">
<li><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span> <span style="color: #2b91af;">NewtonsoftJsonBehavior</span> : <span style="color: #2b91af;">WebHttpBehavior</span></li>
<li style="background: #f3f3f3;">{</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">public</span> <span style="color: #0000ff;">override</span> <span style="color: #0000ff;">void</span> Validate(<span style="color: #2b91af;">ServiceEndpoint</span> endpoint)</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;{</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">base</span>.Validate(endpoint);</li>
<li style="background: #f3f3f3;">&nbsp;</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #2b91af;">BindingElementCollection</span> elements = endpoint.Binding.CreateBindingElements();</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #2b91af;">WebMessageEncodingBindingElement</span> webEncoder = elements.Find&lt;<span style="color: #2b91af;">WebMessageEncodingBindingElement</span>&gt;();</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">if</span> (webEncoder == <span style="color: #0000ff;">null</span>)</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">throw</span> <span style="color: #0000ff;">new</span> <span style="color: #2b91af;">InvalidOperationException</span>(<span style="color: #a31515;">"This behavior must be used in an endpoint with the WebHttpBinding (or a custom binding with the WebMessageEncodingBindingElement)."</span>);</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</li>
<li>&nbsp;</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">foreach</span> (<span style="color: #2b91af;">OperationDescription</span> operation <span style="color: #0000ff;">in</span> endpoint.Contract.Operations)</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">this</span>.ValidateOperation(operation);</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;}</li>
<li>&nbsp;</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">protected</span> <span style="color: #0000ff;">override</span> <span style="color: #2b91af;">IDispatchMessageFormatter</span> GetRequestDispatchFormatter(<span style="color: #2b91af;">OperationDescription</span> operationDescription, <span style="color: #2b91af;">ServiceEndpoint</span> endpoint)</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;{</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">if</span> (<span style="color: #0000ff;">this</span>.IsGetOperation(operationDescription))</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #008000;">// no change for GET operations</span></li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">return</span> <span style="color: #0000ff;">base</span>.GetRequestDispatchFormatter(operationDescription, endpoint);</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</li>
<li>&nbsp;</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">if</span> (operationDescription.Messages[0].Body.Parts.Count == 0)</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #008000;">// nothing in the body, still use the default</span></li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">return</span> <span style="color: #0000ff;">base</span>.GetRequestDispatchFormatter(operationDescription, endpoint);</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</li>
<li>&nbsp;</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">return</span> <span style="color: #0000ff;">new</span> <span style="color: #2b91af;">NewtonsoftJsonDispatchFormatter</span>(operationDescription, <span style="color: #0000ff;">true</span>);</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;}</li>
<li style="background: #f3f3f3;">&nbsp;</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">protected</span> <span style="color: #0000ff;">override</span> <span style="color: #2b91af;">IDispatchMessageFormatter</span> GetReplyDispatchFormatter(<span style="color: #2b91af;">OperationDescription</span> operationDescription, <span style="color: #2b91af;">ServiceEndpoint</span> endpoint)</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;{</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">if</span> (operationDescription.Messages.Count == 1 || operationDescription.Messages[1].Body.ReturnValue.Type == <span style="color: #0000ff;">typeof</span>(<span style="color: #0000ff;">void</span>))</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">return</span> <span style="color: #0000ff;">base</span>.GetReplyDispatchFormatter(operationDescription, endpoint);</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">else</span></li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">return</span> <span style="color: #0000ff;">new</span> <span style="color: #2b91af;">NewtonsoftJsonDispatchFormatter</span>(operationDescription, <span style="color: #0000ff;">false</span>);</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;}</li>
<li style="background: #f3f3f3;">&nbsp;</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">protected</span> <span style="color: #0000ff;">override</span> <span style="color: #2b91af;">IClientMessageFormatter</span> GetRequestClientFormatter(<span style="color: #2b91af;">OperationDescription</span> operationDescription, <span style="color: #2b91af;">ServiceEndpoint</span> endpoint)</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;{</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">if</span> (operationDescription.Behaviors.Find&lt;<span style="color: #2b91af;">WebGetAttribute</span>&gt;() != <span style="color: #0000ff;">null</span>)</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #008000;">// no change for GET operations</span></li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">return</span> <span style="color: #0000ff;">base</span>.GetRequestClientFormatter(operationDescription, endpoint);</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">else</span></li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #2b91af;">WebInvokeAttribute</span> wia = operationDescription.Behaviors.Find&lt;<span style="color: #2b91af;">WebInvokeAttribute</span>&gt;();</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">if</span> (wia != <span style="color: #0000ff;">null</span>)</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">if</span> (wia.Method == <span style="color: #a31515;">"HEAD"</span>)</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #008000;">// essentially a GET operation</span></li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">return</span> <span style="color: #0000ff;">base</span>.GetRequestClientFormatter(operationDescription, endpoint);</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</li>
<li style="background: #f3f3f3;">&nbsp;</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">if</span> (operationDescription.Messages[0].Body.Parts.Count == 0)</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #008000;">// nothing in the body, still use the default</span></li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">return</span> <span style="color: #0000ff;">base</span>.GetRequestClientFormatter(operationDescription, endpoint);</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</li>
<li style="background: #f3f3f3;">&nbsp;</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">return</span> <span style="color: #0000ff;">new</span> <span style="color: #2b91af;">NewtonsoftJsonClientFormatter</span>(operationDescription, endpoint);</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;}</li>
<li>&nbsp;</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">protected</span> <span style="color: #0000ff;">override</span> <span style="color: #2b91af;">IClientMessageFormatter</span> GetReplyClientFormatter(<span style="color: #2b91af;">OperationDescription</span> operationDescription, <span style="color: #2b91af;">ServiceEndpoint</span> endpoint)</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;{</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">if</span> (operationDescription.Messages.Count == 1 || operationDescription.Messages[1].Body.ReturnValue.Type == <span style="color: #0000ff;">typeof</span>(<span style="color: #0000ff;">void</span>))</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">return</span> <span style="color: #0000ff;">base</span>.GetReplyClientFormatter(operationDescription, endpoint);</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">else</span></li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">return</span> <span style="color: #0000ff;">new</span> <span style="color: #2b91af;">NewtonsoftJsonClientFormatter</span>(operationDescription, endpoint);</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;}</li>
<li>}</li>
</ol> </div>
</div>
</div>
<p>On to the formatters. I&rsquo;ll start with the dispatch formatter. As the new formatter was only added for POST operations with at least one parameter, we can safely assume that the messages will not be empty, and the request will be stored in the message body. But the message body is always represented in XML, so in order to support a custom format, we need to either define a mapping between that format and XML (which is what was done internally for supporting JSON in 3.5) &ndash; and implement XML readers and writers to support this mapping &ndash; or reuse one of the existing formats for which we already have a mapping for. And as in the raw programming model, the raw format (described in the <a href="https://blogs.msdn.com/b/carlosfigueira/archive/2011/04/19/wcf-extensibility-message-inspectors.aspx">post about message inspectors</a>) is a great candidate for it &ndash; we can get the raw bytes from the request (and write raw bytes in the response) and parse them using our custom format. We must ensure, however, that the incoming message actually was read with the raw encoder (by default messages with JSON content type are read with the JSON encoder, we need a custom content type mapper to ensure that they are read as raw messages).</p>
<div style="margin: 0px; display: inline; float: none; padding: 0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:c2c6fb1f-ae0b-421e-927d-0bd6b3e61f93" class="wlWriterEditableSmartContent">
<div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt;">
<div style="background: #fff; max-height: 400px; overflow: auto;"> <ol style="background: #ffffff; margin: 0; padding: 0 0 0 5px; white-space: nowrap;">
<li><span style="color: #0000ff;">class</span> <span style="color: #2b91af;">NewtonsoftJsonDispatchFormatter</span> : <span style="color: #2b91af;">IDispatchMessageFormatter</span></li>
<li style="background: #f3f3f3;">{</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #2b91af;">OperationDescription</span> operation;</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #2b91af;">Dictionary</span>&lt;<span style="color: #0000ff;">string</span>, <span style="color: #0000ff;">int</span>&gt; parameterNames;</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">public</span> NewtonsoftJsonDispatchFormatter(<span style="color: #2b91af;">OperationDescription</span> operation, <span style="color: #0000ff;">bool</span> isRequest)</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;{</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">this</span>.operation = operation;</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">if</span> (isRequest)</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">int</span> operationParameterCount = operation.Messages[0].Body.Parts.Count;</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">if</span> (operationParameterCount &gt; 1)</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">this</span>.parameterNames = <span style="color: #0000ff;">new</span> <span style="color: #2b91af;">Dictionary</span>&lt;<span style="color: #0000ff;">string</span>, <span style="color: #0000ff;">int</span>&gt;();</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">for</span> (<span style="color: #0000ff;">int</span> i = 0; i &lt; operationParameterCount; i++)</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">this</span>.parameterNames.Add(operation.Messages[0].Body.Parts[i].Name, i);</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;}</li>
<li>&nbsp;</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> DeserializeRequest(<span style="color: #2b91af;">Message</span> message, <span style="color: #0000ff;">object</span>[] parameters)</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;{</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">object</span> bodyFormatProperty;</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">if</span> (!message.Properties.TryGetValue(<span style="color: #2b91af;">WebBodyFormatMessageProperty</span>.Name, <span style="color: #0000ff;">out</span> bodyFormatProperty) ||</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(bodyFormatProperty <span style="color: #0000ff;">as</span> <span style="color: #2b91af;">WebBodyFormatMessageProperty</span>).Format != <span style="color: #2b91af;">WebContentFormat</span>.Raw)</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">throw</span> <span style="color: #0000ff;">new</span> <span style="color: #2b91af;">InvalidOperationException</span>(<span style="color: #a31515;">"Incoming messages must have a body format of Raw. Is a ContentTypeMapper set on the WebHttpBinding?"</span>);</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</li>
<li style="background: #f3f3f3;">&nbsp;</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #2b91af;">XmlDictionaryReader</span> bodyReader = message.GetReaderAtBodyContents();</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bodyReader.ReadStartElement(<span style="color: #a31515;">"Binary"</span>);</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">byte</span>[] rawBody = bodyReader.ReadContentAsBase64();</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #2b91af;">MemoryStream</span> ms = <span style="color: #0000ff;">new</span> <span style="color: #2b91af;">MemoryStream</span>(rawBody);</li>
<li>&nbsp;</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #2b91af;">StreamReader</span> sr = <span style="color: #0000ff;">new</span> <span style="color: #2b91af;">StreamReader</span>(ms);</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Newtonsoft.Json.<span style="color: #2b91af;">JsonSerializer</span> serializer = <span style="color: #0000ff;">new</span> Newtonsoft.Json.<span style="color: #2b91af;">JsonSerializer</span>();</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">if</span> (parameters.Length == 1)</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #008000;">// single parameter, assuming bare</span></li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;parameters[0] = serializer.Deserialize(sr, operation.Messages[0].Body.Parts[0].Type);</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">else</span></li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #008000;">// multiple parameter, needs to be wrapped</span></li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Newtonsoft.Json.<span style="color: #2b91af;">JsonReader</span> reader = <span style="color: #0000ff;">new</span> Newtonsoft.Json.<span style="color: #2b91af;">JsonTextReader</span>(sr);</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;reader.Read();</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">if</span> (reader.TokenType != Newtonsoft.Json.<span style="color: #2b91af;">JsonToken</span>.StartObject)</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">throw</span> <span style="color: #0000ff;">new</span> <span style="color: #2b91af;">InvalidOperationException</span>(<span style="color: #a31515;">"Input needs to be wrapped in an object"</span>);</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</li>
<li style="background: #f3f3f3;">&nbsp;</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;reader.Read();</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">while</span> (reader.TokenType == Newtonsoft.Json.<span style="color: #2b91af;">JsonToken</span>.PropertyName)</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">string</span> parameterName = reader.Value <span style="color: #0000ff;">as</span> <span style="color: #0000ff;">string</span>;</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;reader.Read();</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">if</span> (<span style="color: #0000ff;">this</span>.parameterNames.ContainsKey(parameterName))</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">int</span> parameterIndex = <span style="color: #0000ff;">this</span>.parameterNames[parameterName];</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;parameters[parameterIndex] = serializer.Deserialize(reader, <span style="color: #0000ff;">this</span>.operation.Messages[0].Body.Parts[parameterIndex].Type);</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">else</span></li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;reader.Skip();</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</li>
<li>&nbsp;</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;reader.Read();</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</li>
<li style="background: #f3f3f3;">&nbsp;</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;reader.Close();</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</li>
<li>&nbsp;</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sr.Close();</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ms.Close();</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;}</li>
<li>&nbsp;</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">public</span> <span style="color: #2b91af;">Message</span> SerializeReply(<span style="color: #2b91af;">MessageVersion</span> messageVersion, <span style="color: #0000ff;">object</span>[] parameters, <span style="color: #0000ff;">object</span> result)</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;{</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">byte</span>[] body;</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Newtonsoft.Json.<span style="color: #2b91af;">JsonSerializer</span> serializer = <span style="color: #0000ff;">new</span> Newtonsoft.Json.<span style="color: #2b91af;">JsonSerializer</span>();</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">using</span> (<span style="color: #2b91af;">MemoryStream</span> ms = <span style="color: #0000ff;">new</span> <span style="color: #2b91af;">MemoryStream</span>())</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">using</span> (<span style="color: #2b91af;">StreamWriter</span> sw = <span style="color: #0000ff;">new</span> <span style="color: #2b91af;">StreamWriter</span>(ms, <span style="color: #2b91af;">Encoding</span>.UTF8))</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">using</span> (Newtonsoft.Json.<span style="color: #2b91af;">JsonWriter</span> writer = <span style="color: #0000ff;">new</span> Newtonsoft.Json.<span style="color: #2b91af;">JsonTextWriter</span>(sw))</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;writer.Formatting = Newtonsoft.Json.<span style="color: #2b91af;">Formatting</span>.Indented;</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;serializer.Serialize(writer, result);</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sw.Flush();</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;body = ms.ToArray();</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</li>
<li>&nbsp;</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #2b91af;">Message</span> replyMessage = <span style="color: #2b91af;">Message</span>.CreateMessage(messageVersion, operation.Messages[1].Action, <span style="color: #0000ff;">new</span> <span style="color: #2b91af;">RawBodyWriter</span>(body));</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;replyMessage.Properties.Add(<span style="color: #2b91af;">WebBodyFormatMessageProperty</span>.Name, <span style="color: #0000ff;">new</span> <span style="color: #2b91af;">WebBodyFormatMessageProperty</span>(<span style="color: #2b91af;">WebContentFormat</span>.Raw));</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #2b91af;">HttpResponseMessageProperty</span> respProp = <span style="color: #0000ff;">new</span> <span style="color: #2b91af;">HttpResponseMessageProperty</span>();</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;respProp.Headers[<span style="color: #2b91af;">HttpResponseHeader</span>.ContentType] = <span style="color: #a31515;">"application/json"</span>;</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;replyMessage.Properties.Add(<span style="color: #2b91af;">HttpResponseMessageProperty</span>.Name, respProp);</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">return</span> replyMessage;</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;}</li>
<li>}</li>
</ol> </div>
</div>
</div>
<p>When the formatter is serializing the reply of the message , it will create a new message using a &ldquo;raw&rdquo; body writer, which is a simple implementation of the <a href="https://msdn.microsoft.com/en-us/library/system.servicemodel.channels.bodywriter.aspx">BodyWriter</a> class. It will then set the body format property to ensure that the raw encoder will be used when converting that message into bytes on the wire.</p>
<div style="margin: 0px; display: inline; float: none; padding: 0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:ebb335bf-af5a-4f43-8803-f4d0b73c179f" class="wlWriterEditableSmartContent">
<div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt;">
<div style="background: #fff; max-height: 400px; overflow: auto;"> <ol style="background: #ffffff; margin: 0; padding: 0 0 0 5px; white-space: nowrap;">
<li><span style="color: #0000ff;">class</span> <span style="color: #2b91af;">RawBodyWriter</span> : <span style="color: #2b91af;">BodyWriter</span></li>
<li style="background: #f3f3f3;">{</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">byte</span>[] content;</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">public</span> RawBodyWriter(<span style="color: #0000ff;">byte</span>[] content)</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;: <span style="color: #0000ff;">base</span>(<span style="color: #0000ff;">true</span>)</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;{</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">this</span>.content = content;</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;}</li>
<li>&nbsp;</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">protected</span> <span style="color: #0000ff;">override</span> <span style="color: #0000ff;">void</span> OnWriteBodyContents(<span style="color: #2b91af;">XmlDictionaryWriter</span> writer)</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;{</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;writer.WriteStartElement(<span style="color: #a31515;">"Binary"</span>);</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;writer.WriteBase64(content, 0, content.Length);</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;writer.WriteEndElement();</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;}</li>
<li style="background: #f3f3f3;">}</li>
</ol> </div>
</div>
</div>
<p>The client formatter is similar to the service one. On reply, since it doesn&rsquo;t support multiple return values (no out/ref support implemented), it simply gets the bytes from the message and feeds them to the JSON.NET deserializer. When serializing the request, the formatter will either simply serialize a single parameter if it&rsquo;s the only one, or it will wrap it in a JSON object with properties named by the operation parameter name. The last thing it does on serialization is to again set the body format property (to ensure that the raw encoder will be used for that message) and also set the &ldquo;To&rdquo; header of the message (which is required for REST messages, since it uses manual addressing on the transport).</p>
<div style="margin: 0px; display: inline; float: none; padding: 0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:ec20db00-3bf9-48ee-9fee-f6cda2daf24f" class="wlWriterEditableSmartContent">
<div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt;">
<div style="background: #fff; max-height: 400px; overflow: auto;"> <ol style="background: #ffffff; margin: 0; padding: 0 0 0 5px; white-space: nowrap;">
<li><span style="color: #0000ff;">class</span> <span style="color: #2b91af;">NewtonsoftJsonClientFormatter</span> : <span style="color: #2b91af;">IClientMessageFormatter</span></li>
<li style="background: #f3f3f3;">{</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #2b91af;">OperationDescription</span> operation;</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #2b91af;">Uri</span> operationUri;</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">public</span> NewtonsoftJsonClientFormatter(<span style="color: #2b91af;">OperationDescription</span> operation, <span style="color: #2b91af;">ServiceEndpoint</span> endpoint)</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;{</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">this</span>.operation = operation;</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">string</span> endpointAddress = endpoint.Address.Uri.ToString();</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">if</span> (!endpointAddress.EndsWith(<span style="color: #a31515;">"/"</span>))</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;endpointAddress = endpointAddress + <span style="color: #a31515;">"/"</span>;</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</li>
<li>&nbsp;</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">this</span>.operationUri = <span style="color: #0000ff;">new</span> <span style="color: #2b91af;">Uri</span>(endpointAddress + operation.Name);</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;}</li>
<li style="background: #f3f3f3;">&nbsp;</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">public</span> <span style="color: #0000ff;">object</span> DeserializeReply(<span style="color: #2b91af;">Message</span> message, <span style="color: #0000ff;">object</span>[] parameters)</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;{</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">object</span> bodyFormatProperty;</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">if</span> (!message.Properties.TryGetValue(<span style="color: #2b91af;">WebBodyFormatMessageProperty</span>.Name, <span style="color: #0000ff;">out</span> bodyFormatProperty) ||</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(bodyFormatProperty <span style="color: #0000ff;">as</span> <span style="color: #2b91af;">WebBodyFormatMessageProperty</span>).Format != <span style="color: #2b91af;">WebContentFormat</span>.Raw)</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">throw</span> <span style="color: #0000ff;">new</span> <span style="color: #2b91af;">InvalidOperationException</span>(<span style="color: #a31515;">"Incoming messages must have a body format of Raw. Is a ContentTypeMapper set on the WebHttpBinding?"</span>);</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</li>
<li>&nbsp;</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #2b91af;">XmlDictionaryReader</span> bodyReader = message.GetReaderAtBodyContents();</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Newtonsoft.Json.<span style="color: #2b91af;">JsonSerializer</span> serializer = <span style="color: #0000ff;">new</span> Newtonsoft.Json.<span style="color: #2b91af;">JsonSerializer</span>();</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bodyReader.ReadStartElement(<span style="color: #a31515;">"Binary"</span>);</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">byte</span>[] body = bodyReader.ReadContentAsBase64();</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">using</span> (<span style="color: #2b91af;">MemoryStream</span> ms = <span style="color: #0000ff;">new</span> <span style="color: #2b91af;">MemoryStream</span>(body))</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">using</span> (<span style="color: #2b91af;">StreamReader</span> sr = <span style="color: #0000ff;">new</span> <span style="color: #2b91af;">StreamReader</span>(ms))</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #2b91af;">Type</span> returnType = <span style="color: #0000ff;">this</span>.operation.Messages[1].Body.ReturnValue.Type;</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">return</span> serializer.Deserialize(sr, returnType);</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;}</li>
<li>&nbsp;</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">public</span> <span style="color: #2b91af;">Message</span> SerializeRequest(<span style="color: #2b91af;">MessageVersion</span> messageVersion, <span style="color: #0000ff;">object</span>[] parameters)</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;{</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">byte</span>[] body;</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Newtonsoft.Json.<span style="color: #2b91af;">JsonSerializer</span> serializer = <span style="color: #0000ff;">new</span> Newtonsoft.Json.<span style="color: #2b91af;">JsonSerializer</span>();</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">using</span> (<span style="color: #2b91af;">MemoryStream</span> ms = <span style="color: #0000ff;">new</span> <span style="color: #2b91af;">MemoryStream</span>())</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">using</span> (<span style="color: #2b91af;">StreamWriter</span> sw = <span style="color: #0000ff;">new</span> <span style="color: #2b91af;">StreamWriter</span>(ms, <span style="color: #2b91af;">Encoding</span>.UTF8))</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">using</span> (Newtonsoft.Json.<span style="color: #2b91af;">JsonWriter</span> writer = <span style="color: #0000ff;">new</span> Newtonsoft.Json.<span style="color: #2b91af;">JsonTextWriter</span>(sw))</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;writer.Formatting = Newtonsoft.Json.<span style="color: #2b91af;">Formatting</span>.Indented;</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">if</span> (parameters.Length == 1)</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #008000;">// Single parameter, assuming bare</span></li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;serializer.Serialize(sw, parameters[0]);</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">else</span></li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;writer.WriteStartObject();</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">for</span> (<span style="color: #0000ff;">int</span> i = 0; i &lt; <span style="color: #0000ff;">this</span>.operation.Messages[0].Body.Parts.Count; i++)</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;writer.WritePropertyName(<span style="color: #0000ff;">this</span>.operation.Messages[0].Body.Parts[i].Name);</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;serializer.Serialize(writer, parameters[0]);</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</li>
<li style="background: #f3f3f3;">&nbsp;</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;writer.WriteEndObject();</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</li>
<li>&nbsp;</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;writer.Flush();</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sw.Flush();</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;body = ms.ToArray();</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</li>
<li style="background: #f3f3f3;">&nbsp;</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #2b91af;">Message</span> requestMessage = <span style="color: #2b91af;">Message</span>.CreateMessage(messageVersion, operation.Messages[0].Action, <span style="color: #0000ff;">new</span> <span style="color: #2b91af;">RawBodyWriter</span>(body));</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;requestMessage.Headers.To = operationUri;</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;requestMessage.Properties.Add(<span style="color: #2b91af;">WebBodyFormatMessageProperty</span>.Name, <span style="color: #0000ff;">new</span> <span style="color: #2b91af;">WebBodyFormatMessageProperty</span>(<span style="color: #2b91af;">WebContentFormat</span>.Raw));</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #2b91af;">HttpRequestMessageProperty</span> reqProp = <span style="color: #0000ff;">new</span> <span style="color: #2b91af;">HttpRequestMessageProperty</span>();</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;reqProp.Headers[<span style="color: #2b91af;">HttpRequestHeader</span>.ContentType] = <span style="color: #a31515;">"application/json"</span>;</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;requestMessage.Properties.Add(<span style="color: #2b91af;">HttpRequestMessageProperty</span>.Name, reqProp);</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">return</span> requestMessage;</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;}</li>
<li>}</li>
</ol> </div>
</div>
</div>
<p>Now for testing the formatters. The first thing we need is the content type mapper, to ensure that all incoming requests will be read by the raw encoder. Its implementation is quite simple.</p>
<div style="margin: 0px; display: inline; float: none; padding: 0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:63c5bb2b-0f20-42e0-9d50-7a255936e564" class="wlWriterEditableSmartContent">
<div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt;">
<div style="background: #fff; max-height: 400px; overflow: auto;"> <ol style="background: #ffffff; margin: 0; padding: 0 0 0 5px; white-space: nowrap;">
<li><span style="color: #0000ff;">class</span> <span style="color: #2b91af;">MyRawMapper</span> : <span style="color: #2b91af;">WebContentTypeMapper</span></li>
<li style="background: #f3f3f3;">{</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">public</span> <span style="color: #0000ff;">override</span> <span style="color: #2b91af;">WebContentFormat</span> GetMessageFormatForContentType(<span style="color: #0000ff;">string</span> contentType)</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;{</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">return</span> <span style="color: #2b91af;">WebContentFormat</span>.Raw;</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;}</li>
<li>}</li>
</ol> </div>
</div>
</div>
<p>Now for the test itself. I&rsquo;m using a helper method (SendRequest) to send arbitrary HTTP requests to the server, but I&rsquo;ll skip it here (it&rsquo;s just setting up HttpWebRequest and displaying the results from HttpWebResponse). The test code sets up a service and adds two endpoints, one &ldquo;normal&rdquo; (SOAP) and one JSON.NET enabled</p>
<div style="margin: 0px; display: inline; float: none; padding: 0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:7d13e0b3-3ba3-4030-b113-056e68c3cc68" class="wlWriterEditableSmartContent">
<div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt;">
<div style="background: #fff; max-height: 300px; overflow: auto;"> <ol style="background: #ffffff; margin: 0; padding: 0 0 0 5px; white-space: nowrap;">
<li><span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span> Main(<span style="color: #0000ff;">string</span>[] args)</li>
<li style="background: #f3f3f3;">{</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">string</span> baseAddress = <span style="color: #a31515;">"https://"</span> + <span style="color: #2b91af;">Environment</span>.MachineName + <span style="color: #a31515;">":8000/Service"</span>;</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #2b91af;">ServiceHost</span> host = <span style="color: #0000ff;">new</span> <span style="color: #2b91af;">ServiceHost</span>(<span style="color: #0000ff;">typeof</span>(<span style="color: #2b91af;">Service</span>), <span style="color: #0000ff;">new</span> <span style="color: #2b91af;">Uri</span>(baseAddress));</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;host.AddServiceEndpoint(<span style="color: #0000ff;">typeof</span>(<span style="color: #2b91af;">ITestService</span>), <span style="color: #0000ff;">new</span> <span style="color: #2b91af;">BasicHttpBinding</span>(), <span style="color: #a31515;">"soap"</span>);</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #2b91af;">WebHttpBinding</span> webBinding = <span style="color: #0000ff;">new</span> <span style="color: #2b91af;">WebHttpBinding</span>();</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;webBinding.ContentTypeMapper = <span style="color: #0000ff;">new</span> <span style="color: #2b91af;">MyRawMapper</span>();</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;host.AddServiceEndpoint(<span style="color: #0000ff;">typeof</span>(<span style="color: #2b91af;">ITestService</span>), webBinding, <span style="color: #a31515;">"json"</span>).Behaviors.Add(<span style="color: #0000ff;">new</span> <span style="color: #2b91af;">NewtonsoftJsonBehavior</span>());</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #2b91af;">Console</span>.WriteLine(<span style="color: #a31515;">"Opening the host"</span>);</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;host.Open();</li>
<li>&nbsp;</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #2b91af;">ChannelFactory</span>&lt;<span style="color: #2b91af;">ITestService</span>&gt; factory = <span style="color: #0000ff;">new</span> <span style="color: #2b91af;">ChannelFactory</span>&lt;<span style="color: #2b91af;">ITestService</span>&gt;(<span style="color: #0000ff;">new</span> <span style="color: #2b91af;">BasicHttpBinding</span>(), <span style="color: #0000ff;">new</span> <span style="color: #2b91af;">EndpointAddress</span>(baseAddress + <span style="color: #a31515;">"/soap"</span>));</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #2b91af;">ITestService</span> proxy = factory.CreateChannel();</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #2b91af;">Console</span>.WriteLine(proxy.GetPerson());</li>
<li>&nbsp;</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;SendRequest(baseAddress + <span style="color: #a31515;">"/json/GetPerson"</span>, <span style="color: #a31515;">"GET"</span>, <span style="color: #0000ff;">null</span>, <span style="color: #0000ff;">null</span>);</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;SendRequest(baseAddress + <span style="color: #a31515;">"/json/EchoPet"</span>, <span style="color: #a31515;">"POST"</span>, <span style="color: #a31515;">"application/json"</span>, <span style="color: #a31515;">"{\"Name\":\"Fido\",\"Color\":\"Black and white\",\"Markings\":\"None\",\"Id\":1}"</span>);</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;SendRequest(baseAddress + <span style="color: #a31515;">"/json/Add"</span>, <span style="color: #a31515;">"POST"</span>, <span style="color: #a31515;">"application/json"</span>, <span style="color: #a31515;">"{\"x\":111,\"z\":null,\"w\":[1,2],\"v\":{\"a\":1},\"y\":222}"</span>);</li>
<li>&nbsp;</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #2b91af;">Console</span>.WriteLine(<span style="color: #a31515;">"Now using the client formatter"</span>);</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #2b91af;">ChannelFactory</span>&lt;<span style="color: #2b91af;">ITestService</span>&gt; newFactory = <span style="color: #0000ff;">new</span> <span style="color: #2b91af;">ChannelFactory</span>&lt;<span style="color: #2b91af;">ITestService</span>&gt;(webBinding, <span style="color: #0000ff;">new</span> <span style="color: #2b91af;">EndpointAddress</span>(baseAddress + <span style="color: #a31515;">"/json"</span>));</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;newFactory.Endpoint.Behaviors.Add(<span style="color: #0000ff;">new</span> <span style="color: #2b91af;">NewtonsoftJsonBehavior</span>());</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #2b91af;">ITestService</span> newProxy = newFactory.CreateChannel();</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #2b91af;">Console</span>.WriteLine(newProxy.Add(444, 555));</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #2b91af;">Console</span>.WriteLine(newProxy.EchoPet(<span style="color: #0000ff;">new</span> <span style="color: #2b91af;">Pet</span> { Color = <span style="color: #a31515;">"gold"</span>, Id = 2, Markings = <span style="color: #a31515;">"Collie"</span>, Name = <span style="color: #a31515;">"Lassie"</span> }));</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #2b91af;">Console</span>.WriteLine(newProxy.GetPerson());</li>
<li>&nbsp;</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #2b91af;">Console</span>.WriteLine(<span style="color: #a31515;">"Press ENTER to close"</span>);</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #2b91af;">Console</span>.ReadLine();</li>
<li style="background: #f3f3f3;">&nbsp;&nbsp;&nbsp;&nbsp;host.Close();</li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #2b91af;">Console</span>.WriteLine(<span style="color: #a31515;">"Host closed"</span>);</li>
<li style="background: #f3f3f3;">}</li>
</ol> </div>
</div>
</div>
<p>And that&rsquo;s it for the formatter example!</p>
<h3>New WCF Web API</h3>
<p>One note that I want to leave here is that it&rsquo;s not too easy to integrate any new formats in the existing WCF REST pipeline. In the example above I had to deal with formatters, Message objects, take a dependency on a content type mapper, just to be able to use this different serializer. And even after all of this, it&rsquo;s not completely integrated with the pipeline &ndash; UriTemplates aren&rsquo;t supported, for example. In an upcoming release, however, the WCF team is working hard to make WCF really a great platform for hosting HTTP services, and that includes a new platform (built on top of the current one) for HTTP services specifically. In the new WCF Web API (you can preview the bits on the <a href="https://wcf.codeplex.com/wikipage?title=WCF HTTP">Codeplex site</a>) implementing such a scenario is a lot simpler, and other features (such as UriTemplate) will just continue to work after the new formatter is plugged in.&nbsp;<a href="https://twitter.com/gblock">Glenn Block</a> has posted a formatter that does exactly that on the thread&nbsp;<a href="https://wcf.codeplex.com/discussions/255873">https://wcf.codeplex.com/discussions/255873</a>, you decide which one is easier :)</p>
<h3>Coming up</h3>
<p>Operation selectors.</p>
<p>[<a href="https://code.msdn.microsoft.com/Supporting-different-data-b0351c9a">Code for this post</a>]</p>
<p>[<a href="https://blogs.msdn.com/b/carlosfigueira/archive/2011/03/14/wcf-extensibility.aspx">Back to the index</a>]</p>
<p>Carlos Figueira    <br /><a href="https://blogs.msdn.com/carlosfigueira">https://blogs.msdn.com/carlosfigueira</a>    <br />Twitter: @carlos_figueira <a href="https://twitter.com/carlos_figueira">https://twitter.com/carlos_figueira</a></p>]]></content:encoded>
    </item>
    <item>
      <title>How to detect if the .NET Framework Platform Update 1 is installed</title>
      <link>https://docs.microsoft.com/archive/blogs/endpoint/how-to-detect-if-the-net-framework-platform-update-1-is-installed</link>
      <pubDate>Fri, 29 Apr 2011 13:42:00 GMT</pubDate>
      <dc:creator><![CDATA[Ron Jacobs]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/endpoint/2011/04/29/how-to-detect-if-the-net-framework-platform-update-1-is-installed/</guid>
      <description><![CDATA[Humans
Go to Control Panel / Programs / Programs and Features / View Installed Updates
Use the...]]></description>
      <content:encoded><![CDATA[<h1>Humans</h1>
<p>Go to Control Panel / Programs / Programs and Features / View Installed Updates</p>
<p>Use the Search Box to search the updates for </p>
<ul>
<li><a href="https://support.microsoft.com/kb/2495593">KB2495593</a> </li>
<li><a href="https://support.microsoft.com/kb/2478063">KB2478063</a> </li>
</ul>
<p><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/03/60/metablogapi/8424.SNAGHTML16a27c2_59E3C500.png" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-03-60-metablogapi/8424.SNAGHTML16a27c2_5F00_59E3C500.png"><img height="277" width="504" src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/03/60/metablogapi/3247.SNAGHTML16a27c2_thumb_6749D806.png" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-03-60-metablogapi/3247.SNAGHTML16a27c2_5F00_thumb_5F00_6749D806.png" alt="SNAGHTML16a27c2" border="0" title="SNAGHTML16a27c2" style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border-width: 0px;" /></a></p>
<p>Or if you prefer, search the list your self</p>
<h3>Microsoft .NET Framework 4 Platform Update 1 - Runtime Update </h3>
<ol>
<li>Look for .NET Framework 4 Extended </li>
<li>Look for <strong>Update for Microsoft .NET Framework 4 Extended (KB2478063)</strong> </li>
<li>Click on the <a href="https://support.microsoft.com/kb/2478063">Help Link</a> to see what you get with this update </li>
</ol>
<p><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/03/60/metablogapi/3250.SNAGHTML1612b2b_068C7EDA.png" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-03-60-metablogapi/3250.SNAGHTML1612b2b_5F00_068C7EDA.png"><img height="277" width="504" src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/03/60/metablogapi/6371.SNAGHTML1612b2b_thumb_2C822F30.png" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-03-60-metablogapi/6371.SNAGHTML1612b2b_5F00_thumb_5F00_2C822F30.png" alt="SNAGHTML1612b2b" border="0" title="SNAGHTML1612b2b" style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border-width: 0px;" /></a></p>
<h3>Microsoft .NET Framework 4 Platform Update 1 &ndash; Design-time Package for Visual Studio 2010 SP1 (<a href="https://support.microsoft.com/kb/2495593">KB2495593</a>)</h3>
<ol>
<li>Look for Microsoft Visual Studio 2010 (&lt;Your Edition) &ndash; (&lt;Language&gt;) (<a href="https://support.microsoft.com/kb/2495593">KB2495593</a>) </li>
<li>Click on the <a href="https://support.microsoft.com/kb/2495593">Help Link</a> to see what this update contains </li>
</ol>
<h1>Programs</h1>
<p>Look in the registry for the following keys</p>
<p><strong>HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\.NET Framework\v4.0.30319\SKUs\</strong></p>
<blockquote>
<p><strong>.NETFramework, Version=v4.0.1</strong></p>
<p><strong>.NETFramework, Version-v4.0.1, Profile=Client</strong></p>
</blockquote>
<p>Ron Jacobs <br /><a href="https://blogs.msdn.com/rjacobs">https://blogs.msdn.com/rjacobs</a> <br />Twitter: @ronljacobs <a href="https://twitter.com/ronljacobs">https://twitter.com/ronljacobs</a></p>]]></content:encoded>
    </item>
    <item>
      <title>WCF Extensibility – IParameterInspector</title>
      <link>https://docs.microsoft.com/archive/blogs/endpoint/wcf-extensibility-iparameterinspector</link>
      <pubDate>Thu, 28 Apr 2011 08:05:53 GMT</pubDate>
      <dc:creator><![CDATA[CarlosFigueira]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/endpoint/2011/04/28/wcf-extensibility-iparameterinspector/</guid>
      <description><![CDATA[This post is part of a series about WCF extensibility points. For a list of all previous posts and...]]></description>
      <content:encoded><![CDATA[<p><em>This post is part of a series about WCF extensibility points. For a list of all previous posts and planned future ones, go to the </em><a href="https://blogs.msdn.com/b/carlosfigueira/archive/2011/03/14/wcf-extensibility.aspx"><em>index page</em></a><em>.</em></p>  <p>The message inspectors, described in the previous post of this series, allows you complete control over the message going through the WCF stack. They’re very powerful, but you have to know how to deal with the <a href="https://msdn.microsoft.com/en-us/library/system.servicemodel.channels.message.aspx">Message</a> object, which is not the most desirable way of programming. If the service model in WCF hides all the messaging framework by allowing us to define our services in terms of strongly-typed operations (i.e., using nice primitive and user defined types), there should be a way of intercepting requests / responses after all the processing to extract those parameters from incoming messages (or before they’re packaged in outgoing messages) is done. The <a href="https://msdn.microsoft.com/en-us/library/system.servicemodel.dispatcher.iparameterinspector.aspx">IParameterInspector</a> is exactly that – before and after each call, the inspector gets a chance to inspect the operation inputs, outputs and return value, in the same types as defined by the operation contract, no conversion needed (the only thing needed is a cast, since the parameters are passed as objects).</p>  <h3>Public implementations in WCF</h3>  <p>None. There aren’t also any internal implementations of this interface in WCF, this is really a user (as opposed to system) extensibility point.</p>  <h3>Interface declaration</h3>  <div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:94d1623a-9262-40d7-9110-4a5ab1eb2d1e" class="wlWriterEditableSmartContent"> <div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"> <div style="background: #fff; max-height: 400px; overflow: auto"> <ol style="background: #ffffff; margin: 0; padding: 0 0 0 5px; white-space: nowrap"> <li><span style="color:#0000ff">public</span> <span style="color:#0000ff">interface</span> <span style="color:#2b91af">IParameterInspector</span></li> <li style="background: #f3f3f3">{</li> <li>    <span style="color:#0000ff">void</span> AfterCall(<span style="color:#0000ff">string</span> operationName, <span style="color:#0000ff">object</span>[] outputs, <span style="color:#0000ff">object</span> returnValue, <span style="color:#0000ff">object</span> correlationState);</li> <li style="background: #f3f3f3">    <span style="color:#0000ff">object</span> BeforeCall(<span style="color:#0000ff">string</span> operationName, <span style="color:#0000ff">object</span>[] inputs);</li> <li>}</li> </ol> </div> </div> </div>  <p>Unlike the message inspector, the parameter inspector interface is the same for both client and server side. At the server side, right before the call is dispatched to the user code, <a href="https://msdn.microsoft.com/en-us/library/system.servicemodel.dispatcher.iparameterinspector.beforecall.aspx">BeforeCall</a> is invoked, where the inspector can look at the inputs to the operation. When the operation returns, <a href="https://msdn.microsoft.com/en-us/library/system.servicemodel.dispatcher.iparameterinspector.aftercall.aspx">AfterCall</a> is invoked to let the inspector look at the return value of the operation, and any out/ref parameters which the operation may have set. At the client side, right after the call on the proxy is made, and before it’s handed down to the WCF stack, <a href="https://msdn.microsoft.com/en-us/library/system.servicemodel.dispatcher.iparameterinspector.beforecall.aspx">BeforeCall</a> is invoked to let the inspector look at the outgoing parameters before they’re packaged into a message object. Finally, when the call returns from the server and the result is about to be returned to the client (either by returning the synchronous invocation or in an asynchronous callback), <a href="https://msdn.microsoft.com/en-us/library/system.servicemodel.dispatcher.iparameterinspector.aftercall.aspx">AfterCall</a> is called at the client side.</p>  <p>Like in the message inspector case, if the operation contract being called by the client is marked with IsOneWay=true, the call to AfterCall on a client inspector does not happen (it does happen at the server, though). And unlike the message inspector case, if an exception is thrown during the processing of the call (e.g., the server returned a fault), AfterCall will also not be called on the client – it makes sense, since there is no guarantees that the return value or any out/ref parameters have valid values at that point.</p>  <h3>How to add parameter inspectors</h3>  <p><strong>At the server side</strong>: the list of parameter inspectors is available at the <a href="https://msdn.microsoft.com/en-us/library/system.servicemodel.dispatcher.dispatchoperation.aspx">DispatchOperation</a> object – each operation has its own list of parameter inspectors. It is typically accessed via an operation behavior in a call to <a href="https://msdn.microsoft.com/en-us/library/system.servicemodel.description.ioperationbehavior.applydispatchbehavior.aspx">ApplyDispatchBehavior</a>:</p>  <div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:4fce33ed-e3d7-42cd-86a6-7b8a1d0627ff" class="wlWriterEditableSmartContent"> <div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"> <div style="background: #fff; max-height: 400px; overflow: auto"> <ol style="background: #ffffff; margin: 0; padding: 0 0 0 5px; white-space: nowrap"> <li><span style="color:#0000ff">public</span> <span style="color:#0000ff">class</span> <span style="color:#2b91af">MyOperationBehavior</span> : <span style="color:#2b91af">Attribute</span></li> <li style="background: #f3f3f3">{</li> <li>    <span style="color:#0000ff">public</span> <span style="color:#0000ff">void</span> ApplyDispatchBehavior(<span style="color:#2b91af">OperationDescription</span> operationDescription, <span style="color:#2b91af">DispatchOperation</span> dispatchOperation)</li> <li style="background: #f3f3f3">    {</li> <li>        dispatchOperation.ParameterInspectors.Add(<span style="color:#0000ff">new</span> <span style="color:#2b91af">MyParameterInspector</span>());</li> <li style="background: #f3f3f3">    }</li> <li>}</li> </ol> </div> </div> </div>  <p><strong>At the client side</strong>: the list of client parameter inspectors is available at the <a href="https://msdn.microsoft.com/en-us/library/system.servicemodel.dispatcher.clientoperation.aspx">ClientOperation</a> object, and typically accessed via an operation behavior’s <a href="https://msdn.microsoft.com/en-us/library/system.servicemodel.description.ioperationbehavior.applyclientbehavior.aspx">ApplyClientBehavior</a> method.. </p>  <div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:f1e3d4a1-ebde-4f62-bf95-813551496420" class="wlWriterEditableSmartContent"> <div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"> <div style="background: #fff; max-height: 300px; overflow: auto"> <ol style="background: #ffffff; margin: 0; padding: 0 0 0 5px; white-space: nowrap"> <li><span style="color:#0000ff">public</span> <span style="color:#0000ff">class</span> <span style="color:#2b91af">MyOperationBehavior</span> : <span style="color:#2b91af">IOperationBehavior</span></li> <li style="background: #f3f3f3">{</li> <li>    <span style="color:#0000ff">public</span> <span style="color:#0000ff">void</span> ApplyClientBehavior(<span style="color:#2b91af">OperationDescription</span> operationDescription, <span style="color:#2b91af">ClientOperation</span> clientOperation)</li> <li style="background: #f3f3f3">    {</li> <li>        clientOperation.ParameterInspectors.Add(<span style="color:#0000ff">new</span> <span style="color:#2b91af">MyParameterInspector</span>());</li> <li style="background: #f3f3f3">    }</li> <li>}</li> </ol> </div> </div> </div>  <p>&#160;</p>  <h3>Real world scenario: simple profiler</h3>  <p>One usage which I’ve seen a couple of times for the parameter inspector is to create a simple profiler / statistics page for WCF services. The parameter inspector interface methods are called really close to the actual method invocation (as can be seen in the example of the <a href="https://blogs.msdn.com/b/carlosfigueira/archive/2011/04/18/wcf-extensibility-runtime.aspx">WCF Runtime post</a>), both on the client and on the server, so they can fairly accurately record starting and ending times of arbitrary method calls. The example below is implemented as an endpoint behavior which adds the statistic collection logic for all the operations in the endpoint. In this example, we need to call a method on a class to show the statistics, but a more realistic scenario would save the profiling information in a file or database for further analysis.</p>  <p>And here comes the usual disclaimer: this is a sample for illustrating the topic of this post, this is not production-ready code. I tested it for a few contracts and it worked, but I cannot guarantee that it will work for all scenarios (please let me know if you find a bug or something missing). Also, for simplicity sake it doesn’t have a lot of error handling which a production-level code would. Also, as I mentioned before, the statistics are all stored in memory, a better profiler would save the profiling information in a file or database, and possibly even expose it as a service itself.</p>  <p>As with all of the runtime examples, it starts with a behavior. I’ll use an endpoint behavior here, but a contract (or even service) behavior would do.</p>  <div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:03bd0a52-c4ea-420a-b58e-ad33c671bc36" class="wlWriterEditableSmartContent"> <div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"> <div style="background: #fff; max-height: 400px; overflow: auto"> <ol style="background: #ffffff; margin: 0; padding: 0 0 0 5px; white-space: nowrap"> <li><span style="color:#0000ff">public</span> <span style="color:#0000ff">class</span> <span style="color:#2b91af">OperationProfilerEndpointBehavior</span> : <span style="color:#2b91af">IEndpointBehavior</span></li> <li style="background: #f3f3f3">{</li> <li>    <span style="color:#0000ff">private</span> <span style="color:#2b91af">OperationProfilerManager</span> manager;</li> <li style="background: #f3f3f3">    <span style="color:#0000ff">public</span> OperationProfilerEndpointBehavior(<span style="color:#2b91af">OperationProfilerManager</span> manager)</li> <li>    {</li> <li style="background: #f3f3f3">        <span style="color:#0000ff">this</span>.manager = manager;</li> <li>    }</li> <li style="background: #f3f3f3">&nbsp;</li> <li>    <span style="color:#0000ff">public</span> <span style="color:#0000ff">void</span> AddBindingParameters(<span style="color:#2b91af">ServiceEndpoint</span> endpoint, <span style="color:#2b91af">BindingParameterCollection</span> bindingParameters)</li> <li style="background: #f3f3f3">    {</li> <li>    }</li> <li style="background: #f3f3f3">&nbsp;</li> <li>    <span style="color:#0000ff">public</span> <span style="color:#0000ff">void</span> ApplyClientBehavior(<span style="color:#2b91af">ServiceEndpoint</span> endpoint, <span style="color:#2b91af">ClientRuntime</span> clientRuntime)</li> <li style="background: #f3f3f3">    {</li> <li>        <span style="color:#0000ff">foreach</span> (<span style="color:#2b91af">ClientOperation</span> operation <span style="color:#0000ff">in</span> clientRuntime.Operations)</li> <li style="background: #f3f3f3">        {</li> <li>            operation.ParameterInspectors.Add(<span style="color:#0000ff">new</span> <span style="color:#2b91af">OperationProfilerParameterInspector</span>(<span style="color:#0000ff">this</span>.manager, operation.IsOneWay));</li> <li style="background: #f3f3f3">        }</li> <li>    }</li> <li style="background: #f3f3f3">&nbsp;</li> <li>    <span style="color:#0000ff">public</span> <span style="color:#0000ff">void</span> ApplyDispatchBehavior(<span style="color:#2b91af">ServiceEndpoint</span> endpoint, <span style="color:#2b91af">EndpointDispatcher</span> endpointDispatcher)</li> <li style="background: #f3f3f3">    {</li> <li>        <span style="color:#0000ff">foreach</span> (<span style="color:#2b91af">DispatchOperation</span> operation <span style="color:#0000ff">in</span> endpointDispatcher.DispatchRuntime.Operations)</li> <li style="background: #f3f3f3">        {</li> <li>            operation.ParameterInspectors.Add(<span style="color:#0000ff">new</span> <span style="color:#2b91af">OperationProfilerParameterInspector</span>(<span style="color:#0000ff">this</span>.manager, operation.IsOneWay));</li> <li style="background: #f3f3f3">        }</li> <li>    }</li> <li style="background: #f3f3f3">&nbsp;</li> <li>    <span style="color:#0000ff">public</span> <span style="color:#0000ff">void</span> Validate(<span style="color:#2b91af">ServiceEndpoint</span> endpoint)</li> <li style="background: #f3f3f3">    {</li> <li>    }</li> <li style="background: #f3f3f3">}</li> </ol> </div> </div> </div>  <p>The behavior takes a “manager” as a parameter. The manager is the entity which will, in this example, store the call information for all the operations in the contract. It can then be invoked to display a summary of the statistics for the calls (I’ll omit most of the formatting methods here, you can find them in the code for this post, linked below).</p>  <div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:765ea4ce-1db2-44ee-8109-77debda3f62b" class="wlWriterEditableSmartContent"> <div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"> <div style="background: #fff; max-height: 400px; overflow: auto"> <ol style="background: #ffffff; margin: 0; padding: 0 0 0 5px; white-space: nowrap"> <li><span style="color:#0000ff">public</span> <span style="color:#0000ff">class</span> <span style="color:#2b91af">OperationProfilerManager</span></li> <li style="background: #f3f3f3">{</li> <li>    <span style="color:#2b91af">Dictionary</span>&lt;<span style="color:#0000ff">string</span>, <span style="color:#2b91af">List</span>&lt;<span style="color:#0000ff">double</span>&gt;&gt; callsPerOperation;</li> <li style="background: #f3f3f3">    <span style="color:#0000ff">public</span> OperationProfilerManager()</li> <li>    {</li> <li style="background: #f3f3f3">        <span style="color:#0000ff">this</span>.callsPerOperation = <span style="color:#0000ff">new</span> <span style="color:#2b91af">Dictionary</span>&lt;<span style="color:#0000ff">string</span>, <span style="color:#2b91af">List</span>&lt;<span style="color:#0000ff">double</span>&gt;&gt;();</li> <li>    }</li> <li style="background: #f3f3f3">&nbsp;</li> <li>    <span style="color:#0000ff">public</span> <span style="color:#0000ff">void</span> AddOneWayCall(<span style="color:#0000ff">string</span> operationName)</li> <li style="background: #f3f3f3">    {</li> <li>        <span style="color:#2b91af">List</span>&lt;<span style="color:#0000ff">double</span>&gt; callTimes;</li> <li style="background: #f3f3f3">        <span style="color:#0000ff">if</span> (<span style="color:#0000ff">this</span>.callsPerOperation.ContainsKey(operationName))</li> <li>        {</li> <li style="background: #f3f3f3">            callTimes = <span style="color:#0000ff">this</span>.callsPerOperation[operationName];</li> <li>        }</li> <li style="background: #f3f3f3">        <span style="color:#0000ff">else</span></li> <li>        {</li> <li style="background: #f3f3f3">            callTimes = <span style="color:#0000ff">new</span> <span style="color:#2b91af">List</span>&lt;<span style="color:#0000ff">double</span>&gt;();</li> <li>            <span style="color:#0000ff">this</span>.callsPerOperation.Add(operationName, callTimes);</li> <li style="background: #f3f3f3">        }</li> <li>&nbsp;</li> <li style="background: #f3f3f3">        callTimes.Add(-1);</li> <li>    }</li> <li style="background: #f3f3f3">&nbsp;</li> <li>    <span style="color:#0000ff">public</span> <span style="color:#0000ff">void</span> AddCall(<span style="color:#0000ff">string</span> operationName, <span style="color:#0000ff">double</span> duration)</li> <li style="background: #f3f3f3">    {</li> <li>        <span style="color:#2b91af">List</span>&lt;<span style="color:#0000ff">double</span>&gt; callTimes;</li> <li style="background: #f3f3f3">        <span style="color:#0000ff">if</span> (<span style="color:#0000ff">this</span>.callsPerOperation.ContainsKey(operationName))</li> <li>        {</li> <li style="background: #f3f3f3">            callTimes = <span style="color:#0000ff">this</span>.callsPerOperation[operationName];</li> <li>        }</li> <li style="background: #f3f3f3">        <span style="color:#0000ff">else</span></li> <li>        {</li> <li style="background: #f3f3f3">            callTimes = <span style="color:#0000ff">new</span> <span style="color:#2b91af">List</span>&lt;<span style="color:#0000ff">double</span>&gt;();</li> <li>            <span style="color:#0000ff">this</span>.callsPerOperation.Add(operationName, callTimes);</li> <li style="background: #f3f3f3">        }</li> <li>&nbsp;</li> <li style="background: #f3f3f3">        callTimes.Add(duration);</li> <li>    }</li> <li style="background: #f3f3f3">}</li> </ol> </div> </div> </div>  <p>Notice that there are two methods to add information about an operation call. For “normal” operations, we provide the duration of the call, but for one-way operations, this information cannot be retrieved (esp. at the client side, since the control will return to the caller almost as if the call was made asynchronously). Next we’ll get to the inspector itself. It’s implementation is actually fairly simple, as it registers the time in which BeforeCall was invoked, and returns it as the correlation state to be passed to the AfterCall method (unless the operation is one-way, in which case we simply register such a call in the manager). There, we again register the time and take the difference between the two times to calculate the duration of the method, then store it in the manager.</p>  <p>And that’s it – this nothing else needed. Now for testing I’ll define a service with some operations which take a certain amount of time (simulated using a random generator), and some “fast” ones (again I’ll skip the definition of the data contracts used in the service, as they’re not relevant to the topic, but they can be found in the code for this post, linked below).</p>  <div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:52f47f42-1a4b-438b-a45e-73b6296f62ab" class="wlWriterEditableSmartContent"> <div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"> <div style="background: #fff; max-height: 400px; overflow: auto"> <ol style="background: #ffffff; margin: 0; padding: 0 0 0 5px; white-space: nowrap"> <li>[<span style="color:#2b91af">ServiceContract</span>]</li> <li style="background: #f3f3f3"><span style="color:#0000ff">public</span> <span style="color:#0000ff">interface</span> <span style="color:#2b91af">ITest</span></li> <li>{</li> <li style="background: #f3f3f3">    [<span style="color:#2b91af">OperationContract</span>]</li> <li>    <span style="color:#0000ff">int</span> Add(<span style="color:#0000ff">int</span> x, <span style="color:#0000ff">int</span> y);</li> <li style="background: #f3f3f3">    [<span style="color:#2b91af">OperationContract</span>]</li> <li>    <span style="color:#0000ff">int</span> ProcessOrder(<span style="color:#2b91af">Order</span> order);</li> <li style="background: #f3f3f3">    [<span style="color:#2b91af">OperationContract</span>(IsOneWay = <span style="color:#0000ff">true</span>)]</li> <li>    <span style="color:#0000ff">void</span> ProcessOneWay(<span style="color:#2b91af">Order</span> order);</li> <li style="background: #f3f3f3">}</li> <li>&nbsp;</li> <li style="background: #f3f3f3"><span style="color:#0000ff">public</span> <span style="color:#0000ff">class</span> <span style="color:#2b91af">Service</span> : <span style="color:#2b91af">ITest</span></li> <li>{</li> <li style="background: #f3f3f3">    <span style="color:#0000ff">static</span> <span style="color:#2b91af">Random</span> rndGen = <span style="color:#0000ff">new</span> <span style="color:#2b91af">Random</span>(1);</li> <li>    <span style="color:#0000ff">public</span> <span style="color:#0000ff">int</span> Add(<span style="color:#0000ff">int</span> x, <span style="color:#0000ff">int</span> y)</li> <li style="background: #f3f3f3">    {</li> <li>        <span style="color:#0000ff">return</span> x + y;</li> <li style="background: #f3f3f3">    }</li> <li>&nbsp;</li> <li style="background: #f3f3f3">    <span style="color:#0000ff">public</span> <span style="color:#0000ff">int</span> ProcessOrder(<span style="color:#2b91af">Order</span> order)</li> <li>    {</li> <li style="background: #f3f3f3">        <span style="color:#2b91af">Thread</span>.Sleep(rndGen.Next(1, 100));</li> <li>        <span style="color:#0000ff">return</span> order.Id;</li> <li style="background: #f3f3f3">    }</li> <li>&nbsp;</li> <li style="background: #f3f3f3">    <span style="color:#0000ff">public</span> <span style="color:#0000ff">void</span> ProcessOneWay(<span style="color:#2b91af">Order</span> order)</li> <li>    {</li> <li style="background: #f3f3f3">        <span style="color:#2b91af">Thread</span>.Sleep(rndGen.Next(1, 100));</li> <li>    }</li> <li style="background: #f3f3f3">}</li> </ol> </div> </div> </div>  <p>And finally, hosting the program to see the simple profiler in action:</p>  <div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:3b46a651-8766-42ec-bf42-10ac84b4ba2e" class="wlWriterEditableSmartContent"> <div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"> <div style="background: #fff; max-height: 400px; overflow: auto"> <ol style="background: #ffffff; margin: 0; padding: 0 0 0 5px; white-space: nowrap"> <li><span style="color:#0000ff">static</span> <span style="color:#0000ff">void</span> Main(<span style="color:#0000ff">string</span>[] args)</li> <li style="background: #f3f3f3">{</li> <li>    <span style="color:#0000ff">string</span> baseAddress = <span style="color:#a31515">&quot;https://&quot;</span> + <span style="color:#2b91af">Environment</span>.MachineName + <span style="color:#a31515">&quot;:8000/Service&quot;</span>;</li> <li style="background: #f3f3f3">    <span style="color:#2b91af">ServiceHost</span> host = <span style="color:#0000ff">new</span> <span style="color:#2b91af">ServiceHost</span>(<span style="color:#0000ff">typeof</span>(<span style="color:#2b91af">Service</span>), <span style="color:#0000ff">new</span> <span style="color:#2b91af">Uri</span>(baseAddress));</li> <li>    <span style="color:#2b91af">ServiceEndpoint</span> endpoint = host.AddServiceEndpoint(<span style="color:#0000ff">typeof</span>(<span style="color:#2b91af">ITest</span>), <span style="color:#0000ff">new</span> <span style="color:#2b91af">BasicHttpBinding</span>(), <span style="color:#a31515">&quot;&quot;</span>);</li> <li style="background: #f3f3f3">    <span style="color:#2b91af">OperationProfilerManager</span> serverProfilerManager = <span style="color:#0000ff">new</span> <span style="color:#2b91af">OperationProfilerManager</span>();</li> <li>    endpoint.Behaviors.Add(<span style="color:#0000ff">new</span> <span style="color:#2b91af">OperationProfilerEndpointBehavior</span>(serverProfilerManager));</li> <li style="background: #f3f3f3">    <span style="color:#2b91af">Console</span>.WriteLine(<span style="color:#a31515">&quot;Opening the host&quot;</span>);</li> <li>    host.Open();</li> <li style="background: #f3f3f3">&nbsp;</li> <li>    <span style="color:#2b91af">ChannelFactory</span>&lt;<span style="color:#2b91af">ITest</span>&gt; factory = <span style="color:#0000ff">new</span> <span style="color:#2b91af">ChannelFactory</span>&lt;<span style="color:#2b91af">ITest</span>&gt;(<span style="color:#0000ff">new</span> <span style="color:#2b91af">BasicHttpBinding</span>(), <span style="color:#0000ff">new</span> <span style="color:#2b91af">EndpointAddress</span>(baseAddress));</li> <li style="background: #f3f3f3">    <span style="color:#2b91af">OperationProfilerManager</span> clientProfilerManager = <span style="color:#0000ff">new</span> <span style="color:#2b91af">OperationProfilerManager</span>();</li> <li>    factory.Endpoint.Behaviors.Add(<span style="color:#0000ff">new</span> <span style="color:#2b91af">OperationProfilerEndpointBehavior</span>(clientProfilerManager));</li> <li style="background: #f3f3f3">    <span style="color:#2b91af">ITest</span> proxy = factory.CreateChannel();</li> <li>&nbsp;</li> <li style="background: #f3f3f3">    <span style="color:#2b91af">Order</span> oneOrder = <span style="color:#0000ff">new</span> <span style="color:#2b91af">Order</span></li> <li>    {</li> <li style="background: #f3f3f3">        Id = 1,</li> <li>        Client = <span style="color:#0000ff">new</span> <span style="color:#2b91af">Person</span></li> <li style="background: #f3f3f3">        {</li> <li>            Name = <span style="color:#a31515">&quot;John Doe&quot;</span>,</li> <li style="background: #f3f3f3">            Address = <span style="color:#a31515">&quot;111 223th Ave&quot;</span>,</li> <li>        },</li> <li style="background: #f3f3f3">        Items = <span style="color:#0000ff">new</span> <span style="color:#2b91af">List</span>&lt;<span style="color:#2b91af">OrderItem</span>&gt;</li> <li>        {</li> <li style="background: #f3f3f3">            <span style="color:#0000ff">new</span> <span style="color:#2b91af">OrderItem</span> { Name = <span style="color:#a31515">&quot;bread&quot;</span>, Unit = <span style="color:#a31515">&quot;un&quot;</span>, UnitPrice = 0.56, Amount = 3 },</li> <li>            <span style="color:#0000ff">new</span> <span style="color:#2b91af">OrderItem</span> { Name = <span style="color:#a31515">&quot;milk&quot;</span>, Unit = <span style="color:#a31515">&quot;gal&quot;</span>, UnitPrice = 2.79, Amount = 1 },</li> <li style="background: #f3f3f3">            <span style="color:#0000ff">new</span> <span style="color:#2b91af">OrderItem</span> { Name = <span style="color:#a31515">&quot;eggs&quot;</span>, Unit = <span style="color:#a31515">&quot;doz&quot;</span>, UnitPrice = 2.23, Amount = 1 },</li> <li>        }</li> <li style="background: #f3f3f3">    };</li> <li>&nbsp;</li> <li style="background: #f3f3f3">    <span style="color:#2b91af">Console</span>.WriteLine(<span style="color:#a31515">&quot;Calling some operations...&quot;</span>);</li> <li>&nbsp;</li> <li style="background: #f3f3f3">    <span style="color:#0000ff">for</span> (<span style="color:#0000ff">int</span> i = 0; i &lt; 200; i++)</li> <li>    {</li> <li style="background: #f3f3f3">        proxy.Add(i, i * i);</li> <li>        <span style="color:#0000ff">if</span> ((i % 3) == 0)</li> <li style="background: #f3f3f3">        {</li> <li>            proxy.ProcessOneWay(oneOrder);</li> <li style="background: #f3f3f3">        }</li> <li>        <span style="color:#0000ff">else</span></li> <li style="background: #f3f3f3">        {</li> <li>            proxy.ProcessOrder(oneOrder);</li> <li style="background: #f3f3f3">        }</li> <li>    }</li> <li style="background: #f3f3f3">&nbsp;</li> <li>    <span style="color:#2b91af">Console</span>.WriteLine(<span style="color:#a31515">&quot;Now printing statistics (server)&quot;</span>);</li> <li style="background: #f3f3f3">    serverProfilerManager.PrintSummary();</li> <li>&nbsp;</li> <li style="background: #f3f3f3">    <span style="color:#2b91af">Console</span>.WriteLine();</li> <li>&nbsp;</li> <li style="background: #f3f3f3">    <span style="color:#2b91af">Console</span>.WriteLine(<span style="color:#a31515">&quot;Now printing statistics (client)&quot;</span>);</li> <li>    clientProfilerManager.PrintSummary();</li> <li style="background: #f3f3f3">&nbsp;</li> <li>    <span style="color:#2b91af">Console</span>.WriteLine(<span style="color:#a31515">&quot;Press ENTER to close&quot;</span>);</li> <li style="background: #f3f3f3">    <span style="color:#2b91af">Console</span>.ReadLine();</li> <li>    host.Close();</li> <li style="background: #f3f3f3">    <span style="color:#2b91af">Console</span>.WriteLine(<span style="color:#a31515">&quot;Host closed&quot;</span>);</li> <li>}</li> </ol> </div> </div> </div>  <h3>Coming up</h3>  <p>Client and server (dispatch) message formatters, with an example showing how to send / receive arbitrary data without using the <a href="https://blogs.msdn.com/b/carlosfigueira/archive/2008/04/17/wcf-raw-programming-model-receiving-arbitrary-data.aspx">WCF REST Raw programming model</a> – this issue came up at the forums a few weeks back.</p>  <p>[<a href="https://code.msdn.microsoft.com/Simple-WCF-operation-e8d52375">Code for this post</a>]</p>  <p>[<a href="https://blogs.msdn.com/b/carlosfigueira/archive/2011/03/14/wcf-extensibility.aspx">Back to the index</a>]</p>  <p>Carlos Figueira    <br /><a href="https://blogs.msdn.com/carlosfigueira">https://blogs.msdn.com/carlosfigueira</a>    <br />Twitter: @carlos_figueira <a href="https://twitter.com/carlos_figueira">https://twitter.com/carlos_figueira</a></p>]]></content:encoded>
    </item>
  </channel>
</rss>