<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://weblogs.asp.net/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Glavs Blog</title><link>http://weblogs.asp.net/pglavich/default.aspx</link><description>The dotDude of .Net</description><dc:language>en</dc:language><generator>CommunityServer 2007 SP1 (Build: 20510.895)</generator><item><title>Owin, Katana and getting started</title><link>http://weblogs.asp.net/pglavich/archive/2013/04/05/owin-katana-and-getting-started.aspx</link><pubDate>Thu, 04 Apr 2013 22:19:22 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:10095754</guid><dc:creator>Glav</dc:creator><slash:comments>3</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/pglavich/rsscomments.aspx?PostID=10095754</wfw:commentRss><comments>http://weblogs.asp.net/pglavich/archive/2013/04/05/owin-katana-and-getting-started.aspx#comments</comments><description>&lt;h3&gt;Introduction&lt;/h3&gt;  &lt;p&gt;This article describes an emerging open source specification, referred to as &lt;a href="http://owin.org/" target="_blank"&gt;Owin – An Open Web Interface for .Net&lt;/a&gt;, what it is, and how this might be beneficial to the .Net technology stack. It will also provide a brief, concise look at how to get started with &lt;a href="http://owin.org/" target="_blank"&gt;Owin&lt;/a&gt; and related technologies. In my initial investigations with &lt;a href="http://owin.org/" target="_blank"&gt;Owin&lt;/a&gt; and how to play with it, I found lots of conflicting documentation and unclear ways of how to make it work and why. This article is an attempt to better articulate those steps so that you may save yourself lots of time and come to a clearer understanding in a much shorter time than I did.&lt;/p&gt;  &lt;p&gt;Note: The code for this article can be download from &lt;a href="https://github.com/glav/Glav.Owin.Middleware" target="_blank"&gt;here&lt;/a&gt;.&lt;/p&gt;  &lt;h3&gt;First, What is it?&lt;/h3&gt;  &lt;p&gt;Just in case you are not aware, a community driven project referred to as &lt;a href="http://owin.org/" target="_blank"&gt;Owin&lt;/a&gt; is really gaining traction. &lt;a href="http://owin.org/" target="_blank"&gt;Owin&lt;/a&gt; is a simple specification that describes how components in a HTTP pipeline should communicate. The details of what is in the communication between components is specific to each component, however there are some common elements to each. &lt;a href="http://owin.org/" target="_blank"&gt;Owin&lt;/a&gt; itself is not a technology, just a specification.&lt;/p&gt;  &lt;p&gt;I am going to gloss over many details here in order to remain concise, but at its very core, &lt;a href="http://owin.org/" target="_blank"&gt;Owin&lt;/a&gt; describes 1 main component which is the following interface:&lt;/p&gt;  &lt;p&gt;&lt;font face="Courier New"&gt;&lt;font color="#4bacc6"&gt;Func&lt;/font&gt;&amp;lt;&lt;font color="#4bacc6"&gt;IDictionary&lt;/font&gt;&amp;lt;&lt;font color="#0000ff"&gt;string&lt;/font&gt;, &lt;font color="#0000ff"&gt;object&lt;/font&gt;&amp;gt;,&lt;font color="#4bacc6"&gt;Task&lt;/font&gt;&amp;gt;&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;This is a function that accepts a simple dictionary of objects, keyed by a string identifier. The function itself returns a task. The object in the dictionary in this instance will vary depending on what the key is referring to.&lt;/p&gt;  &lt;p&gt;More often, you will see it referenced like this:&lt;/p&gt;  &lt;p&gt;&lt;font face="Courier New"&gt;&lt;font color="#0000ff"&gt;using&lt;/font&gt; AppFunc = &lt;font face="Courier New"&gt;&lt;font color="#4bacc6"&gt;Func&lt;/font&gt;&amp;lt;&lt;font color="#4bacc6"&gt;IDictionary&lt;/font&gt;&amp;lt;&lt;font color="#0000ff"&gt;string&lt;/font&gt;, &lt;font color="#0000ff"&gt;object&lt;/font&gt;&amp;gt;,&lt;font color="#4bacc6"&gt;Task&lt;/font&gt;&amp;gt;&lt;/font&gt;&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;An actual implementation might look like this:&lt;/p&gt;  &lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; Task Invoke(IDictionary&amp;lt;&lt;span class="kwrd"&gt;string&lt;/span&gt;, &lt;span class="kwrd"&gt;object&lt;/span&gt;&amp;gt; environment) 
{ 
   var someObject = environment[“some.key”] &lt;span class="kwrd"&gt;as&lt;/span&gt; SomeObject; 
   &lt;span class="rem"&gt;// etc… &lt;/span&gt;
}&lt;/pre&gt;
&lt;style type="text/css"&gt;
















.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;

&lt;p&gt;This is essentially how the “environment” or information about the HTTP context is passed around. Looking at the environment argument of this method, you could interrogate it as follows:&lt;/p&gt;

&lt;pre class="csharpcode"&gt;var httpRequestPath = environment[“owin.RequestPath”] &lt;span class="kwrd"&gt;as&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt;; 
Console.WriteLine(“Your Path &lt;span class="kwrd"&gt;is&lt;/span&gt;: [{0}]”,httpRequestPath);&lt;/pre&gt;
&lt;style type="text/css"&gt;
















.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;

&lt;p&gt;if a HttpRequest was being made to ‘&lt;font face="Courier New"&gt;http://localhost:8080/Content/Main.css&lt;/font&gt;” then the output would be:&lt;/p&gt;

&lt;p&gt;&lt;font face="Courier New"&gt;Your Path is [/Content/Main.css]&lt;/font&gt;&lt;/p&gt;

&lt;p&gt;In addition, while not part of the spec, the &lt;font color="#4bacc6"&gt;IAppBuilder&lt;/font&gt; interface is also core to the functioning of an &lt;a href="http://owin.org/" target="_blank"&gt;Owin&lt;/a&gt; module (or ‘middleware’ in &lt;a href="http://owin.org/" target="_blank"&gt;Owin&lt;/a&gt; speak):&lt;/p&gt;

&lt;p&gt;&lt;font face="Courier New"&gt;&lt;font color="#0000ff"&gt;public interface&lt;/font&gt; &lt;font color="#4bacc6"&gt;IAppBuilder&lt;/font&gt; 

    &lt;br /&gt;{ 

    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;font color="#4bacc6"&gt;IDictionary&lt;/font&gt;&amp;lt;&lt;font color="#0000ff"&gt;string&lt;/font&gt;, &lt;font color="#0000ff"&gt;object&lt;/font&gt;&amp;gt; Properties { &lt;font color="#0000ff"&gt;get&lt;/font&gt;; }&lt;/font&gt;&lt;/p&gt;

&lt;p&gt;&lt;font face="Courier New"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;font color="#0000ff"&gt;object&lt;/font&gt; Build(Type returnType); 

    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;font color="#4bacc6"&gt;IAppBuilder&lt;/font&gt; New(); 

    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;font color="#4bacc6"&gt;IAppBuilder&lt;/font&gt; Use(&lt;font color="#0000ff"&gt;object&lt;/font&gt; middleware, &lt;font color="#0000ff"&gt;params object&lt;/font&gt;[] args); 

    &lt;br /&gt;}&lt;/font&gt;&lt;/p&gt;

&lt;p&gt;The &lt;font color="#4bacc6"&gt;IAppBuilder&lt;/font&gt; interface acts as the ‘glue’ or host to bring any registered &lt;a href="http://owin.org/" target="_blank"&gt;Owin&lt;/a&gt; compatible libraries/modules together.&lt;/p&gt;

&lt;h3&gt;So what? I can do that now without Owin.&lt;/h3&gt;

&lt;p&gt;Okay so it may not look ground breaking, but this is the core of &lt;a href="http://owin.org/" target="_blank"&gt;Owin&lt;/a&gt; and key to understanding what it offers. Since we have a basic understanding of the how, I am going to jump to what it currently offers as a result of supporting this mechanism. With support of this simple mechanism, I can now write isolated components that deal with specific parts of functionality related to Http requests. I can then chain them together to build capabilities of my Http server. Internet Information Server does not need to get a look in. You can literally chain together &lt;a href="http://owin.org/" target="_blank"&gt;Owin&lt;/a&gt; components to form a pipeline of only the necessary features you want. In addition, the components I write do not have to have specific references to any particular pipeline component, and yet can still take advantage of any &lt;a href="http://owin.org/" target="_blank"&gt;Owin&lt;/a&gt; compatible component. &lt;/p&gt;

&lt;p&gt;Basically you can build or use a custom host, then you can insert whatever custom modules into the Http request processing pipeline. &lt;a href="http://owin.org/" target="_blank"&gt;Owin&lt;/a&gt; provides the specification for writing those modules that make it easy to insert into the chain.&lt;/p&gt;

&lt;p&gt;Internet Information Services comes with a plethora of pipeline and request handlers, management functionality, knobs, dials and features that make a comprehensive web server. However many people want a lean, cut down web server with only the features they want. This provides a much leaner pipeline that can provide great performance, in addition to only providing the feature set you want, meaning a smaller surface area which can be beneficial from a complexity and sometimes a security perspective.&lt;/p&gt;

&lt;h3&gt;Quit yer jibba jabba fool, show me some code&amp;#160; &lt;a href="http://weblogs.asp.net/blogs/pglavich/MrT1_460ACF25.jpg"&gt;&lt;img title="MrT1" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; float: none; padding-top: 0px; padding-left: 0px; margin-left: auto; display: block; padding-right: 0px; border-top-width: 0px; margin-right: auto" border="0" alt="MrT1" src="http://weblogs.asp.net/blogs/pglavich/MrT1_thumb_6B281991.jpg" width="187" height="187" /&gt;&lt;/a&gt;&lt;/h3&gt;

&lt;p&gt;Yes yes, I will, but first, a diagram illustrating the previous paragraph. As already mentioned, &lt;a href="http://owin.org/" target="_blank"&gt;Owin&lt;/a&gt; is just a specification of what interface to expose and how to work with that interface.In order for modules written to the &lt;a href="http://owin.org/" target="_blank"&gt;Owin&lt;/a&gt; spec to be functional, they must exist in a host. So at a conceptual level, &lt;a href="http://owin.org/" target="_blank"&gt;Owin&lt;/a&gt; compared to Internet Information Server looks like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="http://weblogs.asp.net/blogs/pglavich/slide1_62C4443A.png"&gt;&lt;img title="slide1" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="slide1" src="http://weblogs.asp.net/blogs/pglavich/slide1_thumb_2868CE59.png" width="522" height="244" /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Since we have an empty &lt;a href="http://owin.org/" target="_blank"&gt;Owin&lt;/a&gt; host, we can programmatically add &lt;a href="http://owin.org/" target="_blank"&gt;Owin&lt;/a&gt; compatible modules based on only what our needs are as illustrated below:&lt;/p&gt;

&lt;p&gt;&lt;a href="http://weblogs.asp.net/blogs/pglavich/slide2_4DF24BBA.png"&gt;&lt;img title="slide2" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; float: none; padding-top: 0px; padding-left: 0px; margin-left: auto; display: block; padding-right: 0px; border-top-width: 0px; margin-right: auto" border="0" alt="slide2" src="http://weblogs.asp.net/blogs/pglavich/slide2_thumb_0CE3CC56.png" width="285" height="244" /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The diagram above shows an &lt;a href="http://owin.org/" target="_blank"&gt;Owin&lt;/a&gt; host with 3 modules registered to be used, Static files,&amp;#160; Authentication and &lt;a href="http://signalr.net/" target="_blank"&gt;SignalR&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;(Note: &lt;a href="http://signalr.net/" target="_blank"&gt;SignalR&lt;/a&gt; is a popular real-time web messaging/connection library and has been written as an &lt;a href="http://owin.org/" target="_blank"&gt;Owin&lt;/a&gt; compatible module) &lt;/p&gt;

&lt;h3&gt;Are we there yet?&lt;/h3&gt;

&lt;p&gt;Ok, so lets finally write an &lt;a href="http://owin.org/" target="_blank"&gt;Owin&lt;/a&gt; compatible module or library. &lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Within Visual Studio, Create a new blank solution. &lt;/li&gt;

  &lt;li&gt;Add a new class library project to it. &lt;/li&gt;

  &lt;li&gt;Install the &lt;a href="http://owin.org/" target="_blank"&gt;Owin&lt;/a&gt; nuget package using: 

    &lt;br /&gt;&lt;font face="Courier New"&gt;Install-Package Owin &lt;/font&gt;

    &lt;br /&gt;(via the package manager console in Visual Studio or right click on the project in Visual Studio and select Manage Nuget Packages) &lt;/li&gt;

  &lt;li&gt;Add a new class, lets call it &lt;font face="Courier New"&gt;TestLogger&lt;/font&gt; and paste in the following code: 

    &lt;br /&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;style type="text/css"&gt;
















.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;

&lt;pre style="overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; border-bottom: #cecece 1px solid; padding-bottom: 5px; padding-top: 5px; padding-left: 5px; min-height: 40px; border-left: #cecece 1px solid; padding-right: 5px; width: 500px; background-color: #fbfbfb"&gt;&lt;pre style="font-size: 11px; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; margin: 0em; width: 100%; background-color: #fbfbfb"&gt;&lt;span style="color: #0000ff"&gt;using&lt;/span&gt; System;
&lt;/pre&gt;&lt;pre style="font-size: 11px; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; margin: 0em; width: 100%; background-color: #fbfbfb"&gt;&lt;span style="color: #0000ff"&gt;using&lt;/span&gt; System.Collections.Generic;
&lt;/pre&gt;&lt;pre style="font-size: 11px; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; margin: 0em; width: 100%; background-color: #fbfbfb"&gt;&lt;span style="color: #0000ff"&gt;using&lt;/span&gt; System.Threading.Tasks;
&lt;/pre&gt;&lt;pre style="font-size: 11px; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; margin: 0em; width: 100%; background-color: #fbfbfb"&gt;&lt;/pre&gt;&lt;pre style="font-size: 11px; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; margin: 0em; width: 100%; background-color: #fbfbfb"&gt;&lt;span style="color: #0000ff"&gt;using&lt;/span&gt; AppFunc = Func&amp;lt;IDictionary&amp;lt;&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;, &lt;span style="color: #0000ff"&gt;object&lt;/span&gt;&amp;gt;, Task&amp;gt;;
&lt;/pre&gt;&lt;pre style="font-size: 11px; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; margin: 0em; width: 100%; background-color: #fbfbfb"&gt;&lt;/pre&gt;&lt;pre style="font-size: 11px; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; margin: 0em; width: 100%; background-color: #fbfbfb"&gt;    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; TestLogger
&lt;/pre&gt;&lt;pre style="font-size: 11px; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; margin: 0em; width: 100%; background-color: #fbfbfb"&gt;    {
&lt;/pre&gt;&lt;pre style="font-size: 11px; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; margin: 0em; width: 100%; background-color: #fbfbfb"&gt;        &lt;span style="color: #0000ff"&gt;private&lt;/span&gt; &lt;span style="color: #0000ff"&gt;readonly&lt;/span&gt; AppFunc _next;
&lt;/pre&gt;&lt;pre style="font-size: 11px; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; margin: 0em; width: 100%; background-color: #fbfbfb"&gt;&lt;/pre&gt;&lt;pre style="font-size: 11px; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; margin: 0em; width: 100%; background-color: #fbfbfb"&gt;        &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; TestLogger(AppFunc next)
&lt;/pre&gt;&lt;pre style="font-size: 11px; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; margin: 0em; width: 100%; background-color: #fbfbfb"&gt;        {
&lt;/pre&gt;&lt;pre style="font-size: 11px; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; margin: 0em; width: 100%; background-color: #fbfbfb"&gt;            &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (next == &lt;span style="color: #0000ff"&gt;null&lt;/span&gt;)
&lt;/pre&gt;&lt;pre style="font-size: 11px; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; margin: 0em; width: 100%; background-color: #fbfbfb"&gt;            {
&lt;/pre&gt;&lt;pre style="font-size: 11px; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; margin: 0em; width: 100%; background-color: #fbfbfb"&gt;                &lt;span style="color: #0000ff"&gt;throw&lt;/span&gt; &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; ArgumentNullException(&amp;quot;&lt;span style="color: #8b0000"&gt;next&lt;/span&gt;&amp;quot;);
&lt;/pre&gt;&lt;pre style="font-size: 11px; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; margin: 0em; width: 100%; background-color: #fbfbfb"&gt;            }
&lt;/pre&gt;&lt;pre style="font-size: 11px; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; margin: 0em; width: 100%; background-color: #fbfbfb"&gt;            _next = next;
&lt;/pre&gt;&lt;pre style="font-size: 11px; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; margin: 0em; width: 100%; background-color: #fbfbfb"&gt;        }
&lt;/pre&gt;&lt;pre style="font-size: 11px; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; margin: 0em; width: 100%; background-color: #fbfbfb"&gt;&lt;/pre&gt;&lt;pre style="font-size: 11px; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; margin: 0em; width: 100%; background-color: #fbfbfb"&gt;        &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; Task Invoke(IDictionary&amp;lt;&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;, &lt;span style="color: #0000ff"&gt;object&lt;/span&gt;&amp;gt; environment)
&lt;/pre&gt;&lt;pre style="font-size: 11px; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; margin: 0em; width: 100%; background-color: #fbfbfb"&gt;        {
&lt;/pre&gt;&lt;pre style="font-size: 11px; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; margin: 0em; width: 100%; background-color: #fbfbfb"&gt;            System.Diagnostics.Trace.WriteLine(&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;.Format(&amp;quot;&lt;span style="color: #8b0000"&gt;Hitting TestLogger, path: {0}&lt;/span&gt;&amp;quot;, environment[&amp;quot;&lt;span style="color: #8b0000"&gt;owin.RequestPath&lt;/span&gt;&amp;quot;]));
&lt;/pre&gt;&lt;pre style="font-size: 11px; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; margin: 0em; width: 100%; background-color: #fbfbfb"&gt;            &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; _next(environment);
&lt;/pre&gt;&lt;pre style="font-size: 11px; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; margin: 0em; width: 100%; background-color: #fbfbfb"&gt;        }
&lt;/pre&gt;&lt;pre style="font-size: 11px; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; margin: 0em; width: 100%; background-color: #fbfbfb"&gt;    }&lt;/pre&gt;&lt;/pre&gt;

&lt;p&gt;That is actually all you need to write an &lt;a href="http://owin.org/" target="_blank"&gt;Owin&lt;/a&gt; compatible library. Strictly speaking, you don’t need the reference to the &lt;a href="http://owin.org/" target="_blank"&gt;Owin&lt;/a&gt; nuget package but we will make use of that in the next part. All this module does is write out the contents of the request path to the trace output for each request that comes in. All we need to do is examine the environment that is provided to us through the environment variable.&lt;/p&gt;

&lt;p&gt;However, we have a module but nothing to host it in and no way to tell it to be included as part of a web server pipeline. We need an &lt;a href="http://owin.org/" target="_blank"&gt;Owin&lt;/a&gt; capable host that loads our module and houses the running code. We also need to programmatically configure that host to include our module. Much like Internet Information Server acts as the host for most web apps in .Net..&lt;/p&gt;

&lt;p&gt;So, we are going to add a web project to our solution which will act as the entry point, allowing us to configure a module for use in the web request pipeline.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Within Visual Studio, add a new empty MVC project (I used MVC4) to your current solution. &lt;/li&gt;

  &lt;li&gt;You need to reference at least the Owin assemblies. In addition, we are going to reference the &lt;a href="http://owin.org/" target="_blank"&gt;Owin&lt;/a&gt; HttpListener written by Microsoft to act as the actual Http server in our &lt;a href="http://owin.org/" target="_blank"&gt;Owin&lt;/a&gt; pipeline. To do this: 

    &lt;br /&gt;Install the &lt;a href="http://owin.org/" target="_blank"&gt;Owin&lt;/a&gt; nuget package and the &lt;font face="Courier New"&gt;Microsoft.Owin.HttpListener&lt;/font&gt; package using: 

    &lt;br /&gt;&lt;font face="Courier New"&gt;Install-Package Owin 
      &lt;br /&gt;Install-Package Microsoft.Owin.Host.HttpListener -Pre&lt;/font&gt; 

    &lt;br /&gt;(via the package manager console in Visual Studio or right click on the project in Visual Studio and select Manage Nuget Packages. Note that the &lt;font face="Courier New"&gt;Microsoft.Owin.HttpListener&lt;/font&gt; package is pre-release so we need include the &lt;font face="Courier New"&gt;–pre&lt;/font&gt; option.) 

    &lt;br /&gt;After installing those packages, you should see the appropriate references in your project as shown in the screen grab below: &lt;img title="proj-refs" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; float: none; padding-top: 0px; padding-left: 0px; margin-left: auto; display: block; padding-right: 0px; border-top-width: 0px; margin-right: auto" border="0" alt="proj-refs" src="http://weblogs.asp.net/blogs/pglavich/proj-refs_thumb_5692A446.png" width="309" height="192" /&gt; &lt;/li&gt;

  &lt;li&gt;Add a new class to this MVC project called ‘&lt;font face="Courier New"&gt;Startup&lt;/font&gt;’ &lt;/li&gt;

  &lt;li&gt;Paste in the following code: 
    &lt;br /&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;pre style="overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; border-bottom: #cecece 1px solid; padding-bottom: 5px; padding-top: 5px; padding-left: 5px; min-height: 40px; border-left: #cecece 1px solid; padding-right: 5px; width: 500px; background-color: #fbfbfb"&gt;&lt;pre style="font-size: 11px; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; margin: 0em; width: 100%; background-color: #fbfbfb"&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; Startup
&lt;/pre&gt;&lt;pre style="font-size: 11px; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; margin: 0em; width: 100%; background-color: #fbfbfb"&gt;{
&lt;/pre&gt;&lt;pre style="font-size: 11px; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; margin: 0em; width: 100%; background-color: #fbfbfb"&gt;    &lt;span style="color: #008000"&gt;// Invoked once at startup to configure your application.&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="font-size: 11px; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; margin: 0em; width: 100%; background-color: #fbfbfb"&gt;    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; Configuration(IAppBuilder builder)
&lt;/pre&gt;&lt;pre style="font-size: 11px; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; margin: 0em; width: 100%; background-color: #fbfbfb"&gt;    {
&lt;/pre&gt;&lt;pre style="font-size: 11px; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; margin: 0em; width: 100%; background-color: #fbfbfb"&gt;        builder.Use(&lt;span style="color: #0000ff"&gt;typeof&lt;/span&gt;(TestLogger));
&lt;/pre&gt;&lt;pre style="font-size: 11px; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; margin: 0em; width: 100%; background-color: #fbfbfb"&gt;    }
&lt;/pre&gt;&lt;pre style="font-size: 11px; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; margin: 0em; width: 100%; background-color: #fbfbfb"&gt;}
&lt;/pre&gt;&lt;pre style="font-size: 11px; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; margin: 0em; width: 100%; background-color: #fbfbfb"&gt;&lt;/pre&gt;&lt;/pre&gt;

&lt;p&gt;Here you can see we are using an instance of &lt;font face="Courier New"&gt;IAppBuilder&lt;/font&gt; passed into our configuration method to tell the host to ‘Use’ our custom pipeline component, often referred to as ‘Middleware’ in Owin speak.&lt;/p&gt;

&lt;p&gt;Owin components use conventions for easy configuration, so when the Owin host starts up and loads our assemblies, it looks for the &lt;font face="Courier New"&gt;Startup&lt;/font&gt; class, and also a ‘&lt;font face="Courier New"&gt;Configuration&lt;/font&gt;’ method which accepts an &lt;font face="Courier New"&gt;IAppBuilder&lt;/font&gt; instance, and calls that, providing the instance.&lt;/p&gt;

&lt;h3&gt;The final piece of the puzzle&lt;/h3&gt;

&lt;p&gt;Ok, we have our custom pipeline component, our middleware, and we have an entry point where we can build or configure our pipeline. We need a host process to load these assemblies and invoke them with the requisite information.&lt;/p&gt;

&lt;p&gt;This is where &lt;a href="http://katanaproject.codeplex.com/" target="_blank"&gt;Project Katana&lt;/a&gt; comes in. &lt;a href="http://katanaproject.codeplex.com/" target="_blank"&gt;Katana&lt;/a&gt; is a Microsoft written generic Owin host. You can &lt;a href="http://katanaproject.codeplex.com/" target="_blank"&gt;go grab the source of this project&lt;/a&gt;, compile and run it, but it is easier to just install it so you can call it from the command line. By far, the easiest way to do this is to install a tool called ‘&lt;a href="http://chocolatey.org/" target="_blank"&gt;Chocolatey’&lt;/a&gt; which is like a Nuget package installer but for windows binaries.&lt;/p&gt;

&lt;p&gt;(&lt;u&gt;Side Note&lt;/u&gt;: &lt;a href="http://chocolatey.org/" target="_blank"&gt;Chocolately&lt;/a&gt; is an awesome tool. If you get nothing else from this post, just use &lt;a href="http://chocolatey.org/" target="_blank"&gt;Chocolately&lt;/a&gt; to install some software and keep it up to date. It is easy to use and has a heap of applications supported.)&lt;/p&gt;

&lt;p&gt;To install &lt;a href="http://chocolatey.org/" target="_blank"&gt;Chocolotey&lt;/a&gt;, run the following from a command line (literally copy and paste into a command line/console window): 

  &lt;br /&gt;&lt;font face="Courier New"&gt;@powershell -NoProfile -ExecutionPolicy unrestricted -Command &amp;quot;iex ((new-object net.webclient).DownloadString('http://chocolatey.org/install.ps1'))&amp;quot; &amp;amp;&amp;amp; SET PATH=%PATH%;%systemdrive%\chocolatey\bin&lt;/font&gt;&lt;/p&gt;

&lt;p&gt;Once its installed, from the command line, simply type: 
  &lt;br /&gt;&lt;font face="Courier New"&gt;cinst Katana –pre&lt;/font&gt;&lt;/p&gt;

&lt;p&gt;And Katana will be magically installed. Now opening up a command line/console window and typing ‘&lt;font face="Courier New"&gt;Katana&lt;/font&gt;’ will load and run the Katana application, which is our Microsoft written Owin host. Nothing will get loaded because we haven’t specified anything, but that is what is next. &lt;/p&gt;

&lt;p&gt;We have written our custom Owin component, we have created our web application entry point used to configure our Owin pipeline and we have installed our host application, Katana, to glue it all together. So lets see in action.&lt;/p&gt;

&lt;p&gt;First we need to tell our web project to NOT automatically use the current page when starting up. Rather, we want it to startup the Katana host, and tell Katana to use our assemblies. To do this:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Open up the properties of the web project in your solution and select the ‘Web’ section. You should see a screen similar to the one shown below:. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="http://weblogs.asp.net/blogs/pglavich/WebProjectKatanaStartupOptions_4DC29BFA.png"&gt;&lt;img title="WebProjectKatanaStartupOptions" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="WebProjectKatanaStartupOptions" src="http://weblogs.asp.net/blogs/pglavich/WebProjectKatanaStartupOptions_thumb_0C47E9A1.png" width="528" height="192" /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;For clarification, the things you need to enter are:&lt;/p&gt;

&lt;p&gt;Ensure the ‘&lt;font face="Courier New"&gt;Start External Program&lt;/font&gt;’ is selected and enter the full path to the Katana executable. I installed to the default location using &lt;a href="http://chocolatey.org/" target="_blank"&gt;Chocolately&lt;/a&gt; so mine is set at:&lt;/p&gt;

&lt;p&gt;&lt;font face="Courier New"&gt;C:\Chocolatey\lib\Katana.0.20-alpha-20220-88\tools\Katana.exe&lt;/font&gt;&lt;/p&gt;

&lt;p&gt;Set the command line arguments to:&lt;/p&gt;

&lt;p&gt;&lt;font face="Courier New"&gt;-p8080 –v&lt;/font&gt;&lt;/p&gt;

&lt;p&gt;This represent the port number of 8080 and enabling verbose mode&lt;/p&gt;

&lt;p&gt;Enter the working directory where your web project is located. In my case it was:&lt;/p&gt;

&lt;p&gt;&lt;font face="Courier New"&gt;D:\Glav.Owin.Middleware\Glav.Owin.Consumer.Test.BasicWebConsumer&lt;/font&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Now hit F5 to run the app. You should see a console application launched which looks similar to the one below: &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="http://weblogs.asp.net/blogs/pglavich/running-katana_2D5AE63B.png"&gt;&lt;img title="running-katana" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="running-katana" src="http://weblogs.asp.net/blogs/pglavich/running-katana_thumb_3E5F141E.png" width="508" height="107" /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now remember, the only component we have injected into the Owin pipeline is our own custom logging component. Since we have included the &lt;font face="Courier New"&gt;Microsoft.Owin.Host.HttpListener &lt;/font&gt;package into our project, that assembly also gets included in the output. When Katana executes, if no specific Http server is defined, it will automatically look for the &lt;font face="Courier New"&gt;Microsoft.Owin.Host.HttpListener &lt;/font&gt;assembly and use that.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Again, we only have our logging component installed, so if we load a browser, a navigate to &lt;a href="http://localhost:8080"&gt;http://localhost:8080&lt;/a&gt; , we wont see a web page but the console should show the logging output of our custom component as in the screen shot below: &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="http://weblogs.asp.net/blogs/pglavich/browsing-to-katana_1CFF6E82.png"&gt;&lt;img title="browsing-to-katana" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="browsing-to-katana" src="http://weblogs.asp.net/blogs/pglavich/browsing-to-katana_thumb_149B992B.png" width="534" height="194" /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;h2&gt;All that work, for that?&lt;/h2&gt;

&lt;p&gt;Yes, but now we have everything in place to easily add other components in the pipeline. In addition, now you should have a reasonable understanding of the various pieces at play.&amp;#160; Remember, we have created a custom web server that only does exactly what we want it to.So conceptually, what we have created looks roughly like the following:&lt;/p&gt;

&lt;p&gt;&lt;a href="http://weblogs.asp.net/blogs/pglavich/custom-katana-pipeline_72CFC099.png"&gt;&lt;img title="custom-katana-pipeline" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; float: none; padding-top: 0px; padding-left: 0px; margin-left: auto; display: block; padding-right: 0px; border-top-width: 0px; margin-right: auto" border="0" alt="custom-katana-pipeline" src="http://weblogs.asp.net/blogs/pglavich/custom-katana-pipeline_thumb_31C14135.png" width="240" height="228" /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now lets create another component but I don’t want to create another project for that.&lt;/p&gt;

&lt;p&gt;Add the following method block to your ‘&lt;font face="Courier New"&gt;Startup&lt;/font&gt;’ class in the web project in your solution:&lt;/p&gt;

&lt;pre style="overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; border-bottom: #cecece 1px solid; padding-bottom: 5px; padding-top: 5px; padding-left: 5px; min-height: 40px; border-left: #cecece 1px solid; padding-right: 5px; width: 500px; background-color: #fbfbfb"&gt;&lt;pre style="font-size: 11px; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; margin: 0em; width: 100%; background-color: #fbfbfb"&gt;&lt;span style="color: #008000"&gt;// Invoked once per request.&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="font-size: 11px; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; margin: 0em; width: 100%; background-color: #fbfbfb"&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; Task Invoke(IDictionary&amp;lt;&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;, &lt;span style="color: #0000ff"&gt;object&lt;/span&gt;&amp;gt; environment)
&lt;/pre&gt;&lt;pre style="font-size: 11px; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; margin: 0em; width: 100%; background-color: #fbfbfb"&gt;{
&lt;/pre&gt;&lt;pre style="font-size: 11px; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; margin: 0em; width: 100%; background-color: #fbfbfb"&gt;   var responseBytes = System.Text.ASCIIEncoding.UTF8.GetBytes(
&lt;/pre&gt;&lt;pre style="font-size: 11px; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; margin: 0em; width: 100%; background-color: #fbfbfb"&gt;            &lt;span style="color: #0000ff"&gt;string&lt;/span&gt;.Format(&amp;quot;&lt;span style="color: #8b0000"&gt;Serviced request on {0} at {1}&lt;/span&gt;&amp;quot;,DateTime.Now.ToLongDateString(), DateTime.Now.ToLongTimeString()));
&lt;/pre&gt;&lt;pre style="font-size: 11px; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; margin: 0em; width: 100%; background-color: #fbfbfb"&gt;&lt;/pre&gt;&lt;pre style="font-size: 11px; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; margin: 0em; width: 100%; background-color: #fbfbfb"&gt;    Stream responseStream = (Stream)environment[&amp;quot;&lt;span style="color: #8b0000"&gt;owin.ResponseBody&lt;/span&gt;&amp;quot;];
&lt;/pre&gt;&lt;pre style="font-size: 11px; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; margin: 0em; width: 100%; background-color: #fbfbfb"&gt;    IDictionary&amp;lt;&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;, &lt;span style="color: #0000ff"&gt;string&lt;/span&gt;[]&amp;gt; responseHeaders =
&lt;/pre&gt;&lt;pre style="font-size: 11px; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; margin: 0em; width: 100%; background-color: #fbfbfb"&gt;        (IDictionary&amp;lt;&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;, &lt;span style="color: #0000ff"&gt;string&lt;/span&gt;[]&amp;gt;)environment[&amp;quot;&lt;span style="color: #8b0000"&gt;owin.ResponseHeaders&lt;/span&gt;&amp;quot;];
&lt;/pre&gt;&lt;pre style="font-size: 11px; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; margin: 0em; width: 100%; background-color: #fbfbfb"&gt;&lt;/pre&gt;&lt;pre style="font-size: 11px; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; margin: 0em; width: 100%; background-color: #fbfbfb"&gt;    responseHeaders[&amp;quot;&lt;span style="color: #8b0000"&gt;Content-Length&lt;/span&gt;&amp;quot;] = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; &lt;span style="color: #0000ff"&gt;string&lt;/span&gt;[] { responseBytes.Length.ToString(CultureInfo.InvariantCulture) };
&lt;/pre&gt;&lt;pre style="font-size: 11px; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; margin: 0em; width: 100%; background-color: #fbfbfb"&gt;    responseHeaders[&amp;quot;&lt;span style="color: #8b0000"&gt;Content-Type&lt;/span&gt;&amp;quot;] = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; &lt;span style="color: #0000ff"&gt;string&lt;/span&gt;[] { &amp;quot;&lt;span style="color: #8b0000"&gt;text/plain&lt;/span&gt;&amp;quot; };
&lt;/pre&gt;&lt;pre style="font-size: 11px; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; margin: 0em; width: 100%; background-color: #fbfbfb"&gt;&lt;/pre&gt;&lt;pre style="font-size: 11px; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; margin: 0em; width: 100%; background-color: #fbfbfb"&gt;    &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; responseStream.WriteAsync(responseBytes, 0, responseBytes.Length);
&lt;/pre&gt;&lt;pre style="font-size: 11px; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; margin: 0em; width: 100%; background-color: #fbfbfb"&gt;}
&lt;/pre&gt;&lt;pre style="font-size: 11px; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; margin: 0em; width: 100%; background-color: #fbfbfb"&gt;&lt;/pre&gt;&lt;/pre&gt;

&lt;p&gt;Then add this line of code as the last line in the ‘&lt;font face="Courier New"&gt;Configuration&lt;/font&gt;’ method within the ‘&lt;font face="Courier New"&gt;Startup&lt;/font&gt;’ class.&lt;/p&gt;

&lt;pre style="overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; border-bottom: #cecece 1px solid; padding-bottom: 5px; padding-top: 5px; padding-left: 5px; min-height: 40px; border-left: #cecece 1px solid; padding-right: 5px; width: 500px; background-color: #fbfbfb"&gt;&lt;pre style="font-size: 11px; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; margin: 0em; width: 100%; background-color: #fbfbfb"&gt;builder.Use(&lt;span style="color: #0000ff"&gt;new&lt;/span&gt; Func&amp;lt;AppFunc, AppFunc&amp;gt;(ignoredNextApp =&amp;gt; (AppFunc)Invoke));&lt;/pre&gt;&lt;/pre&gt;

&lt;p&gt;Now, hit F5 to run your app, and browser to &lt;a href="http://localhost:8080"&gt;http://localhost:8080&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You should see a response in the browser similar to the screen grab shown below:&lt;/p&gt;

&lt;p&gt;&lt;a href="http://weblogs.asp.net/blogs/pglavich/browsing-to-katana2_4978789B.png"&gt;&lt;img title="browsing-to-katana2" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="browsing-to-katana2" src="http://weblogs.asp.net/blogs/pglavich/browsing-to-katana2_thumb_0F1D02BA.png" width="525" height="123" /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;That code we added in the Invoke method, did the following:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Created a simple text response as a byte array. &lt;/li&gt;

  &lt;li&gt;Grabbed the response body stream and the response headers object from the environment variable using standard Owin defined key names. &lt;/li&gt;

  &lt;li&gt;Set the response headers according to the response itself. &lt;/li&gt;

  &lt;li&gt;Wrote the bytes to the response stream. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In order to use the code in the Invoke method, we needed to tell our pipeline to use it. This was achieved in the Configuration method via the line:&lt;/p&gt;

&lt;pre&gt;&lt;pre style="font-size: 11px; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; margin: 0em; width: 100%; background-color: #ffffff"&gt;builder.Use(&lt;span style="color: #0000ff"&gt;new&lt;/span&gt; Func&amp;lt;AppFunc, AppFunc&amp;gt;(ignoredNextApp =&amp;gt; (AppFunc)Invoke));&lt;/pre&gt;&lt;/pre&gt;

&lt;p&gt;This simply injected the delegate for our Invoke function into the pipeline. So now our comceptual pipeline looks like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="http://weblogs.asp.net/blogs/pglavich/final-pipeline_0D6C36E6.png"&gt;&lt;img title="final-pipeline" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; float: none; padding-top: 0px; padding-left: 0px; margin-left: auto; display: block; padding-right: 0px; border-top-width: 0px; margin-right: auto" border="0" alt="final-pipeline" src="http://weblogs.asp.net/blogs/pglavich/final-pipeline_thumb_614F39F4.png" width="240" height="207" /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;All without any real dependencies and in a relatively small amount of code.&lt;/p&gt;

&lt;h3&gt;And you made it&lt;/h3&gt;

&lt;p&gt;After all that, you can finally see how composable and modular the Owin concepts and architecture are. There are quite a few Owin compatible middleware modules out there such as &lt;a href="http://signalr.net/" target="_blank"&gt;SignalR&lt;/a&gt;, &lt;a href="http://mvc.fubu-project.org/" target="_blank"&gt;FubuMVC&lt;/a&gt;, &lt;a href="http://nancyfx.org/" target="_blank"&gt;NancyFx&lt;/a&gt; to name a few. These can be added to your pipeline and you then have the power that each brings at your disposal.&lt;/p&gt;

&lt;p&gt;This is just the beginning. Imagine being able to easily build a custom web server with only the capabilities you need. An open, composable and modular world with no lock in. Sounds good to me. Hopefully the Owin concepts and goals are a little clearer now.&lt;/p&gt;

&lt;p&gt;Remember, the code for this article can be downloaded from &lt;a href="https://github.com/glav/Glav.Owin.Middleware" target="_blank"&gt;here&lt;/a&gt;.&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=10095754" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/pglavich/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/pglavich/archive/tags/.NET/default.aspx">.NET</category><category domain="http://weblogs.asp.net/pglavich/archive/tags/IIS/default.aspx">IIS</category><category domain="http://weblogs.asp.net/pglavich/archive/tags/Component/default.aspx">Component</category><category domain="http://weblogs.asp.net/pglavich/archive/tags/General/default.aspx">General</category><category domain="http://weblogs.asp.net/pglavich/archive/tags/Community+News/default.aspx">Community News</category><category domain="http://weblogs.asp.net/pglavich/archive/tags/MVC/default.aspx">MVC</category><category domain="http://weblogs.asp.net/pglavich/archive/tags/Architecture/default.aspx">Architecture</category></item><item><title>Debugging Pain–End to End</title><link>http://weblogs.asp.net/pglavich/archive/2013/02/21/debugging-pain-end-to-end.aspx</link><pubDate>Wed, 20 Feb 2013 21:18:47 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:9895334</guid><dc:creator>Glav</dc:creator><slash:comments>5</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/pglavich/rsscomments.aspx?PostID=9895334</wfw:commentRss><comments>http://weblogs.asp.net/pglavich/archive/2013/02/21/debugging-pain-end-to-end.aspx#comments</comments><description>&lt;p&gt;We had an issue recently that caused us some time and quite a lot of head scratching. We had made some relatively minor changes to our product and performed a release into staging for testing. We released our main web application as well as our custom built support tool (also a web app).&lt;/p&gt;  &lt;p&gt;After a little bit of testing from our QA team, a few bugs were uncovered. One where a response to a cancel action seemingly was not actioned, and an issue where a timeout occurred on a few requests. Nothing too huge and certainly seemed fixable.&lt;/p&gt;  &lt;h2&gt;Off to work &lt;/h2&gt;  &lt;p&gt;The timeouts “seemed” to be data specific and possibly because of our 3rd party web service call being made. It seemed to be only occurring in our support tool, the main web app was not affected. Since the main web app is the priority, I looked at the “cancel” issue not working. It seemed that the cancel request was being made to our server (via an ajax call) but never returning from said call. This looked very similar to our issue with the support tool.&lt;/p&gt;  &lt;p&gt;A little further investigation showed that both the support tool and our main web app were issuing ajax requests to a few action methods (we use ASP.Net MVC 4.5) and never returning. Ever. I tried recycling the application pool in IIS. This worked for a brief period, then requests to a few particular requests to action methods were not returning. Web pages from other action methods and even other ajax requests were working fine so at least we knew what surface area we had to look at.&lt;/p&gt;  &lt;p&gt;Looking at the requests via Chrome, we could see the request in a constant pending state, never satisfied. We began looking at the server. We instituted some page tracing, looked at event logs and also looked at the Internet Information Server logs. Nothing. Nada. We could see the successful requests come in, but these pending requests were not logged. Fiddler showed they were definitely outgoing, but the web server showed nothing.&lt;/p&gt;  &lt;p&gt;Using the Internet Information Service management console, we looked at the current worker processes which is available when clicking on the root node within the IIS Management console.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://weblogs.asp.net/blogs/pglavich/iis-console_087D259C.png"&gt;&lt;img title="iis-console" style="border-top: 0px; border-right: 0px; background-image: none; border-bottom: 0px; padding-top: 0px; padding-left: 0px; border-left: 0px; display: inline; padding-right: 0px" border="0" alt="iis-console" src="http://weblogs.asp.net/blogs/pglavich/iis-console_thumb_267B3390.png" width="395" height="203" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://weblogs.asp.net/blogs/pglavich/iis-worker-process_04AF5AFF.png"&gt;&lt;img title="iis-worker-process" style="border-top: 0px; border-right: 0px; background-image: none; border-bottom: 0px; padding-top: 0px; padding-left: 0px; border-left: 0px; display: inline; padding-right: 0px" border="0" alt="iis-worker-process" src="http://weblogs.asp.net/blogs/pglavich/iis-worker-process_thumb_07750FF2.png" width="500" height="118" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;We could see our application pool and right clicking on this allowed us to view current requests, and there they were, all waiting to be executed, and waiting…..and waiting.&lt;/p&gt;  &lt;h2&gt;What’s the hold up?&lt;/h2&gt;  &lt;p&gt;So what was causing our requests to get backlogged? We tried going into the code and removing calls to external services and trying to isolate the problem areas. All of this was not reproducible locally, nor in any other environment. Eventually trying to isolate the cause led us to removing everything from the controller actions apart from simple &lt;font face="Courier New"&gt;Thread.Sleep&lt;/font&gt;.While this worked and the problem did not present, we were in no way closer as it was still in potentially any number of code paths.&lt;/p&gt;  &lt;h2&gt;Take a dump&lt;/h2&gt;  &lt;p&gt;A colleague suggested using &lt;a href="http://www.microsoft.com/en-au/download/details.aspx?id=26798" target="_blank"&gt;DebugDiag&lt;/a&gt; (a free diagnostic tool from Microsoft) to look at memory dumps of the process. So that is what we did.&lt;/p&gt;  &lt;p&gt;Using &lt;a href="http://www.microsoft.com/en-au/download/details.aspx?id=26798" target="_blank"&gt;DebugDiag&lt;/a&gt;, we extracted a memory dump of the process. &lt;a href="http://www.microsoft.com/en-au/download/details.aspx?id=26798" target="_blank"&gt;DebugDiag&lt;/a&gt; has some really nice features, one of which is to execute predefined scripts in an attempt to diagnose any issues and present a summary of what was found and also has a nice wizard based set of of steps to get you up and running quickly.&lt;/p&gt;  &lt;p&gt;We chose to monitor for performance:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://weblogs.asp.net/blogs/pglavich/debugdiag-1_05C4441E.png"&gt;&lt;img title="debugdiag-1" style="border-top: 0px; border-right: 0px; background-image: none; border-bottom: 0px; padding-top: 0px; padding-left: 0px; border-left: 0px; display: inline; padding-right: 0px" border="0" alt="debugdiag-1" src="http://weblogs.asp.net/blogs/pglavich/debugdiag-1_thumb_242E8507.png" width="403" height="352" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;and also for HTTP Response time:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://weblogs.asp.net/blogs/pglavich/debugdiag-2_68FAA93B.png"&gt;&lt;img title="debugdiag-2" style="border-top: 0px; border-right: 0px; background-image: none; border-bottom: 0px; padding-top: 0px; padding-left: 0px; border-left: 0px; display: inline; padding-right: 0px" border="0" alt="debugdiag-2" src="http://weblogs.asp.net/blogs/pglavich/debugdiag-2_thumb_00B1E0A2.png" width="398" height="349" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;We then added the specific URL’s we were monitoring. We also chose what kind of dumps to take, in this case web application pools:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://weblogs.asp.net/blogs/pglavich/debugdiag-3-dumptarget_05B41E51.png"&gt;&lt;img title="debugdiag-3-dumptarget" style="border-top: 0px; border-right: 0px; background-image: none; border-bottom: 0px; padding-top: 0px; padding-left: 0px; border-left: 0px; display: inline; padding-right: 0px" border="0" alt="debugdiag-3-dumptarget" src="http://weblogs.asp.net/blogs/pglavich/debugdiag-3-dumptarget_thumb_2BA9CEA7.png" width="369" height="211" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;We decided on the time frequency (we chose every 10 seconds) and a maximum of 10 Full dumps:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://weblogs.asp.net/blogs/pglavich/debugdiag-4_2345F950.png"&gt;&lt;img title="debugdiag-4" style="border-top: 0px; border-right: 0px; background-image: none; border-bottom: 0px; padding-top: 0px; padding-left: 0px; border-left: 0px; display: inline; padding-right: 0px" border="0" alt="debugdiag-4" src="http://weblogs.asp.net/blogs/pglavich/debugdiag-4_thumb_68EA836E.png" width="411" height="360" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;After that, we set the dump path, named and activated the rule, and we good to go. With the requests already built up in the queue and issuing some more ‘pending’ requests, we could see some memory dumps being taken.&lt;/p&gt;            &lt;p&gt;A cool feature of &lt;a href="http://www.microsoft.com/en-au/download/details.aspx?id=26798" target="_blank"&gt;DebugDiag&lt;/a&gt; is the prebuilt scripts to analyze your memory dumps (available on the advanced tab):&lt;/p&gt;  &lt;p&gt;&lt;a href="http://weblogs.asp.net/blogs/pglavich/debugdiag-5_6DECC11D.png"&gt;&lt;img title="debugdiag-5" style="border-top: 0px; border-right: 0px; background-image: none; border-bottom: 0px; padding-top: 0px; padding-left: 0px; border-left: 0px; display: inline; padding-right: 0px" border="0" alt="debugdiag-5" src="http://weblogs.asp.net/blogs/pglavich/debugdiag-5_thumb_77657F93.png" width="394" height="277" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;We initially chose performance, but didn’t glean much information from that. We then chose the “&lt;font face="Courier New"&gt;Crash/Hang Analyzers&lt;/font&gt;” which produced&amp;#160; great summary of all the threads in the pool. It was apparent that almost every thread was waiting on a .Net Lock. We couldn’t get much more than that though.&lt;/p&gt;  &lt;h2&gt;WinDbg to the rescue&lt;/h2&gt;  &lt;p&gt;So I copied the memory dump locally and use &lt;a href="http://en.wikipedia.org/wiki/WinDbg" target="_blank"&gt;WinDbg&lt;/a&gt; to examine the memory process. I loaded in the SOS extension so I could use the managed memory extensions.&lt;/p&gt;  &lt;p&gt;(Side Note: I almost always have issues with incorrect format –32 or 64 bit- and SOS versions when doing this so it usually takes a bit of frigging around before I get the right combination)&lt;/p&gt;  &lt;p&gt;I looked at the threads via:&lt;/p&gt;  &lt;p&gt;&lt;font face="Courier New"&gt;!threads&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;Sure enough, there were heaps of them in there. Using Dump object (&lt;font face="Courier New"&gt;!do&lt;/font&gt;) and trying to poke around didn’t reveal too much except a lot of thread locks. So I used the &lt;font face="Courier New"&gt;!syncblk&lt;/font&gt; command to look at blocked and locking threads:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://weblogs.asp.net/blogs/pglavich/sync-blk_6A8B2975.png"&gt;&lt;img title="sync-blk" style="border-top: 0px; border-right: 0px; background-image: none; border-bottom: 0px; padding-top: 0px; padding-left: 0px; border-left: 0px; display: inline; padding-right: 0px" border="0" alt="sync-blk" src="http://weblogs.asp.net/blogs/pglavich/sync-blk_thumb_1080D9CC.png" width="569" height="86" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;Here we had some real indication of what was going on. You can see we have 285 locks via the Apache.NMS functions. Our application uses &lt;a href="http://activemq.apache.org/" target="_blank"&gt;ActiveMQ&lt;/a&gt; (which is awesome btw) for our message queuing and we use the &lt;a href="http://activemq.apache.org/nms/" target="_blank"&gt;Apache.NMS&lt;/a&gt; library as the C# interface. In addition, the code paths being executed utilise extensive diagnostic information among all code paths. This diagnostic information is posted to the Message Queue for logging.&lt;/p&gt;  &lt;p&gt;A quick test verified this. We commented out all calls to the queue within the queue manager interface (so it effectively did nothing). Put this code on staging and all was working without a hitch.&lt;/p&gt;  &lt;p&gt;So we had our culprit, but not the root cause.&lt;/p&gt;  &lt;p&gt;We used the admin tool for &lt;a href="http://activemq.apache.org/" target="_blank"&gt;ActiveMQ&lt;/a&gt; to look at the queues themselves.Some queued messages, but not subscribers even though our subscription service was running. Restarted the service, nothing. No subscribers. Using the admin tool we purged all messages. Restarted the service. Nothing. Refreshed the admin tool, the purged messages re-appeared!&lt;/p&gt;  &lt;p&gt;We tried deleting the offending queues.Normally, this operation is sub-second. In this case, it took 20+ seconds before we tried again. Something was amiss. We tried creating a new queue from the admin tool.Again, an abnormally long time, but this time it did it. We could then delete queues without issue. Restarted the service and viola, queues subscribed to. We re-instituted the commented code and now all working fine.&lt;/p&gt;  &lt;h2&gt;So what really happened?&lt;/h2&gt;  &lt;p&gt;A short time ago, we had run out of space on our staging server. No big deal, freed up the space promptly, all seemingly good. it was during this time that I believe that our message queue repository, which persists messages to disk, got corrupted and this started the issue occurring. What is apparent is that we need to release resources more aggressively so that this issue would not be so detrimental to the rest of the application function.&lt;/p&gt;  &lt;p&gt;So there you have it. All in all that took about 2-3 days. &lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=9895334" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/pglavich/archive/tags/AJAX/default.aspx">AJAX</category><category domain="http://weblogs.asp.net/pglavich/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/pglavich/archive/tags/.NET/default.aspx">.NET</category><category domain="http://weblogs.asp.net/pglavich/archive/tags/debugging/default.aspx">debugging</category><category domain="http://weblogs.asp.net/pglavich/archive/tags/Community+News/default.aspx">Community News</category><category domain="http://weblogs.asp.net/pglavich/archive/tags/Performance/default.aspx">Performance</category><category domain="http://weblogs.asp.net/pglavich/archive/tags/HTML+5/default.aspx">HTML 5</category><category domain="http://weblogs.asp.net/pglavich/archive/tags/ActiveMQ/default.aspx">ActiveMQ</category></item><item><title>CacheAdapter minor update to version 2.5.1</title><link>http://weblogs.asp.net/pglavich/archive/2013/01/23/cacheadapter-minor-update-to-version-2-5-1.aspx</link><pubDate>Wed, 23 Jan 2013 03:42:57 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:9788156</guid><dc:creator>Glav</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/pglavich/rsscomments.aspx?PostID=9788156</wfw:commentRss><comments>http://weblogs.asp.net/pglavich/archive/2013/01/23/cacheadapter-minor-update-to-version-2-5-1.aspx#comments</comments><description>&lt;p&gt;For information on previous releases, please see &lt;a href="http://weblogs.asp.net/pglavich/archive/2012/03/29/cacheadapter-2-4-bug-fixes-and-minor-functional-update.aspx" target="_blank"&gt;here&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;My &lt;a href="https://nuget.org/packages/Glav.CacheAdapter/2.5.1" target="_blank"&gt;cache adapter library&lt;/a&gt; has been updated to version 2.5.1. This one contains only minor changes which are:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Use of the &lt;a href="https://nuget.org/packages/ServerAppFabric.Client/" target="_blank"&gt;Windows Azure Nuget package&lt;/a&gt; instead of the assemblies directly referenced. &lt;/li&gt;    &lt;li&gt;Use of the ‘&lt;strong&gt;Put&lt;/strong&gt;’ verb instead of ‘&lt;strong&gt;Add&lt;/strong&gt;’ when using AppFabric to prevent errors when calling ‘Add’ more than once. &lt;/li&gt;    &lt;li&gt;Minor updates to the example code to make it clearer that the code is indeed working as expected. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;Thanks to contributor &lt;a href="https://bitbucket.org/LucidSage" target="_blank"&gt;Darren Boon&lt;/a&gt; for forking and providing the Azure updates for this release.&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=9788156" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/pglavich/archive/tags/.NET/default.aspx">.NET</category><category domain="http://weblogs.asp.net/pglavich/archive/tags/Community+News/default.aspx">Community News</category><category domain="http://weblogs.asp.net/pglavich/archive/tags/Architecture/default.aspx">Architecture</category><category domain="http://weblogs.asp.net/pglavich/archive/tags/CacheAdapter/default.aspx">CacheAdapter</category></item><item><title>Writing an ASP.Net Web based TFS Client</title><link>http://weblogs.asp.net/pglavich/archive/2012/11/27/writing-an-asp-net-web-based-tfs-client.aspx</link><pubDate>Tue, 27 Nov 2012 01:53:17 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:9468701</guid><dc:creator>Glav</dc:creator><slash:comments>1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/pglavich/rsscomments.aspx?PostID=9468701</wfw:commentRss><comments>http://weblogs.asp.net/pglavich/archive/2012/11/27/writing-an-asp-net-web-based-tfs-client.aspx#comments</comments><description>&lt;p&gt;So one of the things I needed to do was write an ASP.Net MVC based application for our senior execs to manage a set of arbitrary attributes against stories, bugs etc to be able to attribute whether the item was related to Research and Development, and if so, what kind.&lt;/p&gt;  &lt;p&gt;We are using TFS Azure and don’t have the option of custom templates. I have decided on using a string based field within the template that is not very visible and which we don’t use to write a small set of custom which will determine the research and development association. &lt;/p&gt;  &lt;p&gt;However, this string munging on the field is not very user friendly so we need a simple tool that can display attributes against items in a simple dropdown list or something similar.&lt;/p&gt;  &lt;p&gt;Enter a custom web app that accesses our TFS items in Azure (Note: We are also using Visual Studio 2012)&lt;/p&gt;  &lt;p&gt;Now TFS Azure uses your Live ID and it is not really possible to easily do this in a server based app where no interaction is available. Even if you capture the Live ID credentials yourself and try to submit them to TFS Azure, it wont work.&lt;/p&gt;  &lt;p&gt;Bottom line is that it is not straightforward nor obvious what you have to do. In fact, it is a real pain to find and there are some answers out there which don’t appear to be answers at all given they didn’t work in my scenario.&lt;/p&gt;  &lt;p&gt;So for anyone else who wants to do this, here is a simple breakdown on what you have to do:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Go &lt;a href="http://blog.hinshelwood.com/tfs-service-credential-viewer/"&gt;here&lt;/a&gt; and get the “&lt;strong&gt;TFS Service Credential Viewer&lt;/strong&gt;”. Install it, run it and connect to your TFS instance in azure and create a service account. Note the username and password exactly as it presents it to you. This is the magic identity that will allow unattended, programmatic access.       &lt;ul&gt;       &lt;ul&gt;         &lt;li&gt;&lt;strong&gt;&lt;u&gt;Without this step, don’t bother trying to do anything else&lt;/u&gt;&lt;/strong&gt;. &lt;/li&gt;       &lt;/ul&gt;     &lt;/ul&gt;   &lt;/li&gt;    &lt;li&gt;In your MVC app, reference the following assemblies from “&lt;font face="Courier New"&gt;C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\ReferenceAssemblies\v2.0&lt;/font&gt;”:       &lt;ul&gt;       &lt;li&gt;&lt;font face="Courier New"&gt;Microsoft.TeamFoundation.Client.dll&lt;/font&gt; &lt;/li&gt;        &lt;li&gt;&lt;font face="Courier New"&gt;Microsoft.TeamFoundation.Common.dll&lt;/font&gt; &lt;/li&gt;        &lt;li&gt;&lt;font face="Courier New"&gt;Microsoft.TeamFoundation.VersionControl.Client.dll&lt;/font&gt; &lt;/li&gt;        &lt;li&gt;&lt;font face="Courier New"&gt;Microsoft.TeamFoundation.VersionControl.Common.dll&lt;/font&gt; &lt;/li&gt;        &lt;li&gt;&lt;font face="Courier New"&gt;Microsoft.TeamFoundation.WorkItemTracking.Client.DataStoreLoader.dll&lt;/font&gt; &lt;/li&gt;        &lt;li&gt;&lt;font face="Courier New"&gt;Microsoft.TeamFoundation.WorkItemTracking.Client.dll&lt;/font&gt; &lt;/li&gt;        &lt;li&gt;&lt;font face="Courier New"&gt;Microsoft.TeamFoundation.WorkItemTracking.Common.dll&lt;/font&gt; &lt;/li&gt;     &lt;/ul&gt;   &lt;/li&gt;    &lt;li&gt;If hosting this in Internet Information Server, for the application pool this app runs under, you will need to enable 32 Bit support. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&lt;a href="http://weblogs.asp.net/blogs/pglavich/image_646772F5.png"&gt;&lt;img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://weblogs.asp.net/blogs/pglavich/image_thumb_3BE890E1.png" width="401" height="226" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;You also have to allow the TFS client assemblies to store a cache of files on your system. If you don’t do this, you will authenticate fine, but then get an exception saying that it is unable to access the cache at some directory path when you query work items. You can set this up by adding the following to your web.config, in the &lt;font face="Courier New"&gt;&amp;lt;appSettings&amp;gt;&lt;/font&gt; element as shown below: &lt;/li&gt; &lt;/ul&gt;  &lt;pre&gt;&lt;code&gt;&amp;lt;appSettings&amp;gt; &amp;lt;!-- Add reference to TFS Client Cache --&amp;gt;
   &amp;lt;add key=&amp;quot;WorkItemTrackingCacheRoot&amp;quot; value=&amp;quot;C:\windows\temp&amp;quot; /&amp;gt;
&amp;lt;/appSettings&amp;gt;&lt;/code&gt;&lt;/pre&gt;

&lt;ul&gt;
  &lt;li&gt;With all that in place, you can write the following code: &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;font face="Courier New"&gt;var token = new Microsoft.TeamFoundation.Client.SimpleWebTokenCredential(&amp;quot;{you-service-account-name&amp;quot;, &amp;quot;{your-service-acct-password}&amp;quot;); 
    &lt;br /&gt;var clientCreds = new Microsoft.TeamFoundation.Client.TfsClientCredentials(token); 

    &lt;br /&gt;var currentCollection = new TfsTeamProjectCollection(new Uri(“https://{yourdomain}.visualstudio.com/defaultcollection”), clientCreds);&lt;/font&gt;&lt;/p&gt;

&lt;p&gt;
  &lt;br /&gt;&lt;font face="Courier New"&gt;TfsConfigurationServercurrentCollection.EnsureAuthenticated();&lt;/font&gt;&lt;font face="Courier New"&gt; 
    &lt;br /&gt;&lt;/font&gt;&lt;/p&gt;

&lt;p&gt;In the above code, not the URL contains the “&lt;em&gt;defaultcollection&lt;/em&gt;” at the end of the URL. Obviously replace &lt;font face="Courier New"&gt;{yourdomain}&lt;/font&gt; with whatever is defined for your TFS in Azure instance. In addition, make sure the service user account and password that was generated in the first step is substituted in here.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Note: If something is not right, the “EnsureAuthenticated()” call will throw an exception with the message being you are not authorised. If you forget the “defaultcollection” on the URL, it will still fail but with a message saying you are not authorised. That is, a similar but different exception message.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;And that is it. You can then query the collection using something like:&lt;/p&gt;

&lt;p&gt;&lt;font face="Courier New"&gt;var service = currentCollection.GetService&amp;lt;WorkItemStore&amp;gt;();&lt;/font&gt;&lt;/p&gt;

&lt;p&gt;&lt;font face="Courier New"&gt;var proj = service.Projects[0]; 
    &lt;br /&gt;var allQueries = proj.StoredQueries; 

    &lt;br /&gt;for (int qcnt = 0; qcnt &amp;lt; allQueries.Count; qcnt++) 

    &lt;br /&gt;{ 

    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; var query = allQueries[qcnt]; 

    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; var queryDesc = string.format(“Query found named: {0}”,query.Name); 

    &lt;br /&gt;&lt;/font&gt;&lt;font face="Courier New"&gt;}&lt;/font&gt; 

  &lt;br /&gt;&lt;/p&gt;

&lt;p&gt;You get the idea.&lt;/p&gt;

&lt;p&gt;If you search around, you will find references to the &lt;a href="http://blogs.msdn.com/b/patricb/archive/2012/08/16/write-a-workitem-or-bug-to-tfs-preview-in-the-cloud.aspx"&gt;&lt;font face="Courier New"&gt;ServiceIdentityCredentialProvider&lt;/font&gt;&lt;/a&gt; which is referenced in &lt;a href="http://blogs.msdn.com/b/patricb/archive/2012/08/16/write-a-workitem-or-bug-to-tfs-preview-in-the-cloud.aspx"&gt;this article&lt;/a&gt;. I had no luck with this method and it all looked too hard since it required an extra KB article and other magic sauce.&lt;/p&gt;

&lt;p&gt;So I hope that helps. This article certainly would have helped me save a boat load of time and frustration.&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=9468701" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/pglavich/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/pglavich/archive/tags/.NET/default.aspx">.NET</category><category domain="http://weblogs.asp.net/pglavich/archive/tags/Community+News/default.aspx">Community News</category><category domain="http://weblogs.asp.net/pglavich/archive/tags/TFS/default.aspx">TFS</category><category domain="http://weblogs.asp.net/pglavich/archive/tags/Azure/default.aspx">Azure</category></item><item><title>CacheAdapter 2.5–Memcached revised</title><link>http://weblogs.asp.net/pglavich/archive/2012/05/17/cacheadapter-2-5-memcached-revised.aspx</link><pubDate>Wed, 16 May 2012 23:14:51 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:8489177</guid><dc:creator>Glav</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/pglavich/rsscomments.aspx?PostID=8489177</wfw:commentRss><comments>http://weblogs.asp.net/pglavich/archive/2012/05/17/cacheadapter-2-5-memcached-revised.aspx#comments</comments><description>&lt;p&gt;Note: For more information around the CacheAdapter, see my previous posts &lt;a href="http://weblogs.asp.net/pglavich/archive/2011/05/31/cacheadapter-now-a-nuget-package.aspx" target="_blank"&gt;here&lt;/a&gt;, &lt;a href="http://weblogs.asp.net/pglavich/archive/2011/07/04/cacheadapter-v2-now-with-memcached-support.aspx" target="_blank"&gt;here&lt;/a&gt;, &lt;a href="http://weblogs.asp.net/pglavich/archive/2011/08/21/updates-to-the-cacheadapter-package.aspx" target="_blank"&gt;here&lt;/a&gt; and &lt;a href="http://weblogs.asp.net/pglavich/archive/2012/03/29/cacheadapter-2-4-bug-fixes-and-minor-functional-update.aspx" target="_blank"&gt;here&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;You may have noticed a number of updates to the &lt;a href="http://nuget.org/packages/Glav.CacheAdapter" target="_blank"&gt;CacheAdapter&lt;/a&gt; package on nuget as of late. These are all related to performance and stability improvements for the &lt;a href="http://memcached.org/" target="_blank"&gt;memcached&lt;/a&gt; component of the library.&lt;/p&gt;  &lt;p&gt;However it became apparent that I needed more performance for the &lt;a href="http://memcached.org/" target="_blank"&gt;memcached&lt;/a&gt; support to make the library truly useful and perform as best as can be expected.&lt;/p&gt;  &lt;p&gt;I started playing with optimising serialisation processes and really optimising the socket connections used to communicate with the &lt;a href="http://memcached.org/" target="_blank"&gt;memcached&lt;/a&gt; instance. As anybody who has worked with sockets before may tell you, you quickly start to look at pooling your connections to ensure you do not have to go about recreating a connection every time you want to communicate, especially if we are storing or retrieving items in a cache as this can be very frequent.&lt;/p&gt;  &lt;p&gt;So I started implementing a pooling mechanism to increase the performance. I got to improving it to a reasonable extent, then found I would hit a hurdle where performance could be increased further but it was becoming harder and more complex to retain stability. I became lost in trying to fix the problem when a solution had already been in place for a long time.&lt;/p&gt;  &lt;p&gt;It was about then I decided it best to simply take a dependency on the most excellent &lt;a href="https://github.com/enyim" target="_blank"&gt;Enyim&lt;/a&gt; &lt;a href="http://memcached.org/" target="_blank"&gt;memcached&lt;/a&gt; client. It is fast. Really fast, and blew away everything I had done in terms of performance. So that is the essence of this update. The &lt;a href="http://memcached.org/" target="_blank"&gt;memcached&lt;/a&gt; support in the cache adapter comes courtesy of &lt;a href="https://github.com/enyim" target="_blank"&gt;Enyim&lt;/a&gt;. Enjoy the extra speed that comes with it.&lt;/p&gt;  &lt;p&gt;Note: There are no other updates to any of the other caching implementations.&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=8489177" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/pglavich/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/pglavich/archive/tags/.NET/default.aspx">.NET</category><category domain="http://weblogs.asp.net/pglavich/archive/tags/Community+News/default.aspx">Community News</category><category domain="http://weblogs.asp.net/pglavich/archive/tags/Architecture/default.aspx">Architecture</category><category domain="http://weblogs.asp.net/pglavich/archive/tags/Performance/default.aspx">Performance</category><category domain="http://weblogs.asp.net/pglavich/archive/tags/CacheAdapter/default.aspx">CacheAdapter</category></item><item><title>CacheAdapter 2.4 – Bug fixes and minor functional update</title><link>http://weblogs.asp.net/pglavich/archive/2012/03/29/cacheadapter-2-4-bug-fixes-and-minor-functional-update.aspx</link><pubDate>Wed, 28 Mar 2012 20:22:54 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:8368133</guid><dc:creator>Glav</dc:creator><slash:comments>3</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/pglavich/rsscomments.aspx?PostID=8368133</wfw:commentRss><comments>http://weblogs.asp.net/pglavich/archive/2012/03/29/cacheadapter-2-4-bug-fixes-and-minor-functional-update.aspx#comments</comments><description>&lt;p&gt;&lt;em&gt;Note: If you are unfamiliar with the CacheAdapter library and what it does, you can read all about its awesome ability to utilise memory, Asp.Net Web, Windows Azure AppFabric and memcached caching implementations via a single unified, simple to use API from &lt;a href="http://weblogs.asp.net/pglavich/archive/2011/08/21/updates-to-the-cacheadapter-package.aspx" target="_blank"&gt;here&lt;/a&gt; and &lt;a href="http://weblogs.asp.net/pglavich/archive/2011/07/04/cacheadapter-v2-now-with-memcached-support.aspx" target="_blank"&gt;here&lt;/a&gt;..&lt;/em&gt;&lt;/p&gt;  &lt;p&gt;The CacheAdapter library is receiving an update to version 2.4 and is currently available on Nuget &lt;a href="https://nuget.org/packages/Glav.CacheAdapter/2.4" target="_blank"&gt;here&lt;/a&gt;. &lt;/p&gt;  &lt;p&gt;&lt;em&gt;&lt;font color="#c0504d"&gt;Update&lt;/font&gt;: The &lt;/em&gt;&lt;a href="https://nuget.org/packages/Glav.CacheAdapter/2.4.1" target="_blank"&gt;&lt;em&gt;CacheAdapter&lt;/em&gt;&lt;/a&gt;&lt;em&gt; has actualy just had a &lt;/em&gt;&lt;a href="https://nuget.org/packages/Glav.CacheAdapter/2.4.1" target="_blank"&gt;&lt;em&gt;minor revision to 2.4.1.&lt;/em&gt;&lt;/a&gt;&lt;em&gt; This significantly increases the performance and reliability in memcached scenario under more extreme loads. General to moderate usage wont see any noticeable difference though.&lt;/em&gt;&lt;/p&gt;  &lt;h3&gt;Bugs&lt;/h3&gt;  &lt;p&gt;This latest version fixes a big that is only present in the memcached implementation and is only seen in rare, intermittent times (making i particularly hard to find). The bug is where a cache node would be removed from the farm when errors in deserialization of cached objects would occur due to serialised data not being read from the stream in entirety.&lt;/p&gt;  &lt;p&gt;The code also contains enhancements to better surface serialization exceptions to aid in the debugging process. This is also specifically targeted at the memcached implementation. This is important when moving from something like memory or Asp.Web caching mechanisms to memcached where the serialization rules are not as lenient.&lt;/p&gt;  &lt;p&gt;There are a few other minor bug fixes, code cleanup and a little refactoring.&lt;/p&gt;  &lt;h3&gt;Minor feature addition&lt;/h3&gt;  &lt;p&gt;In addition to this bug fix, many people have asked for a single setting to either enable or disable the cache.In this version, you can disable the cache by setting the &lt;font face="Courier New"&gt;IsCacheEnabled&lt;/font&gt; flag to false in the application configuration file. Something like the example below:&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:7f804107-f546-4586-8bf6-3a765beacdf6" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;overflow: auto;"&gt;&lt;span style="color: #000000;"&gt;  &lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;Glav.CacheAdapter.MainConfig&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;
    &lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;setting name&lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #800000;"&gt;CacheToUse&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #000000;"&gt; serializeAs&lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #800000;"&gt;String&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;
      &lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;value&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;memcached&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #000000;"&gt;value&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;
    &lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #000000;"&gt;setting&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;
    &lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;setting name&lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #800000;"&gt;DistributedCacheServers&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #000000;"&gt; serializeAs&lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #800000;"&gt;String&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;
      &lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;value&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;localhost:&lt;/span&gt;&lt;span style="color: #800080;"&gt;11211&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #000000;"&gt;value&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;
    &lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #000000;"&gt;setting&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;
    &lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;setting name&lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #800000;"&gt;IsCacheEnabled&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #000000;"&gt; serializeAs&lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #800000;"&gt;String&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;
      &lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;value&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;False&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #000000;"&gt;value&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;
    &lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #000000;"&gt;setting&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;
  &lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #000000;"&gt;Glav.CacheAdapter.MainConfig&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;
&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;Your reasons to use this feature may vary (perhaps some performance testing or problem diagnosis). At any rate, disabling the cache will cause every attempt to retrieve data from the cache, resulting in a cache miss and returning null. If you are using the &lt;font face="Courier New"&gt;ICacheProvider&lt;/font&gt; with the delegate/Func&amp;lt;T&amp;gt; syntax to populate the cache, this delegate method will get executed every single time. For example, when the cache is disabled, the following delegate/Func&amp;lt;T&amp;gt; code will be executed every time:&lt;/p&gt;

&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:56163e0e-732e-4b5b-ad2b-a57521928057" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;overflow: auto;"&gt;&lt;span style="color: #000000;"&gt;var data1 &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; cacheProvider.Get&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;SomeData&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;(&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #800000;"&gt;cache-key&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #000000;"&gt;, DateTime.Now.AddHours(&lt;/span&gt;&lt;span style="color: #800080;"&gt;1&lt;/span&gt;&lt;span style="color: #000000;"&gt;), () &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;
{
    &lt;/span&gt;&lt;span style="color: #008000;"&gt;//&lt;/span&gt;&lt;span style="color: #008000;"&gt; With the cache disabled, this data access code is executed every attempt to
    &lt;/span&gt;&lt;span style="color: #008000;"&gt;//&lt;/span&gt;&lt;span style="color: #008000;"&gt; get this data via the CacheProvider.&lt;/span&gt;&lt;span style="color: #008000;"&gt;
&lt;/span&gt;&lt;span style="color: #000000;"&gt;    var someData &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;new&lt;/span&gt;&lt;span style="color: #000000;"&gt; SomeData() { SomeText &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #800000;"&gt;cache example1&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #000000;"&gt;, SomeNumber &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;1&lt;/span&gt;&lt;span style="color: #000000;"&gt; };
    &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;return&lt;/span&gt;&lt;span style="color: #000000;"&gt; someData;
});
&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;One final note: If you access the cache directly via the &lt;font face="Courier New"&gt;ICache&lt;/font&gt; instance, instead of the higher level &lt;font face="Courier New"&gt;ICacheProvider &lt;/font&gt;API, you bypass this setting and still access the underlying cache implementation. Only the &lt;font face="Courier New"&gt;ICacheProvider &lt;/font&gt;instance observes the &lt;font face="Courier New"&gt;IsCacheEnabled&lt;/font&gt; setting.&lt;/p&gt;

&lt;p&gt;Thanks to those individuals who have used this library and provided feedback. Ifyou have any suggestions or ideas, please submit them to the &lt;a href="https://bitbucket.org/glav/cacheadapter" target="_blank"&gt;issue register on bitbucket&lt;/a&gt; (which is where you can grab all the source code from too)&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=8368133" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/pglavich/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/pglavich/archive/tags/.NET/default.aspx">.NET</category><category domain="http://weblogs.asp.net/pglavich/archive/tags/Community+News/default.aspx">Community News</category><category domain="http://weblogs.asp.net/pglavich/archive/tags/Architecture/default.aspx">Architecture</category><category domain="http://weblogs.asp.net/pglavich/archive/tags/CacheAdapter/default.aspx">CacheAdapter</category></item><item><title>ASP.NET Web Api–Request/Response/Usage Logging</title><link>http://weblogs.asp.net/pglavich/archive/2012/02/26/asp-net-web-api-request-response-usage-logging.aspx</link><pubDate>Sun, 26 Feb 2012 10:26:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:8317824</guid><dc:creator>Glav</dc:creator><slash:comments>14</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/pglavich/rsscomments.aspx?PostID=8317824</wfw:commentRss><comments>http://weblogs.asp.net/pglavich/archive/2012/02/26/asp-net-web-api-request-response-usage-logging.aspx#comments</comments><description>&lt;p&gt;For &lt;a href="http://weblogs.asp.net/pglavich/archive/2012/02/18/mvc4-and-web-api-make-an-api-the-way-you-always-wanted-part-1.aspx" target="_blank"&gt;part 1&lt;/a&gt; of this series of blog posts on ASP.Net Web Api – &lt;a href="http://weblogs.asp.net/pglavich/archive/2012/02/18/mvc4-and-web-api-make-an-api-the-way-you-always-wanted-part-1.aspx" target="_blank"&gt;see here.&lt;/a&gt;&lt;/p&gt;  &lt;h3&gt;Introduction&lt;/h3&gt;  &lt;p&gt;In Part 2 of this blog post series, we deal with the common requirement of logging, or recording the usage of your Web Api. That is, recording when an Api call was made, what information was passed in via the Api call, and what response was issued to the consumer.&lt;/p&gt;  &lt;h3&gt;My new Api is awesome – but is it being used?&lt;/h3&gt;  &lt;p&gt;So you have a shiny new Api all built on the latest bits from Microsoft, the ASP.Net Web Api that shipped with the Beta of MVC 4. A common need for a lot of Api’s is to log usage of each call, to record what came in as part of the request, to also record what response was sent to the consumer. This kind of information is really handy for debugging.Not just for you, but also for your customers as well. Being able to backtrack over the history of Api calls to determine the full context of some problem for a consumer can save a lot of time and guesswork.&lt;/p&gt;  &lt;p&gt;So, how do we do this with the Web Api?&lt;/p&gt;  &lt;p&gt;Glad you asked.&lt;/p&gt;  &lt;h3&gt;Determining what kind of injection point to utilise&lt;/h3&gt;  &lt;p&gt;We have a few options when it comes to determining where to inject our custom classes/code to best intercept incoming and outgoing data. To log all incoming and outgoing data, the most applicable interception point is a &lt;font face="Courier New"&gt;System.Net.Http.DelegatingHandler&lt;/font&gt;. These classes are message handlers that apply to all messages for all requests and can be chained together to have multiple handlers registered within the message handling pipeline. For example, in addition to Api usage logging, you may want to provide a generic authentication handler that checks for the presence of some authentication key. I could have chosen to use filters however these are typically more scoped to the action itself. I could potentially use model binders but these are relatively late in the processing cycle and not generic enough (plus it would take some potentially unintuitive code to make it work as we would want)..&lt;/p&gt;  &lt;h3&gt;Enough with theory, show me some code&lt;/h3&gt;  &lt;p&gt;So, our initial Api usage logger will inherit from the &lt;font face="Courier New"&gt;DelegatingHandler&lt;/font&gt; class (in the &lt;font face="Courier New"&gt;System.Net.Http&lt;/font&gt; namespace) and provide its own implementation.&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:51b820a7-991d-44c1-ab73-b1091117d164" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;overflow: auto;"&gt;&lt;span style="color: #0000FF;"&gt;public&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;class&lt;/span&gt;&lt;span style="color: #000000;"&gt; ApiUsageLogger : DelegatingHandler
{
    &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;protected&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;override&lt;/span&gt;&lt;span style="color: #000000;"&gt; System.Threading.Tasks.Task&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;HttpResponseMessage&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt; SendAsync(HttpRequestMessage request, System.Threading.CancellationToken cancellationToken)
    {
        &lt;/span&gt;&lt;span style="color: #008000;"&gt;//&lt;/span&gt;&lt;span style="color: #008000;"&gt;your stuff goes here...&lt;/span&gt;&lt;span style="color: #008000;"&gt;
&lt;/span&gt;&lt;span style="color: #000000;"&gt;    }
}
&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;We only really need to override one method, the ‘&lt;font face="Courier New"&gt;SendAsync'&lt;/font&gt; method which returns a &lt;font face="Courier New"&gt;Task&lt;/font&gt;. &lt;font face="Courier New"&gt;Task&lt;/font&gt; objects play heavily in the new Web Api and allow asynchronous processing to be used as the primary processing paradigm allowing better scalability and processing utilisation.&lt;/p&gt;

&lt;p&gt;Since everything is asynchronous via the use of Tasks, we need to ensure we play by the right rules and utilise the asynchronous capabilities of the Task class. A more fleshed out version of our method is shown below:&lt;/p&gt;

&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:49a4c68f-f7ab-4f54-a0f8-6dca292472e6" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;overflow: auto;"&gt;&lt;span style="color: #0000FF;"&gt;protected&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;override&lt;/span&gt;&lt;span style="color: #000000;"&gt; System.Threading.Tasks.Task&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;HttpResponseMessage&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt; SendAsync(HttpRequestMessage request, System.Threading.CancellationToken cancellationToken)
{
    &lt;/span&gt;&lt;span style="color: #008000;"&gt;//&lt;/span&gt;&lt;span style="color: #008000;"&gt; Extract the request logging information&lt;/span&gt;&lt;span style="color: #008000;"&gt;
&lt;/span&gt;&lt;span style="color: #000000;"&gt;    var requestLoggingInfo &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; ExtractLoggingInfoFromRequest(request);

    &lt;/span&gt;&lt;span style="color: #008000;"&gt;//&lt;/span&gt;&lt;span style="color: #008000;"&gt; Execute the request, this does not block&lt;/span&gt;&lt;span style="color: #008000;"&gt;
&lt;/span&gt;&lt;span style="color: #000000;"&gt;    var response &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;base&lt;/span&gt;&lt;span style="color: #000000;"&gt;.SendAsync(request, cancellationToken);

    &lt;/span&gt;&lt;span style="color: #008000;"&gt;//&lt;/span&gt;&lt;span style="color: #008000;"&gt; Log the incoming data to the database&lt;/span&gt;&lt;span style="color: #008000;"&gt;
&lt;/span&gt;&lt;span style="color: #000000;"&gt;    WriteLoggingInfo(requestLoggingInfo);

    &lt;/span&gt;&lt;span style="color: #008000;"&gt;//&lt;/span&gt;&lt;span style="color: #008000;"&gt; Once the response is processed asynchronously, log the response data
    &lt;/span&gt;&lt;span style="color: #008000;"&gt;//&lt;/span&gt;&lt;span style="color: #008000;"&gt; to the database&lt;/span&gt;&lt;span style="color: #008000;"&gt;
&lt;/span&gt;&lt;span style="color: #000000;"&gt;    response.ContinueWith((responseMsg) &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;
    {
        &lt;/span&gt;&lt;span style="color: #008000;"&gt;//&lt;/span&gt;&lt;span style="color: #008000;"&gt; Extract the response logging info then persist the information&lt;/span&gt;&lt;span style="color: #008000;"&gt;
&lt;/span&gt;&lt;span style="color: #000000;"&gt;        var responseLoggingInfo &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; ExtractResponseLoggingInfo(requestLoggingInfo, responseMsg.Result);
        WriteLoggingInfo(responseLoggingInfo);
    });
    &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;return&lt;/span&gt;&lt;span style="color: #000000;"&gt; response;
}
&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;To step through the code:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;First, we extract relevant information from the request message. &lt;/li&gt;

  &lt;li&gt;We execute the request asynchronously via the base class &lt;font face="Courier New"&gt;SendAsync&lt;/font&gt; method passing in what we received. &lt;/li&gt;

  &lt;li&gt;We log the request information (perhaps to a database). Note that this operation could be performed asynchronously as well. &lt;/li&gt;

  &lt;li&gt;We then define an anonymous function that the asynchronous request task should execute once it has completed.This is done using the ‘&lt;font face="Courier New"&gt;ContinueWith’&lt;/font&gt; construct, and passing in an instance of the response message &lt;/li&gt;

  &lt;li&gt;We return the ‘response’, which is really just a Task object that will process the request asynchronously and execute our response logging code once the request has completed processing. . &lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;Registering our Handler&lt;/h3&gt;

&lt;p&gt;We have created our custom &lt;font face="Courier New"&gt;DelegatingHandler&lt;/font&gt;, so we need to tell the framework about it.&lt;/p&gt;

&lt;p&gt;The registration is done at application startup through access to the &lt;font face="Courier New"&gt;GlobalConfiguration&lt;/font&gt; object provided by the framework. In the &lt;font face="Courier New"&gt;global.asax.cs&lt;/font&gt; file, you will find the &lt;font face="Courier New"&gt;Application_Start&lt;/font&gt; method looking something like this:&lt;/p&gt;

&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:7e8da10b-c2c2-4a16-9275-0a80c9309324" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;overflow: auto;"&gt;&lt;span style="color: #0000FF;"&gt;protected&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;void&lt;/span&gt;&lt;span style="color: #000000;"&gt; Application_Start()
{
    RouteTable.Routes.IgnoreRoute(&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #800000;"&gt;{resource}.axd/{*pathInfo}&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #000000;"&gt;);
    RouteTable.Routes.IgnoreRoute(&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #800000;"&gt;{resource}.ico&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #000000;"&gt;);

    RegisterApis(GlobalConfiguration.Configuration);
}
&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;You can see the &lt;font face="Courier New"&gt;GlobalConfiguration.Configuration&lt;/font&gt; object (which is of type &lt;font face="Courier New"&gt;System.Web.Http.HttpConfiguration&lt;/font&gt;) being passed to the &lt;font face="Courier New"&gt;RegisterApis&lt;/font&gt; method. We then simply register our &lt;font face="Courier New"&gt;DelegatingHandler&lt;/font&gt; into the list of &lt;font face="Courier New"&gt;MessageHandler&lt;/font&gt; from the configuration object as in the following code:&lt;/p&gt;

&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:54815448-4f69-44c1-a893-8a336e3d1d90" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;overflow: auto;"&gt;&lt;span style="color: #0000FF;"&gt;public&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;static&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;void&lt;/span&gt;&lt;span style="color: #000000;"&gt; RegisterApis(HttpConfiguration config)
{
    config.MessageHandlers.Add(&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;new&lt;/span&gt;&lt;span style="color: #000000;"&gt; ApiUsageLogger());
}
&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;&lt;em&gt;Note: If you have dependency injection setup for your Web Api project, you can either pass in the service resolver as a constructor argument when registering this handler (in the global.asax.cs for example ) like:&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;font face="Courier New"&gt;&lt;em&gt;config.MessageHandlers.Add(&lt;font color="#0000ff"&gt;new&lt;/font&gt; ApiUsageLogger(_myContainer));&lt;/em&gt;&lt;/font&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;or you can use that same resolver to resolve an instance of this &lt;font face="Courier New"&gt;DelegatingHandler&lt;/font&gt; when adding to the &lt;font face="Courier New"&gt;MessageHandler&lt;/font&gt; collection as in:&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;font face="Courier New"&gt;&lt;em&gt;config.MessageHandlers.Add(_myContainer.Resolve&amp;lt;IApiUsageLogger&amp;gt;());&lt;/em&gt;&lt;/font&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;I shall address some of the nuances of DI in a separate post.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;And that is it. Well, that is all that is required to register a new handler that will get called for each request coming into your Api. Actually logging the information requires a little more work to ensure we play correctly in this nice asynchronous world of the WebApi.&lt;/p&gt;

&lt;h3&gt;But wait, there’s more&lt;/h3&gt;

&lt;p&gt;Playing nice in the asynchronous world of the WebApi requires a little bit of care. For example, to extract out request information, particularly the body of the request message, you might write a method like the following:&lt;/p&gt;

&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:2456daee-8b7e-43d1-ac74-9abd025fa508" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;overflow: auto;"&gt;&lt;span style="color: #0000FF;"&gt;private&lt;/span&gt;&lt;span style="color: #000000;"&gt; ApiLoggingInfo ExtractLoggingInfoFromRequest(HttpRequestMessage request)
{
    var info &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;new&lt;/span&gt;&lt;span style="color: #000000;"&gt; ApiLoggingInfo();
    info.HttpMethod &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; request.Method.Method;
    info.UriAccessed &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; request.RequestUri.AbsoluteUri;
    info.IPAddress &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; HttpContext.Current &lt;/span&gt;&lt;span style="color: #000000;"&gt;!=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;null&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #000000;"&gt;?&lt;/span&gt;&lt;span style="color: #000000;"&gt; HttpContext.Current.Request.UserHostAddress : &lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #800000;"&gt;0.0.0.0&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #000000;"&gt;;

    ExtractMessageHeadersIntoLoggingInfo(info, request.Headers.ToList());
    &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;if&lt;/span&gt;&lt;span style="color: #000000;"&gt; (request.Content &lt;/span&gt;&lt;span style="color: #000000;"&gt;!=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;null&lt;/span&gt;&lt;span style="color: #000000;"&gt;)
    {
        var byteResponse &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; request.Content.ReadAsByteArrayAsync().Result;
        info.BodyContent &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; System.Text.UTF8Encoding.UTF8.GetString(byteResponse);
    }
    &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;return&lt;/span&gt;&lt;span style="color: #000000;"&gt; info;
}
&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;Notice the line that reads:&lt;/p&gt;

&lt;pre&gt;var byteResponse = request.Content.ReadAsByteArrayAsync().Result;&lt;/pre&gt;

&lt;p&gt;Here we are accessing the &lt;font face="Courier New"&gt;Result&lt;/font&gt; property from an asynchronous task in an attempt to make this code procedural and work in a synchronous manner. It is an easy thing to do, and looks like it makes sense. However, &lt;strong&gt;&lt;u&gt;do not do this&lt;/u&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Generally, you should never access the ‘&lt;font face="Courier New"&gt;Result’&lt;/font&gt; property of a Task unless you know that the task has completed as &lt;strong&gt;this can cause deadlocks in ASP.Net&lt;/strong&gt;. Yes it is true.Sometimes this code may work but, if you don’t want to waste hours debugging deadlock issues in ASP.Net, I would advise against it. &lt;/p&gt;

&lt;p&gt;So how do we know when it is complete? With the ‘&lt;font face="Courier New"&gt;ContinueWith’&lt;/font&gt; construct of course. So your code may change to look more like this:&lt;/p&gt;

&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:5b977386-0023-42a0-8191-73ae776001e3" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;overflow: auto;"&gt;&lt;span style="color: #0000FF;"&gt;private&lt;/span&gt;&lt;span style="color: #000000;"&gt; ApiLoggingInfo ExtractLoggingInfoFromRequest(HttpRequestMessage request)
{
    var info &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;new&lt;/span&gt;&lt;span style="color: #000000;"&gt; ApiLoggingInfo();
    info.HttpMethod &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; request.Method.Method;
    info.UriAccessed &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; request.RequestUri.AbsoluteUri;
    info.IPAddress &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; HttpContext.Current &lt;/span&gt;&lt;span style="color: #000000;"&gt;!=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;null&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #000000;"&gt;?&lt;/span&gt;&lt;span style="color: #000000;"&gt; HttpContext.Current.Request.UserHostAddress : &lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #800000;"&gt;0.0.0.0&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #000000;"&gt;;

    ExtractMessageHeadersIntoLoggingInfo(info, request.Headers.ToList());
    &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;if&lt;/span&gt;&lt;span style="color: #000000;"&gt; (request.Content &lt;/span&gt;&lt;span style="color: #000000;"&gt;!=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;null&lt;/span&gt;&lt;span style="color: #000000;"&gt;)
    {
        request.Content.ReadAsByteArrayAsync()
            .ContinueWith((task) &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;
                            {
                                info.BodyContent &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; System.Text.UTF8Encoding.UTF8.GetString(task.Result);
                                &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;return&lt;/span&gt;&lt;span style="color: #000000;"&gt; info;

                            });
    }
    &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;return&lt;/span&gt;&lt;span style="color: #000000;"&gt; info;
}
&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;In the above example, we can safely use the ‘&lt;font face="Courier New"&gt;Result’&lt;/font&gt; property of the task since the method within the ‘&lt;font face="Courier New"&gt;ContinueWith&lt;/font&gt;’ block is only executed once the initial task is completed.&lt;/p&gt;

&lt;h3&gt;Wrap up&lt;/h3&gt;

&lt;p&gt;The above code fragments provide a simple and basic way of registering a custom interception point within the WebApi pipeline to perform a custom task, in this case usage logging.&lt;/p&gt;

&lt;p&gt;The next series of posts will look at items such as the use of model binding, security and much more.&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=8317824" width="1" height="1"&gt;</description></item><item><title>MVC4 and Web Api– make an Api the way you always wanted–Part 1</title><link>http://weblogs.asp.net/pglavich/archive/2012/02/18/mvc4-and-web-api-make-an-api-the-way-you-always-wanted-part-1.aspx</link><pubDate>Sat, 18 Feb 2012 01:33:49 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:8298985</guid><dc:creator>Glav</dc:creator><slash:comments>4</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/pglavich/rsscomments.aspx?PostID=8298985</wfw:commentRss><comments>http://weblogs.asp.net/pglavich/archive/2012/02/18/mvc4-and-web-api-make-an-api-the-way-you-always-wanted-part-1.aspx#comments</comments><description>&lt;p&gt;&lt;font color="#c0504d"&gt;Update&lt;/font&gt;: &lt;a href="http://weblogs.asp.net/pglavich/archive/2012/02/26/asp-net-web-api-request-response-usage-logging.aspx" target="_blank"&gt;Part 2&lt;/a&gt; of this series is available &lt;a href="http://weblogs.asp.net/pglavich/archive/2012/02/26/asp-net-web-api-request-response-usage-logging.aspx" target="_blank"&gt;here&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;ASP.NET MVC is a huge success as framework and just recently, &lt;a href="http://www.asp.net/mvc/mvc4" target="_blank"&gt;ASP.NET MVC4 Beta&lt;/a&gt; was released. While there are many changes in this release, I want to specifically focus on the WebApi portion of this.&lt;/p&gt;  &lt;p&gt;In previous flavours of MVC, many people wanted to develop REST Api’s and didn’t really want to use WCF for this purpose. The team at Microsoft was progressing along with a library called &lt;a href="http://wcf.codeplex.com/wikipage?title=WCF%20HTTP" target="_blank"&gt;WcfWebApi&lt;/a&gt;. This library used a very WCF’esque way of defining an Api. This mean defining an interface and decorating the interface with the appropriate WCF attributes to constitute your Api endpoints.&lt;/p&gt;  &lt;p&gt;However, a lot of people don’t like to use a WCF style of programming, and are really comfortable in the MVC world. Especially when you can construct similar REST endpoints in MVC with extreme ease. This is exactly what a lot of people who wanted a REST Api did. They simply used ASP.NET MVC to define a route and handled the payload themselves via standard MVC controllers.&lt;/p&gt;  &lt;p&gt;What the &lt;a href="http://www.asp.net/mvc/mvc4" target="_blank"&gt;WcfWebApi&lt;/a&gt; did quite well though was things like content negotiation (do you want XML or json?), auto help generation, message/payload inspection, transformation, parameter population and a lot of other things. &lt;/p&gt;  &lt;p&gt;Microsoft have recognised all this and decided to mix it all together. Take the good bits of &lt;a href="http://wcf.codeplex.com/wikipage?title=WCF%20HTTP" target="_blank"&gt;WcfWebApi&lt;/a&gt; and the Api development approach of MVC, and created an Api framework to easily expose your REST endpoints and retain the MVC ease of development. This is the &lt;a href="http://www.asp.net/mvc/mvc4" target="_blank"&gt;WebApi&lt;/a&gt; and it supersedes the &lt;a href="http://wcf.codeplex.com/wikipage?title=WCF%20HTTP" target="_blank"&gt;WcfWebApi&lt;/a&gt; (which will not continue to be developed).&lt;/p&gt;  &lt;h3&gt;So how do you make a REST sandwich now?&lt;/h3&gt;  &lt;p&gt;Well, best to start with the code.&lt;/p&gt;  &lt;p&gt;Firstly, lets define a route for our REST Api.&lt;/p&gt;  &lt;p&gt;In the &lt;font face="Courier New"&gt;Global.asax&lt;/font&gt;, we may have something like this:&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:198d26ae-d8c9-46d3-a29d-4b34acaf5bfc" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;overflow: auto;"&gt;&lt;span style="color: #0000FF;"&gt;public&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;static&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;void&lt;/span&gt;&lt;span style="color: #000000;"&gt; RegisterApis(HttpConfiguration config)
{
    config.MessageHandlers.Add(&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;new&lt;/span&gt;&lt;span style="color: #000000;"&gt; ApiUsageLogger());

    config.Routes.MapHttpRoute(&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #800000;"&gt;contacts-route&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #000000;"&gt;, &lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #800000;"&gt;contacts&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #000000;"&gt;, &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;new&lt;/span&gt;&lt;span style="color: #000000;"&gt; { controller &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #800000;"&gt;contacts&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #000000;"&gt; });
}
&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;Ignore the &lt;font face="Courier New"&gt;MessageHandler&lt;/font&gt; line for now, we will get back to that in a later post.&lt;/p&gt;

&lt;p&gt;You can see we are defining/mapping a Http route. The first parameter is the unique route name, second is the route template to use for processing these route requests. This means that when I get a request like &lt;a href="http://MyHost/Contacts"&gt;http://MyHost/&lt;strong&gt;Contacts&lt;/strong&gt;&lt;/a&gt;, this route will get a match. The third parameter&amp;#160; the default properties. In this case, I am simply stating that the controller to use is the ContactsController. This is shown below.&lt;/p&gt;

&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:681b16c4-46d9-4b6a-aeae-5ea7224fa49a" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;overflow: auto;"&gt;&lt;span style="color: #0000FF;"&gt;public&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;class&lt;/span&gt;&lt;span style="color: #000000;"&gt; ContactsController : ApiController
{
    &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;public&lt;/span&gt;&lt;span style="color: #000000;"&gt; List&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;ContactSummary&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt; GetContacts()
    {
        &lt;/span&gt;&lt;span style="color: #008000;"&gt;//&lt;/span&gt;&lt;span style="color: #008000;"&gt; do stuff....            &lt;/span&gt;&lt;span style="color: #008000;"&gt;
&lt;/span&gt;&lt;span style="color: #000000;"&gt;    }
}
&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;You can see that we have a class that inherits from a new &lt;font face="Courier New"&gt;ApiController&lt;/font&gt; class. We have a simple controller action that returns a list of &lt;font face="Courier New"&gt;ContactSummary&lt;/font&gt; objects.&lt;/p&gt;

&lt;p&gt;This is (while overly simplistic) a fully fledged REST Api that supports content negotiation and as such will respond with json or XML if it is requested by the client.&lt;/p&gt;

&lt;p&gt;The WebApi fully supports your typical &lt;a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html" target="_blank"&gt;Http verbs&lt;/a&gt; such as GET, PUT, POST, DELETE etc… and in the example above, the fact that I have an ApiController action method prefixed with a ‘Get’ means it will support the GET verb. If I had a method prefixed with ‘Post’ then that action method, by convention, would support the POST verb. You can optionally decorate the methods with &lt;font face="Courier New"&gt;[System.Web.Http.HttpGet], [System.Web.Http.HttpPost]&lt;/font&gt; attributes to achieve the same effect.&lt;/p&gt;

&lt;h4&gt;And OData as well?&lt;/h4&gt;

&lt;p&gt;Want to support OData? Then simply return an &lt;font face="Courier New"&gt;IQueryable&amp;lt;T&amp;gt;&lt;/font&gt;. Your Web Api will support querying via the OData URL conventions.&lt;/p&gt;

&lt;h2&gt;So how do I access this as a consumer?&lt;/h2&gt;

&lt;p&gt;Accessing your shiny new WebApi can now be done via the HttpClient class which really emphasises everything asynchronous, and easily supports the popular Http verbs GET, PUT, POST, DELETE. Let’s take a look.&lt;/p&gt;



&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:3875a466-5f24-41e7-963d-9b871d4f2e93" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;overflow: auto;"&gt;&lt;span style="color: #000000;"&gt;HttpClient client &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;new&lt;/span&gt;&lt;span style="color: #000000;"&gt; HttpClient();
client.DefaultRequestHeaders.Accept.Add(&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;new&lt;/span&gt;&lt;span style="color: #000000;"&gt; MediaTypeWithQualityHeaderValue(&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #800000;"&gt;application/json&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #000000;"&gt;));
var responseMsg &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; client.GetAsync(&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #800000;"&gt;http://SomeHost/Contacts&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #000000;"&gt;).Result;
&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;



&lt;p&gt;The &lt;font face="Courier New"&gt;responseMsg&lt;/font&gt; variable will contain a list of contacts as shown the in Api method described earlier. You can see we are requesting the data in JSON format. This could also be XML by using:&lt;/p&gt;

&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:a62fd954-f2f1-47c8-87f2-4f5b9ce4d6cd" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;overflow: auto;"&gt;&lt;span style="color: #000000;"&gt;HttpClient client &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;new&lt;/span&gt;&lt;span style="color: #000000;"&gt; HttpClient();
client.DefaultRequestHeaders.Accept.Add(&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;new&lt;/span&gt;&lt;span style="color: #000000;"&gt; MediaTypeWithQualityHeaderValue(&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #800000;"&gt;application/xml&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #000000;"&gt;));
var responseMsg &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; client.GetAsync(&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #800000;"&gt;http://SomeHost/Contacts&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #000000;"&gt;).Result;
&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;The Http client has methods for using http verbs explicitly. Namely, &lt;font face="Courier New"&gt;GetAsync, PutAsync, PostAsync and DeleteAsync&lt;/font&gt;.&lt;/p&gt;

&lt;h2&gt;Summary&lt;/h2&gt;

&lt;p&gt;Microsoft have recognised the value of the MVC programming model and incorporated the best of &lt;a href="http://wcf.codeplex.com/wikipage?title=WCF%20HTTP" target="_blank"&gt;WcfWebApi&lt;/a&gt; into MVC for a really nice way of exposing your Api. You get a lot of features for free making great Api’s a hell of a lot easier.&lt;/p&gt;

&lt;p&gt;You can get further information on the overall MVC4 release from &lt;a href="http://weblogs.asp.net/jgalloway/archive/2012/02/16/asp-net-4-beta-released.aspx" target="_blank"&gt;this post&lt;/a&gt; by Jon Galloway.&lt;/p&gt;

&lt;p&gt;In the next part to this post, I will explore how you can do model binding, dependency injection, and insert custom handlers into the MVC/Api processing pipeline.&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=8298985" width="1" height="1"&gt;</description></item><item><title>ScriptHelper now a Nuget package, and managing your Javascript</title><link>http://weblogs.asp.net/pglavich/archive/2012/01/21/scripthelper-now-a-nuget-package-and-managing-your-javascript.aspx</link><pubDate>Sat, 21 Jan 2012 03:45:54 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:8265584</guid><dc:creator>Glav</dc:creator><slash:comments>3</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/pglavich/rsscomments.aspx?PostID=8265584</wfw:commentRss><comments>http://weblogs.asp.net/pglavich/archive/2012/01/21/scripthelper-now-a-nuget-package-and-managing-your-javascript.aspx#comments</comments><description>&lt;p&gt;For a while now I have been looking at different ways of managing javascript inclusion in web pages, and also managing the dependencies that each script inclusion requires. Furthermore, working with ASP.NET MVC and partial views, working with the ever increasing number of dependencies as well as ensuring that each partial view has the script it requires, can be a little challenging. At the very least messy and tedious.&lt;/p&gt;  &lt;p&gt;Ideally, I’d like every page or partial view to be able to express what scripts it requires and let the framework care about removing duplication, minification etc.&lt;/p&gt;  &lt;p&gt;So, after looking at the needs of our application, as well as the needs of others I developed the ScriptHelper component which is now available as a &lt;a href="http://nuget.org/packages/ScriptHelper" target="_blank"&gt;Nuget package&lt;/a&gt;. I had released an initial version some time ago, details are &lt;a href="http://weblogs.asp.net/pglavich/archive/2010/11/18/scripthelper-for-mvc-and-webforms-projects.aspx" target="_blank"&gt;here&lt;/a&gt;. &lt;/p&gt;  &lt;p&gt;This latest version has the following features:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Support for &lt;font face="Courier New"&gt;RequiresScriptsDeferred&lt;/font&gt; and &lt;font face="Courier New"&gt;RenderDeferred&lt;/font&gt; methods to allow you to specify script requirements as many times as you like, where ever you like and to have these requirements only rendered to the page when the &lt;font face="Courier New"&gt;RenderDeferred&lt;/font&gt; method is called. This makes it easy to include the &lt;font face="Courier New"&gt;RenderDeferred&lt;/font&gt; at the bottom of your master page so all deferred scripts are rendered then. These scripts can be minified and combined at this time as well. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;So you can do:&lt;/p&gt;  &lt;pre&gt;&lt;font size="2" face="Arial Narrow"&gt;@ScriptDependencyExtension.ScriptHelper.&lt;strong&gt;RequiresScriptsDeferred&lt;/strong&gt;(&amp;quot;jQuery&amp;quot;)&lt;/font&gt;&lt;/pre&gt;

&lt;p&gt;and maybe somewhere else in the page or in another partial view&lt;/p&gt;

&lt;pre&gt;&lt;font size="2" face="Arial Narrow"&gt;@ScriptDependencyExtension.ScriptHelper.&lt;strong&gt;RequiresScriptsDeferred&lt;/strong&gt;(&amp;quot;jQuery-validate-unobtrusive&amp;quot;)&lt;/font&gt;&lt;/pre&gt;

&lt;pre&gt;&lt;font size="1"&gt;&lt;font size="2" face="Arial Narrow"&gt;@ScriptDependencyExtension.ScriptHelper.&lt;strong&gt;RequiresScriptsDeferred&lt;/strong&gt;(&amp;quot;SomeOtherScripts&amp;quot;)&lt;/font&gt; &lt;/font&gt;&lt;/pre&gt;

&lt;p&gt;Then when the:&lt;/p&gt;

&lt;pre&gt;&lt;font size="2" face="Arial Narrow"&gt;@ScriptDependencyExtension.ScriptHelper.&lt;strong&gt;RenderDeferredScripts&lt;/strong&gt;()&lt;/font&gt;&lt;/pre&gt;

&lt;p&gt;is called, all the previously deferred scripts are combined, minified and rendered as one single file inclusion&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Support for &lt;a href="http://www.dotlesscss.org/" target="_blank"&gt;.Less&lt;/a&gt; so you can have variables and functions in your CSS. No need to use a .less extension as .&lt;a href="http://www.dotlesscss.org/" target="_blank"&gt;Less&lt;/a&gt; is automatically invoked if it is enabled for the first time it is required. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;Note: Although the project source is titled as an MVC script helper, it can also be used within Webforms without issue as it is a simple static method with no reliance on anything from MVC. I just happened to start coding it with an MVC project in mind.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Additionally, the helper fully supports CSS and .less CSS semantics and syntax.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;There are other frameworks that do similar things but I created this one for a few reasons:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;The code is pretty small and simple. Very easy to modify and extend to suit your own purposes. It currently uses the Microsoft Ajax minifier. If you dont like it, implement the “&lt;font face="Courier New"&gt;IScriptProcessingFilter&lt;/font&gt;” and replace the current minifier component with your own. &lt;/li&gt;

  &lt;li&gt;I liked the ability to express dependencies explicitly. The number of JS libraries to use grows every day and it can get tricky in large apps to easily see what is required and needed. &lt;/li&gt;

  &lt;li&gt;I didn’t find one that &lt;em&gt;easily&lt;/em&gt; implemented the deferred loading scenario, took care of duplicates, and minification, and .less support. Maybe there is now, but I got started on it anyway. Or maybe there is one, but the implementation looked ugly. Either way, I wasn’t satisfied with the current offerings. &lt;/li&gt;

  &lt;li&gt;I like coding stuff &lt;img style="border-bottom-style: none; border-left-style: none; border-top-style: none; border-right-style: none" class="wlEmoticon wlEmoticon-smile" alt="Smile" src="http://weblogs.asp.net/blogs/pglavich/wlEmoticon-smile_3A6E8B78.png" /&gt; &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Download the Nuget package from &lt;a href="http://nuget.org/packages/ScriptHelper" target="_blank"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Download the source from &lt;a href="https://bitbucket.org/glav/mvc-script-dependency-extension" target="_blank"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;For full documentation on ScriptHelper (minus the new features here), see &lt;a href="http://weblogs.asp.net/pglavich/archive/2010/11/18/scripthelper-for-mvc-and-webforms-projects.aspx" target="_blank"&gt;this post&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;A &lt;font face="Courier New"&gt;ReadMe.txt&lt;/font&gt; file is included in the package with all configuration details.&lt;/p&gt;

&lt;h4&gt;But .Net 4.5 will include a bundling facility to address some this. Why would I use this?&lt;/h4&gt;

&lt;p&gt;See &lt;a href="http://weblogs.asp.net/scottgu/archive/2011/11/27/new-bundling-and-minification-support-asp-net-4-5-series.aspx" target="_blank"&gt;Scott Gu’s blog post&lt;/a&gt; for more info on this feature.&lt;/p&gt;

&lt;p&gt;Well of course you don't have to and if you were happy with the default way bundling works, it is probably worth sticking with that. I mean, you don’t want to include extra libraries if you don’t have to.&lt;/p&gt;

&lt;p&gt;However the bundling supports expressing dependencies programmatically in code. it is ok, but I prefer the XML file. Easier to read and define IMO. Sure the code isn’t that hard to read, but it could be anywhere and takes a bit more analysis to see all of the dependencies any one component may need.&lt;/p&gt;

&lt;p&gt;There is no support for .less files out of the box. I am sure this wont be far off as adding .less support is pretty trivial. However it is not in there by default.&lt;/p&gt;

&lt;p&gt;Bundling will include what you tell it to when you tell it to. This is good, but I love the idea of views or pages being able to express their dependencies without worry of script duplication. The deferred script loading feature of the ScriptHelper was one of my main reasons for developing this. If you don’t use deferred script loading/inclusion, then ScriptHelper will assume you know what you are doing and include the script file immediately as per bundling.&lt;/p&gt;

&lt;p&gt;The bundling feature will use .Net 4.5. If you are bound to lower versions of the framework, this is a blocker. ScriptHelper is compiled against .Net 4, however there is nothing that really relies on .Net 4 specifics. If you were targeting .Net 1, it could prove a little tricky as generics are used and a few other features, but migrating/compiling against lower versions should not be very hard.&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;h2&gt;Managing your Javascript&lt;/h2&gt;

&lt;p&gt;So how does this help manage your javascript in your projects?&lt;/p&gt;

&lt;h3&gt;PartialViews&lt;/h3&gt;

&lt;p&gt;Normally partial views, other pages either rely on a knowledge of what is included via the master page and what is also included on the content page itself in terms of scripts. A partial view may include its own scripts, provided that script is not included elsewhere to prevent duplicate script files being loaded.&lt;/p&gt;

&lt;p&gt;Using ScriptHelper and DeferredScripts your partial view can include anything all its dependencies. So the partial view can use the “&lt;font face="Courier New"&gt;RequiresScriptsDeferred&lt;/font&gt;” method to express its dependencies. At some point in time, typically at the bottom of your master page/layout, you will call the “&lt;font face="Courier New"&gt;RenderDeferredScripts&lt;/font&gt;” where all the required scripts are rendered. Typically this is also used with script combination and minification so that only 1 include is rendered. All duplicate inclusions are ignored, all files are combined, minified, and .less filter is run over it if required. A single script include is generated and your done. If the master page/layout has already expressed dependencies (such as jQuery for example) and the partial view also expresses this dependencies, it is ignored.&lt;/p&gt;

&lt;p&gt;This means each partial view, component or page can express all its dependencies that it requires without fear of duplication.&lt;/p&gt;

&lt;h3&gt;Script Dependencies&lt;/h3&gt;

&lt;p&gt;The way ScriptHelper works is by using a friendly name to group one or more script files together in the &lt;font face="Courier New"&gt;ScriptDependencies.xml&lt;/font&gt; file. The way this is all grouped is up to you. You can group by component (jQuery, validation, etc…) or even by page or anything else. There only needs to be 1 reference to the explicit name of your script, everything else is by friendly name so when a version number of a script changes, potentially changing the name of the script file, you only need to change the name of the file in one place.&lt;/p&gt;

&lt;p&gt;In addition, the dependencies for scripts in the XML file are listed explicitly. There is no guesswork as to what script require what in terms of dependencies since it is listed for easy identification.&lt;/p&gt;

&lt;p&gt;Finally, if you like having version identifiers on your query strings to assist with browser caching, this can also be expressed in the &lt;font face="Courier New"&gt;ScriptDependencies.xml&lt;/font&gt; file. This means you can have a script with &lt;a href="http://host/script.js?v=123"&gt;&lt;font face="Courier New"&gt;http://host/script.js?v=123&lt;/font&gt;&lt;/a&gt; where the &lt;font face="Courier New"&gt;v=123&lt;/font&gt; is the version identifier. The ‘&lt;font face="Courier New"&gt;v&lt;/font&gt;’ and the ‘&lt;font face="Courier New"&gt;123&lt;/font&gt;’ are all declared in the XML file which is easily updated manually or via a build script.&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;So if you find yourself in Javascript inclusion and dependency hell, give this library a shot, it may help.&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=8265584" width="1" height="1"&gt;</description></item><item><title>My Code Kata–A Solution Kata</title><link>http://weblogs.asp.net/pglavich/archive/2011/11/13/my-code-kata-a-solution-kata.aspx</link><pubDate>Sun, 13 Nov 2011 03:53:56 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:8051632</guid><dc:creator>Glav</dc:creator><slash:comments>4</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/pglavich/rsscomments.aspx?PostID=8051632</wfw:commentRss><comments>http://weblogs.asp.net/pglavich/archive/2011/11/13/my-code-kata-a-solution-kata.aspx#comments</comments><description>&lt;p&gt;There are many developers and coders out there who like to do &lt;a href="http://codekata.pragprog.com/2007/01/code_kata_backg.html" target="_blank"&gt;code Kata’s&lt;/a&gt; to keep their coding ability up to scratch and to practice their skills. I think it is a good idea.&lt;/p&gt;  &lt;p&gt;While I like the concept, I find them dead boring and of minimal purpose. Yes, they serve to hone your skills but that’s about it. They are often quite abstract, in that they usually focus on a small problem set requiring specific solutions. It is fair enough as that is how they are designed but again, I find them quite boring.&lt;/p&gt;  &lt;p&gt;What I personally like to do is go for something a little larger and a little more fun. It takes a little more time and is not as easily executed as a kata though, but it services the same purposes from a practice perspective and allows me to continue to solve some problems that are not directly part of the initial goal. This means I can cover a broader learning range and have a bit more fun. If I am lucky, sometimes they even end up being useful tools.&lt;/p&gt;  &lt;p&gt;With that in mind, I thought I’d share my current ‘kata’. It is not really a code kata as it is too big. I prefer to think of it as a ‘solution kata’.&lt;/p&gt;  &lt;p&gt;The code is on bitbucket &lt;a href="https://bitbucket.org/glav/ecodev/overview" target="_blank"&gt;here&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;What I wanted to do was create a kind of simplistic virtual world where I can create a player, or a class, stuff it into the world, and see if it survives, and can navigate its way to the exit.&lt;/p&gt;  &lt;p&gt;Requirements were pretty simple:&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;Must be able to define a map to describe the world using simple X,Y co-ordinates. Z co-ordinates as well if you feel like getting clever. &lt;/li&gt;    &lt;li&gt;Should have the concept of entrances, exists, solid blocks, and potentially other materials (again if you want to get clever). &lt;/li&gt;    &lt;li&gt;A coder should be able to easily write a class which will act as an inhabitant of the world. &lt;/li&gt;    &lt;li&gt;An inhabitant will receive stimulus from the world in the form of surrounding environment and be able to make a decision on action which it passes back to the ‘world’ for processing. &lt;/li&gt;    &lt;li&gt;At a minimum, an inhabitant will have sight and speed characteristics which determine how far they can ‘see’ in the world, and how fast they can move. &lt;/li&gt;    &lt;li&gt;Coders who write a really bad ‘inhabitant’ should not adversely affect the rest of world. &lt;/li&gt;    &lt;li&gt;Should allow multiple inhabitants in the world. &lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;So that was the solution I set out to act as a practice solution and a little bit of fun. It had some interesting problems to solve and I figured, if it turned out ok, I could potentially use this as a ‘developer test’ for interviews. Ask a potential coder to write a class for an inhabitant. Show the coder the map they will navigate, but also mention that we will use their code to navigate a map they have not yet seen and a little more complex.&lt;/p&gt;  &lt;p&gt;I have been playing with solution for a short time now and have it working in basic concepts. Below is a screen shot using a very basic console visualiser that shows the map, boundaries, blocks, entrance, exit and players/inhabitants.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://weblogs.asp.net/blogs/pglavich/image_66E77FBC.png"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto; padding-top: 0px" title="image" border="0" alt="image" src="http://weblogs.asp.net/blogs/pglavich/image_thumb_4925F4FD.png" width="170" height="306" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;The yellow asterisks ‘&lt;font style="background-color: #a5a5a5" color="#ffff00"&gt;*&lt;/font&gt;’ are the players, green ‘&lt;font style="background-color: #a5a5a5" color="#00ff00"&gt;O&lt;/font&gt;’ the entrance, purple ‘&lt;font style="background-color: #666666" color="#9b00d3"&gt;^&lt;/font&gt;’ the exit, maroon/browny ‘&lt;font style="background-color: #a5a5a5" color="#c0504d"&gt;#&lt;/font&gt;’ are solid blocks.&lt;/p&gt;  &lt;p&gt;The players can move around at different speeds, knock into each others, and make directional movement decisions based on what they see and who is around them. It has been quite fun to write and it is also quite fun to develop different players to inject into the world.&lt;/p&gt;  &lt;p&gt;The code below shows a really simple implementation of an inhabitant that can work out what to do based on stimulus from the world. It is pretty simple and just tries to move in some direction if there is nothing blocking the path.&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:758e0496-1b0f-4bb2-84ee-2cdb6cd81cdb" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;overflow: auto;"&gt;&lt;span style="color: #0000FF;"&gt;public&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;class&lt;/span&gt;&lt;span style="color: #000000;"&gt; TestPlayer:LivingEntity
{
    &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;public&lt;/span&gt;&lt;span style="color: #000000;"&gt; TestPlayer()
    {
        Name &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #800000;"&gt;Beta Boy&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #000000;"&gt;;
        LifeKey &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; Guid.NewGuid();
    }
    &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;public&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;override&lt;/span&gt;&lt;span style="color: #000000;"&gt; ActionResult DecideActionToPerform(EcoDev.Core.Common.Actions.ActionContext actionContext)
    {
        &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;try&lt;/span&gt;&lt;span style="color: #000000;"&gt;
        {
            var action &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;new&lt;/span&gt;&lt;span style="color: #000000;"&gt; MovementAction();

            &lt;/span&gt;&lt;span style="color: #008000;"&gt;//&lt;/span&gt;&lt;span style="color: #008000;"&gt; move forward if we can&lt;/span&gt;&lt;span style="color: #008000;"&gt;
&lt;/span&gt;&lt;span style="color: #000000;"&gt;            &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;if&lt;/span&gt;&lt;span style="color: #000000;"&gt; (actionContext.Position.ForwardFacingPositions.Length &lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;0&lt;/span&gt;&lt;span style="color: #000000;"&gt;)
            {
                &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;if&lt;/span&gt;&lt;span style="color: #000000;"&gt; (CheckAccessibilityOfMapBlock(actionContext.Position.ForwardFacingPositions[&lt;/span&gt;&lt;span style="color: #800080;"&gt;0&lt;/span&gt;&lt;span style="color: #000000;"&gt;]))
                {
                    action.DirectionToMove &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; MovementDirection.Forward;
                    &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;return&lt;/span&gt;&lt;span style="color: #000000;"&gt; action;
                }
            }
            &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;if&lt;/span&gt;&lt;span style="color: #000000;"&gt; (actionContext.Position.LeftFacingPositions.Length &lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;0&lt;/span&gt;&lt;span style="color: #000000;"&gt;)
            {
                &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;if&lt;/span&gt;&lt;span style="color: #000000;"&gt; (CheckAccessibilityOfMapBlock(actionContext.Position.LeftFacingPositions[&lt;/span&gt;&lt;span style="color: #800080;"&gt;0&lt;/span&gt;&lt;span style="color: #000000;"&gt;]))
                {
                    action.DirectionToMove &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; MovementDirection.Left;
                    &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;return&lt;/span&gt;&lt;span style="color: #000000;"&gt; action;
                }
            }
            &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;if&lt;/span&gt;&lt;span style="color: #000000;"&gt; (actionContext.Position.RearFacingPositions.Length &lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;0&lt;/span&gt;&lt;span style="color: #000000;"&gt;)
            {
                &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;if&lt;/span&gt;&lt;span style="color: #000000;"&gt; (CheckAccessibilityOfMapBlock(actionContext.Position.RearFacingPositions[&lt;/span&gt;&lt;span style="color: #800080;"&gt;0&lt;/span&gt;&lt;span style="color: #000000;"&gt;]))
                {
                    action.DirectionToMove &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; MovementDirection.Back;
                    &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;return&lt;/span&gt;&lt;span style="color: #000000;"&gt; action;
                }
            }
            &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;if&lt;/span&gt;&lt;span style="color: #000000;"&gt; (actionContext.Position.RightFacingPositions.Length &lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;0&lt;/span&gt;&lt;span style="color: #000000;"&gt;)
            {
                &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;if&lt;/span&gt;&lt;span style="color: #000000;"&gt; (CheckAccessibilityOfMapBlock(actionContext.Position.RightFacingPositions[&lt;/span&gt;&lt;span style="color: #800080;"&gt;0&lt;/span&gt;&lt;span style="color: #000000;"&gt;]))
                {
                    action.DirectionToMove &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; MovementDirection.Right;
                    &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;return&lt;/span&gt;&lt;span style="color: #000000;"&gt; action;
                }
            }

            &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;return&lt;/span&gt;&lt;span style="color: #000000;"&gt; action;
        }
        &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;catch&lt;/span&gt;&lt;span style="color: #000000;"&gt; (Exception ex)
        {
            World.WriteDebugInformation(&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #800000;"&gt;Player: &lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #000000;"&gt;+&lt;/span&gt;&lt;span style="color: #000000;"&gt; Name, &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;string&lt;/span&gt;&lt;span style="color: #000000;"&gt;.Format(&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #800000;"&gt;Player Generated exception: {0}&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #000000;"&gt;,ex.Message));
            &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;throw&lt;/span&gt;&lt;span style="color: #000000;"&gt; ex;
        }
    }

    &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;private&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;bool&lt;/span&gt;&lt;span style="color: #000000;"&gt; CheckAccessibilityOfMapBlock(MapBlock block)
    {
        &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;if&lt;/span&gt;&lt;span style="color: #000000;"&gt; (block &lt;/span&gt;&lt;span style="color: #000000;"&gt;==&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;null&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #000000;"&gt;||&lt;/span&gt;&lt;span style="color: #000000;"&gt; block.Accessibility &lt;/span&gt;&lt;span style="color: #000000;"&gt;==&lt;/span&gt;&lt;span style="color: #000000;"&gt; MapBlockAccessibility.AllowEntry &lt;/span&gt;&lt;span style="color: #000000;"&gt;||&lt;/span&gt;&lt;span style="color: #000000;"&gt; block.Accessibility &lt;/span&gt;&lt;span style="color: #000000;"&gt;==&lt;/span&gt;&lt;span style="color: #000000;"&gt; MapBlockAccessibility.AllowExit &lt;/span&gt;&lt;span style="color: #000000;"&gt;||&lt;/span&gt;&lt;span style="color: #000000;"&gt; block.Accessibility &lt;/span&gt;&lt;span style="color: #000000;"&gt;==&lt;/span&gt;&lt;span style="color: #000000;"&gt; MapBlockAccessibility.AllowPotentialEntry)
        {
            &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;return&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;true&lt;/span&gt;&lt;span style="color: #000000;"&gt;;
        }
        &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;return&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;false&lt;/span&gt;&lt;span style="color: #000000;"&gt;;
    }

}
&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;It is simple and it seems to work well. The world implementation itself decides the stimulus context that is passed to he inhabitant to make an action decision. All movement is carried out on separate threads and timed appropriately to be as fair as possible and to cater for additional skills such as speed, and eventually maybe stamina, strength, with actions like fighting. It is pretty fun to make up random maps and see how your inhabitant does.&lt;/p&gt;

&lt;p&gt;You can download the code from &lt;a href="https://bitbucket.org/glav/ecodev/overview" target="_blank"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Along the way I have played with parallel extensions to make the compute intensive stuff spread across all cores, had to heavily factor in visibility of methods and properties so design of classes was paramount, work out movement algorithms that play fairly in the world and properly favour the players with higher abilities, as well as a host of other issues.&lt;/p&gt;

&lt;p&gt;So that is my ‘&lt;strong&gt;&lt;em&gt;solution kata&lt;/em&gt;&lt;/strong&gt;’. &lt;/p&gt;

&lt;p&gt;If I keep going with it, I may develop a web interface for it where people can upload assemblies and watch their player within a web browser visualiser and maybe even a map designer. &lt;/p&gt;

&lt;p&gt;What do you do to keep the fires burning?&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=8051632" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/pglavich/archive/tags/.NET/default.aspx">.NET</category><category domain="http://weblogs.asp.net/pglavich/archive/tags/Personal/default.aspx">Personal</category><category domain="http://weblogs.asp.net/pglavich/archive/tags/General/default.aspx">General</category><category domain="http://weblogs.asp.net/pglavich/archive/tags/Community+News/default.aspx">Community News</category><category domain="http://weblogs.asp.net/pglavich/archive/tags/Architecture/default.aspx">Architecture</category></item><item><title>Updates to the CacheAdapter Package</title><link>http://weblogs.asp.net/pglavich/archive/2011/08/21/updates-to-the-cacheadapter-package.aspx</link><pubDate>Sun, 21 Aug 2011 03:12:52 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7919105</guid><dc:creator>Glav</dc:creator><slash:comments>2</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/pglavich/rsscomments.aspx?PostID=7919105</wfw:commentRss><comments>http://weblogs.asp.net/pglavich/archive/2011/08/21/updates-to-the-cacheadapter-package.aspx#comments</comments><description>&lt;p&gt;&lt;em&gt;Note: This post was originally going to detail the changes from 2.0 to 2.1, however in between the time of release and this post, I released 2.2 so this post will detail all the changes up to 2.2.&lt;/em&gt;&lt;/p&gt;  &lt;p&gt;In the last update to my &lt;a href="http://nuget.org/List/Packages/Glav.CacheAdapter" target="_blank"&gt;CacheAdapter&lt;/a&gt; library (you can view it &lt;a href="http://weblogs.asp.net/pglavich/archive/2011/07/04/cacheadapter-v2-now-with-memcached-support.aspx" target="_blank"&gt;here&lt;/a&gt;), I mentioned the support for &lt;a href="http://memcached.org/" target="_blank"&gt;memcached&lt;/a&gt; cache engine. That brought the version to 2.0.&lt;/p&gt;  &lt;p&gt;As previously mentioned, you can get this package from &lt;a href="http://nuget.org/List/Packages/Glav.CacheAdapter" target="_blank"&gt;here&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;I have recently updated the version of the library to &lt;strike&gt;2.1&lt;/strike&gt; 2.2 which now includes the following changes:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;The ability provide specific configuration data to a particular cache engine without introducing a bunch of specific configuration tags that are only relevant to a particular cache mechanism.      &lt;ul&gt;       &lt;li&gt;Addition of a &lt;font face="Courier New"&gt;CacheSpecificData&lt;/font&gt; configuration element. &lt;/li&gt;     &lt;/ul&gt;   &lt;/li&gt;    &lt;li&gt;The addition of 2 new simpler API methods to get data from the cache.      &lt;ul&gt;       &lt;li&gt;&lt;font face="Courier New"&gt;T Get&amp;lt;T&amp;gt;(DateTime absoluteExpiryDate, Func&amp;lt;T&amp;gt; getData)&lt;/font&gt; &lt;/li&gt;        &lt;li&gt;&lt;font face="Courier New"&gt;T Get&amp;lt;T&amp;gt;(TimeSpan slidingExpiryWindow, Func&amp;lt;T&amp;gt; getData)&lt;/font&gt; &lt;/li&gt;     &lt;/ul&gt;   &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&lt;font face="Courier New"&gt;&lt;font face="Arial"&gt;Notice that there is &lt;strong&gt;no&lt;/strong&gt; cache key required to be specified. It is auto generated from the&lt;/font&gt; Func&amp;lt;T&amp;gt; &lt;/font&gt;&lt;font face="Arial"&gt;delegate. (Thanks to &lt;a href="http://www.linkedin.com/in/corneliutusnea" target="_blank"&gt;Corneliu&lt;/a&gt; for this great idea). So you can write really simple code to get data from the cache like so:&lt;/font&gt;&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:ce63feaf-61f8-4331-9151-f3b6b5d75b4c" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;overflow: auto;"&gt;&lt;span style="color: #000000;"&gt;var myData &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; cacheProvider.Get&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;SomeData&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;(DateTime.Now.AddSeconds(&lt;/span&gt;&lt;span style="color: #800080;"&gt;2&lt;/span&gt;&lt;span style="color: #000000;"&gt;), () &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;
                                                                          {
                                                                             var somedata &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;new&lt;/span&gt;&lt;span style="color: #000000;"&gt; SomeData();
                                                                             &lt;/span&gt;&lt;span style="color: #008000;"&gt;//&lt;/span&gt;&lt;span style="color: #008000;"&gt; populate data from somewhere (db, code etc..)&lt;/span&gt;&lt;span style="color: #008000;"&gt;
&lt;/span&gt;&lt;span style="color: #000000;"&gt;                                                                             &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;return&lt;/span&gt;&lt;span style="color: #000000;"&gt; somedata;
                                                                          });
&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;ul&gt;
  &lt;li&gt;Simplification of the interface 
    &lt;ul&gt;
      &lt;li&gt;Replacing the &lt;font face="Courier New"&gt;GetDataToCacheDelegate&amp;lt;T&amp;gt;()&lt;/font&gt; delegate with a &lt;font face="Courier New"&gt;Func&amp;lt;T&amp;gt;&lt;/font&gt; &lt;/li&gt;

      &lt;li&gt;Removal of the &lt;font face="Courier New"&gt;bool addToPerRequestCache = false&lt;/font&gt; parameter from the &lt;font face="Courier New"&gt;Get&amp;lt;T&amp;gt;&lt;/font&gt; methods. &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So why the new configuration element? Well, previous versions of this library supported Windows Azure AppFabric however it was only in a non Azure scenario. When Windows AppFabric is used within Azure, a security mode and authentication key is required. This is easily achieved with config, but I wanted a way that did not introduce redundant elements that were not applicable to other cache mechanisms AND that may be useful for other cache mechanisms if they require it. Basically, a generic way of providing configuration data to cache mechanisms, that could be used by any cache mechanism and not pollute the configuration with lots of items specific to only 1 method.&lt;/p&gt;

&lt;p&gt;To that end, this release has introduced a “&lt;font face="Courier New"&gt;CacheSpecificData&lt;/font&gt;” configuration element as shown below:&lt;/p&gt;

&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:dc2db394-09c3-4ca8-9311-6b94d2ebc7d7" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:White;overflow: auto;"&gt;&lt;span style="color: #0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000;"&gt;setting &lt;/span&gt;&lt;span style="color: #FF0000;"&gt;name&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;="CacheSpecificData"&lt;/span&gt;&lt;span style="color: #FF0000;"&gt; serializeAs&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;="String"&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;
  &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000;"&gt;value&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;UseSsl=false;SecurityMode=Message;MessageSecurityAuthorizationInfo=your_secure_key_from_azure_dashboard&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000;"&gt;value&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;
&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000;"&gt;setting&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;
&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;Currently, this element is only of use to AppFabric cache mechanism, but as support for more cache types grow, this item will be used more and more.&lt;/p&gt;

&lt;p&gt;This element is a simple series of key/value pairs that are separated by a semi-colon. In the example above, the following values are defined:&lt;/p&gt;

&lt;p&gt;&lt;font face="Courier New"&gt;UseSsl = &lt;strong&gt;false&lt;/strong&gt;&lt;/font&gt;&lt;/p&gt;

&lt;p&gt;&lt;font face="Courier New"&gt;SecurityMode = &lt;strong&gt;Message&lt;/strong&gt;&lt;/font&gt;&lt;/p&gt;

&lt;p&gt;&lt;font face="Courier New"&gt;MessageSecurityAuthorizationInfo = &lt;strong&gt;your_secure_key_from_azure_dashboard&lt;/strong&gt;&lt;/font&gt;&lt;/p&gt;

&lt;p&gt;This equates to the following configuration for Windows AppFabric in Azure (Specifically the &lt;font face="Courier New"&gt;&amp;lt;SecurityProperties&amp;gt;&lt;/font&gt; element):&lt;/p&gt;

&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:4500570c-5dca-4e43-bb97-c731c92ef5d7" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:White;overflow: auto;"&gt;&lt;span style="color: #0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000;"&gt;dataCacheClients&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;
  &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000;"&gt;dataCacheClient &lt;/span&gt;&lt;span style="color: #FF0000;"&gt;name&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;="default"&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;
    &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000;"&gt;hosts&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;
      &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000;"&gt;host &lt;/span&gt;&lt;span style="color: #FF0000;"&gt;name&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;="somehost.com"&lt;/span&gt;&lt;span style="color: #FF0000;"&gt; cachePort&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;="22233"&lt;/span&gt;&lt;span style="color: #FF0000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;
    &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000;"&gt;hosts&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;
    &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000;"&gt;securityProperties &lt;/span&gt;&lt;span style="color: #FF0000;"&gt;mode&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;="Message"&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;
      &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000;"&gt;messageSecurity &lt;/span&gt;&lt;span style="color: #FF0000;"&gt;authorizationInfo&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;="your_authorisation_key"&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000;"&gt;messageSecurity&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;
    &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000;"&gt;securityProperties&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;
  &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000;"&gt;dataCacheClient&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;
&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000;"&gt;dataCacheClients&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;
&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;There are also a few bug fixes and code cleanup in there. Thanks to &lt;a href="http://www.linkedin.com/in/corneliutusnea" target="_blank"&gt;Corneliu Tusnea&lt;/a&gt; (creator of &lt;a href="http://onesaas.com/" target="_blank"&gt;OneSaas&lt;/a&gt;) for submitting some really useful changes and bug fixes.&lt;/p&gt;

&lt;p&gt;Hope you enjoy the new features. &lt;/p&gt;

&lt;p&gt;Go get the Nuget package from &lt;a href="http://nuget.org/List/Packages/Glav.CacheAdapter" target="_blank"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Or download the source code from &lt;a href="https://bitbucket.org/glav/cacheadapter" target="_blank"&gt;here&lt;/a&gt;.&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7919105" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/pglavich/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/pglavich/archive/tags/.NET/default.aspx">.NET</category><category domain="http://weblogs.asp.net/pglavich/archive/tags/Component/default.aspx">Component</category><category domain="http://weblogs.asp.net/pglavich/archive/tags/Architecture/default.aspx">Architecture</category><category domain="http://weblogs.asp.net/pglavich/archive/tags/Performance/default.aspx">Performance</category><category domain="http://weblogs.asp.net/pglavich/archive/tags/CacheAdapter/default.aspx">CacheAdapter</category></item><item><title>CacheAdapter–V2 Now with memcached support</title><link>http://weblogs.asp.net/pglavich/archive/2011/07/04/cacheadapter-v2-now-with-memcached-support.aspx</link><pubDate>Mon, 04 Jul 2011 04:00:34 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7854650</guid><dc:creator>Glav</dc:creator><slash:comments>20</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/pglavich/rsscomments.aspx?PostID=7854650</wfw:commentRss><comments>http://weblogs.asp.net/pglavich/archive/2011/07/04/cacheadapter-v2-now-with-memcached-support.aspx#comments</comments><description>&lt;p&gt;Previously I blogged about my &lt;a href="http://weblogs.asp.net/pglavich/archive/2011/05/31/cacheadapter-now-a-nuget-package.aspx" target="_blank"&gt;CacheAdapter&lt;/a&gt; project that is available as a &lt;a href="http://nuget.org/List/Packages/Glav.CacheAdapter" target="_blank"&gt;Nuget&lt;/a&gt; package and allows you to program against a single interface implementation, but have support for using Memory, ASP.NET Web or &lt;a href="http://msdn.microsoft.com/en-us/windowsserver/ee695849" target="_blank"&gt;Windows AppFabric&lt;/a&gt; cache mechanisms via configuration.&lt;/p&gt;  &lt;p&gt;I am happy to announce that my &lt;a href="http://weblogs.asp.net/pglavich/archive/2011/05/31/cacheadapter-now-a-nuget-package.aspx" target="_blank"&gt;CacheAdapter&lt;/a&gt; now has support for &lt;a href="http://memcached.org/" target="_blank"&gt;memcached&lt;/a&gt;. Version 2 of the &lt;a href="http://nuget.org/List/Packages/Glav.CacheAdapter" target="_blank"&gt;Nuget&lt;/a&gt; package is available &lt;a href="http://nuget.org/List/Packages/Glav.CacheAdapter" target="_blank"&gt;here&lt;/a&gt;. Alternatively, all the source code is available &lt;a href="https://bitbucket.org/glav/cacheadapter" target="_blank"&gt;here&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;What this now means is you can write one line of code to get or store an item in the cache, and that code can automatically support using Windows AppFabric, &lt;a href="http://memcached.org/" target="_blank"&gt;memcached&lt;/a&gt;, MemoryCache or ASP.NET web cache. No code change required.&lt;/p&gt;  &lt;p&gt;I am particularly happy about having &lt;a href="http://memcached.org/" target="_blank"&gt;memcached&lt;/a&gt; support as it means a few things:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;strong&gt;Free / Open Source&lt;/strong&gt;: It is a free, well established open source caching engine that is widely used in many applications. &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Easy&lt;/strong&gt;: It is easy to setup. &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Simple and Cheap&lt;/strong&gt;: It provides an alternative to using &lt;a href="http://msdn.microsoft.com/en-us/windowsserver/ee695849" target="_blank"&gt;Windows AppFabric&lt;/a&gt;. &lt;a href="http://msdn.microsoft.com/en-us/windowsserver/ee695849" target="_blank"&gt;AppFabric&lt;/a&gt; can be a little tricky to setup sometimes. If you are using Windows Azure, &lt;a href="http://msdn.microsoft.com/en-us/windowsserver/ee695849" target="_blank"&gt;AppFabric&lt;/a&gt; is a simple checkbox BUT you need to pay extra for the privilege of using it based on how much you use the cache service. By contrast, &lt;a href="http://memcached.org/" target="_blank"&gt;memcached&lt;/a&gt; can be installed easily on Azure and requires no extra cost whatsoever. &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Auto fail over support&lt;/strong&gt;: In addition, Windows &lt;a href="http://msdn.microsoft.com/en-us/windowsserver/ee695849" target="_blank"&gt;AppFabric&lt;/a&gt; has some limitations for a relatively small cache farm. &lt;a href="http://msdn.microsoft.com/en-us/windowsserver/ee695849" target="_blank"&gt;AppFabric&lt;/a&gt; utilises a “lead host” to co-ordinate small cache farms of 3 or less cache servers. If the lead host goes down, they all go down. The &lt;a href="http://memcached.org/" target="_blank"&gt;memcached&lt;/a&gt; implementation has no reliance on any single point of failure so if one &lt;a href="http://memcached.org/" target="_blank"&gt;memcached&lt;/a&gt; node fails, requests will automatically be redirected to the nodes that are alive. If the dead node comes back alive again, it is re-introduced to the cache pool after about 1 minute or so. At worst, it results in a few cache misses. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;Note: This release contains a few namespace changes that may break older versions if you are using the objects directly. Namely &lt;a href="http://msdn.microsoft.com/en-us/windowsserver/ee695849" target="_blank"&gt;AppFabric&lt;/a&gt; object support has been moved from the &lt;font face="Courier New"&gt;Glav.CacheAdapter.Distributed&lt;/font&gt; namespace to the &lt;font face="Courier New"&gt;Glav.CacheAdapter.Distributed.AppFabric&lt;/font&gt; namespace. This is to allow differentiation from &lt;a href="http://msdn.microsoft.com/en-us/windowsserver/ee695849" target="_blank"&gt;AppFabric&lt;/a&gt; and &lt;a href="http://memcached.org/" target="_blank"&gt;memcached&lt;/a&gt; within the distributed namespace.&lt;/p&gt;  &lt;p&gt;In addition, at the request of some users, I have added a simple ‘Add’ method to the &lt;font face="Courier New"&gt;ICacheProvider&lt;/font&gt; interface for ease of use. The interface now looks like this:&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:2e147624-7d72-4532-adf1-843f23b76723" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;white-space:-moz-pre-wrap; white-space: -pre-wrap; white-space: -o-pre-wrap; white-space: pre-wrap; word-wrap: break-word;overflow: auto;"&gt;&lt;span style="color: #0000FF;"&gt;public&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;interface&lt;/span&gt;&lt;span style="color: #000000;"&gt; ICacheProvider
{
    T Get&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;T&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;(&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;string&lt;/span&gt;&lt;span style="color: #000000;"&gt; cacheKey, DateTime absoluteExpiryDate, GetDataToCacheDelegate&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;T&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt; getData, &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;bool&lt;/span&gt;&lt;span style="color: #000000;"&gt; addToPerRequestCache &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;false&lt;/span&gt;&lt;span style="color: #000000;"&gt;) &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;where&lt;/span&gt;&lt;span style="color: #000000;"&gt; T : &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;class&lt;/span&gt;&lt;span style="color: #000000;"&gt;;
    T Get&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;T&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;(&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;string&lt;/span&gt;&lt;span style="color: #000000;"&gt; cacheKey, TimeSpan slidingExpiryWindow, GetDataToCacheDelegate&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;T&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt; getData, &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;bool&lt;/span&gt;&lt;span style="color: #000000;"&gt; addToPerRequestCache &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;false&lt;/span&gt;&lt;span style="color: #000000;"&gt;) &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;where&lt;/span&gt;&lt;span style="color: #000000;"&gt; T : &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;class&lt;/span&gt;&lt;span style="color: #000000;"&gt;;
    &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;void&lt;/span&gt;&lt;span style="color: #000000;"&gt; InvalidateCacheItem(&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;string&lt;/span&gt;&lt;span style="color: #000000;"&gt; cacheKey);
    &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;void&lt;/span&gt;&lt;span style="color: #000000;"&gt; Add(&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;string&lt;/span&gt;&lt;span style="color: #000000;"&gt; cacheKey, DateTime absoluteExpiryDate, &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;object&lt;/span&gt;&lt;span style="color: #000000;"&gt; dataToAdd);
    &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;void&lt;/span&gt;&lt;span style="color: #000000;"&gt; Add(&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;string&lt;/span&gt;&lt;span style="color: #000000;"&gt; cacheKey, TimeSpan slidingExpiryWindow, &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;object&lt;/span&gt;&lt;span style="color: #000000;"&gt; dataToAdd);
    &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;void&lt;/span&gt;&lt;span style="color: #000000;"&gt; AddToPerRequestCache(&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;string&lt;/span&gt;&lt;span style="color: #000000;"&gt; cacheKey, &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;object&lt;/span&gt;&lt;span style="color: #000000;"&gt; dataToAdd);
}
&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;So that is it. I hope you enjoy using &lt;a href="http://memcached.org/" target="_blank"&gt;memcached&lt;/a&gt; support within the &lt;a href="http://weblogs.asp.net/pglavich/archive/2011/05/31/cacheadapter-now-a-nuget-package.aspx" target="_blank"&gt;CacheAdapter&lt;/a&gt;.&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7854650" width="1" height="1"&gt;</description></item><item><title>Converting from Webforms view engine to Razor–Some Tips</title><link>http://weblogs.asp.net/pglavich/archive/2011/06/06/converting-from-webforms-view-engine-to-razor-some-tips.aspx</link><pubDate>Sun, 05 Jun 2011 22:16:55 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7819367</guid><dc:creator>Glav</dc:creator><slash:comments>18</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/pglavich/rsscomments.aspx?PostID=7819367</wfw:commentRss><comments>http://weblogs.asp.net/pglavich/archive/2011/06/06/converting-from-webforms-view-engine-to-razor-some-tips.aspx#comments</comments><description>&lt;p&gt;Recently, I have had to perform a lot of conversion in an MVC application from using the webforms view engine to the Razor view engine.&lt;/p&gt;  &lt;p&gt;I say “had to” but we didn’t really have to, more wanted to. We wanted the cleaner syntax that razor allows. Since we were going to embark on a bunch of new features involving new pages, we wanted to do this conversion earlier rather than later to prevent excessive rework.&lt;/p&gt;  &lt;p&gt;This post simply summarises most of the issues and tips that were experienced along the way and how we overcame them, so that others can get a clear picture of what is involved should they embark on such an undertaking, and be prepared for any issues.&lt;/p&gt;  &lt;h2&gt;Preparation&lt;/h2&gt;  &lt;p&gt;First off, there are some things you should do prior to starting the conversion to make the process as smooth as possible, and to allow easy resolution of any issues.&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Take a copy of your entire source tree and keep it separate to allow for comparison along the way. If you are using HG or GIT, clone your repository to another location. Sure you could look at source control history if you wanted, but its easier to have this code at hand to do side by side comparisons.More importantly though, you can run the backup version to compare HTML output to make sure your conversions are good.      &lt;br /&gt;&lt;/li&gt;    &lt;li&gt;In your solution, turn on MVC compiled views. This will tell the compiler to compile your views and you will catch any errors whenever you compile. Very handy and saves sometime. Unfortunately, it does add some time to the build process. If you have a lot of views, this could be lengthy, so you may want to disable this after the conversion is complete. To enable compiled views:      &lt;ul&gt;       &lt;ul&gt;         &lt;li&gt;Right click on your project in solution explorer, and select Unload Project. &lt;/li&gt;          &lt;li&gt;One its unloaded, right click on it again and select ‘Edit Project. The project XML will load in the editor window. &lt;/li&gt;          &lt;li&gt;Locate the following &lt;/li&gt;       &lt;/ul&gt;     &lt;/ul&gt;   &lt;/li&gt; &lt;/ul&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:a70305a2-4632-46a9-9c54-d68c8157d44e" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;white-space:-moz-pre-wrap; white-space: -pre-wrap; white-space: -o-pre-wrap; white-space: pre-wrap; word-wrap: break-word;overflow: auto;"&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;MvcBuildViews&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;false&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #000000;"&gt;MvcBuildViews&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;blockquote&gt;
  &lt;p&gt;and change it to &lt;/p&gt;
&lt;/blockquote&gt;

&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:54d2cb07-33f3-451c-9288-c47f06bde186" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;white-space:-moz-pre-wrap; white-space: -pre-wrap; white-space: -o-pre-wrap; white-space: pre-wrap; word-wrap: break-word;overflow: auto;"&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;MvcBuildViews&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;true&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #000000;"&gt;MvcBuildViews&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;ul&gt;
  &lt;ul&gt;
    &lt;ul&gt;
      &lt;li&gt;Save the file. Then right click on the project again and select ‘Reload Project’ &lt;/li&gt;

      &lt;li&gt;Now when you compile, your views will also be compiled. &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/ul&gt;

  &lt;li&gt;Download the &lt;a href="http://blogs.telerik.com/blogs/posts/11-01-19/webforms_to_razor_view_converter_tool.aspx" target="_blank"&gt;aspx2razor&lt;/a&gt; conversion tool from Telerik which does a reasonable job of converting your ASPX views into razor views. It probably wont do it all for you,but it removes a good deal of work. &lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;Performing the actual conversion&lt;/h2&gt;

&lt;p&gt;This is the easy part. Simply run the conversion tool over your project. You can do this by running the tool against your base views directory and have it recursively convert all files it founds in all directories to their razor counterparts. Something like this:&lt;/p&gt;

&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:f361e519-dd8c-4ec8-b809-970cb139ab41" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;white-space:-moz-pre-wrap; white-space: -pre-wrap; white-space: -o-pre-wrap; white-space: pre-wrap; word-wrap: break-word;overflow: auto;"&gt;&lt;span style="color: #000000;"&gt;aspx2razor.exe c:\MyProject\MyViews\&lt;/span&gt;&lt;span style="color: #000000;"&gt;*&lt;/span&gt;&lt;span style="color: #000000;"&gt;.cshtml MyOutputDirectory &lt;/span&gt;&lt;span style="color: #000000;"&gt;-&lt;/span&gt;&lt;span style="color: #000000;"&gt;r&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;You will then need to copy the files from the output directory into your solution and ensure they are included in the project. Make sure you delete all the existing ASPX files as having both of these file types present can cause problems.&lt;/p&gt;

&lt;p&gt;So that’s it. Job done right?&lt;/p&gt;

&lt;h2&gt;Testing and Fixing&lt;/h2&gt;

&lt;p&gt;Now the fun begins. The conversion tool works reasonably well, but it has its problems. Here are some of the issues you will find.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;The conversion tool does not support master pages, so don’t expect them to be converted. Roll up your sleeves and do those manually. Even though the tool does not support master pages, it will blindly convert anything you tell it to so it will attempt to convert a master page if one exists in the set of input files. Don’t be tempted to use a partially converted master page if there is one as it will usually not convert properly, putting in an incorrect &lt;a href="mailto:&amp;lsquo;@inherits&amp;rsquo;"&gt;‘@inherits’&lt;/a&gt; directive which just causes more confusion. Start from scratch and do the master pages manually. &lt;/li&gt;

  &lt;li&gt;Sometimes the conversion tool completely misses the server side tags, particularly when embedded in script tags like this: &lt;/li&gt;
&lt;/ul&gt;

&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:f2336762-e26a-4f55-b6bb-3d5b404f2474" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:White;white-space:-moz-pre-wrap; white-space: -pre-wrap; white-space: -o-pre-wrap; white-space: pre-wrap; word-wrap: break-word;overflow: auto;"&gt;&lt;span style="color: #0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000;"&gt;script &lt;/span&gt;&lt;span style="color: #FF0000;"&gt;type&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;="text/javascript"&lt;/span&gt;&lt;span style="color: #FF0000;"&gt; language&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;="javascript"&lt;/span&gt;&lt;span style="color: #FF0000;"&gt; src&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;="&amp;lt;%: Url.Content("&lt;/span&gt;&lt;span style="color: #FF0000;"&gt;~/Content/Scripts/jQuery/jshashtable.js") %&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="background-color: #F5F5F5; color: #000000;"&gt;"&lt;/span&gt;&lt;span style="background-color: #F5F5F5; color: #000000;"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;ul&gt;
  &lt;li&gt;Embedded server tags can get completely messed up when converting to razor. As an example, the following snippet: &lt;/li&gt;
&lt;/ul&gt;

&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:9568ccc5-e4fe-40a2-9535-1d030682ede3" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;white-space:-moz-pre-wrap; white-space: -pre-wrap; white-space: -o-pre-wrap; white-space: pre-wrap; word-wrap: break-word;overflow: auto;"&gt;&lt;span style="color: #000000;"&gt;class="item &lt;/span&gt;&lt;span style="background-color: #FFFF00; color: #000000;"&gt;&amp;lt;%&lt;/span&gt;&lt;span style="background-color: #F5F5F5; color: #000000;"&gt;: isParentDate ? &lt;/span&gt;&lt;span style="background-color: #F5F5F5; color: #800000;"&gt;""&lt;/span&gt;&lt;span style="background-color: #F5F5F5; color: #000000;"&gt; : &lt;/span&gt;&lt;span style="background-color: #F5F5F5; color: #800000;"&gt;"&lt;/span&gt;&lt;span style="background-color: #F5F5F5; color: #800000;"&gt;child-date &lt;/span&gt;&lt;span style="background-color: #F5F5F5; color: #800000;"&gt;"&lt;/span&gt;&lt;span style="background-color: #F5F5F5; color: #000000;"&gt; &lt;/span&gt;&lt;span style="background-color: #FFFF00; color: #000000;"&gt;%&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;
&lt;/span&gt;&lt;span style="background-color: #FFFF00; color: #000000;"&gt;&amp;lt;%&lt;/span&gt;&lt;span style="background-color: #F5F5F5; color: #000000;"&gt;: item.IsReconciled ? &lt;/span&gt;&lt;span style="background-color: #F5F5F5; color: #800000;"&gt;"&lt;/span&gt;&lt;span style="background-color: #F5F5F5; color: #800000;"&gt;reconciled &lt;/span&gt;&lt;span style="background-color: #F5F5F5; color: #800000;"&gt;"&lt;/span&gt;&lt;span style="background-color: #F5F5F5; color: #000000;"&gt; : &lt;/span&gt;&lt;span style="background-color: #F5F5F5; color: #800000;"&gt;""&lt;/span&gt;&lt;span style="background-color: #F5F5F5; color: #000000;"&gt; &lt;/span&gt;&lt;span style="background-color: #FFFF00; color: #000000;"&gt;%&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;"&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;blockquote&gt;
  &lt;p&gt;gets converted to the following razor equivalent:&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:edcd4900-db79-4595-acb1-b36659bdaa0e" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;white-space:-moz-pre-wrap; white-space: -pre-wrap; white-space: -o-pre-wrap; white-space: pre-wrap; word-wrap: break-word;overflow: auto;"&gt;&lt;span style="color: #0000FF;"&gt;class&lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #800000;"&gt;item @isParentDate ? &lt;/span&gt;&lt;span style="color: #800000;"&gt;""&lt;/span&gt;&lt;span style="color: #800000;"&gt; : &lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #000000;"&gt;child&lt;/span&gt;&lt;span style="color: #000000;"&gt;-&lt;/span&gt;&lt;span style="color: #000000;"&gt;date &lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #800000;"&gt;@item.IsReconciled ? &lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #000000;"&gt;reconciled &lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #800000;"&gt; : &lt;/span&gt;&lt;span style="color: #800000;"&gt;"""&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;
&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;blockquote&gt;
  &lt;p&gt;which is obviously incorrect and results in simple text being output to the HTML, not the expected class names. Ideally, this should be refactored into amore explicit code block, something like this:&lt;/p&gt;

  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:8af3837b-20e8-4d0b-addf-8cf3d2ebb819" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;white-space:-moz-pre-wrap; white-space: -pre-wrap; white-space: -o-pre-wrap; white-space: pre-wrap; word-wrap: break-word;overflow: auto;"&gt;&lt;span style="color: #000000;"&gt;@{ 
  var divClass &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;string&lt;/span&gt;&lt;span style="color: #000000;"&gt;.Format(&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #800000;"&gt;item {0}{1}&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #000000;"&gt;, isParentDate &lt;/span&gt;&lt;span style="color: #000000;"&gt;?&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;string&lt;/span&gt;&lt;span style="color: #000000;"&gt;.Empty : &lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #800000;"&gt;child-date &lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #000000;"&gt;, item.IsReconciled &lt;/span&gt;&lt;span style="color: #000000;"&gt;?&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #800000;"&gt;reconciled &lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #000000;"&gt; : &lt;/span&gt;&lt;span style="color: #800000;"&gt;""&lt;/span&gt;&lt;span style="color: #000000;"&gt;);
}
&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;class&lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #800000;"&gt;@divClass&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #000000;"&gt;
&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
  &lt;li&gt;“Tight” concatenation of server tags like this: &lt;/li&gt;
&lt;/ul&gt;

&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:888deb4d-124e-4951-9b57-4244fdd1e3f1" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;white-space:-moz-pre-wrap; white-space: -pre-wrap; white-space: -o-pre-wrap; white-space: pre-wrap; word-wrap: break-word;overflow: auto;"&gt;&lt;span style="color: #0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000;"&gt;input &lt;/span&gt;&lt;span style="color: #FF0000;"&gt;type&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;="hidden"&lt;/span&gt;&lt;span style="color: #FF0000;"&gt; id&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;="itemAmount&amp;lt;%: Model.BankFeed.Id %&amp;gt;"&lt;/span&gt;&lt;span style="color: #FF0000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;blockquote&gt;
  &lt;p&gt;get converted to this:&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:c9519885-47b8-4bbb-bbf8-0d038fadeee0" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;white-space:-moz-pre-wrap; white-space: -pre-wrap; white-space: -o-pre-wrap; white-space: pre-wrap; word-wrap: break-word;overflow: auto;"&gt;&lt;span style="color: #0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000;"&gt;input &lt;/span&gt;&lt;span style="color: #FF0000;"&gt;type&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;="hidden"&lt;/span&gt;&lt;span style="color: #FF0000;"&gt; id&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;="itemAmount@Model.BankFeed.Id"&lt;/span&gt;&lt;span style="color: #FF0000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;
&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;blockquote&gt;
  &lt;p&gt;which is not right and just renders the text. Again, something more explicit is required such as:&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:82b28cb7-f367-4dfc-a18d-ecf1a60b5b62" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;white-space:-moz-pre-wrap; white-space: -pre-wrap; white-space: -o-pre-wrap; white-space: pre-wrap; word-wrap: break-word;overflow: auto;"&gt;&lt;span style="color: #000000;"&gt;@{ 
  var controlId = string.Format("itemAmount{0}", Model.Id);
}
    &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000;"&gt;input &lt;/span&gt;&lt;span style="color: #FF0000;"&gt;type&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;="hidden"&lt;/span&gt;&lt;span style="color: #FF0000;"&gt; id&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;="@controlId"&lt;/span&gt;&lt;span style="color: #FF0000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;ul&gt;
  &lt;li&gt;One last tip. Use the “original” copy of your site and run it up in a browser, do the same with the converted site. For each view that is converted, it is worth doing a “source view” of original and converted page from the browser to ensure you are getting the same output. This is particularly important for scripts and CSS.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That is about it for most of the issues I came across. You will no doubt experience some variations from the examples above, but the set of issues presented here should at least allow you to be prepared for what the actual conversion effort is going to be. &lt;/p&gt;

&lt;p&gt;Hopefully, this article helps you to efficiently convert your projects into the wonderful world of razor &lt;img style="border-bottom-style: none; border-left-style: none; border-top-style: none; border-right-style: none" class="wlEmoticon wlEmoticon-smile" alt="Smile" src="http://weblogs.asp.net/blogs/pglavich/wlEmoticon-smile_0D74FC15.png" /&gt;&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7819367" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/pglavich/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/pglavich/archive/tags/.NET/default.aspx">.NET</category><category domain="http://weblogs.asp.net/pglavich/archive/tags/MVC/default.aspx">MVC</category><category domain="http://weblogs.asp.net/pglavich/archive/tags/Razor/default.aspx">Razor</category><category domain="http://weblogs.asp.net/pglavich/archive/tags/Webforms/default.aspx">Webforms</category></item><item><title>CacheAdapter–Now a Nuget package</title><link>http://weblogs.asp.net/pglavich/archive/2011/05/31/cacheadapter-now-a-nuget-package.aspx</link><pubDate>Tue, 31 May 2011 06:12:05 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7809931</guid><dc:creator>Glav</dc:creator><slash:comments>13</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/pglavich/rsscomments.aspx?PostID=7809931</wfw:commentRss><comments>http://weblogs.asp.net/pglavich/archive/2011/05/31/cacheadapter-now-a-nuget-package.aspx#comments</comments><description>&lt;p&gt;&lt;em&gt;Note: Link to Wiki is &lt;/em&gt;&lt;a href="https://bitbucket.org/glav/cacheadapter/wiki/Home" target="_blank"&gt;&lt;em&gt;here&lt;/em&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;A while ago, I &lt;a href="http://weblogs.asp.net/pglavich/archive/2010/10/13/caching-architecture-testability-dependency-injection-and-multiple-providers.aspx" target="_blank"&gt;blogged about a personal project&lt;/a&gt; around caching that allowed you to abstract away the underlying cache mechanism, and use whatever cache you wanted. Through simple configuration, you can choose to use either the memory cache provided by System.Runtime.Caching in .Net 4, the traditional ASP.NET web cache, or you can use the Windows AppFabric caching to taker advantage of distributed caching.&lt;/p&gt;  &lt;p&gt;Well, its now &lt;a href="http://nuget.org/List/Packages/Glav.CacheAdapter" target="_blank"&gt;a package on Nuget&lt;/a&gt; that you can easily install and use immediately. You can grab it from &lt;a href="http://nuget.org/List/Packages/Glav.CacheAdapter" target="_blank"&gt;here&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://weblogs.asp.net/blogs/pglavich/install-package-image_51005C96.png"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="install-package-image" border="0" alt="install-package-image" src="http://weblogs.asp.net/blogs/pglavich/install-package-image_thumb_43B9D383.png" width="539" height="62" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&lt;em&gt;Update: This package uses the &lt;/em&gt;&lt;a href="http://www.opensource.org/licenses/MS-PL" target="_blank"&gt;&lt;em&gt;Microsoft Public Licence (MS-PL)&lt;/em&gt;&lt;/a&gt;&lt;em&gt;.&lt;/em&gt;&lt;/p&gt;  &lt;p&gt;Just as a recap, you can use this cache adapter like this:&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:8015f111-d5a6-42a2-9cab-180cc895bbec" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;white-space:-moz-pre-wrap; white-space: -pre-wrap; white-space: -o-pre-wrap; white-space: pre-wrap; word-wrap: break-word;overflow: auto;"&gt;&lt;span style="color: #000000;"&gt;var data1 &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; cacheProvider.Get&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;SomeData&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;(&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #800000;"&gt;cache-key&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #000000;"&gt;, DateTime.Now.AddSeconds(&lt;/span&gt;&lt;span style="color: #800080;"&gt;3&lt;/span&gt;&lt;span style="color: #000000;"&gt;), () &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;
{
    &lt;/span&gt;&lt;span style="color: #008000;"&gt;//&lt;/span&gt;&lt;span style="color: #008000;"&gt; This is the anonymous function which gets called if the data is not in the cache.
    &lt;/span&gt;&lt;span style="color: #008000;"&gt;//&lt;/span&gt;&lt;span style="color: #008000;"&gt; This method is executed and whatever is returned, is added to the cache with the
    &lt;/span&gt;&lt;span style="color: #008000;"&gt;//&lt;/span&gt;&lt;span style="color: #008000;"&gt; passed in expiry time.&lt;/span&gt;&lt;span style="color: #008000;"&gt;
&lt;/span&gt;&lt;span style="color: #000000;"&gt;    Console.WriteLine(&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #800000;"&gt;... =&amp;gt; Adding data to the cache... 1st call&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #000000;"&gt;);
    var someData &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;new&lt;/span&gt;&lt;span style="color: #000000;"&gt; SomeData() { SomeText &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #800000;"&gt;cache example1&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #000000;"&gt;, SomeNumber &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;1&lt;/span&gt;&lt;span style="color: #000000;"&gt; };
    &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;return&lt;/span&gt;&lt;span style="color: #000000;"&gt; someData;
});
&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;(Note: You will get a small example .cs file included in your project that contains this example usage when you install the package)&lt;/p&gt;

&lt;p&gt;You can see in the example, that you can request the data, and provide a lambda that retrieves that data if its not in the cache. The data gets added to the cache once the lambda has been executed to retrieve the data from the source. Also, the data that is returned is strongly typed. It is not a generic object that you then need to cast.&lt;/p&gt;

&lt;p&gt;Additionally, you can set the type of cache used by one line of configuration:&lt;/p&gt;

&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:b8a200ab-b537-40e5-a221-02b230ff0de9" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;white-space:-moz-pre-wrap; white-space: -pre-wrap; white-space: -o-pre-wrap; white-space: pre-wrap; word-wrap: break-word;overflow: auto;"&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;setting name&lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #800000;"&gt;CacheToUse&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #000000;"&gt; serializeAs&lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #800000;"&gt;String&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;
    &lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;value&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;Memory&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #000000;"&gt;value&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;
&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #000000;"&gt;setting&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;
&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;If I wanted to use the ASP.NET Web cache, I could simply set the config item like so:&lt;/p&gt;

&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:b3a42a7a-9f23-455a-a9e6-1ba2fb55357c" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;white-space:-moz-pre-wrap; white-space: -pre-wrap; white-space: -o-pre-wrap; white-space: pre-wrap; word-wrap: break-word;overflow: auto;"&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;setting name&lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #800000;"&gt;CacheToUse&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #000000;"&gt; serializeAs&lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #800000;"&gt;String&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;
    &lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;value&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;Web&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #000000;"&gt;value&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;
&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #000000;"&gt;setting&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;
&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;And if I wanted to use the AppFabric distributed cache (assuming it has been installed on the system), then I can simply set the config like so:&lt;/p&gt;

&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:e7d08a9b-a6e7-44f7-8135-323982474dbf" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;white-space:-moz-pre-wrap; white-space: -pre-wrap; white-space: -o-pre-wrap; white-space: pre-wrap; word-wrap: break-word;overflow: auto;"&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;setting name&lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #800000;"&gt;CacheToUse&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #000000;"&gt; serializeAs&lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #800000;"&gt;String&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;
    &lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;value&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;AppFabric&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #000000;"&gt;value&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;
&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #000000;"&gt;setting&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;
&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;The package references the required AppFabric client assemblies for you. This means you can write a web application today using this package and use the traditional ASP.NET cache. Then we you are ready, install AppFabric, setup a cache cluster, change the configuration to enable AppFabric and you are away. No code changes to your app.&lt;/p&gt;

&lt;h4&gt;Using AppFabric&lt;/h4&gt;

&lt;p&gt;To use AppFabric, there are a few extra things to do by way of Infrastructure. Obviously, you need to &lt;a href="http://www.microsoft.com/downloads/en/details.aspx?FamilyID=467e5aa5-c25b-4c80-a6d2-9f8fb0f337d2" target="_blank"&gt;download and install it&lt;/a&gt;. Then you need to setup your cache cluster via the wizard when you install it (or via powershell if you choose). During this process you will be creating some cache nodes and you need to create a named cache for your application. This can be done via powershell using the&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;font face="Courier New"&gt;New-Cache {cachename}&lt;/font&gt; &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;powershell command. In addition, you will need to ensure that the user that will be accessing the cache has access to it. Again, this can be done from powershell via the:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;font face="Courier New"&gt;Grant-CacheAllowedClientAccount {account}&lt;/font&gt; &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There is some additional config you will need to do as well. In the application config file you will need to change the following entries to match your configured infrastructure:&lt;/p&gt;

&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:7ef1c92c-45d6-4080-a66f-3bc54c21d57f" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;white-space:-moz-pre-wrap; white-space: -pre-wrap; white-space: -o-pre-wrap; white-space: pre-wrap; word-wrap: break-word;overflow: auto;"&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;setting name&lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #800000;"&gt;DistributedCacheServers&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #000000;"&gt; serializeAs&lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #800000;"&gt;String&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;
    &lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;value&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;localhost:&lt;/span&gt;&lt;span style="color: #800080;"&gt;22223&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #000000;"&gt;value&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;
&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #000000;"&gt;setting&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;
&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;setting name&lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #800000;"&gt;DistributedCacheName&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #000000;"&gt; serializeAs&lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #800000;"&gt;String&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;
    &lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;value&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;MyCache&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #000000;"&gt;value&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;
&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #000000;"&gt;setting&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;
&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;The “&lt;font face="Courier New"&gt;DistributedCacheServers&lt;/font&gt;” setting can be a comma separated list of servers and ports that the cache client (ie. your app) will communicate with. This can be one or as many comma separated entries as you like and depends on your cache cluster config.&lt;/p&gt;

&lt;p&gt;The “&lt;font face="Courier New"&gt;DistributedCacheName&lt;/font&gt;” is the name of the cache you have setup in the cache cluster for your app. (Detailed above)&lt;/p&gt;

&lt;h4&gt;Final Notes&lt;/h4&gt;

&lt;p&gt;Before I published this package, I cleaned it up a bit as I have been using it on a current project and I didn’t like a number of ways I implemented things initially. Instead of multiple assemblies, everything is in the one assembly. &lt;/p&gt;

&lt;p&gt;Also, I removed the use of Unity as a mechanism for dependency injection and implemented a very very simple method which uses no particular dependency injection implementation. I did this because everyone wants to do DI (Dependency Injection) using their favourite implementation whether this be Unity, Autofac, Ninject, Castle Windsor etc. etc. So I opted for something that does the job, it is not what a purist would be happy with, but is dead simple to rip out and change which is what I expect most people would do.&lt;/p&gt;

&lt;p&gt;Finally, because all cache access is via an interface, it allows for easy testability.&lt;/p&gt;

&lt;p&gt;So &lt;a href="http://nuget.org/List/Packages/Glav.CacheAdapter" target="_blank"&gt;go grab it&lt;/a&gt;. Or you can &lt;a href="https://bitbucket.org/glav/cacheadapter" target="_blank"&gt;download the source code form here&lt;/a&gt; and change it to your hearts content. Its actually a relatively simple implementation and it would be easy to plug in any underlying cache implementation should you wish.&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7809931" width="1" height="1"&gt;</description></item><item><title>Diskeeper–a short review</title><link>http://weblogs.asp.net/pglavich/archive/2011/05/16/diskeeper-a-short-review.aspx</link><pubDate>Sun, 15 May 2011 22:23:40 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7792367</guid><dc:creator>Glav</dc:creator><slash:comments>3</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/pglavich/rsscomments.aspx?PostID=7792367</wfw:commentRss><comments>http://weblogs.asp.net/pglavich/archive/2011/05/16/diskeeper-a-short-review.aspx#comments</comments><description>&lt;p&gt;I have been lucky enough to be ASP.NET MVP for a number of years now. One of the perks of that is you get access to free licences of software such as &lt;a href="http://www.diskeeper.com" target="_blank"&gt;Diskeeper&lt;/a&gt;. This piece of software keeps your disks running as smooth as they possibly can.&lt;/p&gt;  &lt;p&gt;I have been using &lt;a href="http://www.diskeeper.com" target="_blank"&gt;Diskeeper&lt;/a&gt; for around 2 months now and am pretty impressed. It silently does its thing in the background without you having to touch anything. One of the newest and also one of my favourite features is the automatic defrag capability.You don’t have to launch a defrag application and have it run on your system or schedule it to run periodically, as &lt;a href="http://www.diskeeper.com" target="_blank"&gt;Diskeeper&lt;/a&gt; performs this operation all the time, in the background for all disk operations. Basically, you just use your system like normal, and it just magically gets defragmented, all the time. Nice.&lt;/p&gt;  &lt;p&gt;I have 2 disks in my system. A SSD (Solid State Drive) as my primary ( C: ) drive, and a 5200rpm 320Gb hard disk as my secondary data drive ( D: ). After installing &lt;a href="http://www.diskeeper.com" target="_blank"&gt;Diskeeper&lt;/a&gt;, I have noticed a significant difference in the performance of the secondary drive in particular. I imagine because it is the slowest disk and because it stands to benefit the most. The SSD is fast anyway, so it is much harder to notice any difference. I have noticed a small difference, but it’s only slight compared to the secondary ( spinning rust ) drive.&lt;/p&gt;  &lt;p&gt;I should mention that in order for &lt;a href="http://www.diskeeper.com" target="_blank"&gt;Diskeeper&lt;/a&gt; to work with SSD hardware, you need a special edition of &lt;a href="http://www.diskeeper.com" target="_blank"&gt;Diskeeper&lt;/a&gt; with an add-on called Hyper-fast that takes into account the SSD unit and has special algorithms that are designed to optimise SSD operation.&lt;/p&gt;  &lt;p&gt;For those interested in the actual metrics, &lt;a href="http://www.diskeeper.com" target="_blank"&gt;Diskeeper&lt;/a&gt; provides an easy to use management console that displays your current system improvements. The initial display is shown below.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://weblogs.asp.net/blogs/pglavich/image_7E5F61B0.png"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://weblogs.asp.net/blogs/pglavich/image_thumb_5F0C9510.png" width="502" height="330" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;The management console offers a number of ways to look at what has been done to your disks and how it has been performing. I find the history view particularly interesting as it proves a look at how the disk is being used and optimised over time.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://weblogs.asp.net/blogs/pglavich/image_09985021.png"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://weblogs.asp.net/blogs/pglavich/image_thumb_074BA198.png" width="506" height="332" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;So would I recommend it? Yes, and this is not simply because I have been given a free licence and feel obligated to do so. It is particularly useful if you have standard hard disks regardless of how fast they spin. You will notice a much smoother and responsive system as a result. For SSD owners, it is still worthwhile but the benefits are less because of the inherent speed in SSD units. For a system with a mix of SSD and hard disks, it works beautifully.&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7792367" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/pglavich/archive/tags/.NET/default.aspx">.NET</category><category domain="http://weblogs.asp.net/pglavich/archive/tags/Community+News/default.aspx">Community News</category><category domain="http://weblogs.asp.net/pglavich/archive/tags/Performance/default.aspx">Performance</category></item></channel></rss>