<?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>Kiran's Blog</title>
    <atom:link
      href="https://docs.microsoft.com/archive/blogs/kiranchalla/feed.xml"
      rel="self"
      type="application/rss+xml" />
    <link>https://docs.microsoft.com/archive/blogs/kiranchalla/feed.xml</link>
    <description />
    <lastBuildDate>Wed, 17 Apr 2019 03:23:54 GMT</lastBuildDate>
    <language>en-US</language>
    <sy:updatePeriod>hourly</sy:updatePeriod>
    <sy:updateFrequency>1</sy:updateFrequency>
    <item>
      <title>Handling Compression (Accept-Encoding) Sample</title>
      <link>https://docs.microsoft.com/archive/blogs/kiranchalla/handling-compression-accept-encoding-sample</link>
      <pubDate>Tue, 04 Sep 2012 21:14:58 GMT</pubDate>
      <dc:creator><![CDATA[Kiran Challa]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/kiranchalla/2012/09/04/handling-compression-accept-encoding-sample/</guid>
      <description><![CDATA[Long time back there was a question on forums regarding how we could achieve compression scenario...]]></description>
      <content:encoded><![CDATA[<p>Long time back there was a <a href="https://forums.asp.net/p/1771770/4841788.aspx/1?Accept+Encoding+handling+" target="_blank">question on forums</a> regarding how we could achieve compression scenario via Accept-Encoding headers in Web API. Thought of bringing that sample here so that it could be useful for anyone looking for this kind of functionality.</p>  <p><strong>NOTE</strong>: ASP.NET Web API doesn’t have inherent support for Accept-Encoding header.</p>  <p>Let’s assume we have the following controller which is returning 3 different HttpContents: StringContent, StreamContent &amp; ObjectContent.</p>  <div id="codeSnippetWrapper">   <div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">     <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet"><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> AcceptEncodingTestsController : ApiController<br />    {<br />        [HttpGet]<br />        <span style="color: #0000ff">public</span> HttpResponseMessage GetStringContent()<br />        {<br />            HttpResponseMessage response = <span style="color: #0000ff">new</span> HttpResponseMessage();<br />            response.Content = <span style="color: #0000ff">new</span> StringContent(<span style="color: #006080">&quot;Hello World!&quot;</span>);<br /><br />            <span style="color: #0000ff">return</span> response;<br />        }<br /><br />        [HttpGet]<br />        <span style="color: #0000ff">public</span> HttpResponseMessage GetStreamContent()<br />        {<br />            HttpResponseMessage response = <span style="color: #0000ff">new</span> HttpResponseMessage();<br />            response.Content = <span style="color: #0000ff">new</span> StreamContent(<span style="color: #0000ff">new</span> FileStream(<span style="color: #006080">@&quot;D\Sample.txt&quot;</span>, FileMode.Open));<br />            response.Content.Headers.ContentType = <span style="color: #0000ff">new</span> MediaTypeHeaderValue(<span style="color: #006080">&quot;text/plain&quot;</span>);<br />            <span style="color: #0000ff">return</span> response;<br />        }<br /><br />        [HttpGet]<br />        <span style="color: #0000ff">public</span> Customer GetCustomer() <span style="color: #008000">// the return value is actually converted to ObjectContent</span><br />        {<br />            <span style="color: #0000ff">return</span> <span style="color: #0000ff">new</span> Customer() { Id = 100, DateOfBirth = <span style="color: #0000ff">new</span> DateTime(1975, 4, 3) };<br />        }<br />    }<br /></pre>

    <br /></div>

  <br /></div>

<p>&#160;</p>

<p><u>Encoding handler</u>:</p>

<div id="codeSnippetWrapper">
  <div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet"><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> EncodingDelegateHandler : DelegatingHandler<br />    {<br />        <span style="color: #0000ff">protected</span> <span style="color: #0000ff">override</span> Task&lt;HttpResponseMessage&gt; SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)<br />        {<br />            <span style="color: #0000ff">return</span> <span style="color: #0000ff">base</span>.SendAsync(request, cancellationToken).ContinueWith&lt;HttpResponseMessage&gt;((responseToCompleteTask) =&gt;<br />            {<br />                HttpResponseMessage response = responseToCompleteTask.Result;<br /><br />                <span style="color: #0000ff">if</span> (response.RequestMessage.Headers.AcceptEncoding != <span style="color: #0000ff">null</span> &amp;&amp;<br />                    response.RequestMessage.Headers.AcceptEncoding.Count &gt; 0)<br />                {<br />                    <span style="color: #0000ff">string</span> encodingType = response.RequestMessage.Headers.AcceptEncoding.First().Value;<br /><br />                    response.Content = <span style="color: #0000ff">new</span> CompressedContent(response.Content, encodingType);<br />                }<br /><br />                <span style="color: #0000ff">return</span> response;<br />            },<br />            TaskContinuationOptions.OnlyOnRanToCompletion);<br />        }<br />    }<br /><br />    <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> CompressedContent : HttpContent<br />    {<br />        <span style="color: #0000ff">private</span> HttpContent originalContent;<br />        <span style="color: #0000ff">private</span> <span style="color: #0000ff">string</span> encodingType;<br /><br />        <span style="color: #0000ff">public</span> CompressedContent(HttpContent content, <span style="color: #0000ff">string</span> encodingType)<br />        {<br />            <span style="color: #0000ff">if</span> (content == <span style="color: #0000ff">null</span>)<br />            {<br />                <span style="color: #0000ff">throw</span> <span style="color: #0000ff">new</span> ArgumentNullException(<span style="color: #006080">&quot;content&quot;</span>);<br />            }<br /><br />            <span style="color: #0000ff">if</span> (encodingType == <span style="color: #0000ff">null</span>)<br />            {<br />                <span style="color: #0000ff">throw</span> <span style="color: #0000ff">new</span> ArgumentNullException(<span style="color: #006080">&quot;encodingType&quot;</span>);<br />            }<br /><br />            originalContent = content;<br />            <span style="color: #0000ff">this</span>.encodingType = encodingType.ToLowerInvariant();<br /><br />            <span style="color: #0000ff">if</span> (<span style="color: #0000ff">this</span>.encodingType != <span style="color: #006080">&quot;gzip&quot;</span> &amp;&amp; <span style="color: #0000ff">this</span>.encodingType != <span style="color: #006080">&quot;deflate&quot;</span>)<br />            {<br />                <span style="color: #0000ff">throw</span> <span style="color: #0000ff">new</span> InvalidOperationException(<span style="color: #0000ff">string</span>.Format(<span style="color: #006080">&quot;Encoding '{0}' is not supported. Only supports gzip or deflate encoding.&quot;</span>, <span style="color: #0000ff">this</span>.encodingType));<br />            }<br /><br />            <span style="color: #008000">// copy the headers from the original content</span><br />            <span style="color: #0000ff">foreach</span> (KeyValuePair&lt;<span style="color: #0000ff">string</span>, IEnumerable&lt;<span style="color: #0000ff">string</span>&gt;&gt; header <span style="color: #0000ff">in</span> originalContent.Headers)<br />            {<br />                <span style="color: #0000ff">this</span>.Headers.TryAddWithoutValidation(header.Key, header.Value);<br />            }<br /><br />            <span style="color: #0000ff">this</span>.Headers.ContentEncoding.Add(encodingType);<br />        }<br /><br />        <span style="color: #0000ff">protected</span> <span style="color: #0000ff">override</span> <span style="color: #0000ff">bool</span> TryComputeLength(<span style="color: #0000ff">out</span> <span style="color: #0000ff">long</span> length)<br />        {<br />            length = -1;<br /><br />            <span style="color: #0000ff">return</span> <span style="color: #0000ff">false</span>;<br />        }<br /><br />        <span style="color: #0000ff">protected</span> <span style="color: #0000ff">override</span> Task SerializeToStreamAsync(Stream stream, TransportContext context)<br />        {<br />            Stream compressedStream = <span style="color: #0000ff">null</span>;<br /><br />            <span style="color: #0000ff">if</span> (encodingType == <span style="color: #006080">&quot;gzip&quot;</span>)<br />            {<br />                compressedStream = <span style="color: #0000ff">new</span> GZipStream(stream, CompressionMode.Compress, leaveOpen: <span style="color: #0000ff">true</span>);<br />            }<br />            <span style="color: #0000ff">else</span> <span style="color: #0000ff">if</span> (encodingType == <span style="color: #006080">&quot;deflate&quot;</span>)<br />            {<br />                compressedStream = <span style="color: #0000ff">new</span> DeflateStream(stream, CompressionMode.Compress, leaveOpen: <span style="color: #0000ff">true</span>);<br />            }<br /><br />            <span style="color: #0000ff">return</span> originalContent.CopyToAsync(compressedStream).ContinueWith(tsk =&gt;<br />            {<br />                <span style="color: #0000ff">if</span> (compressedStream != <span style="color: #0000ff">null</span>)<br />                {<br />                    compressedStream.Dispose();<br />                }<br />            });<br />        }<br />    }<br /></pre>

    <br /></div>

  <br /></div>

<p>Register the handler:</p>

<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
  <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet">config.MessageHandlers.Add(<span style="color: #0000ff">new</span> EncodingDelegateHandler());<br /></pre>

  <br /></div>

<p>&#160;</p>

<p>With the above example you should see that all the 3 types of contents are compressed via the encoding handler.</p>]]></content:encoded>
    </item>
    <item>
      <title>Opting in controllers to show up on Help Page</title>
      <link>https://docs.microsoft.com/archive/blogs/kiranchalla/opting-in-controllers-to-show-up-on-help-page</link>
      <pubDate>Tue, 04 Sep 2012 16:39:10 GMT</pubDate>
      <dc:creator><![CDATA[Kiran Challa]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/kiranchalla/2012/09/04/opting-in-controllers-to-show-up-on-help-page/</guid>
      <description><![CDATA[By default when you install the Help Page nuget package and launch the help view, you would see help...]]></description>
      <content:encoded><![CDATA[<p>By default when you install the <a href="https://nuget.org/packages/Microsoft.AspNet.WebApi.HelpPage/0.1.0-alpha-120814" target="_blank">Help Page nuget package</a> and launch the help view, you would see help for all the controllers and their actions that are part of your application. There might be requirements where you might not want to expose all the controllers/actions on the help page. You have couple of options to achieve this.</p>  <p><u>Opt-<strong>out</strong> model</u>:</p>  <p>There is&#160; an attribute called <strong><em>ApiExplorerSettings </em></strong>which enables you to opt-out <strong>controllers</strong> and <strong>actions</strong> from showing up on the help page. Examples below:</p>  <div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">   <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet">     <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">[ApiExplorerSettings(IgnoreApi = <span style="color: #0000ff">true</span>)]</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> ValuesController : ApiController</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">    {</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">        <span style="color: #008000">// GET api/values</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">        <span style="color: #0000ff">public</span> IEnumerable&lt;<span style="color: #0000ff">string</span>&gt; Get()</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">        {</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">            <span style="color: #0000ff">return</span> <span style="color: #0000ff">new</span> <span style="color: #0000ff">string</span>[] { <span style="color: #006080">&quot;value1&quot;</span>, <span style="color: #006080">&quot;value2&quot;</span> };</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">        }</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">        <span style="color: #008000">// GET api/values/5</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">        <span style="color: #0000ff">public</span> <span style="color: #0000ff">string</span> Get(<span style="color: #0000ff">int</span> id)</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">        {</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">            <span style="color: #0000ff">return</span> <span style="color: #006080">&quot;value&quot;</span>;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">        }</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">    }</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> AbcController : ApiController</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">    {</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">        [ApiExplorerSettings(IgnoreApi = <span style="color: #0000ff">true</span>)]</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">        <span style="color: #0000ff">public</span> IEnumerable&lt;<span style="color: #0000ff">string</span>&gt; Get()</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">        {</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">            <span style="color: #0000ff">return</span> <span style="color: #0000ff">new</span> <span style="color: #0000ff">string</span>[] { <span style="color: #006080">&quot;value1&quot;</span>, <span style="color: #006080">&quot;value2&quot;</span> };</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">        }</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">        <span style="color: #008000">// GET api/values/5</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">        <span style="color: #0000ff">public</span> <span style="color: #0000ff">string</span> Get(<span style="color: #0000ff">int</span> id)</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">        {</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">            <span style="color: #0000ff">return</span> <span style="color: #006080">&quot;value&quot;</span>;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">        }</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">    }</pre>
<!--CRLF--></div>
</div>

<p>&#160;</p>

<p><u>Opt-<strong>in</strong> model:</u></p>

<p>With opt-in model, you need not decorate your controllers/actions with attributes. You could have all the logic in one central place.</p>

<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
  <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet">
    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> OptInApiExplorer : ApiExplorer</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">    {</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">        <span style="color: #0000ff">public</span> OptInApiExplorer(HttpConfiguration config)</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">            : <span style="color: #0000ff">base</span>(config)</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">        {</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">        }</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">        <span style="color: #0000ff">public</span> <span style="color: #0000ff">override</span> <span style="color: #0000ff">bool</span> ShouldExploreController(<span style="color: #0000ff">string</span> controllerVariableValue, HttpControllerDescriptor controllerDescriptor, IHttpRoute route)</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">        {</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">            <span style="color: #008000">// show help for ValuesController only</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">            <span style="color: #0000ff">if</span> (controllerDescriptor.ControllerType == <span style="color: #0000ff">typeof</span>(ValuesController))</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">            {</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">                <span style="color: #0000ff">return</span> <span style="color: #0000ff">true</span>;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">            }</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">            <span style="color: #0000ff">return</span> <span style="color: #0000ff">false</span>;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">        }</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">        <span style="color: #008000">// I overrode this method just to demonstrate the presence of this overridable behavior</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">        <span style="color: #008000">// For the current discussion, i am going to leave it as it is.</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">        <span style="color: #0000ff">public</span> <span style="color: #0000ff">override</span> Collection&lt;HttpMethod&gt; GetHttpMethodsSupportedByAction(IHttpRoute route, HttpActionDescriptor actionDescriptor)</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">        {</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">            <span style="color: #0000ff">return</span> <span style="color: #0000ff">base</span>.GetHttpMethodsSupportedByAction(route, actionDescriptor);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">        }</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">    }</pre>
<!--CRLF--></div>
</div>

<p>Replace the service on configuration:</p>

<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
  <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet">
    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">config.Services.Replace(<span style="color: #0000ff">typeof</span>(IApiExplorer), <span style="color: #0000ff">new</span> OptInApiExplorer(config));</pre>
<!--CRLF--></div>
</div>]]></content:encoded>
    </item>
    <item>
      <title>Receiving request file or data in Streamed mode at a Web API service</title>
      <link>https://docs.microsoft.com/archive/blogs/kiranchalla/receiving-request-file-or-data-in-streamed-mode-at-a-web-api-service</link>
      <pubDate>Tue, 04 Sep 2012 16:18:40 GMT</pubDate>
      <dc:creator><![CDATA[Kiran Challa]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/kiranchalla/2012/09/04/receiving-request-file-or-data-in-streamed-mode-at-a-web-api-service/</guid>
      <description><![CDATA[By default, the mode of receiving request data at a Web API service is in Buffered mode. This is...]]></description>
      <content:encoded><![CDATA[<p>By default, the mode of receiving request data at a Web API service is in Buffered mode. This is good for most scenarios, but it could be a performance problem when you expect users to upload huge files to the service. In this scenario the files would be first buffered and then you would start working on it. Since this is not ideal, in this short post I show how to enable Streamed mode of <strong><em>receiving </em></strong>data at the Web API service. </p>  <p>Currently the approach for enabling buffered/streamed mode of receiving data is different between SelfHost and WebHost scenarios and also the capabilities are also different.</p>  <p>There is a service called <em><strong>IHostBufferPolicySelector</strong></em> on HttpConfiguration, which as the name indicates is for the purpose of tuning your requests to be enable buffered/streamed mode. Currently there is only one implementation of it called <strong><em>WebHostBufferPolicySelector </em></strong>which as its name indicates is only for Web Host scenarios.</p>  <p><strong><u>Web Host</u></strong></p>  <div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; height: 250px; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">   <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet">     <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> CustomWebHostBufferPolicySelector : WebHostBufferPolicySelector</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">    {</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">        <span style="color: #008000">// Check incoming requests and modify their buffer policy</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">        <span style="color: #0000ff">public</span> <span style="color: #0000ff">override</span> <span style="color: #0000ff">bool</span> UseBufferedInputStream(<span style="color: #0000ff">object</span> hostContext)</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">        {</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">            System.Web.HttpContextBase contextBase = hostContext <span style="color: #0000ff">as</span> System.Web.HttpContextBase;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">            <span style="color: #0000ff">if</span> (contextBase != <span style="color: #0000ff">null</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">                &amp;&amp; contextBase.Request.ContentType != <span style="color: #0000ff">null</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">                &amp;&amp; contextBase.Request.ContentType.Contains(<span style="color: #006080">&quot;multipart&quot;</span>))</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">            {</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">                <span style="color: #008000">// we are enabling streamed mode here</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">                <span style="color: #0000ff">return</span> <span style="color: #0000ff">false</span>;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">            }</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">            <span style="color: #008000">// let the default behavior(buffered mode) to handle the scenario</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">            <span style="color: #0000ff">return</span> <span style="color: #0000ff">base</span>.UseBufferedInputStream(hostContext);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">        }</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">        <span style="color: #008000">// You could also chnage the response behavior too...but for this example, we are not</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">        <span style="color: #008000">// going to do anything here...I overrode this method just to demonstrate the availability</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">        <span style="color: #008000">// of this method.</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">        <span style="color: #0000ff">public</span> <span style="color: #0000ff">override</span> <span style="color: #0000ff">bool</span> UseBufferedOutputStream(System.Net.Http.HttpResponseMessage response)</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">        {</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">            <span style="color: #0000ff">return</span> <span style="color: #0000ff">base</span>.UseBufferedOutputStream(response);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">        }</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">    }</pre>
<!--CRLF--></div>
</div>

<p>Register this service in HttpConfiguration.</p>

<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
  <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet">
    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">config.Services.Replace(<span style="color: #0000ff">typeof</span>(IHostBufferPolicySelector), <span style="color: #0000ff">new</span> CustomWebHostBufferPolicySelector());</pre>
<!--CRLF--></div>
</div>

<div><strong><u></u></strong></div>

<div><strong><u>Self Host</u></strong></div>

<p><strong>No</strong> implementation of IHostBufferPolicySelector currently. However, one could enable Streamed mode in Self Host by doing the following:</p>

<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
  <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet">
    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">selfHostConfiguration.TransferMode = System.ServiceModel.TransferMode.StreamedRequest;</pre>
<!--CRLF--></div>
</div>

<p>You can visit this <a href="https://msdn.microsoft.com/en-us/library/system.servicemodel.transfermode.aspx" target="_blank">documentation</a> for more details about the TransferMode.</p>

<p>As you can see currently you have the ability of enabling streamed/buffered mode on a <strong><em>per-request basis in Web Host</em></strong>, where as in Self Host scenarios, you would need to enable it for the whole service.</p>]]></content:encoded>
    </item>
    <item>
      <title>Response buffering &amp; chunking in Web Host scenarios</title>
      <link>https://docs.microsoft.com/archive/blogs/kiranchalla/response-buffering-chunking-in-web-host-scenarios</link>
      <pubDate>Mon, 14 May 2012 14:12:03 GMT</pubDate>
      <dc:creator><![CDATA[Kiran Challa]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/kiranchalla/2012/05/14/response-buffering-chunking-in-web-host-scenarios/</guid>
      <description><![CDATA[NOTE: The following table represents the behaviors that are part of Release Candidate(RC) version...]]></description>
      <content:encoded><![CDATA[<p><font size="2"><strong><font color="#000000">NOTE</font></strong>: The following table represents the behaviors that are part of Release Candidate(RC) version only. It does NOT represent data for </font><a href="https://aspnetwebstack.codeplex.com/" target="_blank"><font size="2">currently in development</font></a><font size="2"> bits.</font></p>  <font size="2"></font>  <p><font size="2"><strong>What is Chunked Transfer Encoding</strong>: This represented by the header “Transfer-Encoding: chunked” indicates that a Request/Response does <em>not</em> know the Content Length of the body that it is going to generate upfront. This header enables to send data the moment it is available instead of the normal flow that a client/service does, which is to buffer the entire content and then send it. Since buffering big content is expensive, this header could be used to send data in chunks the moment it is ready.       <br />      <br /></font></p>  <font size="2"></font>  <table style="border-collapse: collapse; mso-yfti-tbllook: 1184; mso-padding-alt: 0in 0in 0in 0in;" class="MsoNormalTable" border="0" cellspacing="0" cellpadding="0"><tbody><font size="2"></font>      <tr style="mso-yfti-irow: 0; mso-yfti-firstrow: yes;"><font size="2"></font>        <td style="padding: 0in 5.4pt; border: 1pt solid windowtext; width: 31.1pt;" valign="top" width="41"><font size="2"></font>          <p class="MsoNormal"><b><span style="font-size: 12pt;"><o:p><font size="2">&#160;</font></o:p></span></b></p>         <font size="2"></font></td>       <font size="2"></font>        <td style="border-width: 1pt 1pt 1pt medium; border-style: solid solid solid none; border-color: windowtext windowtext windowtext currentcolor; padding: 0in 5.4pt; width: 160.4pt;" valign="top" width="214"><font size="2"></font>          <p class="MsoNormal" align="center"><font size="2"><strong>Response Content Type</strong></font></p>         <font size="2">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></td>       <font size="2"></font>        <td style="border-width: 1pt 1pt 1pt medium; border-style: solid solid solid none; border-color: windowtext windowtext windowtext currentcolor; padding: 0in 5.4pt; width: 95.75pt;" valign="top" width="128"><font size="2"></font>          <p class="MsoNormal" align="center"><b><span style="font-size: 12pt;"><font size="2">Content-Length<o:p></o:p></font></span></b></p>         <font size="2"></font></td>       <font size="2"></font>        <td style="border-width: 1pt 1pt 1pt medium; border-style: solid solid solid none; border-color: windowtext windowtext windowtext currentcolor; padding: 0in 5.4pt; width: 185.4pt;" valign="top" width="247"><font size="2"></font>          <p class="MsoNormal" align="center"><b><span style="font-size: 12pt;"><font size="2">Response Buffering at Service[1]</font></span></b></p>         <font size="2"></font></td>       <font size="2"></font>        <td style="border-width: 1pt 1pt 1pt medium; border-style: solid solid solid none; border-color: windowtext windowtext windowtext currentcolor; padding: 0in 5.4pt; width: 2in;" valign="top" width="192"><font size="2"></font>          <p class="MsoNormal" align="center"><b><span style="font-size: 12pt;"><font size="2">Response Chunked ?<o:p></o:p></font></span></b></p>         <font size="2"></font></td>       <font size="2"></font>        <td style="border-width: 1pt 1pt 1pt medium; border-style: solid solid solid none; border-color: windowtext windowtext windowtext currentcolor; padding: 0in 5.4pt; width: 239.2pt;" valign="top" width="319"><font size="2"></font>          <p class="MsoNormal" align="center"><b><span style="font-size: 12pt;"><font size="2">Notes<o:p></o:p></font></span></b></p>         <font size="2"></font></td>       <font size="2"></font></tr>     <font size="2"></font>      <tr style="mso-yfti-irow: 1;"><font size="2"></font>        <td style="background: rgb(79, 129, 189); border-width: medium 1pt 1pt; border-style: none solid solid; border-color: currentcolor windowtext windowtext; padding: 0in 5.4pt; width: 855.85pt;" valign="top" width="1141" colspan="6">         <p class="MsoNormal"><font color="#ffffff" size="2"><strong>Main Scenarios</strong></font></p>         <font size="2"></font></td>       <font size="2"></font></tr>     <font size="2"></font>      <tr style="mso-yfti-irow: 2;"><font size="2"></font>        <td style="border-width: medium 1pt 1pt; border-style: none solid solid; border-color: currentcolor windowtext windowtext; padding: 0in 5.4pt; width: 31.1pt;" valign="top" width="41"><font size="2"></font>          <p class="MsoNormal" align="center"><font size="2">1</font></p>         <font size="2"></font></td>       <font size="2"></font>        <td style="border-width: medium 1pt 1pt medium; border-style: none solid solid none; border-color: currentcolor windowtext windowtext currentcolor; padding: 0in 5.4pt; width: 160.4pt;" valign="top" width="214"><font size="2"></font>          <p class="MsoNormal"><font size="2">StringContent </font></p>         <font size="2"></font></td>       <font size="2"></font>        <td style="border-width: medium 1pt 1pt medium; border-style: none solid solid none; border-color: currentcolor windowtext windowtext currentcolor; padding: 0in 5.4pt; width: 95.75pt;" valign="top" width="128"><font size="2"></font>          <p class="MsoNormal"><font size="2">Known</font></p>         <font size="2"></font></td>       <font size="2"></font>        <td style="border-width: medium 1pt 1pt medium; border-style: none solid solid none; border-color: currentcolor windowtext windowtext currentcolor; padding: 0in 5.4pt; width: 185.4pt;" valign="top" width="247"><font size="2"></font>          <p class="MsoNormal"><font size="2">Not Buffered</font></p>         <font size="2"></font></td>       <font size="2"></font>        <td style="border-width: medium 1pt 1pt medium; border-style: none solid solid none; border-color: currentcolor windowtext windowtext currentcolor; padding: 0in 5.4pt; width: 2in;" valign="top" width="192"><font size="2"></font>          <p class="MsoNormal"><font size="2">Not Chunked</font></p>         <font size="2"></font></td>       <font size="2"></font>        <td style="border-width: medium 1pt 1pt medium; border-style: none solid solid none; border-color: currentcolor windowtext windowtext currentcolor; padding: 0in 5.4pt; width: 239.2pt;" valign="top" width="319"><font size="2"></font>          <p class="MsoNormal"><o:p><font size="2">&#160;</font></o:p></p>         <font size="2"></font></td>       <font size="2"></font></tr>     <font size="2"></font>      <tr style="mso-yfti-irow: 3;"><font size="2"></font>        <td style="border-width: medium 1pt 1pt; border-style: none solid solid; border-color: currentcolor windowtext windowtext; padding: 0in 5.4pt; width: 31.1pt;" valign="top" width="41"><font size="2"></font>          <p class="MsoNormal" align="center"><font size="2">2</font></p>         <font size="2"></font></td>       <font size="2"></font>        <td style="border-width: medium 1pt 1pt medium; border-style: none solid solid none; border-color: currentcolor windowtext windowtext currentcolor; padding: 0in 5.4pt; width: 160.4pt;" valign="top" width="214"><font size="2"></font>          <p class="MsoNormal"><font size="2">ByteArrayContent</font></p>         <font size="2"></font></td>       <font size="2"></font>        <td style="border-width: medium 1pt 1pt medium; border-style: none solid solid none; border-color: currentcolor windowtext windowtext currentcolor; padding: 0in 5.4pt; width: 95.75pt;" valign="top" width="128"><font size="2"></font>          <p class="MsoNormal"><font size="2">Known</font></p>         <font size="2"></font></td>       <font size="2"></font>        <td style="border-width: medium 1pt 1pt medium; border-style: none solid solid none; border-color: currentcolor windowtext windowtext currentcolor; padding: 0in 5.4pt; width: 185.4pt;" valign="top" width="247"><font size="2"></font>          <p class="MsoNormal"><font size="2">Not Buffered</font></p>         <font size="2"></font></td>       <font size="2"></font>        <td style="border-width: medium 1pt 1pt medium; border-style: none solid solid none; border-color: currentcolor windowtext windowtext currentcolor; padding: 0in 5.4pt; width: 2in;" valign="top" width="192"><font size="2"></font>          <p class="MsoNormal"><font size="2">Not Chunked</font></p>         <font size="2"></font></td>       <font size="2"></font>        <td style="border-width: medium 1pt 1pt medium; border-style: none solid solid none; border-color: currentcolor windowtext windowtext currentcolor; padding: 0in 5.4pt; width: 239.2pt;" valign="top" width="319"><font size="2"></font>          <p class="MsoNormal"><o:p><font size="2">&#160;</font></o:p></p>         <font size="2"></font></td>       <font size="2"></font></tr>     <font size="2"></font>      <tr style="mso-yfti-irow: 4;"><font size="2"></font>        <td style="border-width: medium 1pt 1pt; border-style: none solid solid; border-color: currentcolor windowtext windowtext; padding: 0in 5.4pt; width: 31.1pt;" valign="top" width="41"><font size="2"></font>          <p class="MsoNormal" align="center"><font size="2">3</font></p>         <font size="2"></font></td>       <font size="2"></font>        <td style="border-width: medium 1pt 1pt medium; border-style: none solid solid none; border-color: currentcolor windowtext windowtext currentcolor; padding: 0in 5.4pt; width: 160.4pt;" valign="top" width="214"><font size="2"></font>          <p class="MsoNormal"><font size="2">StreamContent with inner Stream whose <strong>length</strong> <b>can be calculated</b></font></p>         <font size="2"></font></td>       <font size="2"></font>        <td style="border-width: medium 1pt 1pt medium; border-style: none solid solid none; border-color: currentcolor windowtext windowtext currentcolor; padding: 0in 5.4pt; width: 95.75pt;" valign="top" width="128"><font size="2"></font>          <p class="MsoNormal"><font size="2">Known</font></p>         <font size="2"></font></td>       <font size="2"></font>        <td style="border-width: medium 1pt 1pt medium; border-style: none solid solid none; border-color: currentcolor windowtext windowtext currentcolor; padding: 0in 5.4pt; width: 185.4pt;" valign="top" width="247"><font size="2"></font>          <p class="MsoNormal"><font size="2">Not Buffered</font></p>         <font size="2"></font></td>       <font size="2"></font>        <td style="border-width: medium 1pt 1pt medium; border-style: none solid solid none; border-color: currentcolor windowtext windowtext currentcolor; padding: 0in 5.4pt; width: 2in;" valign="top" width="192"><font size="2"></font>          <p class="MsoNormal"><font size="2">Not Chunked</font></p>         <font size="2"></font></td>       <font size="2"></font>        <td style="border-width: medium 1pt 1pt medium; border-style: none solid solid none; border-color: currentcolor windowtext windowtext currentcolor; padding: 0in 5.4pt; width: 239.2pt;" valign="top" width="319"><font size="2"></font>          <p class="MsoNormal"><font size="2">Example: MemoryStream, for which we can calculate the length.</font></p>         <font size="2"></font></td>       <font size="2"></font></tr>     <font size="2"></font>      <tr style="mso-yfti-irow: 5;"><font size="2"></font>        <td style="border-width: medium 1pt 1pt; border-style: none solid solid; border-color: currentcolor windowtext windowtext; padding: 0in 5.4pt; width: 31.1pt;" valign="top" width="41"><font size="2"></font>          <p class="MsoNormal" align="center"><font size="2">4</font></p>         <font size="2"></font></td>       <font size="2"></font>        <td style="border-width: medium 1pt 1pt medium; border-style: none solid solid none; border-color: currentcolor windowtext windowtext currentcolor; padding: 0in 5.4pt; width: 160.4pt;" valign="top" width="214"><font size="2"></font>          <p class="MsoNormal"><font size="2">ObjectContent</font></p>         <font size="2"></font></td>       <font size="2"></font>        <td style="border-width: medium 1pt 1pt medium; border-style: none solid solid none; border-color: currentcolor windowtext windowtext currentcolor; padding: 0in 5.4pt; width: 95.75pt;" valign="top" width="128"><font size="2"></font>          <p class="MsoNormal"><font size="2">Not Known</font></p>         <font size="2"></font></td>       <font size="2"></font>        <td style="border-width: medium 1pt 1pt medium; border-style: none solid solid none; border-color: currentcolor windowtext windowtext currentcolor; padding: 0in 5.4pt; width: 185.4pt;" valign="top" width="247"><font size="2"></font>          <p class="MsoNormal"><font size="2">Buffered</font></p>         <font size="2"></font></td>       <font size="2"></font>        <td style="border-width: medium 1pt 1pt medium; border-style: none solid solid none; border-color: currentcolor windowtext windowtext currentcolor; padding: 0in 5.4pt; width: 2in;" valign="top" width="192"><font size="2"></font>          <p class="MsoNormal"><font size="2">Not Chunked</font></p>         <font size="2"></font></td>       <font size="2"></font>        <td style="border-width: medium 1pt 1pt medium; border-style: none solid solid none; border-color: currentcolor windowtext windowtext currentcolor; padding: 0in 5.4pt; width: 239.2pt;" valign="top" width="319"><font size="2"></font>          <p class="MsoNormal"><font size="2">Response is not chunked here because the Server will buffer the entire ObjectContent response, which results in knowing the Content-Length, in which case chunking is no longer required.</font></p>         <font size="2"></font></td>       <font size="2"></font></tr>     <font size="2"></font>      <tr style="mso-yfti-irow: 6;"><font size="2"></font>        <td style="border-width: medium 1pt 1pt; border-style: none solid solid; border-color: currentcolor windowtext windowtext; padding: 0in 5.4pt; width: 31.1pt;" valign="top" width="41"><font size="2"></font>          <p class="MsoNormal" align="center"><font size="2">5</font></p>         <font size="2"></font></td>       <font size="2"></font>        <td style="border-width: medium 1pt 1pt medium; border-style: none solid solid none; border-color: currentcolor windowtext windowtext currentcolor; padding: 0in 5.4pt; width: 160.4pt;" valign="top" width="214"><font size="2"></font>          <p class="MsoNormal"><font size="2">StreamContent with inner Stream whose <strong>length</strong> <b>cannot be calculated</b></font></p>         <font size="2"></font></td>       <font size="2"></font>        <td style="border-width: medium 1pt 1pt medium; border-style: none solid solid none; border-color: currentcolor windowtext windowtext currentcolor; padding: 0in 5.4pt; width: 95.75pt;" valign="top" width="128"><font size="2"></font>          <p class="MsoNormal"><font size="2">Not Known</font></p>         <font size="2"></font></td>       <font size="2"></font>        <td style="border-width: medium 1pt 1pt medium; border-style: none solid solid none; border-color: currentcolor windowtext windowtext currentcolor; padding: 0in 5.4pt; width: 185.4pt;" valign="top" width="247"><font size="2"></font>          <p class="MsoNormal"><font size="2">Not Buffered</font></p>         <font size="2"></font></td>       <font size="2"></font>        <td style="border-width: medium 1pt 1pt medium; border-style: none solid solid none; border-color: currentcolor windowtext windowtext currentcolor; padding: 0in 5.4pt; width: 2in;" valign="top" width="192"><font size="2"></font>          <p class="MsoNormal"><font size="2">Chunked</font></p>         <font size="2"></font></td>       <font size="2"></font>        <td style="border-width: medium 1pt 1pt medium; border-style: none solid solid none; border-color: currentcolor windowtext windowtext currentcolor; padding: 0in 5.4pt; width: 239.2pt;" valign="top" width="319"><font size="2"></font>          <p class="MsoNormal"><font size="2">Since we do NOT know the content-length upfront + it’s a StreamContent(we special case this content type), we do not buffer it and hence chunked encoding is used.</font></p>         <font size="2"></font></td>       <font size="2"></font></tr>     <font size="2"></font>      <tr style="mso-yfti-irow: 7;"><font size="2"></font>        <td style="background: rgb(79, 129, 189); border-width: medium 1pt 1pt; border-style: none solid solid; border-color: currentcolor windowtext windowtext; padding: 0in 5.4pt; width: 855.85pt;" valign="top" width="1141" colspan="6">         <p><font size="2"></font><strong><font color="#ffffff" size="2">Advanced Scenarios</font></strong></p>       </td>       <font size="2"></font></tr>     <font size="2"></font>      <tr style="mso-yfti-irow: 8;"><font size="2"></font>        <td style="border-width: medium 1pt 1pt; border-style: none solid solid; border-color: currentcolor windowtext windowtext; padding: 0in 5.4pt; width: 31.1pt;" valign="top" width="41"><font size="2"></font>          <p class="MsoNormal" align="center"><font size="2">6</font></p>         <font size="2"></font></td>       <font size="2"></font>        <td style="border-width: medium 1pt 1pt medium; border-style: none solid solid none; border-color: currentcolor windowtext windowtext currentcolor; padding: 0in 5.4pt; width: 160.4pt;" valign="top" width="214"><font size="2"></font>          <p class="MsoNormal"><font size="2">Custom HttpContent <b>deriving from HttpContent</b></font></p>         <font size="2"></font></td>       <font size="2"></font>        <td style="border-width: medium 1pt 1pt medium; border-style: none solid solid none; border-color: currentcolor windowtext windowtext currentcolor; padding: 0in 5.4pt; width: 95.75pt;" valign="top" width="128"><font size="2"></font>          <p class="MsoNormal"><font size="2">Not Known</font></p>         <font size="2"></font></td>       <font size="2"></font>        <td style="border-width: medium 1pt 1pt medium; border-style: none solid solid none; border-color: currentcolor windowtext windowtext currentcolor; padding: 0in 5.4pt; width: 185.4pt;" valign="top" width="247"><font size="2"></font>          <p class="MsoNormal"><font size="2">Buffered</font></p>         <font size="2"></font></td>       <font size="2"></font>        <td style="border-width: medium 1pt 1pt medium; border-style: none solid solid none; border-color: currentcolor windowtext windowtext currentcolor; padding: 0in 5.4pt; width: 2in;" valign="top" width="192"><font size="2"></font>          <p class="MsoNormal"><font size="2">Not Chunked</font></p>         <font size="2"></font></td>       <font size="2"></font>        <td style="border-width: medium 1pt 1pt medium; border-style: none solid solid none; border-color: currentcolor windowtext windowtext currentcolor; padding: 0in 5.4pt; width: 239.2pt;" valign="top" width="319"><font size="2"></font>          <p class="MsoNormal"><font size="2">Similar behavior as in ObjectContent (#4)</font></p>         <font size="2"></font></td>       <font size="2"></font></tr>     <font size="2"></font>      <tr><font size="2"></font>        <td style="border-width: medium 1pt 1pt; border-style: none solid solid; border-color: currentcolor windowtext windowtext; padding: 0in 5.4pt; width: 31.1pt;" valign="top" width="41"><font size="2"></font>          <p class="MsoNormal" align="center"><font size="2">7</font></p>         <font size="2"></font></td>       <font size="2"></font>        <td style="border-width: medium 1pt 1pt medium; border-style: none solid solid none; border-color: currentcolor windowtext windowtext currentcolor; padding: 0in 5.4pt; width: 160.4pt;" valign="top" width="214"><font size="2"></font>          <p class="MsoNormal"><font size="2">Custom HttpContent <strong>deriving from HttpContent<em> </em></strong>and which knows its length</font></p>         <font size="2"></font></td>       <font size="2"></font>        <td style="border-width: medium 1pt 1pt medium; border-style: none solid solid none; border-color: currentcolor windowtext windowtext currentcolor; padding: 0in 5.4pt; width: 95.75pt;" valign="top" width="128"><font size="2"></font>          <p class="MsoNormal"><font size="2">Known</font></p>         <font size="2"></font></td>       <font size="2"></font>        <td style="border-width: medium 1pt 1pt medium; border-style: none solid solid none; border-color: currentcolor windowtext windowtext currentcolor; padding: 0in 5.4pt; width: 185.4pt;" valign="top" width="247"><font size="2"></font>          <p class="MsoNormal"><font size="2">Not Buffered</font></p>         <font size="2"></font></td>       <font size="2"></font>        <td style="border-width: medium 1pt 1pt medium; border-style: none solid solid none; border-color: currentcolor windowtext windowtext currentcolor; padding: 0in 5.4pt; width: 2in;" valign="top" width="192"><font size="2"></font>          <p class="MsoNormal"><font size="2">Not Chunked</font></p>         <font size="2"></font></td>       <font size="2"></font>        <td style="border-width: medium 1pt 1pt medium; border-style: none solid solid none; border-color: currentcolor windowtext windowtext currentcolor; padding: 0in 5.4pt; width: 239.2pt;" valign="top" width="319"><font size="2"></font>          <p class="MsoNormal"><font size="2">Similar behavior as in StringContent (#1), ByteArrayContent(#2).</font></p>         <font size="2"></font></td>       <font size="2"></font></tr>     <font size="2"></font>      <tr style="mso-yfti-irow: 9;"><font size="2"></font>        <td style="border-width: medium 1pt 1pt; border-style: none solid solid; border-color: currentcolor windowtext windowtext; padding: 0in 5.4pt; width: 31.1pt;" valign="top" width="41"><font size="2"></font>          <p class="MsoNormal" align="center"><font size="2">8</font></p>         <font size="2"></font></td>       <font size="2"></font>        <td style="border-width: medium 1pt 1pt medium; border-style: none solid solid none; border-color: currentcolor windowtext windowtext currentcolor; padding: 0in 5.4pt; width: 160.4pt;" valign="top" width="214"><font size="2"></font>          <p class="MsoNormal"><font size="2">Custom HttpContent <b>deriving from StreamContent<em> </em></b>and <i></i>with inner Stream whose <strong>length</strong> <b>can be</b> <b>calculated</b>.</font></p>         <font size="2"></font></td>       <font size="2"></font>        <td style="border-width: medium 1pt 1pt medium; border-style: none solid solid none; border-color: currentcolor windowtext windowtext currentcolor; padding: 0in 5.4pt; width: 95.75pt;" valign="top" width="128"><font size="2"></font>          <p class="MsoNormal"><font size="2">Known</font></p>         <font size="2"></font></td>       <font size="2"></font>        <td style="border-width: medium 1pt 1pt medium; border-style: none solid solid none; border-color: currentcolor windowtext windowtext currentcolor; padding: 0in 5.4pt; width: 185.4pt;" valign="top" width="247"><font size="2"></font>          <p class="MsoNormal"><font size="2">Not Buffered</font></p>         <font size="2"></font></td>       <font size="2"></font>        <td style="border-width: medium 1pt 1pt medium; border-style: none solid solid none; border-color: currentcolor windowtext windowtext currentcolor; padding: 0in 5.4pt; width: 2in;" valign="top" width="192"><font size="2"></font>          <p class="MsoNormal"><font size="2">Not Chunked</font></p>         <font size="2"></font></td>       <font size="2"></font>        <td style="border-width: medium 1pt 1pt medium; border-style: none solid solid none; border-color: currentcolor windowtext windowtext currentcolor; padding: 0in 5.4pt; width: 239.2pt;" valign="top" width="319"><font size="2"></font>          <p class="MsoNormal"><font size="2">Similar behavior as in #3.</font></p>         <font size="2"></font></td>       <font size="2"></font></tr>     <font size="2"></font>      <tr style="mso-yfti-irow: 10; mso-yfti-lastrow: yes;"><font size="2"></font>        <td style="border-width: medium 1pt 1pt; border-style: none solid solid; border-color: currentcolor windowtext windowtext; padding: 0in 5.4pt; width: 31.1pt;" valign="top" width="41"><font size="2"></font>          <p class="MsoNormal" align="center"><font size="2">9</font></p>         <font size="2"></font></td>       <font size="2"></font>        <td style="border-width: medium 1pt 1pt medium; border-style: none solid solid none; border-color: currentcolor windowtext windowtext currentcolor; padding: 0in 5.4pt; width: 160.4pt;" valign="top" width="214"><font size="2"></font>          <p class="MsoNormal"><font size="2">Custom HttpContent <b>deriving from StreamContent<em> </em></b>and <i></i>with inner Stream whose <strong>length</strong> <b>cannot be</b> <b>calculated</b>.</font></p>         <font size="2"></font></td>       <font size="2"></font>        <td style="border-width: medium 1pt 1pt medium; border-style: none solid solid none; border-color: currentcolor windowtext windowtext currentcolor; padding: 0in 5.4pt; width: 95.75pt;" valign="top" width="128"><font size="2"></font>          <p class="MsoNormal"><font size="2">Not Known</font></p>         <font size="2"></font></td>       <font size="2"></font>        <td style="border-width: medium 1pt 1pt medium; border-style: none solid solid none; border-color: currentcolor windowtext windowtext currentcolor; padding: 0in 5.4pt; width: 185.4pt;" valign="top" width="247"><font size="2"></font>          <p class="MsoNormal"><font size="2">Not Buffered</font></p>         <font size="2"></font></td>       <font size="2"></font>        <td style="border-width: medium 1pt 1pt medium; border-style: none solid solid none; border-color: currentcolor windowtext windowtext currentcolor; padding: 0in 5.4pt; width: 2in;" valign="top" width="192"><font size="2"></font>          <p class="MsoNormal"><font size="2">Chunked</font></p>         <font size="2"></font></td>       <font size="2"></font>        <td style="border-width: medium 1pt 1pt medium; border-style: none solid solid none; border-color: currentcolor windowtext windowtext currentcolor; padding: 0in 5.4pt; width: 239.2pt;" valign="top" width="319"><font size="2"></font>          <p class="MsoNormal"><font size="2">Similar behavior as in #5.</font></p>         <font size="2"></font></td>       <font size="2"></font></tr>     <font size="2"></font></tbody></table>  <font size="2"></font>  <p><font size="2"><strong><font color="#ff0000">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <br clear="all" /></font></strong></font><font size="2">[1] – By “<span style="font-size: 12pt;"><font size="2"><strong>Response Buffering at Service”</strong>, I mean the buffering ability provided by the hosting layers (ex: IIS/ASP.NET in case of Web Host scenarios). </font></span></font></p>  <p><font size="2"><span style="font-size: 12pt;"><font size="2">One of the main things that we try to</font></span></font><font size="2"><span style="font-size: 12pt;"><font size="2"> do is to <em>not</em>&#160;<em>double buffer</em> the response content if we already know that a content has been buffered in the upper layers of the&#160; stack. (Example: Action is returning a HttpResponseMessage having a StringContent). We depend on the Content-Length header of the response content to figure out if a content has already been buffered or not. For example, </font></span></font><font size="2"><span style="font-size: 12pt;"><font size="2">If you notice the above table, all the content types for which we know the Content Length, we do <em>not</em> buffer again.</font></span></font></p>  <p><font size="2"><strong>       <br clear="all" />ObjectContent is Buffered</strong> : This is important because it enables to provide a good user experience in case when MediaTypeFormatters throw exceptions while writing to a stream. </font></p>  <font size="2">&#160; </font>  <p><font size="2"><strong><font color="#ff0000">NOTE</font></strong>: I deliberately left out these details for Self Host scenarios, because the above behaviors are not in symmetry with Self Host. I will create a new post in future for Self Host specifically.</font></p>&#160;&#160;&#160;&#160;&#160;&#160; <p><font size="2">Have fun!.</font>&#160;<font size="2"></font>&#160; </p>  <p><font size="2"></font></p>]]></content:encoded>
    </item>
    <item>
      <title>ASP.NET MVC4 Web API Stack Diagram – currently in development</title>
      <link>https://docs.microsoft.com/archive/blogs/kiranchalla/asp-net-mvc4-web-api-stack-diagram-currently-in-development</link>
      <pubDate>Sun, 06 May 2012 02:30:13 GMT</pubDate>
      <dc:creator><![CDATA[Kiran Challa]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/kiranchalla/2012/05/06/asp-net-mvc4-web-api-stack-diagram-currently-in-development/</guid>
      <description><![CDATA[NOTE: The following diagram is NOT a representation of the stack of Release Candidate(RC) version....]]></description>
      <content:encoded><![CDATA[<p><font size="2"><strong><font color="#ff0000">NOTE</font></strong>: The following diagram is NOT a representation of the stack of Release Candidate(RC) version. But it is something which is </font><a href="https://aspnetwebstack.codeplex.com/" target="_blank"><font size="2">currently in development</font></a><font size="2"> and most probably would be available for the final release. If you would like to look at the diagram representing the stack of RC version, you can go </font><a href="https://blogs.msdn.com/b/kiranchalla/archive/2012/05/05/asp-net-mvc4-web-api-stack-diagram.aspx" target="_blank"><font size="2">here</font></a><font size="2">.</font></p>  <font size="2"></font>  <p><font size="2">A notable difference between the following </font><a href="https://aspnetwebstack.codeplex.com/" target="_blank"><font size="2">currently in development</font></a><font size="2"> diagram and the RC version </font><a href="https://blogs.msdn.com/b/kiranchalla/archive/2012/05/05/asp-net-mvc4-web-api-stack-diagram.aspx" target="_blank"><font size="2">stack</font></a><font size="2"> is the addition of a nice feature: <strong>Ability for having route-specific message handler pipelines</strong>.</font></p>  <font size="2"></font>  <p><font size="2">Henrik provides a nice overview of this feature over </font><a href="https://blogs.msdn.com/b/henrikn/archive/2012/05/03/asp-net-web-api-updates-may-3.aspx" target="_blank"><font size="2">here</font></a><font size="2">. </font></p>  <font size="2"></font>  <br clear="all" />  <p><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/46/38/WebApiStackDiagramsNew/6428.ASP.NET_MVC4_WebAPI_StackDiagram_Future.jpg" original-url="https://blogs.msdn.com/cfs-filesystemfile.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-46-38-WebApiStackDiagramsNew/6428.ASP.NET_5F00_MVC4_5F00_WebAPI_5F00_StackDiagram_5F00_Future.jpg" target="_blank"><img style="display: inline; background-image: none;" title="ASP.NET_MVC4_WebAPI_StackDiagram_Future" border="0" alt="ASP.NET_MVC4_WebAPI_StackDiagram_Future" src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/46/38/metablogapi/6153.ASP.NET_MVC4_WebAPI_StackDiagram_Future_676D4D0D.jpg" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-46-38-metablogapi/6153.ASP.NET_5F00_MVC4_5F00_WebAPI_5F00_StackDiagram_5F00_Future_5F00_676D4D0D.jpg" width="568" height="777" /></a></p>  <p><font size="2">(click the image to get a larger image)</font><font size="2">&#160; </font></p>  <p><font size="2">Have fun!</font></p>]]></content:encoded>
    </item>
    <item>
      <title>ASP.NET MVC4 Web API Stack Diagram</title>
      <link>https://docs.microsoft.com/archive/blogs/kiranchalla/asp-net-mvc4-web-api-stack-diagram</link>
      <pubDate>Sat, 05 May 2012 20:00:49 GMT</pubDate>
      <dc:creator><![CDATA[Kiran Challa]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/kiranchalla/2012/05/05/asp-net-mvc4-web-api-stack-diagram/</guid>
      <description><![CDATA[“A picture is worth a thousand words.” :-)  NOTE: This stack represents the Release Candidate(RC)...]]></description>
      <content:encoded><![CDATA[<p><font size="2">“A picture is worth a thousand words.” :-)</font></p>  <p><font size="2"><strong>NOTE</strong>: This stack represents the Release Candidate(RC) version only. It does <strong>not</strong> represent the stack that is part of our </font><a href="https://aspnetwebstack.codeplex.com/"><font size="2">ongoing</font></a><font size="2"> development.</font></p>&#160;&#160;&#160; <br clear="all" />&#160; <p><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/46/38/WebApiStackDiagramsNew/4375.ASP.NET_MVC4_WebAPI_StackDiagram_RC.jpg" original-url="https://blogs.msdn.com/cfs-filesystemfile.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-46-38-WebApiStackDiagramsNew/4375.ASP.NET_5F00_MVC4_5F00_WebAPI_5F00_StackDiagram_5F00_RC.jpg" target="_blank"><img style="display: inline; background-image: none;" title="ASP.NET_MVC4_WebAPI_StackDiagram_RC" border="0" alt="ASP.NET_MVC4_WebAPI_StackDiagram_RC" src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/46/38/metablogapi/3146.ASP.NET_MVC4_WebAPI_StackDiagram_RC_6FDE2008.jpg" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-46-38-metablogapi/3146.ASP.NET_5F00_MVC4_5F00_WebAPI_5F00_StackDiagram_5F00_RC_5F00_6FDE2008.jpg" width="577" height="837" /></a></p>  <p><font size="2">(click the image to get a larger image)</font></p> <font size="2">I hope you find it useful.</font>]]></content:encoded>
    </item>
    <item>
      <title>In memory client, host and integration testing of your Web API service</title>
      <link>https://docs.microsoft.com/archive/blogs/kiranchalla/in-memory-client-host-and-integration-testing-of-your-web-api-service</link>
      <pubDate>Sat, 05 May 2012 17:34:10 GMT</pubDate>
      <dc:creator><![CDATA[Kiran Challa]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/kiranchalla/2012/05/05/in-memory-client-host-and-integration-testing-of-your-web-api-service/</guid>
      <description><![CDATA[One of the great things about ASP.NET MVC4 Web API is its testability.  If you have gone through my...]]></description>
      <content:encoded><![CDATA[<p><font size="2">One of the great things about ASP.NET MVC4 Web API is its testability.</font></p>  <p><font size="2">If you have gone through my post <strong><a href="https://blogs.msdn.com/b/kiranchalla/archive/2012/05/05/asp-net-mvc4-web-api-stack-diagram.aspx" target="_blank">ASP.NET MVC4 WebAPI Stack Diagram</a></strong>, you will notice that there is a block (green color) which says <strong><em>“</em>In memory client-host using no network (HttpMessageInvoker/HttpClient)”</strong>. </font><font size="2">This post is about that block and how you could do in-memory testing without using the network at all.</font></p>  <p><font size="2">Some Pros and Cons of using In-memory testing:</font></p>  <p><font size="2"><strong>Pros</strong>:</font></p>  <blockquote>   <p><font size="2">- Tests run very fast as there is no additional overhead of setting up port etc. Since these tests run very fast, you could write integration tests itself, but still expect the kind of performance that you would see in your unit tests.</font>&#160;&#160; </p> </blockquote>  <blockquote>   <p><font size="2">- Since <a href="https://blogs.msdn.com/b/kiranchalla/archive/2012/05/05/asp-net-mvc4-web-api-stack-diagram.aspx" target="_blank">HttpServer</a> is the common layer for Web Host and Self Host layers, you could test your functionality independent of the hosting layers. (Of course, if you are testing a feature which is very specific to a host, using a real host would make sense.)</font></p> </blockquote>  <p><font size="2"><strong>Cons</strong>:</font></p>  <blockquote>   <p><font size="2">- Cannot use the awesome tool <a href="https://www.fiddler2.com/fiddler2/" target="_blank">Fiddler</a> to capture the Request/Response messages!. Fiddler is a great tool for debugging purposes where you can exactly see how your request/response messages look like over the wire. Since we are testing everything in-memory, we would loose this capability. </font></p>    <p><font size="2">- When doing in-memory testing, the request and response contents are basically stream contents backed up by <em>buffered streams</em> (example: in my example in this post, you would notice that the method ConvertToStreamContent() uses MemoryStream). </font><font size="2">This might not be ideal in some situations where you might be expecting a non-buffered stream.</font></p>    <p><font size="2">- Error response messages behavior is currently different between Web Host and Self Host due to some bugs. </font></p> </blockquote>  <p><font size="2"></font></p>  <p><font size="2">In view of some of the above Pros &amp; Cons, you should be a better judge of whether in-memory kind of testing works for you or not. </font></p>  <p><font size="2"><u>Following is an example of a typical in-memory test</u>:</font></p>  <p><font size="2">Some things to consider while writing an in-memory test:</font></p>  <blockquote>   <p><font size="2">- Client to use: You can use HttpClient or HttpMessageInvoker. In fact HttpClient derives from HttpMessageInvoker. HttpMessageInvoker is lightweight compared to HttpClient, so I usually like to use it in my testing.</font></p> </blockquote>  <blockquote>   <p><font size="2">- In a real user scenario, data would be sent in a serialized format over the wire and its later deserialized at the client/service. For example, in our current case, data is serialized/deserialized by MediaTypeFormatters. Since we are doing in-memory testing, there are some caveats that we need to take care of. </font></p> </blockquote>  <blockquote>   <p><font size="2"><strong>Important: </strong>For example, the <a href="https://blogs.msdn.com/b/kiranchalla/archive/2012/05/05/asp-net-mvc4-web-api-stack-diagram.aspx" target="_blank">ParameterBinding stage</a> tries to read the incoming request’s content using the HttpContent extension called “ReadAsAsync&lt;&gt;()” . This extension has internal checks to see if the request’s content is an ObjectContent or not. If its is an ObjectContent, then it does NOT try to read the request content stream again to deserialize the data. This is a cause of concern for us here since we are doing in-memory testing and the request/response contents could be of ObjectContent type, which means that we will <em>not</em> go through the MediaTypeFormatters’ ReadFromStream or WriteToStream methods. </font></p> </blockquote>  <blockquote>   <p><font size="2">To overcome this problem, we can create a Message Handler which causes the in-memory objects to be serialized into a stream of bytes in order to make the MediaTypeFormatters to take part in the serialization/deserialization process. In the following example code, you should see the message handler <strong>InMemoryHttpContentSerializationHandler </strong>doing that.</font></p> </blockquote>  <p><font size="2"><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/46/38/6253.InMemoryClientHostIntegrationTesting.jpg" original-url="https://blogs.msdn.com/cfs-filesystemfile.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-46-38/6253.InMemoryClientHostIntegrationTesting.jpg" target="_blank"><img style="display: inline; background-image: none;" title="InMemoryClientHostIntegrationTesting" border="0" alt="InMemoryClientHostIntegrationTesting" src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/46/38/metablogapi/7288.InMemoryClientHostIntegrationTesting_37F5C27D.jpg" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-46-38-metablogapi/7288.InMemoryClientHostIntegrationTesting_5F00_37F5C27D.jpg" width="549" height="198" /></a></font></p>  <p><font size="2">(click the image to get a larger image)</font></p>  <p><font size="2">Here I am using XUnit framework for testing. I hope you can relate the following code to your test framework:</font></p>  <div style="margin: 20px 0px 10px; padding: 4px; border: 1px solid silver; width: 97.5%; text-align: left; line-height: 12pt; overflow: auto; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; cursor: text; direction: ltr; max-height: 100%; background-color: rgb(244, 244, 244);" id="codeSnippetWrapper">   <pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);" id="codeSnippet"><span style="color: rgb(0, 0, 255);">public</span> <span style="color: rgb(0, 0, 255);">class</span> OrdersController : ApiController<br />{<br />    [HttpPost]<br />    <span style="color: rgb(0, 0, 255);">public</span> Order CreateOrder(Order order)<br />    {<br />        <span style="color: rgb(0, 0, 255);">return</span> order;<br />    }<br />}<br /><br /><span style="color: rgb(0, 0, 255);">public</span> <span style="color: rgb(0, 0, 255);">class</span> InMemoryTesting<br />{<br />    [Fact]<br />    <span style="color: rgb(0, 0, 255);">public</span> <span style="color: rgb(0, 0, 255);">void</span> CreateOrderTest()<br />    {<br />        <span style="color: rgb(0, 0, 255);">string</span> baseAddress = <span style="color: rgb(0, 96, 128);">&quot;https://dummyname/&quot;</span>;<br /><br />        <span style="color: rgb(0, 128, 0);">// Server</span><br />        HttpConfiguration config = <span style="color: rgb(0, 0, 255);">new</span> HttpConfiguration();<br />        config.Routes.MapHttpRoute(<span style="color: rgb(0, 96, 128);">&quot;Default&quot;</span>, <span style="color: rgb(0, 96, 128);">&quot;api/{controller}/{action}/{id}&quot;</span>, <br />                                    <span style="color: rgb(0, 0, 255);">new</span> { id = RouteParameter.Optional });<br />        config.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;<br /><br />        HttpServer server = <span style="color: rgb(0, 0, 255);">new</span> HttpServer(config);<br /><br />        <span style="color: rgb(0, 128, 0);">// Client</span><br />        HttpMessageInvoker messageInvoker = <span style="color: rgb(0, 0, 255);">new</span> HttpMessageInvoker(<span style="color: rgb(0, 0, 255);">new</span> InMemoryHttpContentSerializationHandler(server));<br /><br />        <span style="color: rgb(0, 128, 0);">//order to be created</span><br />        Order requestOrder = <span style="color: rgb(0, 0, 255);">new</span> Order() { OrderId = <span style="color: rgb(0, 96, 128);">&quot;A101&quot;</span>, OrderValue = 125.00, OrderedDate = DateTime.Now.ToUniversalTime(), ShippedDate = DateTime.Now.AddDays(2).ToUniversalTime() };<br /><br />        HttpRequestMessage request = <span style="color: rgb(0, 0, 255);">new</span> HttpRequestMessage();<br />        request.Content = <span style="color: rgb(0, 0, 255);">new</span> ObjectContent&lt;Order&gt;(requestOrder, <span style="color: rgb(0, 0, 255);">new</span> JsonMediaTypeFormatter());<br />        request.RequestUri = <span style="color: rgb(0, 0, 255);">new</span> Uri(baseAddress + <span style="color: rgb(0, 96, 128);">&quot;api/Orders/CreateOrder&quot;</span>);<br />        request.Headers.Accept.Add(<span style="color: rgb(0, 0, 255);">new</span> MediaTypeWithQualityHeaderValue(<span style="color: rgb(0, 96, 128);">&quot;application/xml&quot;</span>));<br />        request.Method = HttpMethod.Post;<br /><br />        CancellationTokenSource cts = <span style="color: rgb(0, 0, 255);">new</span> CancellationTokenSource();<br /><br />        <span style="color: rgb(0, 0, 255);">using</span> (HttpResponseMessage response = messageInvoker.SendAsync(request, cts.Token).Result)<br />        {<br />            Assert.NotNull(response.Content);<br />            Assert.NotNull(response.Content.Headers.ContentType);<br />            Assert.Equal&lt;<span style="color: rgb(0, 0, 255);">string</span>&gt;(<span style="color: rgb(0, 96, 128);">&quot;application/xml; charset=utf-8&quot;</span>, response.Content.Headers.ContentType.ToString());<br />            Assert.Equal&lt;Order&gt;(requestOrder, response.Content.ReadAsAsync&lt;Order&gt;().Result);<br />        }<br />    }<br />}<br /><br /><span style="color: rgb(0, 0, 255);">public</span> <span style="color: rgb(0, 0, 255);">class</span> InMemoryHttpContentSerializationHandler : DelegatingHandler<br />{<br />    <span style="color: rgb(0, 0, 255);">public</span> InMemoryHttpContentSerializationHandler()<br />    {<br />    }<br /><br />    <span style="color: rgb(0, 0, 255);">public</span> InMemoryHttpContentSerializationHandler(HttpMessageHandler innerHandler)<br />        : <span style="color: rgb(0, 0, 255);">base</span>(innerHandler)<br />    {<br />    }<br /><br />    <span style="color: rgb(0, 0, 255);">protected</span> <span style="color: rgb(0, 0, 255);">override</span> Task&lt;HttpResponseMessage&gt; SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)<br />    {<br />        <span style="color: rgb(0, 128, 0);">// Replace the original content with a StreamContent before the request</span><br />        <span style="color: rgb(0, 128, 0);">// passes through upper layers in the stack</span><br />        request.Content = ConvertToStreamContent(request.Content);<br /><br />        <span style="color: rgb(0, 0, 255);">return</span> <span style="color: rgb(0, 0, 255);">base</span>.SendAsync(request, cancellationToken).ContinueWith&lt;HttpResponseMessage&gt;((responseTask) =&gt;<br />                                                                    {<br />                                                                        HttpResponseMessage response = responseTask.Result;<br /><br />                                                                        <span style="color: rgb(0, 128, 0);">// Replace the original content with a StreamContent before the response</span><br />                                                                        <span style="color: rgb(0, 128, 0);">// passes through lower layers in the stack</span><br />                                                                        response.Content = ConvertToStreamContent(response.Content);<br /><br />                                                                        <span style="color: rgb(0, 0, 255);">return</span> response;<br />                                                                    });<br />    }<br /><br />    <span style="color: rgb(0, 0, 255);">private</span> StreamContent ConvertToStreamContent(HttpContent originalContent)<br />    {<br />        <span style="color: rgb(0, 0, 255);">if</span> (originalContent == <span style="color: rgb(0, 0, 255);">null</span>)<br />        {<br />            <span style="color: rgb(0, 0, 255);">return</span> <span style="color: rgb(0, 0, 255);">null</span>;<br />        }<br /><br />        StreamContent streamContent = originalContent <span style="color: rgb(0, 0, 255);">as</span> StreamContent;<br /><br />        <span style="color: rgb(0, 0, 255);">if</span> (streamContent != <span style="color: rgb(0, 0, 255);">null</span>)<br />        {<br />            <span style="color: rgb(0, 0, 255);">return</span> streamContent;<br />        }<br /><br />        MemoryStream ms = <span style="color: rgb(0, 0, 255);">new</span> MemoryStream();<br /><br />        <span style="color: rgb(0, 128, 0);">// **** NOTE: ideally you should NOT be doing calling Wait() as its going to block this thread ****</span><br />        <span style="color: rgb(0, 128, 0);">// if the original content is an ObjectContent, then this particular CopyToAsync() call would cause the MediaTypeFormatters to </span><br />        <span style="color: rgb(0, 128, 0);">// take part in Serialization of the ObjectContent and the result of this serialization is stored in the provided target memory stream.</span><br />        originalContent.CopyToAsync(ms).Wait();<br /><br />        <span style="color: rgb(0, 128, 0);">// Reset the stream position back to 0 as in the previous CopyToAsync() call,</span><br />        <span style="color: rgb(0, 128, 0);">// a formatter for example, could have made the position to be at the end after serialization</span><br />        ms.Position = 0;<br /><br />        streamContent = <span style="color: rgb(0, 0, 255);">new</span> StreamContent(ms);<br /><br />        <span style="color: rgb(0, 128, 0);">// copy headers from the original content</span><br />        <span style="color: rgb(0, 0, 255);">foreach</span> (KeyValuePair&lt;<span style="color: rgb(0, 0, 255);">string</span>, IEnumerable&lt;<span style="color: rgb(0, 0, 255);">string</span>&gt;&gt; header <span style="color: rgb(0, 0, 255);">in</span> originalContent.Headers)<br />        {<br />            streamContent.Headers.TryAddWithoutValidation(header.Key, header.Value);<br />        }<br /><br />        <span style="color: rgb(0, 0, 255);">return</span> streamContent;<br />    }<br />}<br /></pre>

  <br /></div>

<p><font size="2">Have fun!</font></p>]]></content:encoded>
    </item>
    <item>
      <title>Content Negotiation in ASP.NET MVC4 Web API Beta – Part 2</title>
      <link>https://docs.microsoft.com/archive/blogs/kiranchalla/content-negotiation-in-asp-net-mvc4-web-api-beta-part-2</link>
      <pubDate>Sun, 26 Feb 2012 17:11:48 GMT</pubDate>
      <dc:creator><![CDATA[Kiran Challa]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/kiranchalla/2012/02/26/content-negotiation-in-asp-net-mvc4-web-api-beta-part-2/</guid>
      <description><![CDATA[This is the second post in this series related to the Default Content Negotiation that ships as part...]]></description>
      <content:encoded><![CDATA[<p><font size="2">This is the second post in this series related to the <strong>Default</strong> Content Negotiation that ships as part of <a href="https://www.asp.net/mvc/mvc4">ASP.NET MVC4 Beta</a>. I am continuing from where we left off in <a href="https://blogs.msdn.com/b/kiranchalla/archive/2012/02/25/content-negotiation-in-asp-net-mvc4-web-api-beta-part-1.aspx">Part 1</a>.</font></p>  <p><font size="2"><strong>7</strong>. Up until now</font>, <font size="2">we have seen how the Accept and Request Content-Type headers play a role in Conneg algorithm’s decision making process. </font><font size="2">There is one more way by which Conneg algorithm decides about the Response’s media type and formatter. It is called <strong>MediaTypeMapping</strong>. Every MediaTypeFormatter has a collection of these mappings in it.</font></p>  <p><font size="2">MediaTypeMapping provides a way for the user to add some custom logic and decide whether they would want the formatter to take part in writing the Response. This is different from the default way of just matching media type values(like “application/xml”) present on the Accept and Request Content-Type headers and making decisions. </font></p>  <p><font size="2"><font size="2">MediaTypeMapping provides a greater control.</font></font></p>  <p><font size="2"><u><strong>Example</strong></u>:</font></p>  <p><font size="2">Here I am creating a media type mapping which checks if the Request has a “Browser” header called “Internet Explorer”. If it finds the header, then it would inform the Conneg algorithm that it’s a full match and that the media type formatter having this media type mapping is ready to write the response.</font><font size="2"></font></p>  <div style="margin: 20px 0px 10px; padding: 4px; border: 1px solid silver; width: 97.5%; height: 227px; text-align: left; line-height: 12pt; overflow: auto; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 10pt; cursor: text; direction: ltr; max-height: 200px; background-color: rgb(244, 244, 244);" id="codeSnippetWrapper">   <pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);" id="codeSnippet"><span style="color: rgb(0, 0, 255);">public</span> <span style="color: rgb(0, 0, 255);">class</span> BrowserMediaTypeMapping : MediaTypeMapping<br />{<br />    <span style="color: rgb(0, 0, 255);">public</span> BrowserMediaTypeMapping() :<br />        <span style="color: rgb(0, 0, 255);">base</span>(<span style="color: rgb(0, 0, 255);">new</span> MediaTypeHeaderValue(<span style="color: rgb(0, 96, 128);">&quot;application/xml&quot;</span>))<br />    {<br />    }<br /><br />    <span style="color: rgb(0, 128, 0);">/// &lt;summary&gt;</span><br />    <span style="color: rgb(0, 128, 0);">/// NOTE: do NOT use this. It was too late for us to remove this method in Beta.</span><br />    <span style="color: rgb(0, 128, 0);">/// It will be gone in RC though.</span><br />    <span style="color: rgb(0, 128, 0);">/// &lt;/summary&gt;</span><br />    <span style="color: rgb(0, 128, 0);">/// &lt;param name=&quot;request&quot;&gt;&lt;/param&gt;</span><br />    <span style="color: rgb(0, 128, 0);">/// &lt;returns&gt;&lt;/returns&gt;</span><br />    <span style="color: rgb(0, 0, 255);">protected</span> <span style="color: rgb(0, 0, 255);">override</span> <span style="color: rgb(0, 0, 255);">double</span> OnTryMatchMediaType(HttpRequestMessage request)<br />    {<br />        <span style="color: rgb(0, 0, 255);">throw</span> <span style="color: rgb(0, 0, 255);">new</span> NotImplementedException();<br />    }<br /><br />    <span style="color: rgb(0, 0, 255);">protected</span> <span style="color: rgb(0, 0, 255);">override</span> <span style="color: rgb(0, 0, 255);">double</span> OnTryMatchMediaType(HttpResponseMessage response)<br />    {<br />        <span style="color: rgb(0, 0, 255);">double</span> match = 0.0; <span style="color: rgb(0, 128, 0);">//no match</span><br /><br />        <span style="color: rgb(0, 128, 0);">//get the Request object associated with this Response</span><br />        HttpRequestMessage request = response.RequestMessage;<br /><br />        <span style="color: rgb(0, 128, 0);">//check the Request's headers for the Browser header</span><br />        IEnumerable&lt;<span style="color: rgb(0, 0, 255);">string</span>&gt; values = <span style="color: rgb(0, 0, 255);">null</span>;<br />        <span style="color: rgb(0, 0, 255);">string</span> browser = <span style="color: rgb(0, 0, 255);">null</span>;<br />        <span style="color: rgb(0, 0, 255);">if</span> (request.Headers.TryGetValues(<span style="color: rgb(0, 96, 128);">&quot;Browser&quot;</span>, <span style="color: rgb(0, 0, 255);">out</span> values))<br />        {<br />            browser = values.First();<br />        }<br /><br />        <span style="color: rgb(0, 0, 255);">if</span> (browser != <span style="color: rgb(0, 0, 255);">null</span> &amp;&amp; browser.ToLowerInvariant() == <span style="color: rgb(0, 96, 128);">&quot;internetexplorer&quot;</span>)<br />        {<br />            match = 1.0; <span style="color: rgb(0, 128, 0);">// full match</span><br />        }<br /><br />        <span style="color: rgb(0, 0, 255);">return</span> match;<br />    }<br />}</pre>

  <br /></div>

<p><font size="2">As I mentioned earlier, each MediaTypeFormatter object has a collection of media type mappings available through the MediaTypeMappings property. So, we can add our BrowserMediaTypeMapping object in that collection like below:</font></p>

<div style="margin: 20px 0px 10px; padding: 4px; border: 1px solid silver; width: 97.5%; text-align: left; line-height: 12pt; overflow: auto; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; cursor: text; direction: ltr; max-height: 200px; background-color: rgb(244, 244, 244);" id="codeSnippetWrapper">
  <pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);" id="codeSnippet"><span style="color: rgb(0, 0, 255);">public</span> SetupSelfHost()<br />{<br />    HttpSelfHostConfiguration config = <span style="color: rgb(0, 0, 255);">new</span> HttpSelfHostConfiguration(<span style="color: rgb(0, 0, 255);">this</span>.BaseAddress);<br />    config.Routes.MapHttpRoute(<span style="color: rgb(0, 96, 128);">&quot;Default&quot;</span>, <span style="color: rgb(0, 96, 128);">&quot;{action}&quot;</span>, <span style="color: rgb(0, 0, 255);">new</span> { controller = <span style="color: rgb(0, 96, 128);">&quot;MappingTests&quot;</span> });<br /><br />    config.Formatters.XmlFormatter.MediaTypeMappings.Add(<span style="color: rgb(0, 0, 255);">new</span> BrowserMediaTypeMapping());<br /><br />    server = <span style="color: rgb(0, 0, 255);">new</span> HttpSelfHostServer(config);<br />    server.OpenAsync().Wait();<br />}</pre>

  <br /></div>

<p><font size="2"><strong>NOTE</strong>: In the above code snippet, I am just adding the mapping to the existing default formatter collection which has the formatters (in this order): JsonMediaTypeFormatter, XmlMediaTypeFormatter &amp; FormUrlEncodedMediaTypeFormatter.</font></p>

<p><font size="2"></font></p>

<p><font size="2">Now let’s see an example of how the Conneg algorithm behaves when a Request has the “Browser” header:</font></p>

<blockquote>
  <p><font size="2"><strong>Request</strong>:</font></p>

  <p><font size="2">GET </font><font size="2">https://kirandev10:9090/MappingTests/GetString</font><font size="2"> HTTP/1.1 
      <br /><font color="#008000"><strong>Browser: InternetExplorer
          <br /></strong></font>Host: kirandev10:9090 

      <br />Connection: Keep-Alive</font></p>

  <p><font size="2"><strong>Response</strong>:</font></p>

  <p><font size="2">HTTP/1.1 200 OK 
      <br />Content-Length: 60 

      <br /><strong><font color="#008000">Content-Type: application/xml 
          <br /></font></strong>Server: Microsoft-HTTPAPI/2.0 

      <br />Date: Sun, 26 Feb 2012 23:58:43 GMT</font></p>
  <font size="2"></font>

  <p><font size="2">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;&lt;string&gt;Hello&lt;/string&gt;</font></p>
</blockquote>

<p><font size="2">In the above scenario, a GET request is sent without any Accept header. Usually if this kind of request is sent, Conneg algorithm would pick the 1st formatter in the list of formatters and that would be the JsonMediaTypeFormatter.</font></p>

<p><font size="2">But, since the request has a “Browser” header with the value “Internet Explorer”, the XmlMediaTypeFormatter’s BrowserMediaTypeMapping checks this and responds to the Conneg algorithm by giving it a full match.</font></p>

<p><font size="2">Hence we are seeing the Response in a “application/xml” format instead of the default “application/json” format.</font></p>

<p><font size="2">As you can imagine, MediaTypeMapping provides full power to you to inspect incoming request and make a decision of whether to participate in a response or not. </font></p>

<p><font size="2">By default, </font><a href="https://www.asp.net/mvc/mvc4"><font size="2">ASP.NET MVC4 Beta</font></a><font size="2"> Web API ships with 4 built-in media type mappings. They are:</font></p>

<blockquote>
  <p><font size="2"><em>QueryStringMapping</em></font></p>

  <p><font size="2"><em>RequestHeaderMapping</em></font></p>

  <p><font size="2"><em>UriPathExtensionMapping</em></font></p>

  <p><font size="2"><em>MediaRangeMapping</em></font></p>
</blockquote>

<p><font size="2">I will discuss more about these individually in my later posts.</font></p>

<p><strong><font size="3">What happens when multiple formatters match an incoming request’s criteria? Which one does the Conneg algorithm choose?</font></strong></p>

<p><font size="2">During the Conneg algorithm run, based on bunch of criteria like the Request Accept header, Content-Type header, MediaTypeMapping etc, there is always a possibility that more than one formatter could indicate its availability in writing the Response. As you can imagine, the Conneg algorithm has to choose only one formatter in the end.</font></p>
<font size="2"></font>

<p><font size="2">The Default Conneg algorithm has the following <em>precedence order </em>to select the final formatter:</font></p>
<font size="2"></font>

<blockquote>
  <p><font size="2">1. <strong>Formatter match based on Media Type Mapping</strong>.</font></p>

  <p><font size="2">2. <strong>Formatter match based on Request Accept header's media type</strong>.</font></p>

  <p><font size="2">3. <strong>Formatter match based on Request Content-Type header's media type</strong>.</font></p>

  <p><font size="2">4. <strong>Formatter match based on if it can serialize the response data’s Type</strong>.</font></p>
</blockquote>

<p><font size="2">In spite of all this, as mentioned in “Point <strong>6.</strong>”, if a user decides to always send a Response in a particular media type, Conneg algorithm will honor it and will not override it.</font></p>]]></content:encoded>
    </item>
    <item>
      <title>Content Negotiation in ASP.NET MVC4 Web API Beta – Part 1</title>
      <link>https://docs.microsoft.com/archive/blogs/kiranchalla/content-negotiation-in-asp-net-mvc4-web-api-beta-part-1</link>
      <pubDate>Sat, 25 Feb 2012 14:58:37 GMT</pubDate>
      <dc:creator><![CDATA[Kiran Challa]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/kiranchalla/2012/02/25/content-negotiation-in-asp-net-mvc4-web-api-beta-part-1/</guid>
      <description><![CDATA[This is my first blog post…Yay! .  In this blog post, I am going to describe about the Default...]]></description>
      <content:encoded><![CDATA[<p><font size="2">This is my first blog post…Yay! <img class="wlEmoticon wlEmoticon-smile" alt="Smile" src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/46/38/metablogapi/5504.wlEmoticon-smile_780E4DAB.png" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-46-38-metablogapi/5504.wlEmoticon_2D00_smile_5F00_780E4DAB.png" />.</font></p>  <p><font size="2">In this blog post, I am going to describe about the <strong>Default</strong> Content Negotiation Algorithm that gets shipped as part of </font><a href="https://www.asp.net/mvc/mvc4"><font size="2">ASP.NET MVC4 Beta</font></a><font size="2">. The Web API provides a lot of nice features with which you can build RESTful services. For more details, you can look at the release notes </font><a href="https://www.asp.net/whitepapers/mvc4-release-notes#_Toc303253807"><font size="2">here</font></a><font size="2">. </font></p>  <p><font size="2">Throughout the rest of this post, I will refer to Content-Negotiation simply as <em>Conneg</em>.</font></p>  <p><font size="2">I use the tool called <a href="https://www.fiddler2.com/fiddler2/">Fiddler</a> to capture the requests and responses. I find it very useful for debugging purposes.</font></p>  <p><font size="2"><strong>1. </strong>If a Request message is sent with an Accept header, Conneg algorithm will use the Accept header to decide about the Response media type and the MediaTypeFormatter to write.       <br />This should make sense to you as here the client is asking for a response in a specific format.</font></p>  <blockquote>   <p><font size="2"><strong><u>Example</u></strong>:</font></p>    <p><font size="2"><strong>Request</strong>:</font></p>    <p><font size="2">GET https://kirandev:9090/DefaultConNegAlgorithmTests/GetData HTTP/1.1        <br /></font><font size="2"><strong><font color="#008000">Accept: application/xml</font>          <br /></strong>Host: kirandev10:9090         <br />Connection: Keep-Alive</font></p>   <font size="2"></font>    <p><font size="2"><strong>Response</strong>:</font></p>    <p><font size="2">HTTP/1.1 200 OK        <br />Content-Length: 60         <br /><font color="#008000"><strong>Content-Type: application/xml; charset=utf-8</strong></font>        <br />Server: Microsoft-HTTPAPI/2.0         <br />Date: Sat, 25 Feb 2012 21:03:47 GMT</font></p>   <font size="2"></font>    <p><font size="2">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;&lt;string&gt;Hello&lt;/string&gt;</font></p> </blockquote>  <p><font size="2"><strong>2</strong>. If a Request message is sent with NO Accept header and with a Content-Type header (let’s say when you are using POST to post content to the server),&#160; Conneg algorithm will use the Content-Type header to decide about the Response media type and the MediaTypeFormatter to write. </font></p>  <p><font size="2">Here the Conneg algorithm uses the <em>best-effort approach</em> to judge which format the client could understand. Since here the client was able to post content in a particular format(let’s say ‘application/xyz’), Conneg algorithm deduces that the Client could probably understand the Response also in the same format(‘application/xyz’).</font></p> <font size="2"></font>  <blockquote>   <p><font size="2"><strong><u>Example</u></strong>:</font></p>    <p><font size="2"><strong>Request</strong>:</font></p>    <p><font size="2">POST https://kirandev:9090/DefaultConNegAlgorithmTests/PostData HTTP/1.1        <br /></font><font size="2"><strong><font color="#008000">Content-Type: application/xml</font>          <br /></strong>Host: kirandev10:9090         <br />Content-Length: 60         <br />Expect: 100-continue         <br />Connection: Keep-Alive</font></p>   <font size="2"></font>    <p><font size="2">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;&lt;string&gt;Hello&lt;/string&gt;</font></p>   <font size="2"></font>    <p><font size="2"><strong>Response</strong>:</font></p>    <p><font size="2">HTTP/1.1 200 OK        <br />Content-Length: 60         <br /></font><font size="2"><strong><font color="#008000">Content-Type: application/xml; charset=utf-8            <br /></font></strong>Server: Microsoft-HTTPAPI/2.0         <br />Date: Sat, 25 Feb 2012 21:12:34 GMT</font></p>   <font size="2"></font>    <p><font size="2">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;&lt;string&gt;Hello&lt;/string&gt;</font></p> </blockquote> <font size="2"></font>  <p><font size="2"><strong>3.</strong> If a Request message is sent with an Accept header and also a Content-Type header, Conneg algorithm will use the Accept header to decide about the Response media type and the MediaTypeFormatter to write.       <br />This should make sense to you as here the Client is asking for a Response in a specific format.</font></p> <font size="2"></font>  <blockquote>   <p><font size="2"><strong><u>Example</u></strong>:</font></p>    <p><font size="2"><strong>Request</strong>:</font></p>    <p><font size="2">POST https://kirandev10:9090/DefaultConNegAlgorithmTests/PostData HTTP/1.1        <br /><strong><font color="#008000">Accept: application/xml</font></strong>        <br /><strong><font color="#ff8040">Content-Type: application/json</font></strong>        <br />Host: kirandev10:9090         <br />Content-Length: 7         <br />Expect: 100-continue         <br />Connection: Keep-Alive</font></p>   <font size="2"></font>    <p><font size="2">&quot;Hello&quot;</font></p>   <font size="2"></font>    <p><font size="2"><strong>Response</strong>:</font></p>    <p><font size="2">HTTP/1.1 200 OK        <br />Content-Length: 60         <br /><strong><font color="#008000">Content-Type: application/xml; charset=utf-8</font></strong>        <br />Server: Microsoft-HTTPAPI/2.0         <br />Date: Sat, 25 Feb 2012 21:09:59 GMT</font></p>   <font size="2"></font>    <p><font size="2">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;&lt;string&gt;Hello&lt;/string&gt;</font></p> </blockquote> <font size="2"></font>  <p><font size="2"><strong>4.</strong> A variation to “Point <strong>3</strong>.” above: If a Request message is sent with an Accept header and also a Content-Type header, then Conneg algorithm will use the Accept header to decide about the Response media type and the MediaTypeFormatter to write.&#160; </font></p>  <p><font size="2">Now, if the Conneg algorithm <em>wasn’t able to find a formatter for the supplied media type in the Accept header</em>, then it uses the <em>best-effort approach</em> and uses the Content-Type header to decide about the Response media type and the MediaTypeFormatter to write.</font></p> <font size="2"></font>  <blockquote>   <p><font size="2"><strong><u>Example</u></strong>:</font></p>    <p><font size="2"><strong>Request</strong>:</font></p>    <p><font size="2">POST https://kirandev:9090/DefaultConNegAlgorithmTests/PostData HTTP/1.1        <br /><font color="#ff0000">Accept: application/nonexisting</font>        <br /></font><font size="2"><strong><font color="#008000">Content-Type: application/xml            <br /></font></strong>Host: kirandev10:9090         <br />Content-Length: 60         <br />Expect: 100-continue         <br />Connection: Keep-Alive</font></p>   <font size="2"></font>    <p><font size="2">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;&lt;string&gt;Hello&lt;/string&gt;</font></p>   <font size="2"></font>    <p><font size="2"><strong>Response</strong>:</font></p>    <p><font size="2">HTTP/1.1 200 OK        <br />Content-Length: 60         <br /></font><font size="2"><strong><font color="#008000">Content-Type: application/xml; charset=utf-8            <br /></font></strong>Server: Microsoft-HTTPAPI/2.0         <br />Date: Sat, 25 Feb 2012 21:15:50 GMT</font></p>   <font size="2"></font>    <p><font size="2">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;&lt;string&gt;Hello&lt;/string&gt;</font></p> </blockquote> <font size="2"></font>  <p><font size="2"><strong>5.</strong> For ALL the above scenarios from 1 to 4, if the Conneg algorithm wasn’t able to find a suitable formatter to write the Response, then by default it would send back content using the 1st formatter in the collection of MediaTypeFormatters present on the Configuration object. </font></p> <font size="2"></font>  <blockquote>   <p><font size="2"><u><strong>Example</strong></u>:</font></p>    <p><font size="2"><strong>Request</strong>:</font></p>    <p><font size="2">GET https://kirandev10:9090/DefaultConNegAlgorithmTests/GetData HTTP/1.1        <br /><font color="#ff0000">Accept: application/nonexisting</font>        <br />Host: kirandev10:9090         <br />Connection: Keep-Alive</font></p>   <font size="2"></font>    <p><font size="2"><strong>Response</strong>(<em>Here the 1st formatter in my list is a JsonMediaTypeFormatter and hence the response is in Json format</em>):</font></p>    <p><font size="2">HTTP/1.1 200 OK        <br />Content-Length: 7         <br /><strong><font color="#008000">Content-Type: application/json; charset=utf-8</font></strong>        <br />Server: Microsoft-HTTPAPI/2.0         <br />Date: Sat, 25 Feb 2012 21:25:40 GMT</font></p>   <font size="2"></font>    <p><font size="2">&quot;Hello&quot;</font></p> </blockquote> <font size="2"></font>  <p><font size="2"></font></p>  <p><font size="2"><strong>6</strong>. If for a particular reason, a user needs to always send a Response in a specific format and NOT caring about the Content Negotiation, we have support for it. For example, in the following piece of code, the Action called “GetOrder” is specifically setting the media type to be “application/xml”. So, Web API honors this and will not let the Conneg algorithm to overwrite it irrespective of the Request’s Accept header media type.</font></p>  <blockquote>   <pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">class</span> OrdersController : ApiController
    {
        <span class="kwrd">public</span> HttpResponseMessage GetOrder()
        {
            Order order = <span class="kwrd">new</span> Order();
            order.Id = <span class="str">&quot;A100&quot;</span>;
            order.OrderedDate = DateTime.Now;
            order.OrderTotal = 150.00;

            HttpResponseMessage&lt;Order&gt; response = <span class="kwrd">new</span> HttpResponseMessage&lt;Order&gt;(order, 
                        <span class="kwrd">new</span> MediaTypeHeaderValue(<span class="str"><font style="background-color: rgb(255, 255, 0);">&quot;<strong>application/xml</strong>&quot;</font></span>));

            <span class="kwrd">return</span> response;
        }
    }</pre>
</blockquote>


<blockquote>
  <p><font size="2"><strong><u>Example</u></strong>:</font></p>
</blockquote>

<blockquote>
  <p><font size="2"><strong>Request</strong>:</font></p>
</blockquote>

<blockquote>
  <p><font size="2">GET https://kirandev:9090/DefaultConNegAlgorithmTests/GetOrder HTTP/1.1 
      <br /></font><font size="2"><strong><font color="#008000">Accept: application/json</font>

        <br /></strong>Host: kirandev10:9090 

      <br />Connection: Keep-Alive</font></p>
</blockquote>

<blockquote>
  <p><font size="2"><strong>Response</strong>:</font></p>

  <p><font size="2">HTTP/1.1 200 OK 
      <br />Content-Length: 253 

      <br /><font style="background-color: rgb(255, 255, 0);"><strong>Content-Type: application/xml; charset=utf-8</strong></font>

      <br />Server: Microsoft-HTTPAPI/2.0 

      <br />Date: Sat, 25 Feb 2012 22:36:22 GMT</font></p>
  <font size="2"></font>

  <p><font size="2">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;&lt;Order xmlns:xsd=&quot;https://www.w3.org/2001/XMLSchema&quot; xmlns:xsi=&quot;https://www.w3.org/2001/XMLSchema-instance&quot;&gt;&lt;Id&gt;A100&lt;/Id&gt;&lt;OrderedDate&gt;2012-02-25T14:36:21.4774615-08:00&lt;/OrderedDate&gt;&lt;OrderTotal&gt;150&lt;/OrderTotal&gt;&lt;/Order&gt;</font></p>
</blockquote>

<p><font size="2"></font></p>

<p><font size="2">I hope you guys find this post useful. I will be posting more content in near future. Keep playing with our bits!</font></p>

<p><font size="2">Also there is a forum regarding ASP.NET Web API over </font><a href="https://forums.asp.net/1246.aspx/"><font size="2">here</font></a><font size="2">. It’s very active and post any questions that you might have. </font></p>]]></content:encoded>
    </item>
  </channel>
</rss>