<?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>James Osborne's Blog</title>
    <atom:link
      href="https://docs.microsoft.com/archive/blogs/james_osbornes_blog/feed.xml"
      rel="self"
      type="application/rss+xml" />
    <link>https://docs.microsoft.com/archive/blogs/james_osbornes_blog/feed.xml</link>
    <description />
    <lastBuildDate>Wed, 17 Apr 2019 04:07:08 GMT</lastBuildDate>
    <language>en-US</language>
    <sy:updatePeriod>hourly</sy:updatePeriod>
    <sy:updateFrequency>1</sy:updateFrequency>
    <item>
      <title>2 auth modes on 1 endpoint - Using Multiple Authentication Schemes with ServiceAuthorizationManager</title>
      <link>https://docs.microsoft.com/archive/blogs/james_osbornes_blog/2-auth-modes-on-1-endpoint-using-multiple-authentication-schemes-with-serviceauthorizationmanager</link>
      <pubDate>Fri, 04 Nov 2011 16:51:00 GMT</pubDate>
      <dc:creator><![CDATA[James Osborne-MSFT]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/james_osbornes_blog/2011/11/04/2-auth-modes-on-1-endpoint-using-multiple-authentication-schemes-with-serviceauthorizationmanager/</guid>
      <description><![CDATA[Here&rsquo;s the general idea: I want a single service endpoint with 2 operations: one can be...]]></description>
      <content:encoded><![CDATA[<p>Here&rsquo;s the general idea: I want a single service endpoint with 2 operations: one can be accessed anonymously, the other requires authentication.<span style="mso-spacerun: yes;">&nbsp; </span><span class="GramE">Simple, right?</span><span style="mso-spacerun: yes;">&nbsp; </span>It should be.<span style="mso-spacerun: yes;">&nbsp; </span>But the trick is that you need multiple authentication schemes specified in IIS, <i style="mso-bidi-font-style: normal;">and</i> on your endpoint.<span style="mso-spacerun: yes;">&nbsp; </span>That&rsquo;s currently only possible with the developer preview, so I&rsquo;ll use that to demonstrate.<o:p></o:p></p>
<div class="WordSection1">
<p class="MsoNormal"><o:p>&nbsp;</o:p></p>
<p class="MsoNormal">First, I create a new WCF Service Application in Visual Studio.<span style="mso-spacerun: yes;">&nbsp; </span>I completely rewrote the contract and implementation to better demonstrate this feature.<span style="mso-spacerun: yes;">&nbsp; </span>Here&rsquo;s the contract:<o:p></o:p></p>
<p class="MsoNormal"><o:p>&nbsp;</o:p></p>
<p style="mso-layout-grid-align: none; text-autospace: none;" class="MsoNormal"><span class="GramE"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">using</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> <span class="SpellE">System.ServiceModel</span>;<o:p></o:p></span></p>
<p style="mso-layout-grid-align: none; text-autospace: none;" class="MsoNormal"><span class="GramE"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">using</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> <span class="SpellE">System.ServiceModel.Web</span>;<o:p></o:p></span></p>
<p style="mso-layout-grid-align: none; text-autospace: none;" class="MsoNormal"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><o:p>&nbsp;</o:p></span></p>
<p style="mso-layout-grid-align: none; text-autospace: none;" class="MsoNormal"><span class="GramE"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">namespace</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> <span class="SpellE">MultiAuthWithServiceAuthorizationManager</span><o:p></o:p></span></p>
<p style="mso-layout-grid-align: none; text-autospace: none;" class="MsoNormal"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">{<o:p></o:p></span></p>
<p style="mso-layout-grid-align: none; text-autospace: none;" class="MsoNormal"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp; </span>[</span><span class="SpellE"><span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white; mso-highlight: white;">ServiceContract</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">]<o:p></o:p></span></p>
<p style="mso-layout-grid-align: none; text-autospace: none;" class="MsoNormal"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp; </span></span><span class="GramE"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">public</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> </span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">interface</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> </span><span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white; mso-highlight: white;">IService1</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><o:p></o:p></span></p>
<p style="mso-layout-grid-align: none; text-autospace: none;" class="MsoNormal"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp; </span>{<o:p></o:p></span></p>
<p style="mso-layout-grid-align: none; text-autospace: none;" class="MsoNormal"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>[</span><span class="SpellE"><span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white; mso-highlight: white;">WebGet</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">]<o:p></o:p></span></p>
<p style="mso-layout-grid-align: none; text-autospace: none;" class="MsoNormal"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>[</span><span class="SpellE"><span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white; mso-highlight: white;">OperationContract</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">]<o:p></o:p></span></p>
<p style="mso-layout-grid-align: none; text-autospace: none;" class="MsoNormal"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;</span><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span class="GramE"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">string</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> <span class="SpellE">GetData</span>();<o:p></o:p></span></p>
<p style="mso-layout-grid-align: none; text-autospace: none;" class="MsoNormal"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><o:p>&nbsp;</o:p></span></p>
<p style="mso-layout-grid-align: none; text-autospace: none;" class="MsoNormal"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size: 9.5pt; font-family: Consolas; color: green; background: white; mso-highlight: white;">// Not really pretty; using a GET to POST something, but I want this to be easy to test with a browser.</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><o:p></o:p></span></p>
<p style="mso-layout-grid-align: none; text-autospace: none;" class="MsoNormal"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>[</span><span class="SpellE"><span class="GramE"><span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white; mso-highlight: white;">WebGet</span></span></span><span class="GramE"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">(</span></span><span class="SpellE"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">UriTemplate</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">=</span><span style="font-size: 9.5pt; font-family: Consolas; color: #a31515; background: white; mso-highlight: white;">"<span class="SpellE">AddData?data</span>={data}"</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">)]<o:p></o:p></span></p>
<p style="mso-layout-grid-align: none; text-autospace: none;" class="MsoNormal"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>[</span><span class="SpellE"><span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white; mso-highlight: white;">OperationContract</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">]<o:p></o:p></span></p>
<p style="mso-layout-grid-align: none; text-autospace: none;" class="MsoNormal"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span class="GramE"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">string</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> <span class="SpellE">AddData</span>(</span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">string</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> data);<o:p></o:p></span></p>
<p style="mso-layout-grid-align: none; text-autospace: none;" class="MsoNormal"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;</span><span style="mso-spacerun: yes;">&nbsp;&nbsp; </span>}<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">}</span><o:p></o:p></p>
<p class="MsoNormal">The implementation isn&rsquo;t anything special:<o:p></o:p></p>
<p style="mso-layout-grid-align: none; text-autospace: none;" class="MsoNormal"><span style="mso-spacerun: yes;">&nbsp;</span><span class="GramE"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">using</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> System;<o:p></o:p></span></p>
<p style="mso-layout-grid-align: none; text-autospace: none;" class="MsoNormal"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><o:p>&nbsp;</o:p></span></p>
<p style="mso-layout-grid-align: none; text-autospace: none;" class="MsoNormal"><span class="GramE"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">namespace</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> <span class="SpellE">MultiAuthWithServiceAuthorizationManager</span><o:p></o:p></span></p>
<p style="mso-layout-grid-align: none; text-autospace: none;" class="MsoNormal"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">{<o:p></o:p></span></p>
<p style="mso-layout-grid-align: none; text-autospace: none;" class="MsoNormal"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp; </span></span><span class="GramE"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">public</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> </span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">class</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> </span><span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white; mso-highlight: white;">Service1</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> : </span><span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white; mso-highlight: white;">IService1</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><o:p></o:p></span></p>
<p style="mso-layout-grid-align: none; text-autospace: none;" class="MsoNormal"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp; </span>{<o:p></o:p></span></p>
<p style="mso-layout-grid-align: none; text-autospace: none;" class="MsoNormal"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span class="GramE"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">public</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> </span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">string</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> <span class="SpellE">GetData</span>()<o:p></o:p></span></p>
<p style="mso-layout-grid-align: none; text-autospace: none;" class="MsoNormal"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>{<o:p></o:p></span></p>
<p style="mso-layout-grid-align: none; text-autospace: none;" class="MsoNormal"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span class="GramE"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">return</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> </span><span class="SpellE"><span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white; mso-highlight: white;">DateTime</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">.Now</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> + </span><span style="font-size: 9.5pt; font-family: Consolas; color: #a31515; background: white; mso-highlight: white;">"Some Data."</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">;<o:p></o:p></span></p>
<p style="mso-layout-grid-align: none; text-autospace: none;" class="MsoNormal"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp; </span><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>}<o:p></o:p></span></p>
<p style="mso-layout-grid-align: none; text-autospace: none;" class="MsoNormal"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><o:p>&nbsp;</o:p></span></p>
<p style="mso-layout-grid-align: none; text-autospace: none;" class="MsoNormal"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span class="GramE"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">public</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> </span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">string</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> <span class="SpellE">AddData</span>(</span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">string</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> data)<o:p></o:p></span></p>
<p style="mso-layout-grid-align: none; text-autospace: none;" class="MsoNormal"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>{<o:p></o:p></span></p>
<p style="mso-layout-grid-align: none; text-autospace: none;" class="MsoNormal"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span class="GramE"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">return</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> </span><span class="SpellE"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">string</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">.Format</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">(</span><span style="font-size: 9.5pt; font-family: Consolas; color: #a31515; background: white; mso-highlight: white;">"{0} data added = \"{1}\""</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">, </span><span class="SpellE"><span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white; mso-highlight: white;">DateTime</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">.Now</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">, data);<o:p></o:p></span></p>
<p style="mso-layout-grid-align: none; text-autospace: none;" class="MsoNormal"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>}<o:p></o:p></span></p>
<p style="mso-layout-grid-align: none; text-autospace: none;" class="MsoNormal"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp; </span>}<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">}</span><span style="font-size: 9.5pt; font-family: Consolas; color: black;"><o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size: 9.5pt; font-family: Consolas; color: black;"><o:p>&nbsp;</o:p></span></p>
<p class="MsoNormal">The magic happens first with a custom <span class="SpellE">ServiceAuthorizationManager</span> implementation.<span style="mso-spacerun: yes;">&nbsp; </span>I added this class:<o:p></o:p></p>
<p style="mso-layout-grid-align: none; text-autospace: none;" class="MsoNormal"><span class="GramE"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">using</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> <span class="SpellE">System.ServiceModel</span>;<o:p></o:p></span></p>
<p style="mso-layout-grid-align: none; text-autospace: none;" class="MsoNormal"><span class="GramE"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">using</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> <span class="SpellE">System.ServiceModel.Channels</span>;<o:p></o:p></span></p>
<p style="mso-layout-grid-align: none; text-autospace: none;" class="MsoNormal"><span class="GramE"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">using</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> <span class="SpellE">System.Web</span>;<o:p></o:p></span></p>
<p style="mso-layout-grid-align: none; text-autospace: none;" class="MsoNormal"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><o:p>&nbsp;</o:p></span></p>
<p style="mso-layout-grid-align: none; text-autospace: none;" class="MsoNormal"><span class="GramE"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">namespace</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> <span class="SpellE">MultiAuthWithServiceAuthorizationManager</span><o:p></o:p></span></p>
<p style="mso-layout-grid-align: none; text-autospace: none;" class="MsoNormal"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">{<o:p></o:p></span></p>
<p style="mso-layout-grid-align: none; text-autospace: none;" class="MsoNormal"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp; </span></span><span class="GramE"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">public</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> </span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">class</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> </span><span class="SpellE"><span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white; mso-highlight: white;">MyServiceAuthorizationManager</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> : </span><span class="SpellE"><span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white; mso-highlight: white;">ServiceAuthorizationManager</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><o:p></o:p></span></p>
<p style="mso-layout-grid-align: none; text-autospace: none;" class="MsoNormal"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp; </span>{<o:p></o:p></span></p>
<p style="mso-layout-grid-align: none; text-autospace: none;" class="MsoNormal"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span class="GramE"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">protected</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> </span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">override</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> </span><span class="SpellE"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">bool</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> <span class="SpellE">CheckAccessCore</span>(</span><span class="SpellE"><span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white; mso-highlight: white;">OperationContext</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> <span class="SpellE">operationContext</span>)<o:p></o:p></span></p>
<p style="mso-layout-grid-align: none; text-autospace: none;" class="MsoNormal"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>{<o:p></o:p></span></p>
<p style="mso-layout-grid-align: none; text-autospace: none;" class="MsoNormal"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span class="SpellE"><span class="GramE"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">bool</span></span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> <span class="SpellE">operationRequiresAuth</span> = (</span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">string</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">)operationContext.RequestContext.RequestMessage.Properties[</span><span style="font-size: 9.5pt; font-family: Consolas; color: #a31515; background: white; mso-highlight: white;">"HttpOperationName"</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">] == </span><span style="font-size: 9.5pt; font-family: Consolas; color: #a31515; background: white; mso-highlight: white;">"<span class="SpellE">AddData</span>"</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">;<o:p></o:p></span></p>
<p style="mso-layout-grid-align: none; text-autospace: none;" class="MsoNormal"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><o:p>&nbsp;</o:p></span></p>
<p style="mso-layout-grid-align: none; text-autospace: none;" class="MsoNormal"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span class="GramE"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">if</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> (<span class="SpellE">operationRequiresAuth</span>)<o:p></o:p></span></p>
<p style="mso-layout-grid-align: none; text-autospace: none;" class="MsoNormal"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>{<o:p></o:p></span></p>
<p style="mso-layout-grid-align: none; text-autospace: none;" class="MsoNormal"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span class="GramE"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">if</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> (</span><span class="SpellE"><span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white; mso-highlight: white;">HttpContext</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">.Current.Request.LogonUserIdentity.IsAuthenticated</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">)<o:p></o:p></span></p>
<p style="mso-layout-grid-align: none; text-autospace: none;" class="MsoNormal"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>{<o:p></o:p></span></p>
<p style="mso-layout-grid-align: none; text-autospace: none;" class="MsoNormal"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span class="GramE"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">return</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> </span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">true</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">;<o:p></o:p></span></p>
<p style="mso-layout-grid-align: none; text-autospace: none;" class="MsoNormal"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>}<o:p></o:p></span></p>
<p style="mso-layout-grid-align: none; text-autospace: none;" class="MsoNormal"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><o:p>&nbsp;</o:p></span></p>
<p style="mso-layout-grid-align: none; text-autospace: none;" class="MsoNormal"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size: 9.5pt; font-family: Consolas; color: green; background: white; mso-highlight: white;">// If we haven't returned yet, it's unauthorized and it should be authorized.</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><o:p></o:p></span></p>
<p style="mso-layout-grid-align: none; text-autospace: none;" class="MsoNormal"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span class="SpellE"><span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white; mso-highlight: white;">HttpResponseMessageProperty</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> <span class="SpellE">messageProp</span> = </span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">new</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> </span><span class="SpellE"><span class="GramE"><span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white; mso-highlight: white;">HttpResponseMessageProperty</span></span></span><span class="GramE"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">(</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">);<o:p></o:p></span></p>
<p style="mso-layout-grid-align: none; text-autospace: none;" class="MsoNormal"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span class="SpellE">messageProp.StatusCode</span> = <span class="SpellE">System.Net.<span style="color: #2b91af;">HttpStatusCode</span>.Unauthorized</span>;<o:p></o:p></span></p>
<p style="mso-layout-grid-align: none; text-autospace: none;" class="MsoNormal"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span class="GramE">operationContext.OutgoingMessageProperties.Add(</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white; mso-highlight: white;">HttpResponseMessageProperty</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">.Name, <span class="SpellE">messageProp</span>);<o:p></o:p></span></p>
<p style="mso-layout-grid-align: none; text-autospace: none;" class="MsoNormal"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span class="GramE"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">return</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> </span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">false</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">;<o:p></o:p></span></p>
<p style="mso-layout-grid-align: none; text-autospace: none;" class="MsoNormal"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>}<o:p></o:p></span></p>
<p style="mso-layout-grid-align: none; text-autospace: none;" class="MsoNormal"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span class="GramE"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">else</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><o:p></o:p></span></p>
<p style="mso-layout-grid-align: none; text-autospace: none;" class="MsoNormal"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>{<o:p></o:p></span></p>
<p style="mso-layout-grid-align: none; text-autospace: none;" class="MsoNormal"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size: 9.5pt; font-family: Consolas; color: green; background: white; mso-highlight: white;">// <span class="GramE">Don't</span> check <span class="SpellE">auth</span> for the operations that don't require it.</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><o:p></o:p></span></p>
<p style="mso-layout-grid-align: none; text-autospace: none;" class="MsoNormal"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span class="GramE"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">return</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> </span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">true</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">;<o:p></o:p></span></p>
<p style="mso-layout-grid-align: none; text-autospace: none;" class="MsoNormal"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>}<o:p></o:p></span></p>
<p style="mso-layout-grid-align: none; text-autospace: none;" class="MsoNormal"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>}<o:p></o:p></span></p>
<p style="mso-layout-grid-align: none; text-autospace: none;" class="MsoNormal"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp; </span>}<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">}</span><o:p></o:p></p>
<p class="MsoNormal"><o:p>&nbsp;</o:p></p>
<p class="MsoNormal">I then have to wire it up in <span class="SpellE">config</span>.<span style="mso-spacerun: yes;">&nbsp; </span>It&rsquo;s a bit odd - I have to specify the assembly explicitly, but here&rsquo;s the <span class="SpellE">config</span>.<span style="mso-spacerun: yes;">&nbsp; </span>See the service behaviors for where the manager is hooked up.<span style="mso-spacerun: yes;">&nbsp; </span>(And don&rsquo;t use this kind of thing in production &ndash; security mode should be Transport, not <span class="SpellE">TransportCredentialOnly</span>!<span style="mso-spacerun: yes;">&nbsp; </span>I just wanted to simplify things and not configure https.)<o:p></o:p></p>
<p style="mso-layout-grid-align: none; text-autospace: none;" class="MsoNormal"><span class="GramE"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">&lt;?</span><span style="font-size: 9.5pt; font-family: Consolas; color: #a31515; background: white; mso-highlight: white;">xml</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;"> </span><span style="font-size: 9.5pt; font-family: Consolas; color: red; background: white; mso-highlight: white;">version</span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">=</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">"</span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">1.0</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">"</span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">?&gt;</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><o:p></o:p></span></p>
<p style="mso-layout-grid-align: none; text-autospace: none;" class="MsoNormal"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">&lt;</span><span class="GramE"><span style="font-size: 9.5pt; font-family: Consolas; color: #a31515; background: white; mso-highlight: white;">configuration</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">&gt;</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><o:p></o:p></span></p>
<p style="mso-layout-grid-align: none; text-autospace: none;" class="MsoNormal"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp; </span>&lt;</span><span class="SpellE"><span style="font-size: 9.5pt; font-family: Consolas; color: #a31515; background: white; mso-highlight: white;">system.web</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">&gt;</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><o:p></o:p></span></p>
<p style="mso-layout-grid-align: none; text-autospace: none;" class="MsoNormal"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp; </span>&lt;</span><span style="font-size: 9.5pt; font-family: Consolas; color: #a31515; background: white; mso-highlight: white;">compilation</span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;"> </span><span style="font-size: 9.5pt; font-family: Consolas; color: red; background: white; mso-highlight: white;">debug</span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">=</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">"</span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">true</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">"</span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;"> </span><span class="SpellE"><span style="font-size: 9.5pt; font-family: Consolas; color: red; background: white; mso-highlight: white;">targetFramework</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">=</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">"</span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">4.5</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">"</span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;"> /&gt;</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><o:p></o:p></span></p>
<p style="mso-layout-grid-align: none; text-autospace: none;" class="MsoNormal"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp; </span>&lt;/</span><span class="SpellE"><span style="font-size: 9.5pt; font-family: Consolas; color: #a31515; background: white; mso-highlight: white;">system.web</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">&gt;</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><o:p></o:p></span></p>
<p style="mso-layout-grid-align: none; text-autospace: none;" class="MsoNormal"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp; </span>&lt;</span><span class="SpellE"><span style="font-size: 9.5pt; font-family: Consolas; color: #a31515; background: white; mso-highlight: white;">system.serviceModel</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">&gt;</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><o:p></o:p></span></p>
<p style="mso-layout-grid-align: none; text-autospace: none;" class="MsoNormal"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp; </span>&lt;</span><span class="GramE"><span style="font-size: 9.5pt; font-family: Consolas; color: #a31515; background: white; mso-highlight: white;">behaviors</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">&gt;</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><o:p></o:p></span></p>
<p style="mso-layout-grid-align: none; text-autospace: none;" class="MsoNormal"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>&lt;</span><span class="SpellE"><span class="GramE"><span style="font-size: 9.5pt; font-family: Consolas; color: #a31515; background: white; mso-highlight: white;">serviceBehaviors</span></span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">&gt;</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><o:p></o:p></span></p>
<p style="mso-layout-grid-align: none; text-autospace: none;" class="MsoNormal"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>&lt;</span><span style="font-size: 9.5pt; font-family: Consolas; color: #a31515; background: white; mso-highlight: white;">behavior</span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;"> </span><span style="font-size: 9.5pt; font-family: Consolas; color: red; background: white; mso-highlight: white;">name</span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">=</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">"</span><span class="SpellE"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">serviceAuthMan</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">"</span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">&gt;</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><o:p></o:p></span></p>
<p style="mso-layout-grid-align: none; text-autospace: none;" class="MsoNormal"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>&lt;</span><span class="SpellE"><span style="font-size: 9.5pt; font-family: Consolas; color: #a31515; background: white; mso-highlight: white;">serviceAuthorization</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;"> </span><span style="font-size: 9.5pt; font-family: Consolas; color: red; background: white; mso-highlight: white;">serviceAuthorizationManagerType</span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">=</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">"</span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">MultiAuthWithServiceAuthorizationManager.MyServiceAuthorizationManager, <span class="SpellE">MultiAuthWithServiceAuthorizationManager</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">"</span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">/&gt;</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><o:p></o:p></span></p>
<p style="mso-layout-grid-align: none; text-autospace: none;" class="MsoNormal"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>&lt;/</span><span style="font-size: 9.5pt; font-family: Consolas; color: #a31515; background: white; mso-highlight: white;">behavior</span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">&gt;</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><o:p></o:p></span></p>
<p style="mso-layout-grid-align: none; text-autospace: none;" class="MsoNormal"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>&lt;/</span><span class="SpellE"><span style="font-size: 9.5pt; font-family: Consolas; color: #a31515; background: white; mso-highlight: white;">serviceBehaviors</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">&gt;</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><o:p></o:p></span></p>
<p style="mso-layout-grid-align: none; text-autospace: none;" class="MsoNormal"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp; </span>&lt;/</span><span style="font-size: 9.5pt; font-family: Consolas; color: #a31515; background: white; mso-highlight: white;">behaviors</span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">&gt;</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><o:p></o:p></span></p>
<p style="mso-layout-grid-align: none; text-autospace: none;" class="MsoNormal"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp; </span>&lt;</span><span class="SpellE"><span style="font-size: 9.5pt; font-family: Consolas; color: #a31515; background: white; mso-highlight: white;">serviceHostingEnvironment</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;"> </span><span class="SpellE"><span style="font-size: 9.5pt; font-family: Consolas; color: red; background: white; mso-highlight: white;">aspNetCompatibilityEnabled</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">=</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">"</span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">true</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">"<o:p></o:p></span></p>
<p style="mso-layout-grid-align: none; text-autospace: none;" class="MsoNormal"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span class="SpellE"><span class="GramE"><span style="font-size: 9.5pt; font-family: Consolas; color: red; background: white; mso-highlight: white;">multipleSiteBindingsEnabled</span></span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">=</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">"</span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">true</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">"</span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;"> /&gt;</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><o:p></o:p></span></p>
<p style="mso-layout-grid-align: none; text-autospace: none;" class="MsoNormal"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp; </span>&lt;</span><span class="GramE"><span style="font-size: 9.5pt; font-family: Consolas; color: #a31515; background: white; mso-highlight: white;">bindings</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">&gt;</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><o:p></o:p></span></p>
<p style="mso-layout-grid-align: none; text-autospace: none;" class="MsoNormal"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>&lt;</span><span class="SpellE"><span class="GramE"><span style="font-size: 9.5pt; font-family: Consolas; color: #a31515; background: white; mso-highlight: white;">webHttpBinding</span></span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">&gt;</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><o:p></o:p></span></p>
<p style="mso-layout-grid-align: none; text-autospace: none;" class="MsoNormal"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>&lt;</span><span style="font-size: 9.5pt; font-family: Consolas; color: #a31515; background: white; mso-highlight: white;">binding</span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;"> </span><span style="font-size: 9.5pt; font-family: Consolas; color: red; background: white; mso-highlight: white;">name</span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">=</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">"</span><span class="SpellE"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">MyBinding</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">"</span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">&gt;</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><o:p></o:p></span></p>
<p style="mso-layout-grid-align: none; text-autospace: none;" class="MsoNormal"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;</span>&lt;</span><span style="font-size: 9.5pt; font-family: Consolas; color: #a31515; background: white; mso-highlight: white;">security</span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;"> </span><span style="font-size: 9.5pt; font-family: Consolas; color: red; background: white; mso-highlight: white;">mode</span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">=</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">"</span><span class="SpellE"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">TransportCredentialOnly</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">"</span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">&gt;</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><o:p></o:p></span></p>
<p style="mso-layout-grid-align: none; text-autospace: none;" class="MsoNormal"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>&lt;</span><span style="font-size: 9.5pt; font-family: Consolas; color: #a31515; background: white; mso-highlight: white;">transport</span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;"> </span><span class="SpellE"><span style="font-size: 9.5pt; font-family: Consolas; color: red; background: white; mso-highlight: white;">clientCredentialType</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">=</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">"</span><span class="SpellE"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">InheritedFromHost</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">"</span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">/&gt;</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><o:p></o:p></span></p>
<p style="mso-layout-grid-align: none; text-autospace: none;" class="MsoNormal"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>&lt;/</span><span style="font-size: 9.5pt; font-family: Consolas; color: #a31515; background: white; mso-highlight: white;">security</span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">&gt;</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><o:p></o:p></span></p>
<p style="mso-layout-grid-align: none; text-autospace: none;" class="MsoNormal"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>&lt;/</span><span style="font-size: 9.5pt; font-family: Consolas; color: #a31515; background: white; mso-highlight: white;">binding</span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">&gt;</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><o:p></o:p></span></p>
<p style="mso-layout-grid-align: none; text-autospace: none;" class="MsoNormal"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>&lt;/</span><span class="SpellE"><span style="font-size: 9.5pt; font-family: Consolas; color: #a31515; background: white; mso-highlight: white;">webHttpBinding</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">&gt;</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><o:p></o:p></span></p>
<p style="mso-layout-grid-align: none; text-autospace: none;" class="MsoNormal"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp; </span>&lt;/</span><span style="font-size: 9.5pt; font-family: Consolas; color: #a31515; background: white; mso-highlight: white;">bindings</span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">&gt;</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><o:p></o:p></span></p>
<p style="mso-layout-grid-align: none; text-autospace: none;" class="MsoNormal"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp; </span>&lt;</span><span class="GramE"><span style="font-size: 9.5pt; font-family: Consolas; color: #a31515; background: white; mso-highlight: white;">services</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">&gt;</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><o:p></o:p></span></p>
<p style="mso-layout-grid-align: none; text-autospace: none;" class="MsoNormal"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>&lt;</span><span style="font-size: 9.5pt; font-family: Consolas; color: #a31515; background: white; mso-highlight: white;">service</span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;"> </span><span style="font-size: 9.5pt; font-family: Consolas; color: red; background: white; mso-highlight: white;">name</span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">=</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">"</span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">MultiAuthWithServiceAuthorizationManager.Service1</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">"</span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;"> </span><span class="SpellE"><span style="font-size: 9.5pt; font-family: Consolas; color: red; background: white; mso-highlight: white;">behaviorConfiguration</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">=</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">"</span><span class="SpellE"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">serviceAuthMan</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">"</span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">&gt;</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><o:p></o:p></span></p>
<p style="mso-layout-grid-align: none; text-autospace: none;" class="MsoNormal"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>&lt;</span><span style="font-size: 9.5pt; font-family: Consolas; color: #a31515; background: white; mso-highlight: white;">endpoint</span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;"> </span><span style="font-size: 9.5pt; font-family: Consolas; color: red; background: white; mso-highlight: white;">address</span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">=</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">""</span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;"> </span><span style="font-size: 9.5pt; font-family: Consolas; color: red; background: white; mso-highlight: white;">contract</span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">=</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">"</span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">MultiAuthWithServiceAuthorizationManager.IService1</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">"</span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;"> </span><span style="font-size: 9.5pt; font-family: Consolas; color: red; background: white; mso-highlight: white;">binding</span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">=</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">"</span><span class="SpellE"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">webHttpBinding</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">"</span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;"> </span><span class="SpellE"><span style="font-size: 9.5pt; font-family: Consolas; color: red; background: white; mso-highlight: white;">bindingConfiguration</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">=</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">"</span><span class="SpellE"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">MyBinding</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">"</span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">/&gt;</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><o:p></o:p></span></p>
<p style="mso-layout-grid-align: none; text-autospace: none;" class="MsoNormal"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>&lt;/</span><span style="font-size: 9.5pt; font-family: Consolas; color: #a31515; background: white; mso-highlight: white;">service</span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">&gt;</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><o:p></o:p></span></p>
<p style="mso-layout-grid-align: none; text-autospace: none;" class="MsoNormal"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp; </span>&lt;/</span><span style="font-size: 9.5pt; font-family: Consolas; color: #a31515; background: white; mso-highlight: white;">services</span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">&gt;</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><o:p></o:p></span></p>
<p style="mso-layout-grid-align: none; text-autospace: none;" class="MsoNormal"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp; </span>&lt;/</span><span class="SpellE"><span style="font-size: 9.5pt; font-family: Consolas; color: #a31515; background: white; mso-highlight: white;">system.serviceModel</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">&gt;</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><o:p></o:p></span></p>
<p style="mso-layout-grid-align: none; text-autospace: none;" class="MsoNormal"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp; </span>&lt;</span><span class="SpellE"><span style="font-size: 9.5pt; font-family: Consolas; color: #a31515; background: white; mso-highlight: white;">system.webServer</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">&gt;</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><o:p></o:p></span></p>
<p style="mso-layout-grid-align: none; text-autospace: none;" class="MsoNormal"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp; </span>&lt;</span><span style="font-size: 9.5pt; font-family: Consolas; color: #a31515; background: white; mso-highlight: white;">modules</span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;"> </span><span class="SpellE"><span style="font-size: 9.5pt; font-family: Consolas; color: red; background: white; mso-highlight: white;">runAllManagedModulesForAllRequests</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">=</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">"</span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">true</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">"</span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">/&gt;</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><o:p></o:p></span></p>
<p style="mso-layout-grid-align: none; text-autospace: none;" class="MsoNormal"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp; </span>&lt;/</span><span class="SpellE"><span style="font-size: 9.5pt; font-family: Consolas; color: #a31515; background: white; mso-highlight: white;">system.webServer</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">&gt;</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">&lt;/</span><span style="font-size: 9.5pt; font-family: Consolas; color: #a31515; background: white; mso-highlight: white;">configuration</span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">&gt;</span><span style="font-size: 9.5pt; font-family: Consolas; color: blue;"><o:p></o:p></span></p>
<p class="MsoNormal"><o:p>&nbsp;</o:p></p>
<p class="MsoNormal">As you can see, I explicitly added the endpoint as well.<span style="mso-spacerun: yes;">&nbsp; </span>So, with this all hooked up, I will create a <span class="SpellE">vdir</span> by going to Properties &ndash; Web &ndash; Unchecked the &ldquo;Use IIS Express&rdquo; and created the virtual directory.<span style="mso-spacerun: yes;">&nbsp; </span>Last thing to do is open up the Authentication feature in IIS for my Application and enable other <span class="SpellE">auth</span> modes besides anonymous.<span style="mso-spacerun: yes;">&nbsp; </span>I&rsquo;ll just use Basic and Anonymous.<span style="mso-spacerun: yes;">&nbsp; </span><o:p></o:p></p>
<p class="MsoNormal"><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/39/69/6038.IIS.jpg" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-39-69/6038.IIS.jpg"><img src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/39/69/6038.IIS.jpg" original-url="https://blogs.msdn.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-01-39-69/6038.IIS.jpg" border="0" alt="" /></a></p>
<p class="MsoNormal"><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/39/69/6038.IIS.jpg" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-39-69/6038.IIS.jpg"></a><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/39/69/5516.Authentication.jpg" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-39-69/5516.Authentication.jpg"><img src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/39/69/5516.Authentication.jpg" original-url="https://blogs.msdn.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-01-39-69/5516.Authentication.jpg" border="0" alt="" /></a></p>
<p class="MsoNormal"><o:p>&nbsp;</o:p></p>
<p class="MsoNormal">Now, when I browse to https://localhost/MultiAuthWithServiceAuthorizationManager/Service1.svc/GetData, I see some xml like this:<o:p></o:p></p>
<p style="margin-top: 5.0pt; margin-right: 0in; margin-bottom: 5.0pt; margin-left: 0in; mso-layout-grid-align: none; text-autospace: none;" class="MsoNormal"><span style="font-size: 12.0pt; font-family: 'Times New Roman','serif';">&lt;string xmlns="https://schemas.microsoft.com/2003/10/Serialization/"&gt;11/4/2011 4:32:35 <span class="SpellE">PMSome</span> Data<span class="GramE">.&lt;</span>/string&gt; <o:p></o:p></span></p>
<p class="MsoNormal">And when I browse to https://localhost/MultiAuthWithServiceAuthorizationManager/Service1.svc/AddData?data=Some%20new%20data, I am faced with a dialog box:<o:p></o:p></p>
<p class="MsoNormal"><o:p>&nbsp;<a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/39/69/2437.WindowsSecurityPopup.jpg" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-39-69/2437.WindowsSecurityPopup.jpg"><img src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/39/69/2437.WindowsSecurityPopup.jpg" original-url="https://blogs.msdn.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-01-39-69/2437.WindowsSecurityPopup.jpg" border="0" alt="" /></a></o:p></p>
<p class="MsoNormal"><o:p>&nbsp;</o:p></p>
<p class="MsoNormal">Adding my credentials allows the request to go through and I see the response:<o:p></o:p></p>
<p style="margin-top: 5.0pt; margin-right: 0in; margin-bottom: 5.0pt; margin-left: 0in; mso-layout-grid-align: none; text-autospace: none;" class="MsoNormal"><span style="font-size: 12.0pt; font-family: 'Times New Roman','serif';">&lt;string xmlns="https://schemas.microsoft.com/2003/10/Serialization/"&gt;11/4/2011 4:36:02 PM data added = "Some new data"&lt;/string&gt; <o:p></o:p></span></p>
<p class="MsoNormal"><o:p>&nbsp;</o:p></p>
<p class="MsoNormal">I tried to keep this example simple to demonstrate how a single endpoint can require authentication on some methods but not all.<span style="mso-spacerun: yes;">&nbsp; </span>How you define what methods to require <span class="SpellE">auth</span> on is up to your implementation of the <span class="SpellE">ServiceAuthorizationManager</span>.<span style="mso-spacerun: yes;">&nbsp; </span>There&rsquo;s a lot going on here, but this is the general idea.<o:p></o:p></p>
<p class="MsoNormal"><o:p>&nbsp;</o:p></p>
<p class="MsoNormal"><o:p>&nbsp;</o:p></p>
<p class="MsoNormal"><o:p>&nbsp;</o:p></p>
</div>]]></content:encoded>
    </item>
    <item>
      <title>Multithreaded Testing...</title>
      <link>https://docs.microsoft.com/archive/blogs/james_osbornes_blog/multithreaded-testing</link>
      <pubDate>Mon, 08 Aug 2011 09:26:00 GMT</pubDate>
      <dc:creator><![CDATA[James Osborne-MSFT]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/james_osbornes_blog/2011/08/08/multithreaded-testing/</guid>
      <description><![CDATA[On multiple occasions, I've found myself in need of a design pattern to allow a single thread...]]></description>
      <content:encoded><![CDATA[<p><meta name="ProgId" content="Word.Document" /><meta name="Generator" content="Microsoft Word 14" /><meta name="Originator" content="Microsoft Word 14" /><link rel="File-List" href="https://blogs.msdn.com/controlpanel/blogs/posteditor.aspx/BlogPost_files/filelist.xml" /> <link rel="themeData" href="https://blogs.msdn.com/controlpanel/blogs/posteditor.aspx/BlogPost_files/themedata.thmx" /> <link rel="colorSchemeMapping" href="https://blogs.msdn.com/controlpanel/blogs/posteditor.aspx/BlogPost_files/colorschememapping.xml" /></p>
<div class="WordSection1">
<p style="line-height: normal; margin-bottom: 0pt;" class="MsoNormal"><span style="mso-fareast-font-family: 'Times New Roman'; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri; mso-bidi-font-family: Calibri;">On multiple occasions, I've found myself in need of a design pattern to allow a single thread to do some work, while other threads block.<span style="mso-spacerun: yes;">&nbsp; </span>I'm not getting into the details about how this environment comes about, or what I'm exactly <span class="GramE">doing ,</span> but let me define the problem space:<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt 27pt; line-height: normal;" class="MsoNormal"><span style="mso-fareast-font-family: 'Times New Roman'; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri; mso-bidi-font-family: Calibri;">&nbsp;</span></p>
<ol>
<li value="1">I've got a method that gets repeatedly executed by 20 threads concurrently.&nbsp; Let's call it, "IterationRun".</li>
<li>Within this method, I want a single thread to execute another method (it's a void method, so it can be&nbsp;used as an Action.)&nbsp;</li>
<li>While the above "DoWork" method is executing, I want all other threads to block, so they don't exit IterationRun, thus entering it again.</li>
</ol>
<p style="margin: 0in 0in 0pt 27pt; line-height: normal;" class="MsoNormal"><span style="mso-fareast-font-family: 'Times New Roman'; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri; mso-bidi-font-family: Calibri;"><o:p></o:p></span></p>
<p style="line-height: normal; margin-bottom: 0pt;" class="MsoNormal"><span style="mso-fareast-font-family: 'Times New Roman'; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri; mso-bidi-font-family: Calibri;">The following design seems to do a good job of satisfying the above criteria.<span style="mso-spacerun: yes;">&nbsp; </span>I'm open to critique, by the way.<span style="mso-spacerun: yes;">&nbsp; </span>Also, this is half pseudo-code.<span style="mso-spacerun: yes;">&nbsp; </span>The driver that executes <span class="SpellE"><span class="GramE">IterationRun</span></span><span class="GramE">,</span> sets up threads, and such is left out.<span style="mso-spacerun: yes;">&nbsp; </span>Have a look at the <span class="SpellE">DoWorkOnASingleThread</span> method.<o:p></o:p></span></p>
<p style="line-height: normal; margin-bottom: 0pt;" class="MsoNormal"><span style="mso-fareast-font-family: 'Times New Roman'; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri; mso-bidi-font-family: Calibri;">&nbsp;<o:p></o:p></span></p>
<p style="line-height: normal; margin-bottom: 0pt;" class="MsoNormal"><span class="GramE"><span style="color: blue; font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: 'Times New Roman';">namespace</span></span><span style="font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: 'Times New Roman';"> <span class="SpellE">PseudoishCode</span><o:p></o:p></span></p>
<p style="line-height: normal; margin-bottom: 0pt;" class="MsoNormal"><span style="font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: 'Times New Roman';">{<o:p></o:p></span></p>
<p style="line-height: normal; margin-bottom: 0pt;" class="MsoNormal"><span style="font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp; </span><span class="GramE"><span style="color: blue;">using</span></span> System;<o:p></o:p></span></p>
<p style="line-height: normal; margin-bottom: 0pt;" class="MsoNormal"><span style="font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp; </span><span class="GramE"><span style="color: blue;">using</span></span> <span class="SpellE">System.Threading</span>;<o:p></o:p></span></p>
<p style="line-height: normal; margin-bottom: 0pt;" class="MsoNormal"><span style="font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: 'Times New Roman';">&nbsp;<o:p></o:p></span></p>
<p style="line-height: normal; margin-bottom: 0pt;" class="MsoNormal"><span style="font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp; </span><span class="GramE"><span style="color: blue;">public</span></span> <span style="color: blue;">class</span> <span class="SpellE"><span style="color: #2b91af;">PseudoishCodeExample</span></span><o:p></o:p></span></p>
<p style="line-height: normal; margin-bottom: 0pt;" class="MsoNormal"><span style="font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp; </span>{<o:p></o:p></span></p>
<p style="line-height: normal; margin-bottom: 0pt;" class="MsoNormal"><span style="font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: green;">// Counter used to identify individual threads</span><o:p></o:p></span></p>
<p style="line-height: normal; margin-bottom: 0pt;" class="MsoNormal"><span style="font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span class="GramE"><span style="color: blue;">private</span></span> <span style="color: blue;">static</span> <span class="SpellE"><span style="color: blue;">int</span></span> <span class="SpellE">threadCount</span> = 0;<o:p></o:p></span></p>
<p style="line-height: normal; margin-bottom: 0pt;" class="MsoNormal"><span style="font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: 'Times New Roman';">&nbsp;<o:p></o:p></span></p>
<p style="line-height: normal; margin-bottom: 0pt;" class="MsoNormal"><span style="font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span class="GramE"><span style="color: blue;">private</span></span> <span style="color: blue;">static</span> <span class="SpellE"><span style="color: #2b91af;">ManualResetEvent</span></span> <span class="SpellE">preWorkBlock</span>;<o:p></o:p></span></p>
<p style="line-height: normal; margin-bottom: 0pt;" class="MsoNormal"><span style="font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span class="GramE"><span style="color: blue;">private</span></span> <span style="color: blue;">static</span> <span class="SpellE"><span style="color: #2b91af;">ManualResetEvent</span></span> <span class="SpellE">postWorkBlock</span>;<o:p></o:p></span></p>
<p style="line-height: normal; margin-bottom: 0pt;" class="MsoNormal"><span style="font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span class="GramE"><span style="color: blue;">private</span></span> <span style="color: blue;">static</span> <span class="SpellE"><span style="color: blue;">int</span></span> <span class="SpellE">runningIterationCount</span> = 0;<o:p></o:p></span></p>
<p style="line-height: normal; margin-bottom: 0pt;" class="MsoNormal"><span style="font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span class="GramE"><span style="color: blue;">private</span></span> <span style="color: blue;">static</span> <span style="color: blue;">object</span> <span class="SpellE">thisLock</span> = <span style="color: blue;">new</span> <span style="color: blue;">object</span>();<o:p></o:p></span></p>
<p style="line-height: normal; margin-bottom: 0pt;" class="MsoNormal"><span style="font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: 'Times New Roman';">&nbsp;<o:p></o:p></span></p>
<p style="line-height: normal; margin-bottom: 0pt;" class="MsoNormal"><span style="font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: green;">// Total threads that will be executing</span><o:p></o:p></span></p>
<p style="line-height: normal; margin-bottom: 0pt;" class="MsoNormal"><span style="font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span class="GramE"><span style="color: blue;">private</span></span> <span class="SpellE"><span style="color: blue;">int</span></span> threads = 10;<o:p></o:p></span></p>
<p style="line-height: normal; margin-bottom: 0pt;" class="MsoNormal"><span style="font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: 'Times New Roman';">&nbsp;<o:p></o:p></span></p>
<p style="line-height: normal; margin-bottom: 0pt;" class="MsoNormal"><span style="font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span class="GramE"><span style="color: blue;">private</span></span> <span class="SpellE"><span style="color: blue;">int</span></span> <span class="SpellE">threadId</span> = 0;<o:p></o:p></span></p>
<p style="line-height: normal; margin-bottom: 0pt;" class="MsoNormal"><span style="font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: 'Times New Roman';">&nbsp;<o:p></o:p></span></p>
<p style="line-height: normal; margin-bottom: 0pt;" class="MsoNormal"><span style="font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: green;">// Set up done once per process - multiple threads not yet created.</span><o:p></o:p></span></p>
<p style="line-height: normal; margin-bottom: 0pt;" class="MsoNormal"><span style="font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span class="GramE"><span style="color: blue;">public</span></span> <span style="color: blue;">void</span> <span class="SpellE">ProcessSetup</span>()<o:p></o:p></span></p>
<p style="line-height: normal; margin-bottom: 0pt;" class="MsoNormal"><span style="font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>{<o:p></o:p></span></p>
<p style="line-height: normal; margin-bottom: 0pt;" class="MsoNormal"><span style="font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span class="SpellE"><span class="GramE">preWorkBlock</span></span> = <span style="color: blue;">new</span> <span class="SpellE"><span style="color: #2b91af;">ManualResetEvent</span></span>(<span style="color: blue;">true</span>);<o:p></o:p></span></p>
<p style="line-height: normal; margin-bottom: 0pt;" class="MsoNormal"><span style="font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span class="SpellE"><span class="GramE">postWorkBlock</span></span> = <span style="color: blue;">new</span> <span class="SpellE"><span style="color: #2b91af;">ManualResetEvent</span></span>(<span style="color: blue;">false</span>);<o:p></o:p></span></p>
<p style="line-height: normal; margin-bottom: 0pt;" class="MsoNormal"><span style="font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: 'Times New Roman';">&nbsp;<o:p></o:p></span></p>
<p style="line-height: normal; margin-bottom: 0pt;" class="MsoNormal"><span style="font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: green;">// Insert other code here that should get executed before <span class="SpellE">ThreadSetup</span> and <span class="SpellE">IterationRun</span></span><o:p></o:p></span></p>
<p style="line-height: normal; margin-bottom: 0pt;" class="MsoNormal"><span style="font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: green;">// . . .</span><o:p></o:p></span></p>
<p style="line-height: normal; margin-bottom: 0pt;" class="MsoNormal"><span style="font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>}<o:p></o:p></span></p>
<p style="line-height: normal; margin-bottom: 0pt;" class="MsoNormal"><span style="font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: 'Times New Roman';">&nbsp;<o:p></o:p></span></p>
<p style="line-height: normal; margin-bottom: 0pt;" class="MsoNormal"><span style="font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: green;">// <span class="GramE">This</span> method is called by each thread.<span style="mso-spacerun: yes;">&nbsp; </span>After all threads call this, they start executing <span class="SpellE">IterationRun</span></span><o:p></o:p></span></p>
<p style="line-height: normal; margin-bottom: 0pt;" class="MsoNormal"><span style="font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span class="GramE"><span style="color: blue;">public</span></span> <span style="color: blue;">void</span> <span class="SpellE">ThreadSetup</span>()<o:p></o:p></span></p>
<p style="line-height: normal; margin-bottom: 0pt;" class="MsoNormal"><span style="font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>{<o:p></o:p></span></p>
<p style="line-height: normal; margin-bottom: 0pt;" class="MsoNormal"><span style="font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span class="SpellE"><span style="color: blue;">this</span>.threadId</span> = <span class="SpellE"><span class="GramE"><span style="color: #2b91af;">Interlocked</span>.Increment</span></span><span class="GramE">(</span><span style="color: blue;">ref</span> <span class="SpellE">threadCount</span>);<o:p></o:p></span></p>
<p style="line-height: normal; margin-bottom: 0pt;" class="MsoNormal"><span style="font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>}<o:p></o:p></span></p>
<p style="line-height: normal; margin-bottom: 0pt;" class="MsoNormal"><span style="font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: 'Times New Roman';">&nbsp;<o:p></o:p></span></p>
<p style="line-height: normal; margin-bottom: 0pt;" class="MsoNormal"><span style="font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">&nbsp;&nbsp; </span><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="GramE"><span style="color: blue;">public</span></span> <span style="color: blue;">void</span> <span class="SpellE">IterationRun</span>()<o:p></o:p></span></p>
<p style="line-height: normal; margin-bottom: 0pt;" class="MsoNormal"><span style="font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>{<o:p></o:p></span></p>
<p style="line-height: normal; margin-bottom: 0pt;" class="MsoNormal"><span style="font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: green;">// Insert code here that all threads should execute.</span><o:p></o:p></span></p>
<p style="line-height: normal; margin-bottom: 0pt;" class="MsoNormal"><span style="font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: green;">// . . .</span><o:p></o:p></span></p>
<p style="line-height: normal; margin-bottom: 0pt;" class="MsoNormal"><span style="font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: 'Times New Roman';">&nbsp;<o:p></o:p></span></p>
<p style="line-height: normal; margin-bottom: 0pt;" class="MsoNormal"><span style="font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span class="SpellE"><span class="GramE"><span style="color: blue;">this</span>.DoWorkOnASingleThread</span></span><span class="GramE">(</span><span style="color: blue;">new</span> <span style="color: #2b91af;">Action</span>(<span class="SpellE"><span style="color: blue;">this</span>.DoWork</span>));<o:p></o:p></span></p>
<p style="line-height: normal; margin-bottom: 0pt;" class="MsoNormal"><span style="font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: 'Times New Roman';">&nbsp;<o:p></o:p></span></p>
<p style="line-height: normal; margin-bottom: 0pt;" class="MsoNormal"><span style="font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: green;">// Insert code here that all threads should execute.</span><o:p></o:p></span></p>
<p style="line-height: normal; margin-bottom: 0pt;" class="MsoNormal"><span style="font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: green;">// . . .</span><o:p></o:p></span></p>
<p style="line-height: normal; margin-bottom: 0pt;" class="MsoNormal"><span style="font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>}<o:p></o:p></span></p>
<p style="line-height: normal; margin-bottom: 0pt;" class="MsoNormal"><span style="font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: 'Times New Roman';">&nbsp;<o:p></o:p></span></p>
<p style="line-height: normal; margin-bottom: 0pt;" class="MsoNormal"><span style="font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span class="GramE"><span style="color: blue;">private</span></span> <span style="color: blue;">void</span> <span class="SpellE">DoWork</span>()<o:p></o:p></span></p>
<p style="line-height: normal; margin-bottom: 0pt;" class="MsoNormal"><span style="font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>{<o:p></o:p></span></p>
<p style="line-height: normal; margin-bottom: 0pt;" class="MsoNormal"><span style="font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: green;">// Do something here</span><o:p></o:p></span></p>
<p style="line-height: normal; margin-bottom: 0pt;" class="MsoNormal"><span style="font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>}<o:p></o:p></span></p>
<p style="line-height: normal; margin-bottom: 0pt;" class="MsoNormal"><span style="font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: 'Times New Roman';">&nbsp;<o:p></o:p></span></p>
<p style="line-height: normal; margin-bottom: 0pt;" class="MsoNormal"><span style="font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: green;">// Takes care of all synchronization necessary to make sure a single thread does the action while blocking all other threads.</span><o:p></o:p></span></p>
<p style="line-height: normal; margin-bottom: 0pt;" class="MsoNormal"><span style="font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span class="GramE"><span style="color: blue;">private</span></span> <span style="color: blue;">void</span> <span class="SpellE">DoWorkOnASingleThread</span>(<span style="color: #2b91af;">Action</span> work)<o:p></o:p></span></p>
<p style="line-height: normal; margin-bottom: 0pt;" class="MsoNormal"><span style="font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>{<o:p></o:p></span></p>
<p style="line-height: normal; margin-bottom: 0pt;" class="MsoNormal"><span style="font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span class="SpellE"><span class="GramE">preWorkBlock.WaitOne</span></span><span class="GramE">(</span>);<o:p></o:p></span></p>
<p style="line-height: normal; margin-bottom: 0pt;" class="MsoNormal"><span style="font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span class="GramE"><span style="color: blue;">lock</span></span> (<span class="SpellE">thisLock</span>)<o:p></o:p></span></p>
<p style="line-height: normal; margin-bottom: 0pt;" class="MsoNormal"><span style="font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>{<o:p></o:p></span></p>
<p style="line-height: normal; margin-bottom: 0pt;" class="MsoNormal"><span style="font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span class="GramE"><span style="color: blue;">if</span></span> (++<span class="SpellE">runningIterationCount</span> == <span class="SpellE"><span style="color: blue;">this</span>.threads</span>)<o:p></o:p></span></p>
<p style="line-height: normal; margin-bottom: 0pt;" class="MsoNormal"><span style="font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>{<o:p></o:p></span></p>
<p style="line-height: normal; margin-bottom: 0pt;" class="MsoNormal"><span style="font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span class="SpellE"><span class="GramE">runningIterationCount</span></span> = 0;<o:p></o:p></span></p>
<p style="line-height: normal; margin-bottom: 0pt;" class="MsoNormal"><span style="font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: 'Times New Roman';">&nbsp;<o:p></o:p></span></p>
<p style="line-height: normal; margin-bottom: 0pt;" class="MsoNormal"><span style="font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: green;">// Single thread can take action here.</span><o:p></o:p></span></p>
<p style="line-height: normal; margin-bottom: 0pt;" class="MsoNormal"><span style="font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span class="SpellE"><span class="GramE">work.Invoke</span></span><span class="GramE">(</span>);<o:p></o:p></span></p>
<p style="line-height: normal; margin-bottom: 0pt;" class="MsoNormal"><span style="font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: 'Times New Roman';">&nbsp;<o:p></o:p></span></p>
<p style="line-height: normal; margin-bottom: 0pt;" class="MsoNormal"><span style="font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span class="SpellE"><span class="GramE">preWorkBlock.Reset</span></span><span class="GramE">(</span>);<o:p></o:p></span></p>
<p style="line-height: normal; margin-bottom: 0pt;" class="MsoNormal"><span style="font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span class="SpellE"><span class="GramE">postWorkBlock.Set</span></span><span class="GramE">(</span>);<o:p></o:p></span></p>
<p style="line-height: normal; margin-bottom: 0pt;" class="MsoNormal"><span style="font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>}<o:p></o:p></span></p>
<p style="line-height: normal; margin-bottom: 0pt;" class="MsoNormal"><span style="font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;</span>}<o:p></o:p></span></p>
<p style="line-height: normal; margin-bottom: 0pt;" class="MsoNormal"><span style="font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: 'Times New Roman';">&nbsp;<o:p></o:p></span></p>
<p style="line-height: normal; margin-bottom: 0pt;" class="MsoNormal"><span style="font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span class="SpellE"><span class="GramE">postWorkBlock.WaitOne</span></span><span class="GramE">(</span>);<o:p></o:p></span></p>
<p style="line-height: normal; margin-bottom: 0pt;" class="MsoNormal"><span style="font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: 'Times New Roman';">&nbsp;<o:p></o:p></span></p>
<p style="line-height: normal; margin-bottom: 0pt;" class="MsoNormal"><span style="font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span class="GramE"><span style="color: blue;">lock</span></span> (<span class="SpellE">thisLock</span>)<o:p></o:p></span></p>
<p style="line-height: normal; margin-bottom: 0pt;" class="MsoNormal"><span style="font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>{<o:p></o:p></span></p>
<p style="line-height: normal; margin-bottom: 0pt;" class="MsoNormal"><span style="font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span class="GramE"><span style="color: blue;">if</span></span> (++<span class="SpellE">runningIterationCount</span> == <span class="SpellE"><span style="color: blue;">this</span>.threads</span>)<o:p></o:p></span></p>
<p style="line-height: normal; margin-bottom: 0pt;" class="MsoNormal"><span style="font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>{<o:p></o:p></span></p>
<p style="line-height: normal; margin-bottom: 0pt;" class="MsoNormal"><span style="font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span class="SpellE"><span class="GramE">runningIterationCount</span></span> = 0;<o:p></o:p></span></p>
<p style="line-height: normal; margin-bottom: 0pt;" class="MsoNormal"><span style="font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: 'Times New Roman';">&nbsp;<o:p></o:p></span></p>
<p style="line-height: normal; margin-bottom: 0pt;" class="MsoNormal"><span style="font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span class="SpellE"><span class="GramE">postWorkBlock.Reset</span></span><span class="GramE">(</span>);<o:p></o:p></span></p>
<p style="line-height: normal; margin-bottom: 0pt;" class="MsoNormal"><span style="font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span class="SpellE"><span class="GramE">preWorkBlock.Set</span></span><span class="GramE">(</span>);<o:p></o:p></span></p>
<p style="line-height: normal; margin-bottom: 0pt;" class="MsoNormal"><span style="font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>}<o:p></o:p></span></p>
<p style="line-height: normal; margin-bottom: 0pt;" class="MsoNormal"><span style="font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>}<o:p></o:p></span></p>
<p style="line-height: normal; margin-bottom: 0pt;" class="MsoNormal"><span style="font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>}<o:p></o:p></span></p>
<p style="line-height: normal; margin-bottom: 0pt;" class="MsoNormal"><span style="font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: 'Times New Roman';"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp; </span>}<o:p></o:p></span></p>
<p style="line-height: normal; margin-bottom: 0pt;" class="MsoNormal"><span style="font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: 'Times New Roman';">}<o:p></o:p></span></p>
<p style="line-height: normal; margin-bottom: 0pt;" class="MsoNormal"><span style="mso-fareast-font-family: 'Times New Roman'; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri; mso-bidi-font-family: Calibri;">&nbsp;<o:p></o:p></span></p>
<p style="line-height: normal; margin-bottom: 0pt;" class="MsoNormal"><span style="mso-fareast-font-family: 'Times New Roman'; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri; mso-bidi-font-family: Calibri;">Note - Keep in mind, this pattern is intended for use in a stress testing environment.<span style="mso-spacerun: yes;">&nbsp; </span>I don't really see much use for it outside of that, but if you know of a production scenario where this is useful, I'd be interested to hear about it!<o:p></o:p></span></p>
<p class="MsoNormal"><o:p>&nbsp;</o:p></p>
</div>]]></content:encoded>
    </item>
    <item>
      <title>Streaming with WCF - Sidebar: What is really going on with my stream?</title>
      <link>https://docs.microsoft.com/archive/blogs/james_osbornes_blog/streaming-with-wcf-sidebar-what-is-really-going-on-with-my-stream</link>
      <pubDate>Fri, 17 Jun 2011 11:01:00 GMT</pubDate>
      <dc:creator><![CDATA[James Osborne-MSFT]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/james_osbornes_blog/2011/06/17/streaming-with-wcf-sidebar-what-is-really-going-on-with-my-stream/</guid>
      <description><![CDATA[Streaming with WCF&nbsp;
Sidebar - What is really going on with my stream?
This posting might not...]]></description>
      <content:encoded><![CDATA[<p style="margin: 0in; font-family: Calibri; color: #366092; font-size: 13pt;"><span style="font-weight: bold;">Streaming with WCF</span>&nbsp;</p>
<p style="margin: 0in; font-family: Calibri; color: #366092; font-size: 11pt;"><span style="font-weight: bold;">Sidebar - What is really going on with my stream?</span></p>
<p style="margin: 0in; font-family: Calibri; font-size: 11pt;">This posting might not directly help you implement something, but it should give some insight as to what's going on under the hood with a stream sent to a client from a service.</p>
<p style="margin: 0in; font-family: Calibri; font-size: 11pt;">&nbsp;</p>
<p style="margin: 0in; font-family: Calibri; color: #366092; font-size: 11pt;"><span style="font-style: italic; font-weight: bold;">Scenario</span></p>
<p style="margin: 0in; font-family: Calibri; font-size: 11pt;">There's a WCF service with an operation which returns a Stream.</p>
<p style="margin: 0in; font-family: Calibri; font-size: 11pt;">A WCF Client invokes this operation.</p>
<p style="margin: 0in; font-family: Calibri; font-size: 11pt;">The Transfer Mode is Streamed.</p>
<p style="margin: 0in; font-family: Calibri; font-size: 11pt;">&nbsp;</p>
<p style="margin: 0in; font-family: Calibri; font-size: 11pt;">So, here's what happens, diagram first:</p>
<p style="margin: 0in; font-family: Calibri; font-size: 11pt;">&nbsp;</p>
<p style="margin: 0in; font-family: Calibri; font-size: 11pt;">&nbsp;</p>
<p style="margin: 0in;"><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/39/69/1781.StreamFromService.jpg" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-39-69/1781.StreamFromService.jpg"><img src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/39/69/1781.StreamFromService.jpg" original-url="https://blogs.msdn.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-01-39-69/1781.StreamFromService.jpg" border="0" /></a></p>
<p style="margin: 0in; font-family: Calibri; font-size: 11pt;">&nbsp;</p>
<p style="margin: 0in; font-family: Calibri; font-size: 11pt;">To elaborate:</p>
<ol type="1" style="font-style: normal; margin-top: 0in; unicode-bidi: embed; font-family: Calibri; direction: ltr; margin-bottom: 0in; margin-left: 0.375in; font-size: 11pt; font-weight: normal;">
<li value="1" style="margin-top: 0px; margin-bottom: 0px; vertical-align: middle;"><span style="font-style: normal; font-family: Calibri; font-size: 11pt; font-weight: normal;">The client call is received by the service.<span style="mso-spacerun: yes;">&nbsp; </span>The service creates some custom stream in the operation and returns it.</span></li>
</ol><ol type="1" style="font-style: normal; margin-top: 0in; unicode-bidi: embed; font-family: Calibri; direction: ltr; margin-bottom: 0in; margin-left: 0.375in; font-size: 11pt; font-weight: normal;">
<li value="2" style="margin-top: 0px; margin-bottom: 0px; vertical-align: middle;"><span style="font-style: normal; font-family: Calibri; font-size: 11pt; font-weight: normal;">Because transfer mode is Streamed, the Encoder gets a wire stream handed to it from the transport layer.<span style="mso-spacerun: yes;">&nbsp; </span></span></li>
<li style="margin-top: 0px; margin-bottom: 0px; vertical-align: middle;"><span style="font-family: Calibri; font-size: 11pt;">Using the wire stream and the stream handed from the service (which I'll call CustomStream from here on), the encoder then begins to create a message.<span style="mso-spacerun: yes;">&nbsp; </span>It starts with all the Soap stuff, but when it comes to the body, it starts the streamed transfer.<span style="mso-spacerun: yes;">&nbsp; </span>It calls a method which takes the two streams.<span style="mso-spacerun: yes;">&nbsp; </span>This method basically gets a buffer from the wire stream and fills it up with bytes from the CustomStream.<span style="mso-spacerun: yes;">&nbsp; </span>Then it calls Write on the wire stream, passing the buffer.<span style="mso-spacerun: yes;">&nbsp; </span>This is what sends the bytes out on the wire.<span style="mso-spacerun: yes;">&nbsp; </span>If the Read call on the CustomStream returned exactly the same value as that buffer, then we know there are more bytes to write.<span style="mso-spacerun: yes;">&nbsp; </span>To make things more efficient, subsequent Read calls get a larger buffer allocated.</span></li>
</ol>
<p style="margin: 0in 0in 0in 0.375in; font-family: Calibri; font-size: 11pt;">&nbsp;</p>
<p style="margin: 0in; font-family: Calibri; color: #366092; font-size: 11pt;"><span style="font-style: italic; font-weight: bold;">What if I used TransferMode.Buffered?</span></p>
<p style="margin: 0in; font-family: Calibri; font-size: 11pt;">Instead of the Transport giving the WireStream to the encoder, the encoder will simply read the entire<span style="mso-spacerun: yes;">&nbsp; </span>CustomStream, package it up in a single message, and hand that message off to the transport which will then send it on the wire.<span style="mso-spacerun: yes;">&nbsp; </span>If your implementation is actually returning very small streams with many client calls, this might actually be optimal.<span style="mso-spacerun: yes;">&nbsp; </span>Otherwise, TransferMode.Streamed is the better option.</p>
<p>&nbsp;</p>
<div class="mcePaste" id="_mcePaste" style="position: absolute; width: 1px; height: 1px; overflow: hidden; top: 0px; left: -10000px;">﻿</div>]]></content:encoded>
    </item>
    <item>
      <title>Streaming with WCF - Part 2: Plugging a custom stream into a WCF service</title>
      <link>https://docs.microsoft.com/archive/blogs/james_osbornes_blog/streaming-with-wcf-part-2-plugging-a-custom-stream-into-a-wcf-service</link>
      <pubDate>Fri, 17 Jun 2011 10:55:46 GMT</pubDate>
      <dc:creator><![CDATA[James Osborne-MSFT]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/james_osbornes_blog/2011/06/17/streaming-with-wcf-part-2-plugging-a-custom-stream-into-a-wcf-service/</guid>
      <description><![CDATA[﻿
Last time, I introduced this topic and gave an example of a custom stream implementation.&nbsp;...]]></description>
      <content:encoded><![CDATA[<div class="mcePaste" id="_mcePaste" style="position: absolute; width: 1px; height: 1px; overflow: hidden; top: 0px; left: -10000px;">﻿</div>
<p style="margin: 0in 0in 0pt;"><span style="font-family: 'Calibri','sans-serif'; font-size: 11pt;">Last time, I introduced this topic and gave an example of a custom stream implementation.<span style="mso-spacerun: yes;">&nbsp; </span>This time, I'm going to make a few enhancements to this implementation and get into the topic of using it in a WCF Service.<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: 'Calibri','sans-serif'; font-size: 11pt;">&nbsp;<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><b><span style="font-family: 'Calibri','sans-serif'; color: #366092; font-size: 13pt;">Streaming with WCF</span></b><span style="font-family: 'Calibri','sans-serif'; color: #366092; font-size: 13pt;"><o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><b><span style="font-family: 'Calibri','sans-serif'; color: #366092; font-size: 11pt;">Part 2: Plugging a custom stream into a WCF service</span></b><span style="font-family: 'Calibri','sans-serif'; color: #366092; font-size: 11pt;"><o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: 'Calibri','sans-serif'; font-size: 11pt;">So, I've made a couple modifications to the CustomStreamImplementation.<span style="mso-spacerun: yes;">&nbsp; </span>I added a couple knobs to control when it stops streaming.<span style="mso-spacerun: yes;">&nbsp; </span>Here's the complete code:<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; color: blue; font-size: 9.5pt;">using</span><span style="font-family: Consolas; font-size: 9.5pt;"> System;<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; color: blue; font-size: 9.5pt;">using</span><span style="font-family: Consolas; font-size: 9.5pt;"> System.IO;<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; color: blue; font-size: 9.5pt;">using</span><span style="font-family: Consolas; font-size: 9.5pt;"> System.Threading;<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;">&nbsp;<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; color: blue; font-size: 9.5pt;">namespace</span><span style="font-family: Consolas; font-size: 9.5pt;"> CustomStreamImplementation<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;">{<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp; </span><span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">ContinuousStream</span> : <span style="color: #2b91af;">Stream</span><o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp; </span>{<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: green;">// Used to control when Read will return 0.</span><o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: blue;">public</span> <span style="color: blue;">bool</span> StopStreaming { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;">&nbsp;<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: blue;">public</span> <span style="color: #2b91af;">TimeSpan</span> ReadThrottle { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;">&nbsp;<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: green;">// Only set this if you don't want to manually control when </span><o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: green;">// the stream stops.</span><o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: green;">// Keep it low - less than 1 second.<span style="mso-spacerun: yes;">&nbsp; </span>The server can send bytes very quickly (without a throttle), so</span><o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: green;">// sending a continuous stream will easily blow the MaxReceivedMessageSize buffer.</span><o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: blue;">public</span> <span style="color: #2b91af;">TimeSpan</span> StreamDuration { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;">&nbsp;<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: #2b91af;">DateTime</span> readStartedTime;<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: blue;">long</span> totalBytesRead = 0;<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;">&nbsp;<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: blue;">public</span> <span style="color: blue;">override</span> <span style="color: blue;">bool</span> CanRead<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>{<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: blue;">get</span> { <span style="color: blue;">return</span> !StopStreaming; }<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>}<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;">&nbsp;<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: blue;">public</span> <span style="color: blue;">override</span> <span style="color: blue;">bool</span> CanSeek<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>{<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: blue;">get</span> { <span style="color: blue;">return</span> <span style="color: blue;">false</span>; }<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>}<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;">&nbsp;<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: blue;">public</span> <span style="color: blue;">override</span> <span style="color: blue;">bool</span> CanWrite<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>{<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: blue;">get</span> { <span style="color: blue;">return</span> <span style="color: blue;">false</span>; }<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>}<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;">&nbsp;<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: blue;">public</span> <span style="color: blue;">override</span> <span style="color: blue;">long</span> Length<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>{<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: blue;">get</span> { <span style="color: blue;">throw</span> <span style="color: blue;">new</span> <span style="color: #2b91af;">NotImplementedException</span>(); }<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>}<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;">&nbsp;<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: blue;">public</span> <span style="color: blue;">override</span> <span style="color: blue;">long</span> Position<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>{<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: blue;">get</span><o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>{<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: blue;">return</span> totalBytesRead;<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>}<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: blue;">set</span><o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>{<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>totalBytesRead = <span style="color: blue;">value</span>;<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>}<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>}<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;">&nbsp;<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: blue;">public</span> <span style="color: blue;">override</span> <span style="color: blue;">void</span> Flush()<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>{<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>}<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;">&nbsp;<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: blue;">public</span> <span style="color: blue;">override</span> <span style="color: blue;">int</span> Read(<span style="color: blue;">byte</span>[] buffer, <span style="color: blue;">int</span> offset, <span style="color: blue;">int</span> count)<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>{<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: green;">// Duration-based streaming logic: Control the "StopStreaming" flag based on a Duration</span><o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: blue;">if</span> (StreamDuration != <span style="color: #2b91af;">TimeSpan</span>.Zero)<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>{<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: blue;">if</span> (readStartedTime == <span style="color: #2b91af;">DateTime</span>.MinValue)<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>{<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>readStartedTime = <span style="color: #2b91af;">DateTime</span>.Now;<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>}<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: blue;">if</span> (<span style="color: #2b91af;">DateTime</span>.Now - readStartedTime &gt;= StreamDuration)<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>{<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>StopStreaming = <span style="color: blue;">true</span>;<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>}<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp; </span><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>}<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;">&nbsp;<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: blue;">if</span> (StopStreaming)<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>{<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>buffer[offset] = 0;<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: blue;">return</span> 0;<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>}<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;">&nbsp;<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: green;">// Allow Read to continue as long as StopStreaming is false.</span><o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: green;">// Just fill buffer with as many random bytes as necessary.</span><o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: blue;">int</span> seed = <span style="color: #2b91af;">DateTime</span>.Now.Millisecond;<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>Random rand = new Random(seed);<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>byte[] randomBuffer = new byte[count];<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>rand.NextBytes(randomBuffer);<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;">&nbsp;<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>randomBuffer.CopyTo(buffer, offset);<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>totalBytesRead += count;<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;">&nbsp;<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: blue;">if</span> (ReadThrottle != <span style="color: #2b91af;">TimeSpan</span>.Zero)<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>{<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: #2b91af;">Thread</span>.CurrentThread.Join(ReadThrottle);<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>}<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: blue;">return</span> count;<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>}<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;">&nbsp;<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: blue;">public</span> <span style="color: blue;">override</span> <span style="color: blue;">long</span> Seek(<span style="color: blue;">long</span> offset, <span style="color: #2b91af;">SeekOrigin</span> origin)<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>{<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: blue;">throw</span> <span style="color: blue;">new</span> <span style="color: #2b91af;">NotImplementedException</span>();<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>}<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;">&nbsp;<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: blue;">public</span> <span style="color: blue;">override</span> <span style="color: blue;">void</span> SetLength(<span style="color: blue;">long</span> value)<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>{<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: blue;">throw</span> <span style="color: blue;">new</span> <span style="color: #2b91af;">NotImplementedException</span>();<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>}<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;">&nbsp;<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: blue;">public</span> <span style="color: blue;">override</span> <span style="color: blue;">void</span> Write(<span style="color: blue;">byte</span>[] buffer, <span style="color: blue;">int</span> offset, <span style="color: blue;">int</span> count)<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>{<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: blue;">throw</span> <span style="color: blue;">new</span> <span style="color: #2b91af;">NotImplementedException</span>();<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>}<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp; </span>}<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;">}<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: 'Calibri','sans-serif'; font-size: 11pt;">&nbsp;<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><b><span style="font-family: 'Calibri','sans-serif'; color: #366092; font-size: 11pt;">Downloading the stream from the service</span></b><span style="font-family: 'Calibri','sans-serif'; color: #366092; font-size: 11pt;"><o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: 'Calibri','sans-serif'; font-size: 11pt;">First, I'll create the WCF service.<span style="mso-spacerun: yes;">&nbsp; </span>I'll make it selfhosted to simplify things a bit and give me full control of what code I add.<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: 'Calibri','sans-serif'; font-size: 11pt;">The service is a new Console Application in my solution.<span style="mso-spacerun: yes;">&nbsp; </span>I added a reference to the CustomStreamImplementation project.<span style="mso-spacerun: yes;">&nbsp; </span>The first time I ever created a WCF service that was supposed to return a custom stream, I thought I'd end up having a contract that returned something of type "CustomStream."<span style="mso-spacerun: yes;">&nbsp; </span>But then I was trying to figure out how to stream something from within the service operation, yet still return from the operation with the stream still going.<span style="mso-spacerun: yes;">&nbsp; </span>It just didn't make sense, until I asked around and figured out what really happens.<span style="mso-spacerun: yes;">&nbsp; </span>Your Stream object is basically passed down to the channel layer, which then invokes Read on the stream when the request comes in to download the stream.<span style="mso-spacerun: yes;">&nbsp; </span>Bytes from the stream are loaded into a buffer and sent on the wire.<span style="mso-spacerun: yes;">&nbsp; </span>As long as Read doesn't return 0, then it continues to create buffers and send them on the wire.<span style="mso-spacerun: yes;">&nbsp; </span>I'll go into high-level details about how this works in a separate post.<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: 'Calibri','sans-serif'; font-size: 11pt;">&nbsp;<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><b><span style="font-family: 'Calibri','sans-serif'; color: #366092; font-size: 11pt;">Service Implementation</span></b><span style="font-family: 'Calibri','sans-serif'; color: #366092; font-size: 11pt;"><o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: 'Calibri','sans-serif'; font-size: 11pt;">I'll just paste the entire code; it's pretty simple.<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: 'Calibri','sans-serif'; font-size: 11pt;">&nbsp;<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: 'Calibri','sans-serif'; font-size: 11pt;">Contract:<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; color: blue; font-size: 9.5pt;">using</span><span style="font-family: Consolas; font-size: 9.5pt;"> System.IO;<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; color: blue; font-size: 9.5pt;">using</span><span style="font-family: Consolas; font-size: 9.5pt;"> System.ServiceModel;<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;">&nbsp;<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; color: blue; font-size: 9.5pt;">namespace</span><span style="font-family: Consolas; font-size: 9.5pt;"> CustomStreamService<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;">{<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp; </span>[<span style="color: #2b91af;">ServiceContract</span>]<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp; </span><span style="color: blue;">public</span> <span style="color: blue;">interface</span> <span style="color: #2b91af;">ICustomStreamService</span><o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp; </span>{<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>[<span style="color: #2b91af;">OperationContract</span>]<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp; </span><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #2b91af;">Stream</span> DownloadStream();<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp; </span>}<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;">}<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: 'Calibri','sans-serif'; font-size: 11pt;">&nbsp;<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: 'Calibri','sans-serif'; font-size: 11pt;">Implementation:<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; color: blue; font-size: 9.5pt;">using</span><span style="font-family: Consolas; font-size: 9.5pt;"> System;<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; color: blue; font-size: 9.5pt;">using</span><span style="font-family: Consolas; font-size: 9.5pt;"> System.IO;<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; color: blue; font-size: 9.5pt;">using</span><span style="font-family: Consolas; font-size: 9.5pt;"> System.ServiceModel;<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; color: blue; font-size: 9.5pt;">using</span><span style="font-family: Consolas; font-size: 9.5pt;"> CustomStreamImplementation;<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;">&nbsp;<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; color: blue; font-size: 9.5pt;">namespace</span><span style="font-family: Consolas; font-size: 9.5pt;"> CustomStreamService<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;">{<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp; </span>[<span style="color: #2b91af;">ServiceBehavior</span>]<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp; </span><span style="color: blue;">class</span> <span style="color: #2b91af;">CustomStreamService</span> : <span style="color: #2b91af;">ICustomStreamService</span><o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp; </span>{<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>[<span style="color: #2b91af;">OperationBehavior</span>]<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: blue;">public</span> <span style="color: #2b91af;">Stream</span> DownloadStream()<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>{<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: #2b91af;">ContinuousStream</span> continuousStream = <span style="color: blue;">new</span> <span style="color: #2b91af;">ContinuousStream</span>();<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>continuousStream.StreamDuration = <span style="color: #2b91af;">TimeSpan</span>.FromSeconds(30);<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;</span>continuousStream.ReadThrottle = <span style="color: #2b91af;">TimeSpan</span>.FromSeconds(1);<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: blue;">return</span> continuousStream;<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>}<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp; </span>}<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;">}<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: 'Calibri','sans-serif'; font-size: 11pt;">&nbsp;<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: 'Calibri','sans-serif'; font-size: 11pt;">Driver - The service host and client code:<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; color: blue; font-size: 9.5pt;">using</span><span style="font-family: Consolas; font-size: 9.5pt;"> System;<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; color: blue; font-size: 9.5pt;">using</span><span style="font-family: Consolas; font-size: 9.5pt;"> System.IO;<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; color: blue; font-size: 9.5pt;">using</span><span style="font-family: Consolas; font-size: 9.5pt;"> System.ServiceModel;<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;">&nbsp;<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; color: blue; font-size: 9.5pt;">namespace</span><span style="font-family: Consolas; font-size: 9.5pt;"> CustomStreamService<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;">{<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp; </span><span style="mso-spacerun: yes;">&nbsp;</span><span style="color: blue;">class</span> <span style="color: #2b91af;">Program</span><o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp; </span>{<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: blue;">static</span> <span style="color: blue;">void</span> Main(<span style="color: blue;">string</span>[] args)<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>{<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: green;">// Service code</span><o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: blue;">string</span> address = <span style="color: #a31515;">"</span><a href="https://localhost/CustomStreamService"><span style="color: #a31515; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin;">https://localhost/CustomStreamService</span></a><span style="color: #a31515;">"</span>;<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: #2b91af;">BasicHttpBinding</span> binding = <span style="color: blue;">new</span> <span style="color: #2b91af;">BasicHttpBinding</span>();<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>binding.TransferMode = <span style="color: #2b91af;">TransferMode</span>.Streamed;<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: #2b91af;">ServiceHost</span> host = <span style="color: blue;">new</span> <span style="color: #2b91af;">ServiceHost</span>(<span style="color: blue;">typeof</span>(<span style="color: #2b91af;">CustomStreamService</span>), <span style="color: blue;">new</span> <span style="color: #2b91af;">Uri</span>(address));<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>host.AddServiceEndpoint(<span style="color: blue;">typeof</span>(<span style="color: #2b91af;">ICustomStreamService</span>), binding, address);<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>host.Open();<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: #2b91af;">Console</span>.WriteLine(<span style="color: #a31515;">"Service is {0}, Press ENTER to continue."</span>, host.State);<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: #2b91af;">Console</span>.ReadLine();<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;">&nbsp;<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: green;">// Client code</span><o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: green;">// For ad-hoc testing, I'll just re-use the binding, address, and contract that's defined in this project.</span><o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: green;">// If I wanted to test the client cross-machine, I would have to either generate it using wsdl and svcutil, or</span><o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: green;">// simply copy-paste the contract (ICustomStreamService) into the client project.<span style="mso-spacerun: yes;">&nbsp; </span>Then make sure the binding and address are correct.</span><o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;">&nbsp;<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: #2b91af;">ChannelFactory</span>&lt;<span style="color: #2b91af;">ICustomStreamService</span>&gt; factory = <span style="color: blue;">new</span> <span style="color: #2b91af;">ChannelFactory</span>&lt;<span style="color: #2b91af;">ICustomStreamService</span>&gt;(binding, address);<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="mso-spacerun: yes;">&nbsp;&nbsp;</span><span style="color: #2b91af;">ICustomStreamService</span> client = factory.CreateChannel();<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: #2b91af;">Stream</span> streamFromService = client.DownloadStream(); <span style="color: green;">// Note that my Client knows nothing about the stream implementation.</span><o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;">&nbsp;<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: #2b91af;">DateTime</span> beginReadTime = <span style="color: #2b91af;">DateTime</span>.Now;<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: #2b91af;">Console</span>.WriteLine(<span style="color: #a31515;">"{0}: starting to read."</span>, beginReadTime);<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: green;">// My oddball way of reading the bytes from the stream and writing them to the console.</span><o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: blue;">int</span> bytesRead;<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: blue;">int</span> bufferSize = 256;<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: blue;">byte</span>[] buffer = <span style="color: blue;">new</span> <span style="color: blue;">byte</span>[bufferSize];<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: blue;">do</span><o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>{<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>bytesRead = streamFromService.Read(buffer, 0, buffer.Length);<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: blue;">foreach</span> (<span style="color: blue;">byte</span> b <span style="color: blue;">in</span> buffer)<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>{<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: #2b91af;">Console</span>.Write(b);<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: #2b91af;">Console</span>.Write(<span style="color: #a31515;">' '</span>);<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>}<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>} <span style="color: blue;">while</span> (bytesRead != 0);<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: #2b91af;">Console</span>.WriteLine(<span style="color: #a31515;">"{0}: done reading.<span style="mso-spacerun: yes;">&nbsp; </span>Elapsed time is {1}"</span>, <span style="color: #2b91af;">DateTime</span>.Now, <span style="color: #2b91af;">DateTime</span>.Now - beginReadTime);<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;">&nbsp;<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: #2b91af;">Console</span>.ReadLine();<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>}<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp; </span>}<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;">}<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: 'Calibri','sans-serif'; font-size: 11pt;">&nbsp;<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><b><span style="font-family: 'Calibri','sans-serif'; color: #366092; font-size: 11pt;">Troubleshooting</span></b><span style="font-family: 'Calibri','sans-serif'; color: #366092; font-size: 11pt;"><o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: 'Calibri','sans-serif'; font-size: 11pt;">If you were to add all this code into a console app and run it, you'd see the client downloading bytes for a few seconds, then the streamFromService.Read call would eventually throw an IOException ("An exception has been thrown when reading the stream.")<span style="mso-spacerun: yes;">&nbsp; </span>Examining the exception shows the following inner exception:<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: 'Calibri','sans-serif'; font-size: 11pt;">"The maximum message size quota for incoming messages (65536) has been exceeded. To increase the quota, use the MaxReceivedMessageSize property on the appropriate binding element."<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><b><i><span style="font-family: 'Calibri','sans-serif'; color: #366092; font-size: 11pt;">But I thought I was Streaming?</span></i></b><span style="font-family: 'Calibri','sans-serif'; color: #366092; font-size: 11pt;"><o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: 'Calibri','sans-serif'; font-size: 11pt;">Don't worry, you still are.<span style="mso-spacerun: yes;">&nbsp; </span>But the MaxReceivedMessageSize is a mechanism for "time-boxing" the reading of the stream.<span style="mso-spacerun: yes;">&nbsp; </span>Generally, you'd want this on a service that reads a stream from a client.<span style="mso-spacerun: yes;">&nbsp; </span>You don't want a client to be able to tie up the service forever with an infinite stream, so this property takes care of that.<span style="mso-spacerun: yes;">&nbsp; </span>Similarly, the property applies for clients.<span style="mso-spacerun: yes;">&nbsp; </span>If you want to read a larger stream, simply do what the exception says.<span style="mso-spacerun: yes;">&nbsp; </span>In this scenario, I'm just going to open the floodgates and let the client read<span style="mso-spacerun: yes;">&nbsp; </span>as long as I can:<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>binding.MaxReceivedMessageSize = <span style="color: blue;">int</span>.MaxValue;<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: 'Calibri','sans-serif'; font-size: 11pt;">&nbsp;<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: 'Calibri','sans-serif'; font-size: 11pt;">With that, my client could read the entire 30 seconds of the stream from the service:<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: 'Calibri','sans-serif'; font-size: 11pt;">17/2011 10:12:51 AM: done reading.<span style="mso-spacerun: yes;">&nbsp; </span>Elapsed time is 00:00:34.6390000<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: 'Calibri','sans-serif'; font-size: 11pt;">&nbsp;<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: 'Calibri','sans-serif'; font-size: 11pt;">If you're wondering about that extra 4.639 seconds of processing time, increasing the buffer size used by the read operation can increase the performance of my ad-hoc client.<span style="mso-spacerun: yes;">&nbsp; </span>But I suggest you play with it - there's certainly an optimal value for this buffer, based on the size of the buffer used by the wire stream on the service.<span style="mso-spacerun: yes;">&nbsp; </span>Set the bufferSize to something large, like 4k, and you'll see what I mean.<span style="mso-spacerun: yes;">&nbsp; </span>Of course, logging each byte has the largest impact on performance.<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: 'Calibri','sans-serif'; font-size: 11pt;">&nbsp;<o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><b><span style="font-family: 'Calibri','sans-serif'; color: #366092; font-size: 11pt;">Still can't get streaming to work?</span></b><span style="font-family: 'Calibri','sans-serif'; color: #366092; font-size: 11pt;"><o:p></o:p></span></p>
<p style="margin: 0in 0in 0pt;"><span style="font-family: 'Calibri','sans-serif'; font-size: 11pt;">Leave me a comment.<span style="mso-spacerun: yes;">&nbsp; </span>I'll try to troubleshoot any issues with streaming and WCF that I can.<span style="mso-spacerun: yes;">&nbsp; </span>I'm considering writing a brief posting about the things in WCF that don't support streaming.<span style="mso-spacerun: yes;">&nbsp; </span>Off the top of my head, I can think of 2: MessageSecurity and Streaming on a Duplex Callback.<span style="mso-spacerun: yes;">&nbsp; </span>There's probably a handful of other streaming-not-supported scenarios.<o:p></o:p></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><o:p><span style="font-family: Calibri; font-size: small;">&nbsp;</span></o:p></p>]]></content:encoded>
    </item>
    <item>
      <title>Streaming with WCF - Part 1: Custom Stream Implementation</title>
      <link>https://docs.microsoft.com/archive/blogs/james_osbornes_blog/streaming-with-wcf-part-1-custom-stream-implementation</link>
      <pubDate>Fri, 10 Jun 2011 10:56:00 GMT</pubDate>
      <dc:creator><![CDATA[James Osborne-MSFT]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/james_osbornes_blog/2011/06/10/streaming-with-wcf-part-1-custom-stream-implementation/</guid>
      <description><![CDATA[Somewhat recently, I've been adding some tests that use streaming with WCF.&nbsp; I'm not going to...]]></description>
      <content:encoded><![CDATA[<p style="margin: 0in; font-family: Calibri; font-size: 11pt;">Somewhat recently, I've been adding some tests that use streaming with WCF.<span style="mso-spacerun: yes;">&nbsp; </span>I'm not going to get into the specifics of my tests, but I will say that, for me, setting up WCF for streaming and creating oddball custom stream implementations wasn't exactly intuitive to me.<span style="mso-spacerun: yes;">&nbsp; </span></p>
<p style="margin: 0in; font-family: Calibri; font-size: 11pt;">After realizing I wasn't the only one stumbling around with this, I decided I should write a series of posts related to streaming in WCF.<span style="mso-spacerun: yes;">&nbsp; </span>So with that, let me start the posts.</p>
<p style="margin: 0in; font-family: Calibri; font-size: 11pt;">&nbsp;</p>
<p style="margin: 0in; font-family: Calibri; color: #366092; font-size: 13pt;"><span style="font-weight: bold;">Streaming with WCF</span></p>
<p style="margin: 0in; font-family: Calibri; color: #366092; font-size: 11pt;"><span style="font-weight: bold;">Part 1: Custom Stream Implementations</span></p>
<p style="margin: 0in; font-family: Calibri; font-size: 11pt;">Before I get into details of WCF and streaming, I first want to give an example of a custom stream implementation.<span style="mso-spacerun: yes;">&nbsp; </span>See, anyone can stream to or from, say, a file with the following code:</p>
<p style="margin: 0in; font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: blue;">public</span> <span style="color: blue;">static</span> <span style="color: blue;">void</span> TestFileStream()</p>
<p style="margin: 0in; font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>{</p>
<p style="margin: 0in; font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: green;">// Writing</span></p>
<p style="margin: 0in; font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: #2b91af;">FileStream</span> fs = <span style="color: blue;">new</span> <span style="color: #2b91af;">FileStream</span>(<span style="color: #a31515;">"C:\\test.txt"</span>, <span style="color: #2b91af;">FileMode</span>.OpenOrCreate);</p>
<p style="margin: 0in; font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: blue;">byte</span>[] bytesToWrite = <span style="color: #2b91af;">Encoding</span>.ASCII.GetBytes(<span style="color: #a31515;">"Yeah, this is the API for writing a string to a file."</span>);</p>
<p style="margin: 0in; font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>fs.Write(bytesToWrite, 0, bytesToWrite.Length);</p>
<p style="margin: 0in; font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>fs.Close();</p>
<p style="margin: 0in; font-family: Consolas; font-size: 9.5pt;">&nbsp;</p>
<p style="margin: 0in; font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: green;">// Reading</span></p>
<p style="margin: 0in; font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>fs = <span style="color: blue;">new</span> <span style="color: #2b91af;">FileStream</span>(<span style="color: #a31515;">"C:\\test.txt"</span>, <span style="color: #2b91af;">FileMode</span>.Open);</p>
<p style="margin: 0in; font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: blue;">byte</span>[] buffer = <span style="color: blue;">new</span> <span style="color: blue;">byte</span>[bytesToWrite.Length];</p>
<p style="margin: 0in; font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>fs.Read(buffer, 0, bytesToWrite.Length);</p>
<p style="margin: 0in; font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: #2b91af;">Console</span>.WriteLine(<span style="color: #2b91af;">Encoding</span>.ASCII.GetString(buffer));</p>
<p style="margin: 0in; font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>}</p>
<p style="margin: 0in; font-family: Calibri; font-size: 11pt;">&nbsp;</p>
<p style="margin: 0in; font-family: Calibri; font-size: 11pt;">"fs" is the stream, obviously.<span style="mso-spacerun: yes;">&nbsp; </span>Which is fine for basic tests, but what if I wanted to send an infinite stream?<span style="mso-spacerun: yes;">&nbsp; </span>What if I wanted to open a stream to the server and start pushing bytes, one at a time, until I hit CTRL + C?<span style="mso-spacerun: yes;">&nbsp; </span>What if someone had a WCF service that would stream data back to the client on the fly?<span style="mso-spacerun: yes;">&nbsp; </span>How do you write a custom stream for that?</p>
<p style="margin: 0in; font-family: Calibri; font-size: 11pt;">&nbsp;</p>
<ol type="1" style="font-style: normal; margin-top: 0in; unicode-bidi: embed; font-family: Calibri; direction: ltr; margin-bottom: 0in; margin-left: 0.375in; font-size: 11pt; font-weight: normal;">
<li value="1" style="margin-top: 0px; margin-bottom: 0px; vertical-align: middle;"><span style="font-style: normal; font-family: Calibri; font-size: 11pt; font-weight: normal;">I start by extending the Stream class:</span></li>
</ol>
<p style="margin: 0in 0in 0in 0.375in; font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp; </span><span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">ContinuousStream</span> : <span style="color: #2b91af;">Stream</span></p>
<p style="margin: 0in 0in 0in 0.375in; font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp; </span>{</p>
<ol type="1" style="font-style: normal; margin-top: 0in; unicode-bidi: embed; font-family: Calibri; direction: ltr; margin-bottom: 0in; margin-left: 0.375in; font-size: 11pt; font-weight: normal;">
<li value="2" style="margin-top: 0px; margin-bottom: 0px; vertical-align: middle;"><span style="font-style: normal; font-family: Calibri; font-size: 11pt; font-weight: normal;">Next, I think about what I want to do.<span style="mso-spacerun: yes;">&nbsp; </span>I want it to be able to be used by a WCF service to write bytes to the wire continually.<span style="mso-spacerun: yes;">&nbsp; </span>How do you override the way a stream writes bytes?<span style="mso-spacerun: yes;">&nbsp; </span>Why, override the Read method, of course:</span></li>
</ol>
<p style="margin: 0in; font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: blue;">public</span> <span style="color: blue;">override</span> <span style="color: blue;">int</span> Read(<span style="color: blue;">byte</span>[] buffer, <span style="color: blue;">int</span> offset, <span style="color: blue;">int</span> count)</p>
<p style="margin: 0in; font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>{</p>
<p style="margin: 0in; font-family: Consolas; font-size: 9.5pt;">&nbsp;</p>
<p style="margin: 0in; font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: green;">// Allow Read to continue forever</span></p>
<p style="margin: 0in; font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: green;">// Just fill buffer with as many random bytes as necessary.</span></p>
<p style="margin: 0in; font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: blue;">int</span> seed = <span style="color: #2b91af;">DateTime</span>.Now.Millisecond;</p>
<p style="margin: 0in; font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: #2b91af;">Random</span> rand = <span style="color: blue;">new</span> <span style="color: #2b91af;">Random</span>(seed);</p>
<p style="margin: 0in; font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: blue;">byte</span>[] randomBuffer = <span style="color: blue;">new</span> <span style="color: blue;">byte</span>[count];</p>
<p style="margin: 0in; font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>rand.NextBytes(randomBuffer);</p>
<p style="margin: 0in; font-family: Consolas; font-size: 9.5pt;">&nbsp;</p>
<p style="margin: 0in; font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>randomBuffer.CopyTo(buffer, offset);</p>
<p style="margin: 0in; font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>totalBytesRead += count;</p>
<p style="margin: 0in; font-family: Consolas; font-size: 9.5pt;">&nbsp;</p>
<p style="margin: 0in; font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: blue;">return</span> count;</p>
<p style="margin: 0in; font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>}</p>
<p style="margin: 0in; font-family: Calibri; font-size: 11pt;">&nbsp;</p>
<p style="margin: 0in; font-family: Calibri; font-size: 11pt;">Now, it's time to give some background information about how WCF sends a stream on the wire.<span style="mso-spacerun: yes;">&nbsp; </span>If I were to use this in a service operation called, "DownloadStream" it would return an object of type Stream and take no parameters.<span style="mso-spacerun: yes;">&nbsp; </span>I would just have to return a new instance of this object.<span style="mso-spacerun: yes;">&nbsp; </span>When the service gets invoked by a client call, assuming it's using streamed transfer mode, WCF will basically hand this object down to the transport which will call the Read method, passing in a buffer that will be sent on the wire.<span style="mso-spacerun: yes;">&nbsp; </span>Assuming Read doesn't return 0, indicating there are no bytes to read, it will continually call Read and send the filled buffers on the wire.<span style="mso-spacerun: yes;">&nbsp; </span></p>
<p style="margin: 0in; font-family: Calibri; font-size: 11pt;">&nbsp;</p>
<p style="margin: 0in; font-family: Calibri; font-size: 11pt;">With that bit of knowledge, the first thing I want to add to the Continuous stream is a throttle to slow down the time between Read calls.<span style="mso-spacerun: yes;">&nbsp; </span>To do this, I add a property to the ContinuousStream class:</p>
<p style="margin: 0in; font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: blue;">public</span> <span style="color: #2b91af;">TimeSpan</span> ReadThrottle { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</p>
<p style="margin: 0in; font-family: Calibri; font-size: 11pt;">And I change the implementation of Read:</p>
<p style="margin: 0in; font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: blue;">public</span> <span style="color: blue;">override</span> <span style="color: blue;">int</span> Read(<span style="color: blue;">byte</span>[] buffer, <span style="color: blue;">int</span> offset, <span style="color: blue;">int</span> count)</p>
<p style="margin: 0in; font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>{</p>
<p style="margin: 0in; font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: green;">// Allow Read to continue forever</span></p>
<p style="margin: 0in; font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: green;">// Just fill buffer with as many random bytes as necessary.</span></p>
<p style="margin: 0in; font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: blue;">int</span> seed = <span style="color: #2b91af;">DateTime</span>.Now.Millisecond;</p>
<p style="margin: 0in; font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: #2b91af;">Random</span> rand = <span style="color: blue;">new</span> <span style="color: #2b91af;">Random</span>(seed);</p>
<p style="margin: 0in; font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: blue;">byte</span>[] randomBuffer = <span style="color: blue;">new</span> <span style="color: blue;">byte</span>[count];</p>
<p style="margin: 0in; font-family: Consolas; font-size: 9.5pt;">&nbsp;</p>
<p style="margin: 0in; font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>randomBuffer.CopyTo(buffer, offset);</p>
<p style="margin: 0in; font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>totalBytesRead += count;</p>
<p style="margin: 0in; font-family: Consolas; font-size: 9.5pt;">&nbsp;</p>
<p style="margin: 0in; font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: blue;">if</span> (ReadThrottle != <span style="color: #2b91af;">TimeSpan</span>.Zero)</p>
<p style="margin: 0in; font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>{</p>
<p style="margin: 0in; font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>Thread.CurrentThread.Join(ReadThrottle);</p>
<p style="margin: 0in; font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>}</p>
<p style="margin: 0in; font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: blue;">return</span> count;</p>
<p style="margin: 0in; font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>}</p>
<p style="margin: 0in; font-family: Calibri; font-size: 11pt;">&nbsp;</p>
<p style="margin: 0in; font-family: Calibri; font-size: 11pt;">How you override the Read method is totally up to you.<span style="mso-spacerun: yes;">&nbsp; </span>You could create a WCF client in here and call a service to get some data which you then put in the buffer somehow.</p>
<p style="margin: 0in; font-family: Calibri; font-size: 11pt;">Next time, I'll show some more minor enhancements to the ContinuousStream and use it in a WCF service.</p>]]></content:encoded>
    </item>
    <item>
      <title>Writing bytes to the wire</title>
      <link>https://docs.microsoft.com/archive/blogs/james_osbornes_blog/writing-bytes-to-the-wire</link>
      <pubDate>Fri, 01 Apr 2011 14:39:00 GMT</pubDate>
      <dc:creator><![CDATA[James Osborne-MSFT]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/james_osbornes_blog/2011/04/01/writing-bytes-to-the-wire/</guid>
      <description><![CDATA[&nbsp;
As a tester in .NET (more specifically, WCF), every now and then, I end up defining a test...]]></description>
      <content:encoded><![CDATA[<p>&nbsp;</p>
<p style="margin: 0in; font-family: Calibri; font-size: 11pt;">As a tester in .NET (more specifically, WCF), every now and then, I end up defining a test requirement that isn't possible to cover with simply using our WCF product code.<span style="mso-spacerun: yes;">&nbsp; </span>For example, in 4.0, I wanted to test hitting a service with an Http request that contained an invalid&nbsp;host header.<span style="mso-spacerun: yes;">&nbsp; </span>Now, the first thing I turned to was the familiar <a title="HttpWebRequest class" href="https://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.aspx">HttpWebRequest</a>.<span style="mso-spacerun: yes;">&nbsp; </span>Poking around at the properties, I couldn't find any way to provida an intentionally incorrect Host header.<span style="mso-spacerun: yes;">&nbsp; </span>It was at that time I realized the only way to send this type of request to my server was to use a raw socket.<span style="mso-spacerun: yes;">&nbsp; </span>Today, I'd like to share that code with you.<span style="mso-spacerun: yes;">&nbsp; </span>The following snippet sends a GET request to Bing and prints the result to the console.<span style="mso-spacerun: yes;">&nbsp; </span>Use your imagination with this on how you can potentially modify the raw request sent on the wire.<span style="mso-spacerun: yes;">&nbsp; </span>Basically, with this snippet, you have absolute control over the bytes you send.</p>
<p style="margin: 0in; font-family: Calibri; font-size: 11pt;">&nbsp;</p>
<p style="margin: 0in; font-family: Consolas; font-size: 9.5pt;"><span style="color: blue;">class</span> <span style="color: #2b91af;">Program</span></p>
<p style="margin: 0in; font-family: Consolas; font-size: 9.5pt;">{</p>
<p style="margin: 0in; font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp; </span><span style="color: blue;">static</span> <span style="color: blue;">void</span> Main(<span style="color: blue;">string</span>[] args)</p>
<p style="margin: 0in; font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp; </span>{</p>
<p style="margin: 0in; font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: green;">// Create the message to be sent on the wire</span></p>
<p style="margin: 0in; font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: green;">// The first header contains the resource to which the request is directed.</span></p>
<p style="margin: 0in; font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: green;">// It's basically just "</span><a href="https://%7bhost%7d/%7bresource%7d"><span style="color: green;">https://{host}/{resource}</span></a><span style="color: green;">".<span style="mso-spacerun: yes;">&nbsp; </span>Here, my resource is "/".</span></p>
<p style="margin: 0in; font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: blue;">string</span> message = <span style="color: #a31515;">"GET </span><a href="https://www.bing.com/"><span style="color: #a31515;">https://www.bing.com/</span></a><span style="color: #a31515;"> HTTP/1.1"</span>;</p>
<p style="margin: 0in; font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>message += <span style="color: #a31515;">"\nAccept: text/html, application/xhtml+xml, */*"</span>;</p>
<p style="margin: 0in; font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>message += <span style="color: #a31515;">"\nAccept-Language: en-US"</span>;</p>
<p style="margin: 0in; font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>message += <span style="color: #a31515;">"\nHost: </span><a href="https://www.bing.com/"><span style="color: #a31515;">www.bing.com</span></a><span style="color: #a31515;">"</span>;</p>
<p style="margin: 0in; font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>message += <span style="color: #a31515;">"\n\n"</span>;</p>
<p style="margin: 0in; font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: green;">// Don't forget those 2 newlines. After that, the message body could be added, but</span></p>
<p style="margin: 0in; font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: green;">// for this example, I'm just sending a GET which isn't allowed to contain a body.</span></p>
<p style="margin: 0in; font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: green;">// (Though I could still add one since I'm accessing the stream directly.&nbsp; You can't do that with Fiddler.)</span></p>
<p style="margin: 0in; font-family: Consolas; font-size: 9.5pt;">&nbsp;</p>
<p style="margin: 0in; font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: green;">// The TcpClient specifies the destination of the request (host) as well as the port.</span></p>
<p style="margin: 0in; font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: #2b91af;">TcpClient</span> client = <span style="color: blue;">new</span> <span style="color: #2b91af;">TcpClient</span>(<span style="color: #a31515;">"www.bing.com"</span>, 80);</p>
<p style="margin: 0in; font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: #2b91af;">NetworkStream</span> networkStream = client.GetStream();</p>
<p style="margin: 0in; font-family: Consolas; font-size: 9.5pt;">&nbsp;</p>
<p style="margin: 0in; font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: #2b91af;">Byte</span>[] data = <span style="color: #2b91af;">Encoding</span>.ASCII.GetBytes(message);</p>
<p style="margin: 0in; font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>networkStream.Write(data, 0, data.Length);</p>
<p style="margin: 0in; font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>networkStream.Flush();</p>
<p style="margin: 0in; font-family: Consolas; font-size: 9.5pt;">&nbsp;</p>
<p style="margin: 0in; font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: blue;">string</span> responseData = <span style="color: blue;">string</span>.Empty;</p>
<p style="margin: 0in; font-family: Consolas; font-size: 9.5pt;">&nbsp;</p>
<p style="margin: 0in; font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: #2b91af;">Int32</span> bytes;</p>
<p style="margin: 0in; font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: blue;">do</span></p>
<p style="margin: 0in; font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>{</p>
<p style="margin: 0in; font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>data = <span style="color: blue;">new</span> <span style="color: #2b91af;">Byte</span>[4]; <span style="color: green;">// Yes, this is reading very small chunks at a time.</span></p>
<p style="margin: 0in; font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>bytes = networkStream.Read(data, 0, data.Length);</p>
<p style="margin: 0in; font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>responseData += <span style="color: #2b91af;">Encoding</span>.UTF8.GetString(data, 0, bytes);</p>
<p style="margin: 0in; font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>} <span style="color: blue;">while</span> (networkStream.DataAvailable);</p>
<p style="margin: 0in; font-family: Consolas; font-size: 9.5pt;">&nbsp;</p>
<p style="margin: 0in; font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: green;">// Now we have the response, in a string.</span></p>
<p style="margin: 0in; font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: #2b91af;">Console</span>.WriteLine(responseData);</p>
<p style="margin: 0in; font-family: Consolas; font-size: 9.5pt;">&nbsp;</p>
<p style="margin: 0in; font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: #2b91af;">Console</span>.ReadLine();</p>
<p style="margin: 0in; font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp; </span>}</p>
<p style="margin: 0in; font-family: Consolas; font-size: 9.5pt;">}</p>]]></content:encoded>
    </item>
    <item>
      <title>Webhosting a WCF service over Https (with your own Certificates)</title>
      <link>https://docs.microsoft.com/archive/blogs/james_osbornes_blog/webhosting-a-wcf-service-over-https-with-your-own-certificates</link>
      <pubDate>Fri, 25 Feb 2011 17:52:26 GMT</pubDate>
      <dc:creator><![CDATA[James Osborne-MSFT]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/james_osbornes_blog/2011/02/25/webhosting-a-wcf-service-over-https-with-your-own-certificates/</guid>
      <description><![CDATA[&nbsp;
Disclaimer: The information contained in this posting is not intended for production...]]></description>
      <content:encoded><![CDATA[<p>&nbsp;</p>
<p style="margin: 0in; font-family: Calibri; font-size: 11pt;">Disclaimer: The information contained in this posting is not intended for production environments.<span style="mso-spacerun: yes;">&nbsp; </span>But it should effectively mimic a production environment.</p>
<p style="margin: 0in; font-family: Calibri; font-size: 11pt;">&nbsp;</p>
<p style="margin: 0in; font-family: Calibri; font-size: 11pt;">A while back, I posted a <a target="_blank" href="https://blogs.msdn.com/b/james_osbornes_blog/archive/2010/12/10/selfhosting-a-wcf-service-over-https.aspx" title="Selfhosting a WCF service over https">lengthy how-to for setting up a selfhosted WCF service that uses Transport level security (https.)</a><span style="mso-spacerun: yes;">&nbsp; </span>I found that posting useful, when trying to remember how to set that up, but recently I was helping a colleague set up a <span style="font-style: italic;">webhosted</span> service using https.<span style="mso-spacerun: yes;">&nbsp; </span>Now, for most cases, testing this with your own self-signed certificate works fine, but if you ever try browsing to the service cross-machine, you'll notice the certificate isn't trusted.<span style="mso-spacerun: yes;">&nbsp; </span>In this posting, I'll review how to create the chain-signed certificate and how to export the trusted root certificate to a "client" machine.</p>
<p style="margin: 0in; font-family: Calibri; font-size: 11pt;">&nbsp;</p>
<p style="margin: 0in; font-family: Calibri; color: #366092; font-size: 13pt;"><span style="font-weight: bold;">Setting up the service</span></p>
<p style="margin: 0in; font-family: Calibri; font-size: 11pt;">For reference, I'll be using the service implementation in the "Selfhosting a WCF service over Https" posting.</p>
<p style="margin: 0in; font-family: Calibri; font-size: 11pt;">The difference is that I've changed the project type to a class library and added a HelloWorldService.svc file with the following content:</p>
<p style="margin: 0in; font-family: Consolas; font-size: 9.5pt;"><span style="background: yellow; mso-highlight: yellow;">&lt;%</span><span style="color: blue;">@</span> <span style="color: maroon;">ServiceHost</span><span style="mso-spacerun: yes;">&nbsp; </span><span style="color: red;">Debug</span><span style="color: blue;">="true"</span> <span style="color: red;">Service</span><span style="color: blue;">="HelloWorldWebhost.HelloWorldService"</span> <span style="color: red;">Language</span><span style="color: blue;">="C#"</span> <span style="color: red;">Factory</span><span style="color: blue;">="System.ServiceModel.Activation.ServiceHostFactory"</span> <span style="background: yellow; mso-highlight: yellow;">%&gt;</span></p>
<p style="margin: 0in; font-family: Consolas; font-size: 9.5pt;">&nbsp;</p>
<p style="margin: 0in; font-family: Calibri; font-size: 11pt;">And I added a Web.Config with the following content:</p>
<p style="margin: 0in; font-family: Consolas; font-size: 9.5pt;"><span style="color: blue;">&lt;</span><span style="color: #a31515;">configuration</span><span style="color: blue;">&gt;</span></p>
<p style="margin: 0in; font-family: Consolas; font-size: 9.5pt;"><span style="color: blue;"><span style="mso-spacerun: yes;">&nbsp; </span>&lt;</span><span style="color: #a31515;">system.serviceModel</span><span style="color: blue;">&gt;</span></p>
<p style="margin: 0in; font-family: Consolas; font-size: 9.5pt;"><span style="color: blue;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp; </span>&lt;</span><span style="color: #a31515;">bindings</span><span style="color: blue;">&gt;</span></p>
<p style="margin: 0in; font-family: Consolas; font-size: 9.5pt;"><span style="color: blue;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>&lt;</span><span style="color: #a31515;">basicHttpBinding</span><span style="color: blue;">&gt;</span></p>
<p style="margin: 0in; font-family: Consolas; font-size: 9.5pt;"><span style="color: blue;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>&lt;</span><span style="color: #a31515;">binding</span><span style="color: blue;"> </span><span style="color: red;">name</span><span style="color: blue;">=</span>"<span style="color: blue;">MyBinding</span>"<span style="color: blue;">/&gt;</span></p>
<p style="margin: 0in; font-family: Consolas; font-size: 9.5pt;"><span style="color: blue;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>&lt;/</span><span style="color: #a31515;">basicHttpBinding</span><span style="color: blue;">&gt;</span></p>
<p style="margin: 0in; font-family: Consolas; font-size: 9.5pt;"><span style="color: blue;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp; </span>&lt;/</span><span style="color: #a31515;">bindings</span><span style="color: blue;">&gt;</span></p>
<p style="margin: 0in; font-family: Consolas; font-size: 9.5pt;"><span style="color: blue;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp; </span>&lt;</span><span style="color: #a31515;">behaviors</span><span style="color: blue;">&gt;</span></p>
<p style="margin: 0in; font-family: Consolas; font-size: 9.5pt;"><span style="color: blue;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>&lt;</span><span style="color: #a31515;">serviceBehaviors</span><span style="color: blue;">&gt;</span></p>
<p style="margin: 0in; font-family: Consolas; font-size: 9.5pt;"><span style="color: blue;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>&lt;</span><span style="color: #a31515;">behavior</span><span style="color: blue;"> </span><span style="color: red;">name</span><span style="color: blue;">=</span>"<span style="color: blue;">metadata</span>"<span style="color: blue;">&gt;</span></p>
<p style="margin: 0in; font-family: Consolas; font-size: 9.5pt;"><span style="color: blue;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>&lt;</span><span style="color: #a31515;">serviceMetadata</span><span style="color: blue;"> </span><span style="color: red;">httpGetEnabled</span><span style="color: blue;">=</span>"<span style="color: blue;">true</span>"<span style="color: blue;">/&gt;</span></p>
<p style="margin: 0in; font-family: Consolas; font-size: 9.5pt;"><span style="color: blue;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>&lt;/</span><span style="color: #a31515;">behavior</span><span style="color: blue;">&gt;</span></p>
<p style="margin: 0in; font-family: Consolas; font-size: 9.5pt;"><span style="color: blue;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>&lt;/</span><span style="color: #a31515;">serviceBehaviors</span><span style="color: blue;">&gt;</span></p>
<p style="margin: 0in; font-family: Consolas; font-size: 9.5pt;"><span style="color: blue;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp; </span>&lt;/</span><span style="color: #a31515;">behaviors</span><span style="color: blue;">&gt;</span></p>
<p style="margin: 0in; font-family: Consolas; font-size: 9.5pt;"><span style="color: blue;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp; </span>&lt;</span><span style="color: #a31515;">services</span><span style="color: blue;">&gt;</span></p>
<p style="margin: 0in; font-family: Consolas; font-size: 9.5pt;"><span style="color: blue;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>&lt;</span><span style="color: #a31515;">service</span><span style="color: blue;"> </span><span style="color: red;">name</span><span style="color: blue;">=</span>"<span style="color: blue;">HelloWorldWebhost.HelloWorldService</span>"<span style="color: blue;"> </span><span style="color: red;">behaviorConfiguration</span><span style="color: blue;">=</span>"<span style="color: blue;">metadata</span>"<span style="color: blue;">&gt;</span></p>
<p style="margin: 0in; font-family: Consolas; font-size: 9.5pt;"><span style="color: blue;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>&lt;</span><span style="color: #a31515;">endpoint</span><span style="color: blue;"> </span><span style="color: red;">address</span><span style="color: blue;">=</span>""<span style="color: blue;"> </span><span style="color: red;">binding</span><span style="color: blue;">=</span>"<span style="color: blue;">basicHttpBinding</span>"<span style="color: blue;"> </span><span style="color: red;">bindingConfiguration</span><span style="color: blue;">=</span>"<span style="color: blue;">MyBinding</span>"<span style="color: blue;"> </span><span style="color: red;">contract</span><span style="color: blue;">=</span>"<span style="color: blue;">HelloWorldWebhost.IHelloWorld</span>"<span style="color: blue;">/&gt;</span></p>
<p style="margin: 0in; font-family: Consolas; font-size: 9.5pt;"><span style="color: blue;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>&lt;/</span><span style="color: #a31515;">service</span><span style="color: blue;">&gt;</span></p>
<p style="margin: 0in; font-family: Consolas; font-size: 9.5pt;"><span style="color: blue;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp; </span>&lt;/</span><span style="color: #a31515;">services</span><span style="color: blue;">&gt;</span></p>
<p style="margin: 0in; font-family: Consolas; font-size: 9.5pt;"><span style="color: blue;"><span style="mso-spacerun: yes;">&nbsp; </span>&lt;/</span><span style="color: #a31515;">system.serviceModel</span><span style="color: blue;">&gt;</span></p>
<p style="margin: 0in; font-family: Consolas; font-size: 9.5pt;"><span style="color: blue;">&lt;/</span><span style="color: #a31515;">configuration</span><span style="color: blue;">&gt;</span></p>
<p style="margin: 0in; font-family: Consolas; font-size: 9.5pt;">&nbsp;</p>
<p style="margin: 0in; font-family: Calibri; font-size: 11pt;">&nbsp;</p>
<p style="margin: 0in; font-family: Calibri; font-size: 11pt;">Finally, I changed the output path from "bin\Debug\" to "bin\" in the project properties.<span style="mso-spacerun: yes;">&nbsp; </span>Next, I open IIS (IIS7, in my case), right click "Default Web Site" and select "Add Application."<span style="mso-spacerun: yes;">&nbsp; </span>The virtual path (Alias) is AdHocHelloWorld and the physical path is the project folder I was using in VS.<span style="mso-spacerun: yes;">&nbsp; </span>Next, I right-click the application in IIS and select "Edit Permissions" to give IIS_IUSRS access to the folder.<span style="mso-spacerun: yes;">&nbsp; </span>Finally, I switched to Content View in IIS and browsed to the .svc file to make sure I got the service activation page.</p>
<p style="margin: 0in; font-family: Calibri; font-size: 11pt;">&nbsp;</p>
<p style="margin: 0in; font-family: Calibri; color: #366092; font-size: 13pt;"><span style="font-weight: bold;">Modifying for https</span></p>
<p style="margin: 0in; font-family: Calibri; font-size: 11pt;">To change the service to use Transport security, all I have to do is change the binding:</p>
<p style="margin: 0in; font-family: Consolas; font-size: 9.5pt;"><span style="color: blue;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>&lt;</span><span style="color: #a31515;">basicHttpBinding</span><span style="color: blue;">&gt;</span></p>
<p style="margin: 0in; font-family: Consolas; font-size: 9.5pt;"><span style="color: blue;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>&lt;</span><span style="color: #a31515;">binding</span><span style="color: blue;"> </span><span style="color: red;">name</span><span style="color: blue;">=</span>"<span style="color: blue;">MyBinding</span>"<span style="color: blue;">&gt;</span></p>
<p style="margin: 0in; font-family: Consolas; font-size: 9.5pt;"><span style="color: blue;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>&lt;</span><span style="color: #a31515;">security</span><span style="color: blue;"> </span><span style="color: red;">mode</span><span style="color: blue;">=</span>"<span style="color: blue;">Transport</span>"<span style="color: blue;">/&gt;</span></p>
<p style="margin: 0in; font-family: Consolas; font-size: 9.5pt;"><span style="color: blue;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>&lt;/</span><span style="color: #a31515;">binding</span><span style="color: blue;">&gt;</span></p>
<p style="margin: 0in; font-family: Consolas; font-size: 9.5pt;"><span style="color: blue;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>&lt;/</span><span style="color: #a31515;">basicHttpBinding</span><span style="color: blue;">&gt;</span></p>
<p style="margin: 0in; font-family: Calibri; font-size: 11pt;">&nbsp;</p>
<p style="margin: 0in; font-family: Calibri; font-size: 11pt;">Now, if I just refresh the service in IE, I get the following error:</p>
<p style="margin: 0in; font-family: Calibri; font-size: 11pt;"><span style="font-weight: bold;">Could not find a base address that matches scheme https for the endpoint with binding BasicHttpBinding. Registered base address schemes are [http].</span> </p>
<p style="margin: 0in; font-family: Calibri; font-size: 11pt;">This is where you need to add a binding in IIS for https.<span style="mso-spacerun: yes;">&nbsp; </span>And to do that, you need a certificate.<span style="mso-spacerun: yes;">&nbsp; </span>Looking at my old blog post, I run the following two commands on my machine:</p>
<p style="margin: 0in; font-family: Consolas; font-size: 9.5pt;">makecert.exe -sk RootCA -sky signature -pe -n CN=&lt;machineName&gt; -r -sr LocalMachine -ss Root MyCA.cer</p>
<p style="margin: 0in; font-family: Consolas; font-size: 9.5pt;">&nbsp;</p>
<p style="margin: 0in; font-family: Consolas; font-size: 9.5pt;">makecert.exe -sk server -sky exchange -pe -n CN=&lt;machineName&gt; -ir LocalMachine -is Root -ic MyCA.cer -sr LocalMachine -ss My &lt;certificate path&gt;</p>
<p style="margin: 0in; font-family: Consolas; font-size: 9.5pt;">&nbsp;</p>
<p style="margin: 0in; font-family: Calibri; font-size: 11pt;">Of course, I changed &lt;machineName&gt; to be my machine's fully-qualified machine name.<span style="mso-spacerun: yes;">&nbsp; </span>And the &lt;certificate path&gt; was MyAdHocWebhostHttpsCert.cer.</p>
<p style="margin: 0in; font-family: Calibri; font-size: 11pt;">Next, I right-click Default Web Site, select Edit Bindings&hellip;<span style="mso-spacerun: yes;">&nbsp; </span>Under Type, I select https.<span style="mso-spacerun: yes;">&nbsp; </span>The SSL certificate dropdown now is populated with the cert I created in the above steps.<span style="mso-spacerun: yes;">&nbsp; </span>These are labeled by "CN" parameter used.<span style="mso-spacerun: yes;">&nbsp; </span></p>
<p style="margin: 0in; font-family: Calibri; font-size: 11pt;">&nbsp;</p>
<p style="margin: 0in; font-family: Calibri; font-size: 11pt;">Now, when I refresh my IE page to the service, it activates just fine.<span style="mso-spacerun: yes;">&nbsp; </span>In fact, if I look at the wsdl, I see the address to the service uses the https scheme now.</p>
<p style="margin: 0in; font-family: Calibri; font-size: 11pt;">&nbsp;</p>
<p style="margin: 0in; font-family: Calibri; color: #366092; font-size: 13pt;"><span style="font-weight: bold;">Setting up the remote client machine</span></p>
<p style="margin: 0in; font-family: Calibri; font-size: 11pt;">If I now browse to the service (on https) using IE on a remote machine, I get the following (after remembering to allow Http and Https publishing through my firewall):</p>
<div sizset="0" sizcache="7" style="direction: ltr;">
<table valign="top" cellpadding="0" cellspacing="0" border="0" style="border-collapse: collapse; direction: ltr; border: #a3a3a3 0pt solid;">
<tbody>
<tr>
<td style="width: 10.516in; vertical-align: top; border-width: 0pt; padding: 4pt;">
<p style="margin: 0in; font-family: Calibri; font-size: 20pt;"><span style="font-weight: bold;">There is a problem with this website's security certificate.</span></p>
</td>
</tr>
<tr>
<td style="width: 10.516in; vertical-align: top; border-width: 0pt; padding: 4pt;">
<p style="margin: 0in; font-family: Calibri; font-size: 16pt;"><span style="font-weight: bold;">The security certificate presented by this website was not issued by a trusted certificate authority.</span></p>
<p style="margin: 0in; font-family: Calibri; font-size: 16pt;"><span style="font-weight: bold;">The security certificate presented by this website was issued for a different website's address.</span></p>
<p style="margin: 0in; font-family: Calibri; font-size: 16pt;"><span style="font-weight: bold;">Security certificate problems may indicate an attempt to fool you or intercept any data you send to the server. </span></p>
</td>
</tr>
</tbody>
</table>
</div>
<p style="margin: 0in; font-family: Calibri; font-size: 11pt;">&nbsp;</p>
<p style="margin: 0in; font-family: Calibri; font-size: 11pt;">You can make your client machine trust the server's certificate by exporting the trusted root certificate from the server machine and importing it on the client machine.<span style="mso-spacerun: yes;">&nbsp; </span>Here's how to do this.<span style="mso-spacerun: yes;">&nbsp; </span>On the server machine, run mmc.exe.<span style="mso-spacerun: yes;">&nbsp; </span>File -&gt; Add or Remove Snap-ins -&gt; Certificates -&gt; Add.<span style="mso-spacerun: yes;">&nbsp; </span>Select Computer account (Local).<span style="mso-spacerun: yes;">&nbsp; </span>Click Ok and unfold down to Trusted Root Certification Authorities.<span style="mso-spacerun: yes;">&nbsp; </span>In there, you should find the certificate that was installed by the first makecert.exe command.<span style="mso-spacerun: yes;">&nbsp; </span>The "Issued To" and "Issued By" are both the machine name.<span style="mso-spacerun: yes;">&nbsp; </span>Right click it and select Export.<span style="mso-spacerun: yes;">&nbsp; </span>For this test, I'm not exporting the private key.<span style="mso-spacerun: yes;">&nbsp; </span>I select the defaults on everything else and pick a file name at the end, placing it in my documents folder.<span style="mso-spacerun: yes;">&nbsp; </span></p>
<p style="margin: 0in; font-family: Calibri; font-size: 11pt;">Next, I copy the certificate to the client machine.<span style="mso-spacerun: yes;">&nbsp; </span>This is sort of mimicking how Verisign installs their trusted root certificate on all windows machines.<span style="mso-spacerun: yes;">&nbsp; </span>Of course, providers then need to ask Verisign to issue them a certificate to secure their web services with.<span style="mso-spacerun: yes;">&nbsp; </span>But more on that topic is probably widely available elsewhere.<span style="mso-spacerun: yes;">&nbsp; </span></p>
<p style="margin: 0in; font-family: Calibri; font-size: 11pt;">&nbsp;</p>
<p style="margin: 0in; font-family: Calibri; font-size: 11pt;">On the client machine, simply double-click the certificate that was copied.<span style="mso-spacerun: yes;">&nbsp; </span>When the wizard asks which certificate store to install it to, select Trusted Root Certification Authorities.<span style="mso-spacerun: yes;">&nbsp; </span>Everything else, just use the defaults.<span style="mso-spacerun: yes;">&nbsp; </span>Now, if you look at the certificates on the client machine (mmc.exe - Certificates) you should see the certificate under Trusted Root Certification Authorities.<span style="mso-spacerun: yes;">&nbsp; </span>Further, refreshing the IE window on the client machine shows there is no longer a problem with the certificate.</p>
<p style="margin: 0in; font-family: Calibri; font-size: 11pt;">&nbsp;</p>
<p style="margin: 0in; font-family: Calibri; font-size: 11pt;">Happy testing!</p>]]></content:encoded>
    </item>
    <item>
      <title>WebMatrix Visual Studio Integration</title>
      <link>https://docs.microsoft.com/archive/blogs/james_osbornes_blog/webmatrix-visual-studio-integration</link>
      <pubDate>Thu, 13 Jan 2011 16:23:00 GMT</pubDate>
      <dc:creator><![CDATA[James Osborne-MSFT]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/james_osbornes_blog/2011/01/13/webmatrix-visual-studio-integration/</guid>
      <description><![CDATA[&nbsp;
Today, I just installed the release of WebMatrix, along with the Beta version of SP1 for...]]></description>
      <content:encoded><![CDATA[<p>&nbsp;</p>
<p style="margin: 0in; font-family: Calibri; font-size: 11pt;">Today, I just installed the release of WebMatrix, along with the Beta version of SP1 for Visual Studio 2010.<span style="mso-spacerun: yes;">&nbsp; </span>One of the many cool features added with this combination is the IISExpress integration in Visual Studio.<span style="mso-spacerun: yes;">&nbsp; </span>Prior to this, the only options you had was to either use the default ASP.NET Development server in Visual Studio to test your web applications or web sites, or to use the Local IIS.<span style="mso-spacerun: yes;">&nbsp; </span>But assume you don't have IIS installed.<span style="mso-spacerun: yes;">&nbsp; </span>Or you don't have admin rights on your development box.<span style="mso-spacerun: yes;">&nbsp; </span>With IISExpress, you get the advantage on running on the same code base as IIS 7.5 when you test your site.<span style="mso-spacerun: yes;">&nbsp; </span>Even if you are running on a machine without IIS installed.</p>
<p style="margin: 0in; font-family: Calibri; font-size: 11pt;">&nbsp;</p>
<p style="margin: 0in; font-family: Calibri; font-size: 11pt;">I was considering publishing an end-to-end walkthrough of creating an ad-hoc HelloWorld WCF service entirely by hand and how to make Visual Studio host it in IISExpress, but this is so simple, once you know where to look, that I think I'll just point it out.</p>
<p style="margin: 0in; font-family: Calibri; font-size: 11pt;">&nbsp;</p>
<p style="margin: 0in; font-family: Calibri; font-size: 11pt;">Scenario 1: Web Application</p>
<p style="margin: 0in; font-family: Calibri; font-size: 11pt;">Assuming you have a Web Application in Visual Studio.<span style="mso-spacerun: yes;">&nbsp; </span>When you hit F5, it fires up the ASP.NET Development Server.<span style="mso-spacerun: yes;">&nbsp; </span>To change that to use IISExpress, open the project properties window.<span style="mso-spacerun: yes;">&nbsp; </span>With WebMatrix installed along with SP1 (I'm using Beta as of this post), you'll see the option to use IISExpress:</p>
<p style="margin: 0in; font-family: Calibri; font-size: 11pt;"><img src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/39/69/7510.WebApplicationSwitchToIISExpress.png" original-url="https://blogs.msdn.com/resized-image.ashx/__size/550x0/__key/CommunityServer-Blogs-Components-WeblogFiles/00-00-01-39-69/7510.WebApplicationSwitchToIISExpress.png" border="0" /></p>
<p>&nbsp;Scenario 2: Web Site</p>
<p style="margin: 0in; font-family: Calibri; font-size: 11pt;">Assuming you have a Web Site in Visual Studio, there's a couple ways you can configure it to be launched using IISExpress.</p>
<ol type="1" style="font-style: normal; margin-top: 0in; unicode-bidi: embed; font-family: Calibri; direction: ltr; margin-bottom: 0in; margin-left: 0.375in; font-size: 11pt; font-weight: normal;">
<li value="1" style="margin-top: 0px; margin-bottom: 0px; vertical-align: middle;"><span style="font-style: normal; font-family: Calibri; font-size: 11pt; font-weight: normal;">From Publish Web Site</span><ol type="a" style="font-style: normal; margin-top: 0in; unicode-bidi: embed; font-family: Calibri; direction: ltr; margin-bottom: 0in; margin-left: 0.375in; font-size: 11pt; font-weight: normal;">
<li value="1" style="margin-top: 0px; margin-bottom: 0px; vertical-align: middle;"><span style="font-style: normal; font-family: Calibri; font-size: 11pt; font-weight: normal;">Right-click the project and select "Publish Web Site."</span></li>
<li style="margin-top: 0px; margin-bottom: 0px; vertical-align: middle;"><span style="font-family: Calibri; font-size: 11pt;">Under Target Location, click the [&hellip;] button.</span></li>
<li style="margin-top: 0px; margin-bottom: 0px; vertical-align: middle;"><span style="font-family: Calibri; font-size: 11pt;">Select the "Local IIS" button on the left.<span style="mso-spacerun: yes;">&nbsp; </span>IIS Express Sites shows up there, and you can select which vdir to use, or use the "Create new site" button at the top right to make a new one.</span></li>
</ol></li>
</ol><ol type="1" style="font-style: normal; margin-top: 0in; unicode-bidi: embed; font-family: Calibri; direction: ltr; margin-bottom: 0in; margin-left: 0.375in; font-size: 11pt; font-weight: normal;">
<li value="2" style="margin-top: 0px; margin-bottom: 0px; vertical-align: middle;"><span style="font-style: normal; font-family: Calibri; font-size: 11pt; font-weight: normal;">From "Use IIS Express&hellip;"</span><ol type="a" style="font-style: normal; margin-top: 0in; unicode-bidi: embed; font-family: Calibri; direction: ltr; margin-bottom: 0in; margin-left: 0.375in; font-size: 11pt; font-weight: normal;">
<li value="1" style="margin-top: 0px; margin-bottom: 0px; vertical-align: middle;"><span style="font-style: normal; font-family: Calibri; font-size: 11pt; font-weight: normal;">Right-click the project and select "Use IIS Express&hellip;"<span style="mso-spacerun: yes;">&nbsp; </span>Click Yes and OK&nbsp;on the dialogs that follow:</span></li>
</ol></li>
</ol>
<p style="margin-top: 0px; margin-bottom: 0px; vertical-align: middle;"><span style="font-style: normal; font-family: Calibri; font-size: 11pt; font-weight: normal;"><img src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/39/69/7750.SwitchToIISExpress.png" original-url="https://blogs.msdn.com/resized-image.ashx/__size/550x0/__key/CommunityServer-Blogs-Components-WeblogFiles/00-00-01-39-69/7750.SwitchToIISExpress.png" border="0" /></span></p>
<p style="margin-top: 0px; margin-bottom: 0px; vertical-align: middle;"><span style="font-style: normal; font-family: Calibri; font-size: 11pt; font-weight: normal;"><img src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/39/69/6253.SwitchedToIISExpress.png" original-url="https://blogs.msdn.com/resized-image.ashx/__size/550x0/__key/CommunityServer-Blogs-Components-WeblogFiles/00-00-01-39-69/6253.SwitchedToIISExpress.png" border="0" /></span></p>
<p style="margin-top: 0px; margin-bottom: 0px; vertical-align: middle;"><span style="font-style: normal; font-family: Calibri; font-size: 11pt; font-weight: normal;"></span></p>
<p><span style="font-family: Calibri;"></span></p>
<p><span style="font-family: Calibri;"></span></p>]]></content:encoded>
    </item>
    <item>
      <title>Tracing and Message Logging in WebMatrix</title>
      <link>https://docs.microsoft.com/archive/blogs/james_osbornes_blog/tracing-and-message-logging-in-webmatrix</link>
      <pubDate>Thu, 13 Jan 2011 13:59:00 GMT</pubDate>
      <dc:creator><![CDATA[James Osborne-MSFT]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/james_osbornes_blog/2011/01/13/tracing-and-message-logging-in-webmatrix/</guid>
      <description><![CDATA[&nbsp;
In Beta3, there was an issue where WCF trace logs would be truncated after closing...]]></description>
      <content:encoded><![CDATA[<p>&nbsp;</p>
<p style="margin: 0in; font-family: Calibri; font-size: 11pt;">In Beta3, there was an issue where WCF trace logs would be truncated after closing WebMatrix.<span style="mso-spacerun: yes;">&nbsp; </span>See the posting <a target="_blank" href="https://blogs.msdn.com/b/james_osbornes_blog/archive/2010/12/06/tracing-and-message-logging-in-webmatrix-iisexpress-beta3.aspx">here for historical details</a>.</p>
<p style="margin: 0in; font-family: Calibri; font-size: 11pt;">With the full release of WebMatrix, there is a way to get non-truncated logs.&nbsp; <span style="mso-spacerun: yes;">If you are hosting the site </span>from within WebMatrix UI and just close WebMatrix or select Exit from the menu, you still will likely get truncated logs.<span style="mso-spacerun: yes;">&nbsp; </span>To cleanly exit WebMatrix, first shut down your service by Exiting IISExpress in the system tray:</p>
<p style="margin: 0in; font-family: Calibri; font-size: 11pt;">&nbsp;</p>
<p style="margin: 0in;"><img src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/39/69/7024.ExitIISExpress.png" original-url="https://blogs.msdn.com/resized-image.ashx/__size/550x0/__key/CommunityServer-Blogs-Components-WeblogFiles/00-00-01-39-69/7024.ExitIISExpress.png" border="0" /></p>
<p style="margin: 0in; font-family: Calibri; font-size: 11pt;">&nbsp;</p>
<p style="margin: 0in; font-family: Calibri; font-size: 11pt;">This will allow WebMatrix to flush the trace data to the log and release the handle on the log file.<span style="mso-spacerun: yes;">&nbsp; </span>You can then view the log and keep WebMatrix open.<span style="mso-spacerun: yes;">&nbsp; </span>Later, you can start the service up again from within WebMatrix.</p>]]></content:encoded>
    </item>
    <item>
      <title>Mismatching Auth Schemes</title>
      <link>https://docs.microsoft.com/archive/blogs/james_osbornes_blog/mismatching-auth-schemes</link>
      <pubDate>Fri, 07 Jan 2011 16:46:00 GMT</pubDate>
      <dc:creator><![CDATA[James Osborne-MSFT]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/james_osbornes_blog/2011/01/07/mismatching-auth-schemes/</guid>
      <description><![CDATA[&nbsp;
Recently, I've come across an interesting exception message in one of my Ad-Hoc WCF projects...]]></description>
      <content:encoded><![CDATA[<p>&nbsp;</p>
<p style="margin: 0in; font-family: Calibri; font-size: 11pt;">Recently, I've come across an interesting exception message in one of my Ad-Hoc WCF projects using .Net 4.0.<span style="mso-spacerun: yes;">&nbsp; </span>The exception message is:</p>
<p style="margin: 0in; font-family: Calibri; font-size: 11pt;">&nbsp;</p>
<p style="margin: 0in 0in 0in 0.375in; font-family: Calibri; font-size: 11pt;">The HTTP request is unauthorized with client authentication scheme<span style="mso-spacerun: yes;">&nbsp; </span>'Basic', The authentication header received from the server was 'Negotiate,NTLM,Basic realm="[omitted]"'.</p>
<p style="margin: 0in; font-family: Calibri; font-size: 11pt;">&nbsp;</p>
<p style="margin: 0in; font-family: Calibri; font-size: 11pt;">This exception message is a bit odd because it says my client was using Basic, and that the server only allows requests using Negotiate, NTLM, or Basic.<span style="mso-spacerun: yes;">&nbsp; </span>But I used Basic, so why would it throw?</p>
<p style="margin: 0in; font-family: Calibri; font-size: 11pt;">&nbsp;</p>
<p style="margin: 0in; font-family: Calibri; font-size: 11pt;">The two possibilities are:</p>
<ol type="1" style="font-style: normal; margin-top: 0in; unicode-bidi: embed; font-family: Calibri; direction: ltr; margin-bottom: 0in; margin-left: 0.375in; font-size: 11pt; font-weight: normal;">
<li value="1" style="margin-top: 0px; margin-bottom: 0px; vertical-align: middle;"><span style="font-style: normal; font-family: Calibri; font-size: 11pt; font-weight: normal;">The username or password were wrong.<span style="mso-spacerun: yes;">&nbsp; </span>This is the most obvious.</span></li>
</ol><ol type="1" style="font-style: normal; margin-top: 0in; unicode-bidi: embed; font-family: Calibri; direction: ltr; margin-bottom: 0in; margin-left: 0.375in; font-size: 11pt; font-weight: normal;">
<li value="2" style="margin-top: 0px; margin-bottom: 0px; vertical-align: middle;"><span style="font-style: normal; font-family: Calibri; font-size: 11pt; font-weight: normal;">The Auth schemes on the service don't match those supported by the host.<span style="mso-spacerun: yes;">&nbsp; </span>In this case, by host, I mean IIS:</span></li>
</ol>
<p style="margin-top: 0px; margin-bottom: 0px; vertical-align: middle;"><span style="font-style: normal; font-family: Calibri; font-size: 11pt; font-weight: normal;"><img src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/39/69/3022.Authentication_IIS.png" original-url="https://blogs.msdn.com/resized-image.ashx/__size/550x0/__key/CommunityServer-Blogs-Components-WeblogFiles/00-00-01-39-69/3022.Authentication_5F00_IIS.png" border="0" /></span></p>
<p style="margin: 0in 0in 0in 0.375in;">&nbsp;</p>
<p style="margin: 0in 0in 0in 0.375in; font-family: Calibri; font-size: 11pt;">So, the host supports Anonymous, Basic, and Windows (NTLM + Negotiate).<span style="mso-spacerun: yes;">&nbsp; (</span>If the host didn't support the auth mode set on the service binding, I would have gotten an activation exception.)<span style="mso-spacerun: yes;">&nbsp; </span>So, a quick peek at the service binding shows that the binding set the ClientCredentialType to "Windows."</p>
<p style="margin: 0in 0in 0in 0.375in; font-family: Calibri; font-size: 11pt;">&nbsp;</p>
<p style="margin: 0in; font-family: Calibri; font-size: 11pt;">Most of the time, this wouldn't pose a problem.<span style="mso-spacerun: yes;">&nbsp; </span>Especially if you are dynamically building the clients from WSDL, because the auth schemes for the binding are exposed there.<span style="mso-spacerun: yes;">&nbsp; </span></p>]]></content:encoded>
    </item>
  </channel>
</rss>