<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:a10="http://www.w3.org/2005/Atom" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0"><channel><title>Tugberk's Blog</title><link>http://www.tugberkugurlu.com/Feeds/Rss</link><description>Tugberk's Blog</description><image><url>http://tugberkugurlu.com/Content/Images/feed-image.png</url><title>Tugberk's Blog</title><link>http://www.tugberkugurlu.com/Feeds/Rss</link></image><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/TugberkUgurlu" /><feedburner:info uri="tugberkugurlu" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item><guid isPermaLink="false">http://tugberkugurlu.com/86</guid><link>http://feedproxy.google.com/~r/TugberkUgurlu/~3/jYKI54AqbMo/86</link><title>Getting Started With ASP.NET Web API - Tutorials, Videos, Samples</title><description>&lt;p&gt;&lt;a title="http://asp.net" href="http://asp.net"&gt;ASP.NET&lt;/a&gt; team shipped the beta version of &lt;a title="http://www.asp.net/web-api" href="http://www.asp.net/web-api"&gt;ASP.NET Web API&lt;/a&gt; which has been known as &lt;a title="http://www.tugberkugurlu.com/archive/introduction-to-wcf-web-api-new-rest-face-ofnet" href="http://www.tugberkugurlu.com/archive/introduction-to-wcf-web-api-new-rest-face-ofnet"&gt;WCF Web API&lt;/a&gt; for more than a year. It has seen 6 preview versions and we have now nearly the final bits and pieces. The beta version of the product has been merged with ASP.NET MVC so that we won&amp;rsquo;t feel ourselves in a fork in the road in order to create REST APIs.&lt;/p&gt;
&lt;p&gt;In my opinion, ASP.NET Web API is really exciting because it offers a simple and pluggable way to expose your data to the World. The one of the best parts is that you don&amp;rsquo;t have to just expose it as XML or JSON. You have the XML and JSON support out of the box but the format of the data is really up to you. Since nearly all of the devices out there today know how to communicate through HTTP, your data has the best possible reach.&lt;/p&gt;
&lt;p&gt;One of the other great features of ASP.NET Web API is that everything has been built asynchrony in mind. So, you can scale out very easily. On the other hand, don&amp;rsquo;t act prejudicial that it is ASP.NET based and cannot run anywhere other than IIS. It has a self hosting capability as well (I honestly don&amp;rsquo;t know how it works at the background but it looks promising).&lt;/p&gt;
&lt;p&gt;It has been not much since the product beta version has shipped but there are pretty good blog posts and articles about ASP.NET Web API out there. Instead of writing one, I would like to point you those resources.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Getting Started&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a title="http://weblogs.asp.net/scottgu/archive/2012/02/23/asp-net-web-api-part-1.aspx" href="http://weblogs.asp.net/scottgu/archive/2012/02/23/asp-net-web-api-part-1.aspx"&gt;ASP.NET Web API (Part 1)&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;&lt;a title="http://www.hanselman.com/blog/OneASPNETMakingJSONWebAPIsWithASPNETMVC4BetaAndASPNETWebAPI.aspx" href="http://www.hanselman.com/blog/OneASPNETMakingJSONWebAPIsWithASPNETMVC4BetaAndASPNETWebAPI.aspx"&gt;One ASP.NET - Making JSON Web APIs with ASP.NET MVC 4 Beta and ASP.NET Web API&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a title="http://weblogs.asp.net/jgalloway/archive/2012/02/16/asp-net-4-beta-released.aspx" href="http://weblogs.asp.net/jgalloway/archive/2012/02/16/asp-net-4-beta-released.aspx"&gt;ASP.NET MVC 4 Beta Released!&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;&lt;a title="http://blogs.msdn.com/b/carlosfigueira/archive/2012/02/16/introducing-asp-net-mvc-4-beta-with-web-apis.aspx" href="http://blogs.msdn.com/b/carlosfigueira/archive/2012/02/16/introducing-asp-net-mvc-4-beta-with-web-apis.aspx"&gt;Introducing ASP.NET MVC 4 Beta with Web APIs&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a title="http://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/tutorial-your-first-web-api" href="http://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/tutorial-your-first-web-api"&gt;Your First ASP.NET Web API (C#)&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;&lt;a title="http://www.asp.net/web-api/overview/web-api-routing-and-actions/routing-in-aspnet-web-api" href="http://www.asp.net/web-api/overview/web-api-routing-and-actions/routing-in-aspnet-web-api"&gt;Routing in ASP.NET Web API&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Validation and Data Annotations&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a title="http://weblogs.asp.net/cibrax/archive/2012/02/23/validating-your-models-in-asp-net-web-api.aspx" href="http://weblogs.asp.net/cibrax/archive/2012/02/23/validating-your-models-in-asp-net-web-api.aspx"&gt;Validating your models in ASP.NET Web API&lt;/a&gt;&amp;nbsp;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Dependency Injection &amp;amp; Unit Testing&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a title="http://www.asp.net/web-api/overview/extensibility/using-the-web-api-dependency-resolver" href="http://www.asp.net/web-api/overview/extensibility/using-the-web-api-dependency-resolver"&gt;Using the Web API Dependency Resolver&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Message Handlers,&amp;nbsp;&lt;strong&gt;Filters &amp;amp;&amp;nbsp;&lt;strong&gt;Formatters&lt;/strong&gt;&lt;/strong&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a title="http://www.asp.net/web-api/overview/working-with-http/http-message-handlers" href="http://www.asp.net/web-api/overview/working-with-http/http-message-handlers"&gt;HTTP Message Handlers&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;&lt;a title="http://www.tugberkugurlu.com/archive/asp-net-web-api-and-elmah-integration" href="http://www.tugberkugurlu.com/archive/asp-net-web-api-and-elmah-integration"&gt;ASP.NET Web API and ELMAH Integration&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a title="http://blogs.msdn.com/b/henrikn/archive/2012/02/18/using-json-net-with-asp-net-web-api.aspx" href="http://blogs.msdn.com/b/henrikn/archive/2012/02/18/using-json-net-with-asp-net-web-api.aspx"&gt;Using JSON.NET with ASP.NET Web API&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;HttpClient&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a title="http://blogs.msdn.com/b/henrikn/archive/2012/02/16/httpclient-is-here.aspx" href="http://blogs.msdn.com/b/henrikn/archive/2012/02/16/httpclient-is-here.aspx"&gt;HttpClient is Here!&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;&lt;a title="http://blogs.msdn.com/b/henrikn/archive/2012/02/20/asp-net-web-api-and-httpclient-available-on-nuget.aspx" href="http://blogs.msdn.com/b/henrikn/archive/2012/02/20/asp-net-web-api-and-httpclient-available-on-nuget.aspx"&gt;ASP.NET Web API and HttpClient Available on NuGet&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;&lt;a title="http://blogs.msdn.com/b/henrikn/archive/2012/02/17/downloading-a-google-map-to-local-file.aspx" href="http://blogs.msdn.com/b/henrikn/archive/2012/02/17/downloading-a-google-map-to-local-file.aspx"&gt;HttpClient: Downloading to a Local File&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;&lt;a title="http://blogs.msdn.com/b/henrikn/archive/2012/02/17/push-and-pull-streams-using-httpclient.aspx" href="http://blogs.msdn.com/b/henrikn/archive/2012/02/17/push-and-pull-streams-using-httpclient.aspx"&gt;Push and Pull Streams using HttpClient&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;&lt;a title="http://blogs.msdn.com/b/henrikn/archive/2012/02/16/extending-httpclient-with-oauth-to-access-twitter.aspx" href="http://blogs.msdn.com/b/henrikn/archive/2012/02/16/extending-httpclient-with-oauth-to-access-twitter.aspx"&gt;Extending HttpClient with OAuth to Access Twitter&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Self Hosting&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a title="http://www.asp.net/web-api/overview/hosting-aspnet-web-api/self-host-a-web-api" href="http://www.asp.net/web-api/overview/hosting-aspnet-web-api/self-host-a-web-api"&gt;Self-Host a Web API (C#)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a title="http://code.msdn.microsoft.com/ASPNET-Web-API-Self-Host-30abca12" href="http://code.msdn.microsoft.com/ASPNET-Web-API-Self-Host-30abca12"&gt;ASP.NET Web API Self-Host (Sample)&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Uncategorized&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a title="http://www.asp.net/web-api/overview/web-api-routing-and-actions/creating-a-web-api-that-supports-crud-operations" href="http://www.asp.net/web-api/overview/web-api-routing-and-actions/creating-a-web-api-that-supports-crud-operations"&gt;Creating a Web API that Supports CRUD Operations&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a title="http://weblogs.asp.net/cibrax/archive/2012/02/17/integrating-backbone-js-with-asp-net-web-api.aspx" href="http://weblogs.asp.net/cibrax/archive/2012/02/17/integrating-backbone-js-with-asp-net-web-api.aspx"&gt;Integrating Backbone.js with ASP.NET Web API&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a title="http://blogs.msdn.com/b/henrikn/archive/2012/02/23/using-asp-net-web-api-with-asp-net-web-forms.aspx" href="http://blogs.msdn.com/b/henrikn/archive/2012/02/23/using-asp-net-web-api-with-asp-net-web-forms.aspx"&gt;Using ASP.NET Web API with ASP.NET Web Forms&lt;/a&gt;&lt;/li&gt;
&lt;li&gt; &lt;a title="http://blogs.msdn.com/b/carlosfigueira/archive/2012/02/20/implementing-cors-support-in-asp-net-web-apis.aspx" href="http://blogs.msdn.com/b/carlosfigueira/archive/2012/02/20/implementing-cors-support-in-asp-net-web-apis.aspx"&gt;Implementing CORS support in ASP.NET Web APIs&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;&lt;a href="http://blogs.msdn.com/b/carlosfigueira/archive/2012/02/21/implementing-cors-support-in-asp-net-web-apis-take-2.aspx" title="http://blogs.msdn.com/b/carlosfigueira/archive/2012/02/21/implementing-cors-support-in-asp-net-web-apis-take-2.aspx"&gt;Implementing CORS support in ASP.NET Web APIs &amp;ndash; take 2&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;
&lt;div align="left"&gt;&lt;a title="http://blogs.msdn.com/b/henrikn/archive/2012/02/19/using-web-api-with-mongodb.aspx" href="http://blogs.msdn.com/b/henrikn/archive/2012/02/19/using-web-api-with-mongodb.aspx"&gt;Using MongoDB with ASP.NET Web API&lt;/a&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Samples&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="http://code.msdn.microsoft.com/Contact-Manager-Web-API-0e8e373d" title="http://code.msdn.microsoft.com/Contact-Manager-Web-API-0e8e373d"&gt;Contact Manager Web API&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a title="http://code.msdn.microsoft.com/ASPNET-Web-API-JavaScript-d0d64dd7" href="http://code.msdn.microsoft.com/ASPNET-Web-API-JavaScript-d0d64dd7"&gt;ASP.NET Web API JavaScript Scenarios - Comments&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a title="http://code.msdn.microsoft.com/Implementing-CORS-support-a677ab5d" href="http://code.msdn.microsoft.com/Implementing-CORS-support-a677ab5d"&gt;Implementing CORS support in ASP.NET Web APIs&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a title="https://github.com/tugberkugurlu/ASPNETWebAPISamples" href="https://github.com/tugberkugurlu/ASPNETWebAPISamples"&gt;ASPNETWebAPISamples&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a title="https://github.com/ChristianWeyer/Thinktecture.Web.Http" href="https://github.com/ChristianWeyer/Thinktecture.Web.Http"&gt;Thinktecture.Web.Http - Extensions for ASP.NET Web API&lt;/a&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Also, there are some great videos done by &lt;a title="http://twitter.com/jongalloway" href="http://twitter.com/jongalloway"&gt;@jongalloway&lt;/a&gt; on ASP.NET web site as well as some other videos:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;ASP.NET Web API Video Series&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="http://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/video-your-first-web-api"&gt;ASP.NET Web API, Part 1: Your First Web API&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;&lt;a href="http://www.asp.net/web-api/overview/web-api-routing-and-actions/video-getting-data"&gt;ASP.NET Web API, Part 2: Getting Data&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;&lt;a href="http://www.asp.net/web-api/overview/web-api-routing-and-actions/video-delete-and-create"&gt;ASP.NET Web API, Part 3: Delete and Update&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;&lt;a href="http://www.asp.net/web-api/overview/web-api-routing-and-actions/video-paging-and-querying"&gt;ASP.NET Web API, Part 4: Paging and Querying&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;&lt;a href="http://www.asp.net/web-api/overview/web-api-routing-and-actions/video-custom-validation"&gt;ASP.NET Web API, Part 5: Custom Validation&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;&lt;a href="http://www.asp.net/web-api/overview/web-api-routing-and-actions/video-authorization"&gt;ASP.NET Web API, Part 6: Authorization&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Some Other Great Talks, Webcasts and Other Type of Visuals&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a title="http://www.c4mvc.net/meeting/?id=25" href="http://www.c4mvc.net/meeting/?id=25"&gt;Building HTTP services with ASP.NET Web API in MVC 4 Beta&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a title="http://channel9.msdn.com/Shows/Web+Camps+TV/Dan-Roth-on-the-new-ASPNET-Web-API" href="http://channel9.msdn.com/Shows/Web+Camps+TV/Dan-Roth-on-the-new-ASPNET-Web-API"&gt;Dan Roth on the new ASP.NET Web API&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a title="http://channel9.msdn.com/Events/TechDays/Techdays-2012-the-Netherlands/2159" href="http://channel9.msdn.com/Events/TechDays/Techdays-2012-the-Netherlands/2159"&gt;Building Single Page Apps for desktop, mobile and tablet with ASP.NET MVC 4&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a title="http://channel9.msdn.com/Events/TechDays/Techdays-2012-the-Netherlands/2364" href="http://channel9.msdn.com/Events/TechDays/Techdays-2012-the-Netherlands/2364"&gt;A look at ASP.NET MVC 4&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;If you have any other related blog post, tutorial, sample or whatever related to ASP.NET Web API, please comment and I will add them to this list. I will also add new things to this list that I come across. Enjoy:)&lt;/p&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/Vj0S0qzCduvBiMEWTcQwgzDDri8/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/Vj0S0qzCduvBiMEWTcQwgzDDri8/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/Vj0S0qzCduvBiMEWTcQwgzDDri8/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/Vj0S0qzCduvBiMEWTcQwgzDDri8/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/TugberkUgurlu/~4/jYKI54AqbMo" height="1" width="1"/&gt;</description><pubDate>Fri, 24 Feb 2012 06:49:00 Z</pubDate><a10:updated>2012-02-24T06:49:00Z</a10:updated><a10:content type="text">&lt;p&gt;&lt;a title="http://asp.net" href="http://asp.net"&gt;ASP.NET&lt;/a&gt; team shipped the beta version of &lt;a title="http://www.asp.net/web-api" href="http://www.asp.net/web-api"&gt;ASP.NET Web API&lt;/a&gt; which has been known as &lt;a title="http://www.tugberkugurlu.com/archive/introduction-to-wcf-web-api-new-rest-face-ofnet" href="http://www.tugberkugurlu.com/archive/introduction-to-wcf-web-api-new-rest-face-ofnet"&gt;WCF Web API&lt;/a&gt; for more than a year. It has seen 6 preview versions and we have now nearly the final bits and pieces. The beta version of the product has been merged with ASP.NET MVC so that we won&amp;rsquo;t feel ourselves in a fork in the road in order to create REST APIs.&lt;/p&gt;
&lt;p&gt;In my opinion, ASP.NET Web API is really exciting because it offers a simple and pluggable way to expose your data to the World. The one of the best parts is that you don&amp;rsquo;t have to just expose it as XML or JSON. You have the XML and JSON support out of the box but the format of the data is really up to you. Since nearly all of the devices out there today know how to communicate through HTTP, your data has the best possible reach.&lt;/p&gt;
&lt;p&gt;One of the other great features of ASP.NET Web API is that everything has been built asynchrony in mind. So, you can scale out very easily. On the other hand, don&amp;rsquo;t act prejudicial that it is ASP.NET based and cannot run anywhere other than IIS. It has a self hosting capability as well (I honestly don&amp;rsquo;t know how it works at the background but it looks promising).&lt;/p&gt;
&lt;p&gt;It has been not much since the product beta version has shipped but there are pretty good blog posts and articles about ASP.NET Web API out there. Instead of writing one, I would like to point you those resources.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Getting Started&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a title="http://weblogs.asp.net/scottgu/archive/2012/02/23/asp-net-web-api-part-1.aspx" href="http://weblogs.asp.net/scottgu/archive/2012/02/23/asp-net-web-api-part-1.aspx"&gt;ASP.NET Web API (Part 1)&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;&lt;a title="http://www.hanselman.com/blog/OneASPNETMakingJSONWebAPIsWithASPNETMVC4BetaAndASPNETWebAPI.aspx" href="http://www.hanselman.com/blog/OneASPNETMakingJSONWebAPIsWithASPNETMVC4BetaAndASPNETWebAPI.aspx"&gt;One ASP.NET - Making JSON Web APIs with ASP.NET MVC 4 Beta and ASP.NET Web API&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a title="http://weblogs.asp.net/jgalloway/archive/2012/02/16/asp-net-4-beta-released.aspx" href="http://weblogs.asp.net/jgalloway/archive/2012/02/16/asp-net-4-beta-released.aspx"&gt;ASP.NET MVC 4 Beta Released!&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;&lt;a title="http://blogs.msdn.com/b/carlosfigueira/archive/2012/02/16/introducing-asp-net-mvc-4-beta-with-web-apis.aspx" href="http://blogs.msdn.com/b/carlosfigueira/archive/2012/02/16/introducing-asp-net-mvc-4-beta-with-web-apis.aspx"&gt;Introducing ASP.NET MVC 4 Beta with Web APIs&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a title="http://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/tutorial-your-first-web-api" href="http://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/tutorial-your-first-web-api"&gt;Your First ASP.NET Web API (C#)&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;&lt;a title="http://www.asp.net/web-api/overview/web-api-routing-and-actions/routing-in-aspnet-web-api" href="http://www.asp.net/web-api/overview/web-api-routing-and-actions/routing-in-aspnet-web-api"&gt;Routing in ASP.NET Web API&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Validation and Data Annotations&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a title="http://weblogs.asp.net/cibrax/archive/2012/02/23/validating-your-models-in-asp-net-web-api.aspx" href="http://weblogs.asp.net/cibrax/archive/2012/02/23/validating-your-models-in-asp-net-web-api.aspx"&gt;Validating your models in ASP.NET Web API&lt;/a&gt;&amp;nbsp;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Dependency Injection &amp;amp; Unit Testing&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a title="http://www.asp.net/web-api/overview/extensibility/using-the-web-api-dependency-resolver" href="http://www.asp.net/web-api/overview/extensibility/using-the-web-api-dependency-resolver"&gt;Using the Web API Dependency Resolver&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Message Handlers,&amp;nbsp;&lt;strong&gt;Filters &amp;amp;&amp;nbsp;&lt;strong&gt;Formatters&lt;/strong&gt;&lt;/strong&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a title="http://www.asp.net/web-api/overview/working-with-http/http-message-handlers" href="http://www.asp.net/web-api/overview/working-with-http/http-message-handlers"&gt;HTTP Message Handlers&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;&lt;a title="http://www.tugberkugurlu.com/archive/asp-net-web-api-and-elmah-integration" href="http://www.tugberkugurlu.com/archive/asp-net-web-api-and-elmah-integration"&gt;ASP.NET Web API and ELMAH Integration&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a title="http://blogs.msdn.com/b/henrikn/archive/2012/02/18/using-json-net-with-asp-net-web-api.aspx" href="http://blogs.msdn.com/b/henrikn/archive/2012/02/18/using-json-net-with-asp-net-web-api.aspx"&gt;Using JSON.NET with ASP.NET Web API&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;HttpClient&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a title="http://blogs.msdn.com/b/henrikn/archive/2012/02/16/httpclient-is-here.aspx" href="http://blogs.msdn.com/b/henrikn/archive/2012/02/16/httpclient-is-here.aspx"&gt;HttpClient is Here!&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;&lt;a title="http://blogs.msdn.com/b/henrikn/archive/2012/02/20/asp-net-web-api-and-httpclient-available-on-nuget.aspx" href="http://blogs.msdn.com/b/henrikn/archive/2012/02/20/asp-net-web-api-and-httpclient-available-on-nuget.aspx"&gt;ASP.NET Web API and HttpClient Available on NuGet&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;&lt;a title="http://blogs.msdn.com/b/henrikn/archive/2012/02/17/downloading-a-google-map-to-local-file.aspx" href="http://blogs.msdn.com/b/henrikn/archive/2012/02/17/downloading-a-google-map-to-local-file.aspx"&gt;HttpClient: Downloading to a Local File&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;&lt;a title="http://blogs.msdn.com/b/henrikn/archive/2012/02/17/push-and-pull-streams-using-httpclient.aspx" href="http://blogs.msdn.com/b/henrikn/archive/2012/02/17/push-and-pull-streams-using-httpclient.aspx"&gt;Push and Pull Streams using HttpClient&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;&lt;a title="http://blogs.msdn.com/b/henrikn/archive/2012/02/16/extending-httpclient-with-oauth-to-access-twitter.aspx" href="http://blogs.msdn.com/b/henrikn/archive/2012/02/16/extending-httpclient-with-oauth-to-access-twitter.aspx"&gt;Extending HttpClient with OAuth to Access Twitter&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Self Hosting&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a title="http://www.asp.net/web-api/overview/hosting-aspnet-web-api/self-host-a-web-api" href="http://www.asp.net/web-api/overview/hosting-aspnet-web-api/self-host-a-web-api"&gt;Self-Host a Web API (C#)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a title="http://code.msdn.microsoft.com/ASPNET-Web-API-Self-Host-30abca12" href="http://code.msdn.microsoft.com/ASPNET-Web-API-Self-Host-30abca12"&gt;ASP.NET Web API Self-Host (Sample)&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Uncategorized&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a title="http://www.asp.net/web-api/overview/web-api-routing-and-actions/creating-a-web-api-that-supports-crud-operations" href="http://www.asp.net/web-api/overview/web-api-routing-and-actions/creating-a-web-api-that-supports-crud-operations"&gt;Creating a Web API that Supports CRUD Operations&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a title="http://weblogs.asp.net/cibrax/archive/2012/02/17/integrating-backbone-js-with-asp-net-web-api.aspx" href="http://weblogs.asp.net/cibrax/archive/2012/02/17/integrating-backbone-js-with-asp-net-web-api.aspx"&gt;Integrating Backbone.js with ASP.NET Web API&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a title="http://blogs.msdn.com/b/henrikn/archive/2012/02/23/using-asp-net-web-api-with-asp-net-web-forms.aspx" href="http://blogs.msdn.com/b/henrikn/archive/2012/02/23/using-asp-net-web-api-with-asp-net-web-forms.aspx"&gt;Using ASP.NET Web API with ASP.NET Web Forms&lt;/a&gt;&lt;/li&gt;
&lt;li&gt; &lt;a title="http://blogs.msdn.com/b/carlosfigueira/archive/2012/02/20/implementing-cors-support-in-asp-net-web-apis.aspx" href="http://blogs.msdn.com/b/carlosfigueira/archive/2012/02/20/implementing-cors-support-in-asp-net-web-apis.aspx"&gt;Implementing CORS support in ASP.NET Web APIs&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;&lt;a href="http://blogs.msdn.com/b/carlosfigueira/archive/2012/02/21/implementing-cors-support-in-asp-net-web-apis-take-2.aspx" title="http://blogs.msdn.com/b/carlosfigueira/archive/2012/02/21/implementing-cors-support-in-asp-net-web-apis-take-2.aspx"&gt;Implementing CORS support in ASP.NET Web APIs &amp;ndash; take 2&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;
&lt;div align="left"&gt;&lt;a title="http://blogs.msdn.com/b/henrikn/archive/2012/02/19/using-web-api-with-mongodb.aspx" href="http://blogs.msdn.com/b/henrikn/archive/2012/02/19/using-web-api-with-mongodb.aspx"&gt;Using MongoDB with ASP.NET Web API&lt;/a&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Samples&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="http://code.msdn.microsoft.com/Contact-Manager-Web-API-0e8e373d" title="http://code.msdn.microsoft.com/Contact-Manager-Web-API-0e8e373d"&gt;Contact Manager Web API&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a title="http://code.msdn.microsoft.com/ASPNET-Web-API-JavaScript-d0d64dd7" href="http://code.msdn.microsoft.com/ASPNET-Web-API-JavaScript-d0d64dd7"&gt;ASP.NET Web API JavaScript Scenarios - Comments&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a title="http://code.msdn.microsoft.com/Implementing-CORS-support-a677ab5d" href="http://code.msdn.microsoft.com/Implementing-CORS-support-a677ab5d"&gt;Implementing CORS support in ASP.NET Web APIs&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a title="https://github.com/tugberkugurlu/ASPNETWebAPISamples" href="https://github.com/tugberkugurlu/ASPNETWebAPISamples"&gt;ASPNETWebAPISamples&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a title="https://github.com/ChristianWeyer/Thinktecture.Web.Http" href="https://github.com/ChristianWeyer/Thinktecture.Web.Http"&gt;Thinktecture.Web.Http - Extensions for ASP.NET Web API&lt;/a&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Also, there are some great videos done by &lt;a title="http://twitter.com/jongalloway" href="http://twitter.com/jongalloway"&gt;@jongalloway&lt;/a&gt; on ASP.NET web site as well as some other videos:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;ASP.NET Web API Video Series&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="http://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/video-your-first-web-api"&gt;ASP.NET Web API, Part 1: Your First Web API&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;&lt;a href="http://www.asp.net/web-api/overview/web-api-routing-and-actions/video-getting-data"&gt;ASP.NET Web API, Part 2: Getting Data&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;&lt;a href="http://www.asp.net/web-api/overview/web-api-routing-and-actions/video-delete-and-create"&gt;ASP.NET Web API, Part 3: Delete and Update&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;&lt;a href="http://www.asp.net/web-api/overview/web-api-routing-and-actions/video-paging-and-querying"&gt;ASP.NET Web API, Part 4: Paging and Querying&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;&lt;a href="http://www.asp.net/web-api/overview/web-api-routing-and-actions/video-custom-validation"&gt;ASP.NET Web API, Part 5: Custom Validation&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;&lt;a href="http://www.asp.net/web-api/overview/web-api-routing-and-actions/video-authorization"&gt;ASP.NET Web API, Part 6: Authorization&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Some Other Great Talks, Webcasts and Other Type of Visuals&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a title="http://www.c4mvc.net/meeting/?id=25" href="http://www.c4mvc.net/meeting/?id=25"&gt;Building HTTP services with ASP.NET Web API in MVC 4 Beta&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a title="http://channel9.msdn.com/Shows/Web+Camps+TV/Dan-Roth-on-the-new-ASPNET-Web-API" href="http://channel9.msdn.com/Shows/Web+Camps+TV/Dan-Roth-on-the-new-ASPNET-Web-API"&gt;Dan Roth on the new ASP.NET Web API&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a title="http://channel9.msdn.com/Events/TechDays/Techdays-2012-the-Netherlands/2159" href="http://channel9.msdn.com/Events/TechDays/Techdays-2012-the-Netherlands/2159"&gt;Building Single Page Apps for desktop, mobile and tablet with ASP.NET MVC 4&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a title="http://channel9.msdn.com/Events/TechDays/Techdays-2012-the-Netherlands/2364" href="http://channel9.msdn.com/Events/TechDays/Techdays-2012-the-Netherlands/2364"&gt;A look at ASP.NET MVC 4&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;If you have any other related blog post, tutorial, sample or whatever related to ASP.NET Web API, please comment and I will add them to this list. I will also add new things to this list that I come across. Enjoy:)&lt;/p&gt;</a10:content><feedburner:origLink>http://tugberkugurlu.com/86</feedburner:origLink></item><item><guid isPermaLink="false">http://tugberkugurlu.com/85</guid><link>http://feedproxy.google.com/~r/TugberkUgurlu/~3/prTgPV5hjWA/85</link><title>ASP.NET Web API and ELMAH Integration</title><description>&lt;p&gt;As you all probably heard, &lt;a target="_blank" href="http://asp.net/mvc/mvc4"&gt;ASP.NET MVC 4&lt;/a&gt; Beta is now available and has new features in it. One of them is that ASP.NET MVC has shipped with &lt;a target="_blank" href="http://www.asp.net/web-api" title="http://www.asp.net/web-api"&gt;ASP.NET Web API&lt;/a&gt; (which was previously know as &lt;a target="_blank" href="http://www.tugberkugurlu.com/archive/introduction-to-wcf-web-api-new-rest-face-ofnet" title="http://www.tugberkugurlu.com/archive/introduction-to-wcf-web-api-new-rest-face-ofnet"&gt;WCF Web API&lt;/a&gt;). Here is the quote from ASP.NET web site which explains what Web API Framework is all about shortly:&lt;/p&gt;
&lt;p&gt;&lt;em&gt;ASP.NET Web API is a framework that makes it easy to build HTTP services that reach a broad range of clients, including browsers and mobile devices. ASP.NET Web API is an ideal platform for building RESTful applications on the .NET Framework.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;I am not going to give an into on ASP.NET Web API. There are great into tutorials and videos on ASP.NET web site for ASP.NET Web API. Instead, I will give you an example of a custom filter implementation.&lt;/p&gt;
&lt;p&gt;A couple of months ago, I wrote a blog post about &lt;em&gt;WCF Web API HttpErrorHandlers&lt;/em&gt;: &lt;a target="_blank" href="http://www.tugberkugurlu.com/archive/wcf-web-api-plays-nice-with-elmah-a-quick-introduction-to-wcf-web-api-httperrorhandler" title="http://www.tugberkugurlu.com/archive/wcf-web-api-plays-nice-with-elmah-a-quick-introduction-to-wcf-web-api-httperrorhandler"&gt;WCF Web API Plays Nice With ELMAH - A Quick Introduction to WCF Web API HttpErrorHandler&lt;/a&gt;. It works nicely on WCF Web API but this version of the framework, things a little bit changed. Instead of ErrorHandlers, we now have Filters which is more generic.&lt;/p&gt;
&lt;p&gt;In order to make ELMAH work with ASP.NET Web API, we need to create a new Attribute which implements IExceptionFilter interface. Since we have ExceptionFilterAttribute (which implements the IExceptionFilter interface) available at the framework, we will derived from that class instead. Here is the whole implementation:&lt;/p&gt;
&lt;div class="code-wrapper border-shadow-1"&gt;
&lt;div style="background-color: white; color: black;"&gt;
&lt;pre&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;class&lt;/span&gt; ElmahErrorAttribute : 
    System.Web.Http.Filters.ExceptionFilterAttribute {

    &lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;override&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; OnException(
        System.Web.Http.Filters.HttpActionExecutedContext actionExecutedContext) {

        &lt;span style="color: blue;"&gt;if&lt;/span&gt;(actionExecutedContext.Exception != &lt;span style="color: blue;"&gt;null&lt;/span&gt;)
            Elmah.ErrorSignal.FromCurrentContext().Raise(actionExecutedContext.Exception);

        &lt;span style="color: blue;"&gt;base&lt;/span&gt;.OnException(actionExecutedContext);
    }
}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Now we have our attribute, we need to tell the framework to use it. It is very straight forward as well. Here is how my Global.asax (Global.asax.cs) looks like:&lt;/p&gt;
&lt;div class="code-wrapper border-shadow-1"&gt;
&lt;div style="background-color: white; color: black;"&gt;
&lt;pre&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;class&lt;/span&gt; WebApiApplication : System.Web.HttpApplication {

    &lt;span style="color: blue;"&gt;protected&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; Application_Start() {

        Configure(
            System.Web.Http.GlobalConfiguration.Configuration
        );
    }

    &lt;span style="color: blue;"&gt;private&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; Configure(HttpConfiguration httpConfiguration) {

        httpConfiguration.Filters.Add(
            &lt;span style="color: blue;"&gt;new&lt;/span&gt; ElmahErrorAttribute()
        );

        httpConfiguration.Routes.MapHttpRoute(
            name: &lt;span style="color: #a31515;"&gt;"DefaultApi"&lt;/span&gt;,
            routeTemplate: &lt;span style="color: #a31515;"&gt;"api/{controller}/{id}"&lt;/span&gt;,
            defaults: &lt;span style="color: blue;"&gt;new&lt;/span&gt; { id = RouteParameter.Optional }
        );
    }
}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;This will make Elmah log the errors. Now, you can configure ELMAH to send you an e-mail when an error occurred or you can log the error inside an XML file, SQL Server Database, wherever you what.&lt;/p&gt;
&lt;p&gt;One more thing to mention about is that when you hit an error, the response will carry the exception details at the body of the response if you run your application locally. You can configure this option as well with the following configuration:&lt;/p&gt;
&lt;div class="code-wrapper border-shadow-1"&gt;
&lt;div style="background-color: white; color: black;"&gt;
&lt;pre&gt;httpConfiguration.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Never;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/eSUyA-xtJRZWbd_KMRbwoWqGK_8/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/eSUyA-xtJRZWbd_KMRbwoWqGK_8/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/eSUyA-xtJRZWbd_KMRbwoWqGK_8/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/eSUyA-xtJRZWbd_KMRbwoWqGK_8/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/TugberkUgurlu/~4/prTgPV5hjWA" height="1" width="1"/&gt;</description><pubDate>Thu, 23 Feb 2012 11:53:00 Z</pubDate><a10:updated>2012-02-23T11:53:00Z</a10:updated><a10:content type="text">&lt;p&gt;As you all probably heard, &lt;a target="_blank" href="http://asp.net/mvc/mvc4"&gt;ASP.NET MVC 4&lt;/a&gt; Beta is now available and has new features in it. One of them is that ASP.NET MVC has shipped with &lt;a target="_blank" href="http://www.asp.net/web-api" title="http://www.asp.net/web-api"&gt;ASP.NET Web API&lt;/a&gt; (which was previously know as &lt;a target="_blank" href="http://www.tugberkugurlu.com/archive/introduction-to-wcf-web-api-new-rest-face-ofnet" title="http://www.tugberkugurlu.com/archive/introduction-to-wcf-web-api-new-rest-face-ofnet"&gt;WCF Web API&lt;/a&gt;). Here is the quote from ASP.NET web site which explains what Web API Framework is all about shortly:&lt;/p&gt;
&lt;p&gt;&lt;em&gt;ASP.NET Web API is a framework that makes it easy to build HTTP services that reach a broad range of clients, including browsers and mobile devices. ASP.NET Web API is an ideal platform for building RESTful applications on the .NET Framework.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;I am not going to give an into on ASP.NET Web API. There are great into tutorials and videos on ASP.NET web site for ASP.NET Web API. Instead, I will give you an example of a custom filter implementation.&lt;/p&gt;
&lt;p&gt;A couple of months ago, I wrote a blog post about &lt;em&gt;WCF Web API HttpErrorHandlers&lt;/em&gt;: &lt;a target="_blank" href="http://www.tugberkugurlu.com/archive/wcf-web-api-plays-nice-with-elmah-a-quick-introduction-to-wcf-web-api-httperrorhandler" title="http://www.tugberkugurlu.com/archive/wcf-web-api-plays-nice-with-elmah-a-quick-introduction-to-wcf-web-api-httperrorhandler"&gt;WCF Web API Plays Nice With ELMAH - A Quick Introduction to WCF Web API HttpErrorHandler&lt;/a&gt;. It works nicely on WCF Web API but this version of the framework, things a little bit changed. Instead of ErrorHandlers, we now have Filters which is more generic.&lt;/p&gt;
&lt;p&gt;In order to make ELMAH work with ASP.NET Web API, we need to create a new Attribute which implements IExceptionFilter interface. Since we have ExceptionFilterAttribute (which implements the IExceptionFilter interface) available at the framework, we will derived from that class instead. Here is the whole implementation:&lt;/p&gt;
&lt;div class="code-wrapper border-shadow-1"&gt;
&lt;div style="background-color: white; color: black;"&gt;
&lt;pre&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;class&lt;/span&gt; ElmahErrorAttribute : 
    System.Web.Http.Filters.ExceptionFilterAttribute {

    &lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;override&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; OnException(
        System.Web.Http.Filters.HttpActionExecutedContext actionExecutedContext) {

        &lt;span style="color: blue;"&gt;if&lt;/span&gt;(actionExecutedContext.Exception != &lt;span style="color: blue;"&gt;null&lt;/span&gt;)
            Elmah.ErrorSignal.FromCurrentContext().Raise(actionExecutedContext.Exception);

        &lt;span style="color: blue;"&gt;base&lt;/span&gt;.OnException(actionExecutedContext);
    }
}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Now we have our attribute, we need to tell the framework to use it. It is very straight forward as well. Here is how my Global.asax (Global.asax.cs) looks like:&lt;/p&gt;
&lt;div class="code-wrapper border-shadow-1"&gt;
&lt;div style="background-color: white; color: black;"&gt;
&lt;pre&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;class&lt;/span&gt; WebApiApplication : System.Web.HttpApplication {

    &lt;span style="color: blue;"&gt;protected&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; Application_Start() {

        Configure(
            System.Web.Http.GlobalConfiguration.Configuration
        );
    }

    &lt;span style="color: blue;"&gt;private&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; Configure(HttpConfiguration httpConfiguration) {

        httpConfiguration.Filters.Add(
            &lt;span style="color: blue;"&gt;new&lt;/span&gt; ElmahErrorAttribute()
        );

        httpConfiguration.Routes.MapHttpRoute(
            name: &lt;span style="color: #a31515;"&gt;"DefaultApi"&lt;/span&gt;,
            routeTemplate: &lt;span style="color: #a31515;"&gt;"api/{controller}/{id}"&lt;/span&gt;,
            defaults: &lt;span style="color: blue;"&gt;new&lt;/span&gt; { id = RouteParameter.Optional }
        );
    }
}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;This will make Elmah log the errors. Now, you can configure ELMAH to send you an e-mail when an error occurred or you can log the error inside an XML file, SQL Server Database, wherever you what.&lt;/p&gt;
&lt;p&gt;One more thing to mention about is that when you hit an error, the response will carry the exception details at the body of the response if you run your application locally. You can configure this option as well with the following configuration:&lt;/p&gt;
&lt;div class="code-wrapper border-shadow-1"&gt;
&lt;div style="background-color: white; color: black;"&gt;
&lt;pre&gt;httpConfiguration.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Never;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;</a10:content><feedburner:origLink>http://tugberkugurlu.com/85</feedburner:origLink></item><item><guid isPermaLink="false">http://tugberkugurlu.com/83</guid><link>http://feedproxy.google.com/~r/TugberkUgurlu/~3/13Yuz0RofT0/83</link><title>My Story of Seeing a Company Which Has High Level Cheapness At Their Core</title><description>&lt;p&gt;&lt;img src="http://www.tugberkugurlu.com/Content/Images/UploadedByAuthors/dscn4243-400x300.jpg" width="200" height="150" style="float: left; margin: 0 15px 15px 0;" /&gt;Yesterday, I went to &lt;strong&gt;fast food restaurant&lt;/strong&gt; to have lunch after a wonderful movie (in Aydin, Turkey). As you expected, I was starving and try to figure out how to &lt;strong&gt;&lt;em&gt;have it my way&lt;/em&gt;&lt;/strong&gt; there. At the end I decided to have a burger menu.&lt;/p&gt;
&lt;p&gt;Just after I ordered, I nicely asked one more tiny&amp;nbsp;&lt;span&gt;mayonnaise and ketchup and the response that I got was unreasonably shocking and stupid. In a programmers brain, it was a HTTP&amp;nbsp;&lt;a href="http://en.wikipedia.org/wiki/List_of_HTTP_status_codes#509" title="http://en.wikipedia.org/wiki/List_of_HTTP_status_codes#509" target="_blank"&gt;509 Bandwidth Limit Exceeded&lt;/a&gt;. The reason why this is stupid is not that they asked me extra money for that, because I have been going their restaurants pretty often and they responded nicely and tried to make my dreams come true if I request the same thing. Not to mention their stuff told me that this is the rule that they have been applying for nearly two years. That's lie and a big one.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;In the middle of my day, t&lt;/span&gt;his really hit me and&lt;em&gt; I thought that this was the lowest level a company can go down&lt;/em&gt;. This shows how poor and cheap their service is and that they have no idea on how to train their stuff so that they behave the same way. I am not judging them to ask extra money for these little boxes of souce, this is their cheapness in my opinion unless a person abuses this. I mean it is reasonable to reject the request of the person if s/he asked for 100 boxes of&amp;nbsp;mayonnaise and ketchup but for one more, this is the high level of cheapness my dear friend.&lt;/p&gt;
&lt;p&gt;&lt;span&gt;The funny part is that they told me I could have one special souce since I ordered Chicken Tenders along with the burgers. That sentence really made my day. I told them to keep it as my donation to their poor company so that the company can grow more and get rid of this cheapness.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;I am writing this as a customer complaint and you, that restaurant, should take this into considiration.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/iTIlRjGVaQ4VyeroqTAVVxI-D1g/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/iTIlRjGVaQ4VyeroqTAVVxI-D1g/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/iTIlRjGVaQ4VyeroqTAVVxI-D1g/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/iTIlRjGVaQ4VyeroqTAVVxI-D1g/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/TugberkUgurlu/~4/13Yuz0RofT0" height="1" width="1"/&gt;</description><pubDate>Sat, 18 Feb 2012 09:35:04 Z</pubDate><a10:updated>2012-02-18T09:35:04Z</a10:updated><a10:content type="text">&lt;p&gt;&lt;img src="http://www.tugberkugurlu.com/Content/Images/UploadedByAuthors/dscn4243-400x300.jpg" width="200" height="150" style="float: left; margin: 0 15px 15px 0;" /&gt;Yesterday, I went to &lt;strong&gt;fast food restaurant&lt;/strong&gt; to have lunch after a wonderful movie (in Aydin, Turkey). As you expected, I was starving and try to figure out how to &lt;strong&gt;&lt;em&gt;have it my way&lt;/em&gt;&lt;/strong&gt; there. At the end I decided to have a burger menu.&lt;/p&gt;
&lt;p&gt;Just after I ordered, I nicely asked one more tiny&amp;nbsp;&lt;span&gt;mayonnaise and ketchup and the response that I got was unreasonably shocking and stupid. In a programmers brain, it was a HTTP&amp;nbsp;&lt;a href="http://en.wikipedia.org/wiki/List_of_HTTP_status_codes#509" title="http://en.wikipedia.org/wiki/List_of_HTTP_status_codes#509" target="_blank"&gt;509 Bandwidth Limit Exceeded&lt;/a&gt;. The reason why this is stupid is not that they asked me extra money for that, because I have been going their restaurants pretty often and they responded nicely and tried to make my dreams come true if I request the same thing. Not to mention their stuff told me that this is the rule that they have been applying for nearly two years. That's lie and a big one.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;In the middle of my day, t&lt;/span&gt;his really hit me and&lt;em&gt; I thought that this was the lowest level a company can go down&lt;/em&gt;. This shows how poor and cheap their service is and that they have no idea on how to train their stuff so that they behave the same way. I am not judging them to ask extra money for these little boxes of souce, this is their cheapness in my opinion unless a person abuses this. I mean it is reasonable to reject the request of the person if s/he asked for 100 boxes of&amp;nbsp;mayonnaise and ketchup but for one more, this is the high level of cheapness my dear friend.&lt;/p&gt;
&lt;p&gt;&lt;span&gt;The funny part is that they told me I could have one special souce since I ordered Chicken Tenders along with the burgers. That sentence really made my day. I told them to keep it as my donation to their poor company so that the company can grow more and get rid of this cheapness.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;I am writing this as a customer complaint and you, that restaurant, should take this into considiration.&lt;/span&gt;&lt;/p&gt;</a10:content><feedburner:origLink>http://tugberkugurlu.com/83</feedburner:origLink></item><item><guid isPermaLink="false">http://tugberkugurlu.com/82</guid><link>http://feedproxy.google.com/~r/TugberkUgurlu/~3/hify_uafV3U/82</link><title>Windows Azure, SSL, Self-Signed Certificate and Annoying HTTPS Input Endpoint Does Not Contain Private Key Error</title><description>&lt;p&gt;While I was trying out the &lt;a target="_blank" href="http://www.windowsazure.com/" title="http://www.windowsazure.com"&gt;Windows Azure&lt;/a&gt;&amp;nbsp;features  yesterday, I had a deployment problem. The case was to deploying SSL enabled web role. Let&amp;rsquo;s walk  through the steps I have taken.&lt;/p&gt;
&lt;p&gt;Since it was a try out, I decided to create a self-signed certificate instead  of buying one. The case of how I created the self-signed certificate was fairly  simple. I opened up the Visual Studio Command Prompt (2010) and &lt;em&gt;cd&lt;/em&gt; to directory path where I would like to put the certificate file I was about to create. Then, I used the following &lt;a href="http://msdn.microsoft.com/en-us/library/bfsktky3(v=vs.80).aspx" title="http://msdn.microsoft.com/en-us/library/bfsktky3(v=vs.80).aspx" target="_blank"&gt;makecert&lt;/a&gt;&amp;nbsp;command line utility to create the certificate. Here is the code I used:&lt;/p&gt;
&lt;div class="code-wrapper border-shadow-1"&gt;
&lt;div style="background-color: white; color: black;"&gt;
&lt;pre&gt;makecert &lt;span style="color: gray;"&gt;-&lt;/span&gt;sky exchange &lt;span style="color: gray;"&gt;-&lt;/span&gt;r &lt;span style="color: gray;"&gt;-&lt;/span&gt;n &lt;span style="color: #a31515;"&gt;"CN=TugberkUgurlu.Com"&lt;/span&gt; &lt;span style="color: gray;"&gt;-&lt;/span&gt;pe &lt;span style="color: gray;"&gt;-&lt;/span&gt;a sha1 &lt;span style="color: gray;"&gt;-le&lt;/span&gt;n 2048 &lt;span style="color: gray;"&gt;-&lt;/span&gt;ss My &lt;span style="color: #a31515;"&gt;"Azure.TugberkUgurlu.Com.cer"&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;I had my .cer file under &lt;strong&gt;c:\Azure\Certs&lt;/strong&gt; directory. Also, I executed the following script on PowerShell console and I saw the certificate listed there.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;div class="code-wrapper border-shadow-1"&gt;
&lt;div style="background-color: white; color: black;"&gt;
&lt;pre&gt;dir cert:\CurrentUser\My&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;In order to upload the certificate to windows azure, I needed to export the certificate from &lt;em&gt;.cer&lt;/em&gt; file to &lt;em&gt;.pfx&lt;/em&gt;. Here where things get messy. I used the followig powershell script to create one but I was about to realize that it was the wrong decision. We'll see why in a minute.&lt;/p&gt;
&lt;div class="code-wrapper border-shadow-1"&gt;
&lt;div style="background-color: white; color: black;"&gt;
&lt;pre&gt;&lt;span style="color: orangered;"&gt;$c&lt;/span&gt; &lt;span style="color: gray;"&gt;=&lt;/span&gt; New&lt;span style="color: gray;"&gt;-&lt;/span&gt;Object System.Security.Cryptography.X509Certificates.X509Certificate2(&lt;span style="color: #a31515;"&gt;"c:\azure\certs\Azure.TugberkUgurlu.Com.cer"&lt;/span&gt;)
&lt;span style="color: orangered;"&gt;$bytes&lt;/span&gt; &lt;span style="color: gray;"&gt;=&lt;/span&gt; &lt;span style="color: orangered;"&gt;$c&lt;/span&gt;.Export(&lt;span style="color: #a31515;"&gt;"Pfx"&lt;/span&gt;,&lt;span style="color: #a31515;"&gt;"password"&lt;/span&gt;)
&lt;span style="color: gray;"&gt;[&lt;/span&gt;&lt;span style="color: teal;"&gt;System.IO.File&lt;/span&gt;&lt;span style="color: gray;"&gt;]&lt;/span&gt;&lt;span style="color: gray;"&gt;::&lt;/span&gt;WriteAllBytes(&lt;span style="color: #a31515;"&gt;"c:\azure\certs\Azure.TugberkUgurlu.Com.pfx"&lt;/span&gt;, &lt;span style="color: orangered;"&gt;$bytes&lt;/span&gt;)
&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;img src="http://www.tugberkugurlu.com/Content/Images/UploadedByAuthors/azure_certs.PNG" width="852" height="175" alt=".cer and .pfx certificates" /&gt;&lt;/p&gt;
&lt;p&gt;The next step was to deploy this .pfx file to Certificates store of my windows azure hosted service. In order to complate this challange, I went to Windows Azure portal, navigated to my hosted service. Right click on the blue &lt;strong&gt;Certificate &lt;/strong&gt;folder (I think it is a folder icon but not sure exactly what it is) and click &lt;strong&gt;Add Certificate&lt;/strong&gt;:&lt;/p&gt;
&lt;p&gt;&lt;img src="http://www.tugberkugurlu.com/Content/Images/UploadedByAuthors/azure_hosted_service.PNG" width="838" height="209" alt="Azure Hosted Service, Add Certificate" /&gt;&lt;/p&gt;
&lt;p&gt;It poped up a dialog box for me to upload that certificate file. I completed the steps and there it was. I had the certificate deployed on my hosted service.&lt;/p&gt;
&lt;p&gt;Finally, I was done setting things up and I can jump right to my application. Wait, I wasn't done yet complately! I had to set things up at the application level so that I could hook it up to that certificate I had just uploded.&lt;/p&gt;
&lt;p&gt;At that stage, first thing I did was to grab the thumbprint of the certificate. I ran the following PowerShell command to grab the thumbprint of the certificate:&lt;/p&gt;
&lt;div class="code-wrapper border-shadow-1"&gt;
&lt;div style="background-color: white; color: black;"&gt;
&lt;pre&gt;(New&lt;span style="color: gray;"&gt;-&lt;/span&gt;Object System.Security.Cryptography.X509Certificates.X509Certificate2(&lt;span style="color: #a31515;"&gt;"c:\azure\certs\Azure.TugberkUgurlu.Com.cer"&lt;/span&gt;)).Thumbprint
&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;With that thumbprint, I went to my project and added the following code inside ServiceConfiguration.Clound.cscfg and ServiceConfiguration.Local.cscfg under &lt;strong&gt;Role &lt;/strong&gt;node:&lt;/p&gt;
&lt;div class="code-wrapper border-shadow-1"&gt;
&lt;div style="background-color: white; color: black;"&gt;
&lt;pre&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;Certificates&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;
  &lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;Certificate&lt;/span&gt; &lt;span style="color: red;"&gt;name&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt;&lt;span style="color: blue;"&gt;Azure.TugberkUgurlu.Com&lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt; &lt;span style="color: red;"&gt;thumbprint&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt;&lt;span style="color: blue;"&gt;AAD5DDD0CA9B4D3CFEF1652130142020770B8BDF&lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt; &lt;span style="color: red;"&gt;thumbprintAlgorithm&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt;&lt;span style="color: blue;"&gt;sha1&lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt;&lt;span style="color: blue;"&gt;/&amp;gt;&lt;/span&gt;
&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515;"&gt;Certificates&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;The ServiceDefinition.csdef file needed a little more touch than the configuration files. Here is the complate csdef file after the set-up:&lt;/p&gt;
&lt;div class="code-wrapper border-shadow-1"&gt;
&lt;div style="background-color: white; color: black;"&gt;
&lt;pre&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;ServiceDefinition&lt;/span&gt; &lt;span style="color: red;"&gt;name&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt;&lt;span style="color: blue;"&gt;HttpsEnabledCloudProject&lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt; &lt;span style="color: red;"&gt;xmlns&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt;&lt;span style="color: blue;"&gt;http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceDefinition&lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;
  &lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;WebRole&lt;/span&gt; &lt;span style="color: red;"&gt;name&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt;&lt;span style="color: blue;"&gt;MvcWebRole1&lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt; &lt;span style="color: red;"&gt;vmsize&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt;&lt;span style="color: blue;"&gt;Small&lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;
    &lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;Sites&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;
      &lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;Site&lt;/span&gt; &lt;span style="color: red;"&gt;name&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt;&lt;span style="color: blue;"&gt;Web&lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;
        &lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;Bindings&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;
          &lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;Binding&lt;/span&gt; &lt;span style="color: red;"&gt;name&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt;&lt;span style="color: blue;"&gt;Endpoint1&lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt; &lt;span style="color: red;"&gt;endpointName&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt;&lt;span style="color: blue;"&gt;Endpoint1&lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt; &lt;span style="color: red;"&gt;hostHeader&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt;&lt;span style="color: blue;"&gt;azure.tugberkugurlu.com&lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt; &lt;span style="color: blue;"&gt;/&amp;gt;&lt;/span&gt;
          &lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;Binding&lt;/span&gt; &lt;span style="color: red;"&gt;name&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt;&lt;span style="color: blue;"&gt;HttpsIn&lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt; &lt;span style="color: red;"&gt;endpointName&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt;&lt;span style="color: blue;"&gt;HttpsIn&lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt; &lt;span style="color: red;"&gt;hostHeader&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt;&lt;span style="color: blue;"&gt;azure.tugberkugurlu.com&lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt; &lt;span style="color: blue;"&gt;/&amp;gt;&lt;/span&gt;
        &lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515;"&gt;Bindings&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;
      &lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515;"&gt;Site&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;
    &lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515;"&gt;Sites&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;
    &lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;Endpoints&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;
      &amp;lt;InputEndpoint name="Endpoint1" protocol="http" port="80" /&amp;gt;
      &amp;lt;InputEndpoint name="HttpsIn" protocol="https" port="443" certificate="Azure.TugberkUgurlu.Com" /&amp;gt;
    &lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515;"&gt;Endpoints&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;
    &amp;lt;Imports&amp;gt;
      &amp;lt;Import moduleName="Diagnostics" /&amp;gt;
    &amp;lt;/Imports&amp;gt;
    &lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;Certificates&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;
      &lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;Certificate&lt;/span&gt; &lt;span style="color: red;"&gt;name&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt;&lt;span style="color: blue;"&gt;Azure.TugberkUgurlu.Com&lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt; &lt;span style="color: red;"&gt;storeLocation&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt;&lt;span style="color: blue;"&gt;LocalMachine&lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt; &lt;span style="color: red;"&gt;storeName&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt;&lt;span style="color: blue;"&gt;My&lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt; &lt;span style="color: blue;"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515;"&gt;Certificates&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;
  &lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515;"&gt;WebRole&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;
&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515;"&gt;ServiceDefinition&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Everything looked right at that point but got an ugly error message telling me that my deployment had been failed:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;HTTP Status Code: 400. Error Message: Certificate with thumbprint AAD5DDD0CA9B4D3CFEF1652130142020770B8BDF associated with HTTPS input endpoint HttpsIn does not contain private key&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;img src="http://www.tugberkugurlu.com/Content/Images/UploadedByAuthors/windows_azure_vs_deployment_error.PNG" width="743" height="316" alt="Windows Azure Visual Studio Deployment Error" /&gt;&lt;/p&gt;
&lt;p&gt;After a couple of searched on the internet, I ended up checking the private key of my certificate and here is the result:&lt;/p&gt;
&lt;p&gt;&lt;img src="http://www.tugberkugurlu.com/Content/Images/UploadedByAuthors/ps_cert_hasprivatekey.PNG" width="681" height="398" alt="$cert.HasPrivateKey" /&gt;&lt;/p&gt;
&lt;p&gt;That looked awkward and might be the problem. Then I grabed the certificate from the certificate store and check the private key. That was the evidence that it was the problem:&lt;/p&gt;
&lt;p&gt;&lt;img src="http://www.tugberkugurlu.com/Content/Images/UploadedByAuthors/ps_cert_hasprivatekey_2.PNG" width="743" height="314" alt="$cert.HasPrivateKey" /&gt;&lt;/p&gt;
&lt;p&gt;At that point, I created the .pfx file from the certificate in my certificate store with following code:&lt;/p&gt;
&lt;div class="code-wrapper border-shadow-1"&gt;
&lt;div style="background-color: white; color: black;"&gt;
&lt;pre&gt;&lt;span style="color: orangered;"&gt;$cert&lt;/span&gt; &lt;span style="color: gray;"&gt;=&lt;/span&gt; (get&lt;span style="color: gray;"&gt;-&lt;/span&gt;item cert:\CurrentUser\My\AAD5DDD0CA9B4D3CFEF1652130142020770B8BDF)
&lt;span style="color: orangered;"&gt;$bytes&lt;/span&gt; &lt;span style="color: gray;"&gt;=&lt;/span&gt; &lt;span style="color: orangered;"&gt;$cert&lt;/span&gt;.Export(&lt;span style="color: #a31515;"&gt;"Pfx"&lt;/span&gt;,&lt;span style="color: #a31515;"&gt;"password"&lt;/span&gt;)
&lt;span style="color: gray;"&gt;[&lt;/span&gt;&lt;span style="color: teal;"&gt;System.IO.File&lt;/span&gt;&lt;span style="color: gray;"&gt;]&lt;/span&gt;&lt;span style="color: gray;"&gt;::&lt;/span&gt;WriteAllBytes(&lt;span style="color: #a31515;"&gt;"c:\azure\certs\Azure.TugberkUgurlu.Com_2.pfx"&lt;/span&gt;, &lt;span style="color: orangered;"&gt;$bytes&lt;/span&gt;)
&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;At last, I deleted the certificate which was under my hosted service and reuploaded the new one I had just created.&lt;/p&gt;
&lt;p&gt;Lastly, I ran the publish process again without changing anything inside my code, configuration or service definition files and I suceedded!&lt;/p&gt;
&lt;p&gt;&lt;img src="http://www.tugberkugurlu.com/Content/Images/UploadedByAuthors/windows_azure_vs_deployment.PNG" width="743" height="318" alt="Windows Azure Visual Studio Deployment" /&gt;&lt;/p&gt;
&lt;p&gt;Here is the SSL enabled application running in the cloud:&lt;/p&gt;
&lt;p&gt;&lt;img src="http://www.tugberkugurlu.com/Content/Images/UploadedByAuthors/azure_ssl_app.PNG" width="686" height="429" alt="Windows Azure SSL Enable Application" /&gt;&lt;/p&gt;
&lt;p&gt;Please comment if you have the same problem as me so that I won't feel lonely in this small World:)&lt;/p&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/MlWCsWk0FvgWdGBOVbwU7hiSheY/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/MlWCsWk0FvgWdGBOVbwU7hiSheY/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/MlWCsWk0FvgWdGBOVbwU7hiSheY/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/MlWCsWk0FvgWdGBOVbwU7hiSheY/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/TugberkUgurlu/~4/hify_uafV3U" height="1" width="1"/&gt;</description><pubDate>Fri, 03 Feb 2012 22:23:08 Z</pubDate><a10:updated>2012-02-03T22:23:08Z</a10:updated><a10:content type="text">&lt;p&gt;While I was trying out the &lt;a target="_blank" href="http://www.windowsazure.com/" title="http://www.windowsazure.com"&gt;Windows Azure&lt;/a&gt;&amp;nbsp;features  yesterday, I had a deployment problem. The case was to deploying SSL enabled web role. Let&amp;rsquo;s walk  through the steps I have taken.&lt;/p&gt;
&lt;p&gt;Since it was a try out, I decided to create a self-signed certificate instead  of buying one. The case of how I created the self-signed certificate was fairly  simple. I opened up the Visual Studio Command Prompt (2010) and &lt;em&gt;cd&lt;/em&gt; to directory path where I would like to put the certificate file I was about to create. Then, I used the following &lt;a href="http://msdn.microsoft.com/en-us/library/bfsktky3(v=vs.80).aspx" title="http://msdn.microsoft.com/en-us/library/bfsktky3(v=vs.80).aspx" target="_blank"&gt;makecert&lt;/a&gt;&amp;nbsp;command line utility to create the certificate. Here is the code I used:&lt;/p&gt;
&lt;div class="code-wrapper border-shadow-1"&gt;
&lt;div style="background-color: white; color: black;"&gt;
&lt;pre&gt;makecert &lt;span style="color: gray;"&gt;-&lt;/span&gt;sky exchange &lt;span style="color: gray;"&gt;-&lt;/span&gt;r &lt;span style="color: gray;"&gt;-&lt;/span&gt;n &lt;span style="color: #a31515;"&gt;"CN=TugberkUgurlu.Com"&lt;/span&gt; &lt;span style="color: gray;"&gt;-&lt;/span&gt;pe &lt;span style="color: gray;"&gt;-&lt;/span&gt;a sha1 &lt;span style="color: gray;"&gt;-le&lt;/span&gt;n 2048 &lt;span style="color: gray;"&gt;-&lt;/span&gt;ss My &lt;span style="color: #a31515;"&gt;"Azure.TugberkUgurlu.Com.cer"&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;I had my .cer file under &lt;strong&gt;c:\Azure\Certs&lt;/strong&gt; directory. Also, I executed the following script on PowerShell console and I saw the certificate listed there.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;div class="code-wrapper border-shadow-1"&gt;
&lt;div style="background-color: white; color: black;"&gt;
&lt;pre&gt;dir cert:\CurrentUser\My&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;In order to upload the certificate to windows azure, I needed to export the certificate from &lt;em&gt;.cer&lt;/em&gt; file to &lt;em&gt;.pfx&lt;/em&gt;. Here where things get messy. I used the followig powershell script to create one but I was about to realize that it was the wrong decision. We'll see why in a minute.&lt;/p&gt;
&lt;div class="code-wrapper border-shadow-1"&gt;
&lt;div style="background-color: white; color: black;"&gt;
&lt;pre&gt;&lt;span style="color: orangered;"&gt;$c&lt;/span&gt; &lt;span style="color: gray;"&gt;=&lt;/span&gt; New&lt;span style="color: gray;"&gt;-&lt;/span&gt;Object System.Security.Cryptography.X509Certificates.X509Certificate2(&lt;span style="color: #a31515;"&gt;"c:\azure\certs\Azure.TugberkUgurlu.Com.cer"&lt;/span&gt;)
&lt;span style="color: orangered;"&gt;$bytes&lt;/span&gt; &lt;span style="color: gray;"&gt;=&lt;/span&gt; &lt;span style="color: orangered;"&gt;$c&lt;/span&gt;.Export(&lt;span style="color: #a31515;"&gt;"Pfx"&lt;/span&gt;,&lt;span style="color: #a31515;"&gt;"password"&lt;/span&gt;)
&lt;span style="color: gray;"&gt;[&lt;/span&gt;&lt;span style="color: teal;"&gt;System.IO.File&lt;/span&gt;&lt;span style="color: gray;"&gt;]&lt;/span&gt;&lt;span style="color: gray;"&gt;::&lt;/span&gt;WriteAllBytes(&lt;span style="color: #a31515;"&gt;"c:\azure\certs\Azure.TugberkUgurlu.Com.pfx"&lt;/span&gt;, &lt;span style="color: orangered;"&gt;$bytes&lt;/span&gt;)
&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;img src="http://www.tugberkugurlu.com/Content/Images/UploadedByAuthors/azure_certs.PNG" width="852" height="175" alt=".cer and .pfx certificates" /&gt;&lt;/p&gt;
&lt;p&gt;The next step was to deploy this .pfx file to Certificates store of my windows azure hosted service. In order to complate this challange, I went to Windows Azure portal, navigated to my hosted service. Right click on the blue &lt;strong&gt;Certificate &lt;/strong&gt;folder (I think it is a folder icon but not sure exactly what it is) and click &lt;strong&gt;Add Certificate&lt;/strong&gt;:&lt;/p&gt;
&lt;p&gt;&lt;img src="http://www.tugberkugurlu.com/Content/Images/UploadedByAuthors/azure_hosted_service.PNG" width="838" height="209" alt="Azure Hosted Service, Add Certificate" /&gt;&lt;/p&gt;
&lt;p&gt;It poped up a dialog box for me to upload that certificate file. I completed the steps and there it was. I had the certificate deployed on my hosted service.&lt;/p&gt;
&lt;p&gt;Finally, I was done setting things up and I can jump right to my application. Wait, I wasn't done yet complately! I had to set things up at the application level so that I could hook it up to that certificate I had just uploded.&lt;/p&gt;
&lt;p&gt;At that stage, first thing I did was to grab the thumbprint of the certificate. I ran the following PowerShell command to grab the thumbprint of the certificate:&lt;/p&gt;
&lt;div class="code-wrapper border-shadow-1"&gt;
&lt;div style="background-color: white; color: black;"&gt;
&lt;pre&gt;(New&lt;span style="color: gray;"&gt;-&lt;/span&gt;Object System.Security.Cryptography.X509Certificates.X509Certificate2(&lt;span style="color: #a31515;"&gt;"c:\azure\certs\Azure.TugberkUgurlu.Com.cer"&lt;/span&gt;)).Thumbprint
&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;With that thumbprint, I went to my project and added the following code inside ServiceConfiguration.Clound.cscfg and ServiceConfiguration.Local.cscfg under &lt;strong&gt;Role &lt;/strong&gt;node:&lt;/p&gt;
&lt;div class="code-wrapper border-shadow-1"&gt;
&lt;div style="background-color: white; color: black;"&gt;
&lt;pre&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;Certificates&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;
  &lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;Certificate&lt;/span&gt; &lt;span style="color: red;"&gt;name&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt;&lt;span style="color: blue;"&gt;Azure.TugberkUgurlu.Com&lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt; &lt;span style="color: red;"&gt;thumbprint&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt;&lt;span style="color: blue;"&gt;AAD5DDD0CA9B4D3CFEF1652130142020770B8BDF&lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt; &lt;span style="color: red;"&gt;thumbprintAlgorithm&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt;&lt;span style="color: blue;"&gt;sha1&lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt;&lt;span style="color: blue;"&gt;/&amp;gt;&lt;/span&gt;
&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515;"&gt;Certificates&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;The ServiceDefinition.csdef file needed a little more touch than the configuration files. Here is the complate csdef file after the set-up:&lt;/p&gt;
&lt;div class="code-wrapper border-shadow-1"&gt;
&lt;div style="background-color: white; color: black;"&gt;
&lt;pre&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;ServiceDefinition&lt;/span&gt; &lt;span style="color: red;"&gt;name&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt;&lt;span style="color: blue;"&gt;HttpsEnabledCloudProject&lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt; &lt;span style="color: red;"&gt;xmlns&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt;&lt;span style="color: blue;"&gt;http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceDefinition&lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;
  &lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;WebRole&lt;/span&gt; &lt;span style="color: red;"&gt;name&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt;&lt;span style="color: blue;"&gt;MvcWebRole1&lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt; &lt;span style="color: red;"&gt;vmsize&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt;&lt;span style="color: blue;"&gt;Small&lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;
    &lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;Sites&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;
      &lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;Site&lt;/span&gt; &lt;span style="color: red;"&gt;name&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt;&lt;span style="color: blue;"&gt;Web&lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;
        &lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;Bindings&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;
          &lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;Binding&lt;/span&gt; &lt;span style="color: red;"&gt;name&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt;&lt;span style="color: blue;"&gt;Endpoint1&lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt; &lt;span style="color: red;"&gt;endpointName&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt;&lt;span style="color: blue;"&gt;Endpoint1&lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt; &lt;span style="color: red;"&gt;hostHeader&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt;&lt;span style="color: blue;"&gt;azure.tugberkugurlu.com&lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt; &lt;span style="color: blue;"&gt;/&amp;gt;&lt;/span&gt;
          &lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;Binding&lt;/span&gt; &lt;span style="color: red;"&gt;name&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt;&lt;span style="color: blue;"&gt;HttpsIn&lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt; &lt;span style="color: red;"&gt;endpointName&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt;&lt;span style="color: blue;"&gt;HttpsIn&lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt; &lt;span style="color: red;"&gt;hostHeader&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt;&lt;span style="color: blue;"&gt;azure.tugberkugurlu.com&lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt; &lt;span style="color: blue;"&gt;/&amp;gt;&lt;/span&gt;
        &lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515;"&gt;Bindings&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;
      &lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515;"&gt;Site&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;
    &lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515;"&gt;Sites&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;
    &lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;Endpoints&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;
      &amp;lt;InputEndpoint name="Endpoint1" protocol="http" port="80" /&amp;gt;
      &amp;lt;InputEndpoint name="HttpsIn" protocol="https" port="443" certificate="Azure.TugberkUgurlu.Com" /&amp;gt;
    &lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515;"&gt;Endpoints&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;
    &amp;lt;Imports&amp;gt;
      &amp;lt;Import moduleName="Diagnostics" /&amp;gt;
    &amp;lt;/Imports&amp;gt;
    &lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;Certificates&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;
      &lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;Certificate&lt;/span&gt; &lt;span style="color: red;"&gt;name&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt;&lt;span style="color: blue;"&gt;Azure.TugberkUgurlu.Com&lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt; &lt;span style="color: red;"&gt;storeLocation&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt;&lt;span style="color: blue;"&gt;LocalMachine&lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt; &lt;span style="color: red;"&gt;storeName&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt;&lt;span style="color: blue;"&gt;My&lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt; &lt;span style="color: blue;"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515;"&gt;Certificates&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;
  &lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515;"&gt;WebRole&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;
&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515;"&gt;ServiceDefinition&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Everything looked right at that point but got an ugly error message telling me that my deployment had been failed:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;HTTP Status Code: 400. Error Message: Certificate with thumbprint AAD5DDD0CA9B4D3CFEF1652130142020770B8BDF associated with HTTPS input endpoint HttpsIn does not contain private key&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;img src="http://www.tugberkugurlu.com/Content/Images/UploadedByAuthors/windows_azure_vs_deployment_error.PNG" width="743" height="316" alt="Windows Azure Visual Studio Deployment Error" /&gt;&lt;/p&gt;
&lt;p&gt;After a couple of searched on the internet, I ended up checking the private key of my certificate and here is the result:&lt;/p&gt;
&lt;p&gt;&lt;img src="http://www.tugberkugurlu.com/Content/Images/UploadedByAuthors/ps_cert_hasprivatekey.PNG" width="681" height="398" alt="$cert.HasPrivateKey" /&gt;&lt;/p&gt;
&lt;p&gt;That looked awkward and might be the problem. Then I grabed the certificate from the certificate store and check the private key. That was the evidence that it was the problem:&lt;/p&gt;
&lt;p&gt;&lt;img src="http://www.tugberkugurlu.com/Content/Images/UploadedByAuthors/ps_cert_hasprivatekey_2.PNG" width="743" height="314" alt="$cert.HasPrivateKey" /&gt;&lt;/p&gt;
&lt;p&gt;At that point, I created the .pfx file from the certificate in my certificate store with following code:&lt;/p&gt;
&lt;div class="code-wrapper border-shadow-1"&gt;
&lt;div style="background-color: white; color: black;"&gt;
&lt;pre&gt;&lt;span style="color: orangered;"&gt;$cert&lt;/span&gt; &lt;span style="color: gray;"&gt;=&lt;/span&gt; (get&lt;span style="color: gray;"&gt;-&lt;/span&gt;item cert:\CurrentUser\My\AAD5DDD0CA9B4D3CFEF1652130142020770B8BDF)
&lt;span style="color: orangered;"&gt;$bytes&lt;/span&gt; &lt;span style="color: gray;"&gt;=&lt;/span&gt; &lt;span style="color: orangered;"&gt;$cert&lt;/span&gt;.Export(&lt;span style="color: #a31515;"&gt;"Pfx"&lt;/span&gt;,&lt;span style="color: #a31515;"&gt;"password"&lt;/span&gt;)
&lt;span style="color: gray;"&gt;[&lt;/span&gt;&lt;span style="color: teal;"&gt;System.IO.File&lt;/span&gt;&lt;span style="color: gray;"&gt;]&lt;/span&gt;&lt;span style="color: gray;"&gt;::&lt;/span&gt;WriteAllBytes(&lt;span style="color: #a31515;"&gt;"c:\azure\certs\Azure.TugberkUgurlu.Com_2.pfx"&lt;/span&gt;, &lt;span style="color: orangered;"&gt;$bytes&lt;/span&gt;)
&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;At last, I deleted the certificate which was under my hosted service and reuploaded the new one I had just created.&lt;/p&gt;
&lt;p&gt;Lastly, I ran the publish process again without changing anything inside my code, configuration or service definition files and I suceedded!&lt;/p&gt;
&lt;p&gt;&lt;img src="http://www.tugberkugurlu.com/Content/Images/UploadedByAuthors/windows_azure_vs_deployment.PNG" width="743" height="318" alt="Windows Azure Visual Studio Deployment" /&gt;&lt;/p&gt;
&lt;p&gt;Here is the SSL enabled application running in the cloud:&lt;/p&gt;
&lt;p&gt;&lt;img src="http://www.tugberkugurlu.com/Content/Images/UploadedByAuthors/azure_ssl_app.PNG" width="686" height="429" alt="Windows Azure SSL Enable Application" /&gt;&lt;/p&gt;
&lt;p&gt;Please comment if you have the same problem as me so that I won't feel lonely in this small World:)&lt;/p&gt;</a10:content><feedburner:origLink>http://tugberkugurlu.com/82</feedburner:origLink></item><item><guid isPermaLink="false">http://tugberkugurlu.com/81</guid><link>http://feedproxy.google.com/~r/TugberkUgurlu/~3/UNFu6fkP2DQ/81</link><title>How to Use Windows Azure Blob Storage Service With ASP.NET MVC Web Application</title><description>&lt;p&gt;I have been digging into &lt;a target="_blank" href="http://www.windowsazure.com" title="http://www.windowsazure.com"&gt;Windows Azure&lt;/a&gt; more and more lately. I wish that it would be supported in Turkey but anyway, emulator is my cloud for now &lt;img src="http://www.tugberkugurlu.com/Content/Images/UploadedByAuthors/wlw/0310308e59b3_10C04/wlEmoticon-smile.png" alt="Smile" class="wlEmoticon wlEmoticon-smile" style="border-style: none;" /&gt; Yesterday, I thought I should note some of things down and told myself "There is no better place than your blog for that, my friend" and here I am.&lt;/p&gt;
&lt;p&gt;One feature of Windows Azure platform which I will be using is &lt;strong&gt;Blob Storage&lt;/strong&gt;. Blob storage enables you to store your unstructured data (like pictures, word docs, excel file, etc.) inside Windows Azure servers and access them over HTTP or HTTPS. &lt;a target="_blank" href="https://github.com/WindowsAzure/azure-sdk-for-net" title="https://github.com/WindowsAzure/azure-sdk-for-net"&gt;With Windows Azure .Net SDK&lt;/a&gt;, you have full control over your blobs and program against that easily. How? Let&amp;rsquo;s see.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Before starting, make sure that you have installed Windows Azure SDK for .Net which will bring down Windows Azure Tools for Microsoft Visual Studio and Windows Azure Client Libraries for .Net. You can find the information on how to install the SDK from &lt;a href="http://www.windowsazure.com/en-us/develop/net/"&gt;http://www.windowsazure.com/en-us/develop/net/&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;First thing is first. We need an ASP.NET MVC project to simulate this (but it doesn&amp;rsquo;t have to be ASP.NET MVC project). We have two options to make our application azure-cloudy:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Directly Create a Cloud Application&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Inside the new project dialog box on Visual Studio, choose Windows Azure Project as indicated below:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.tugberkugurlu.com/Content/Images/UploadedByAuthors/wlw/Windows-Azure-Blob-Storage-and-A.NET-MVC_FA59/image.png"&gt;&lt;img height="394" width="644" src="http://www.tugberkugurlu.com/Content/Images/UploadedByAuthors/wlw/Windows-Azure-Blob-Storage-and-A.NET-MVC_FA59/image.png" alt="image" border="0" title="image" style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;When you hit OK, you will see a dialog as below:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.tugberkugurlu.com/Content/Images/UploadedByAuthors/wlw/Windows-Azure-Blob-Storage-and-A.NET-MVC_FA59/image_3.png"&gt;&lt;img height="404" width="643" src="http://www.tugberkugurlu.com/Content/Images/UploadedByAuthors/wlw/Windows-Azure-Blob-Storage-and-A.NET-MVC_FA59/image_3.png" alt="image" border="0" title="image" style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;This dialog box is like a open buffet, you can choose which project you need for your application here. But we will choose ASP.NET MVC 3 Web Role and then hit OK:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.tugberkugurlu.com/Content/Images/UploadedByAuthors/wlw/Windows-Azure-Blob-Storage-and-A.NET-MVC_FA59/image_4.png"&gt;&lt;img height="404" width="643" src="http://www.tugberkugurlu.com/Content/Images/UploadedByAuthors/wlw/Windows-Azure-Blob-Storage-and-A.NET-MVC_FA59/image_4.png" alt="image" border="0" title="image" style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Then we will see a dialog box which is owned by ASP.NET MVC. From now on everything is same like it is a standard ASP.NET MVC project.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Make Your Application Azure-Cloudy Later&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Assuming that we have an existing ASP.NET MVC application and we want to run this application on Windows Azure. What we need to do is to right click on our project and choose &lt;em&gt;"Add Windows Azure Deployment Project Option" &lt;/em&gt;as below:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.tugberkugurlu.com/Content/Images/UploadedByAuthors/wlw/Windows-Azure-Blob-Storage-and-A.NET-MVC_FA59/image_5.png"&gt;&lt;img height="303" width="596" src="http://www.tugberkugurlu.com/Content/Images/UploadedByAuthors/wlw/Windows-Azure-Blob-Storage-and-A.NET-MVC_FA59/image_5.png" alt="image" border="0" title="image" style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Either way, our solution will look something like this:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.tugberkugurlu.com/Content/Images/UploadedByAuthors/wlw/Windows-Azure-Blob-Storage-and-A.NET-MVC_FA59/image_6.png"&gt;&lt;img height="431" width="324" src="http://www.tugberkugurlu.com/Content/Images/UploadedByAuthors/wlw/Windows-Azure-Blob-Storage-and-A.NET-MVC_FA59/image_6.png" alt="image" border="0" title="image" style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Configure to Work With Blob Storage&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;We have a few steps to make before we can start developing. In a real world scenario, you need a Windows Azure storage account to use the blob storage service and you can create this account from &lt;a href="http://windows.azure.com/"&gt;Windows Azure Management Portal&lt;/a&gt;. After you configure your account, you will have your access keys to that storage account which you will need on your development process.&lt;/p&gt;
&lt;p&gt;One thing to mention before going further is that you will be able to access your files through HTTP or HTTPS as motioned before and the URL for your blobs will look like this:&lt;/p&gt;
&lt;p&gt;&lt;em&gt;http://&amp;lt;storage account&amp;gt;.blob.core.windows.net/&amp;lt;container&amp;gt;/&amp;lt;blob&amp;gt;&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;There is a way to change this so that you can use your own domain. In our example, we will be reaching out our blobs through localhost. we will get to that later in this post.&lt;/p&gt;
&lt;p&gt;In order to develop locally with emulator, we do not need a storage account which means that we don&amp;rsquo;t need access keys. In order to configure it so, right click on the web role file and choose &lt;em&gt;Properties&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.tugberkugurlu.com/Content/Images/UploadedByAuthors/wlw/Windows-Azure-Blob-Storage-and-A.NET-MVC_FA59/image_7.png"&gt;&lt;img height="378" width="644" src="http://www.tugberkugurlu.com/Content/Images/UploadedByAuthors/wlw/Windows-Azure-Blob-Storage-and-A.NET-MVC_FA59/image_7.png" alt="image" border="0" title="image" style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;This action will brings up the properties windows. From there, go to &lt;em&gt;Settings&lt;/em&gt; tab and click add settings and on the new created node, select Connection String as Type and click "..." which stands right hand side.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.tugberkugurlu.com/Content/Images/UploadedByAuthors/wlw/Windows-Azure-Blob-Storage-and-A.NET-MVC_FA59/image_8.png"&gt;&lt;img height="378" width="644" src="http://www.tugberkugurlu.com/Content/Images/UploadedByAuthors/wlw/Windows-Azure-Blob-Storage-and-A.NET-MVC_FA59/image_8.png" alt="image" border="0" title="image" style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;This action will also open up a new window for you and this is where you configure your storage account. But we will choose "&lt;em&gt;Use the Windows Azure storage emulator&lt;/em&gt;" option and give this configuration a new friendly name:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.tugberkugurlu.com/Content/Images/UploadedByAuthors/wlw/Windows-Azure-Blob-Storage-and-A.NET-MVC_FA59/image_9.png"&gt;&lt;img height="351" width="644" src="http://www.tugberkugurlu.com/Content/Images/UploadedByAuthors/wlw/Windows-Azure-Blob-Storage-and-A.NET-MVC_FA59/image_9.png" alt="image" border="0" title="image" style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Those actions are not required but viewed as a best practice while working windows azure cloud projects. Also, you can use Windows Azure Storage and run your application inside your own servers. It is totally fine. So, in that case you won&amp;rsquo;t need a cloud project. What .Net SDK provides is a wrapper around Windows Azure REST APIs which makes it easy to program against.&lt;/p&gt;
&lt;p&gt;Now we are all set and finally we can write some code.&lt;/p&gt;
&lt;p&gt;First of all, we need two additional libraries to develop against Windows Azure with .Net:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Microsoft.WindowsAzure.StorageClient.dll &lt;/li&gt;
&lt;li&gt;Microsoft.WindowsAzure.ServiceRuntime.dll&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Those two will give us everything we need. For the sake of simplicity, I created a simple project which uploads images through Windows Azure Storage service and list those images on a page. In order to do that so, I created a service class (which is a standard class, nothing further than that) called &lt;strong&gt;MyBlobStorageService&lt;/strong&gt;. Let&amp;rsquo;s see the code first:&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;div class="code-wrapper border-shadow-1"&gt;
&lt;div style="background-color: white; color: black;"&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;pre&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;class&lt;/span&gt; MyBlobStorageService {

    &lt;span style="color: blue;"&gt;public&lt;/span&gt; CloudBlobContainer GetCloudBlobContainer() {

        &lt;span style="color: green;"&gt;// Retrieve storage account from connection-string&lt;/span&gt;
        CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
                RoleEnvironment.GetConfigurationSettingValue(&lt;span style="color: #a31515;"&gt;"StorageConnectionString"&lt;/span&gt;)
            );

        &lt;span style="color: green;"&gt;// Create the blob client &lt;/span&gt;
        CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

        &lt;span style="color: green;"&gt;// Retrieve a reference to a container &lt;/span&gt;
        CloudBlobContainer blobContainer = blobClient.GetContainerReference(&lt;span style="color: #a31515;"&gt;"albums"&lt;/span&gt;);

        &lt;span style="color: green;"&gt;// Create the container if it doesn't already exist&lt;/span&gt;
        &lt;span style="color: blue;"&gt;if&lt;/span&gt; (blobContainer.CreateIfNotExist()) {

            blobContainer.SetPermissions(
               &lt;span style="color: blue;"&gt;new&lt;/span&gt; BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Blob }
            );
        }

        &lt;span style="color: blue;"&gt;return&lt;/span&gt; blobContainer;
    }

}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;What this code does it fairly simple. It gets the storage account information form the connection string that we have configured and creates a &lt;strong&gt;CloudStorageAccount&lt;/strong&gt; class according to that. Then, We create a blob storage client (&lt;strong&gt;CloudBlobClient&lt;/strong&gt;) over that storage account. Finally, we create a container (&lt;strong&gt;CloudBlobContainer&lt;/strong&gt;) for our blobs and check if it exists of not. If not, then we simply create it and set the public access permission to it because we will store images inside that container and we want to display those images on our web page.&lt;/p&gt;
&lt;p&gt;In order to demonstrate this, I have two controller actions. One is for HTTP GET and the other is for HTTP POST. Here is the complete controller code:&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;div class="code-wrapper border-shadow-1"&gt;
&lt;div style="background-color: white; color: black;"&gt;
&lt;pre&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;class&lt;/span&gt; HomeController : Controller {

    &lt;span style="color: blue;"&gt;public&lt;/span&gt; MyBlobStorageService 
        _myBlobStorageService = &lt;span style="color: blue;"&gt;new&lt;/span&gt; MyBlobStorageService();

    &lt;span style="color: blue;"&gt;public&lt;/span&gt; ActionResult Index() {

        &lt;span style="color: green;"&gt;// Retrieve a reference to a container &lt;/span&gt;
        CloudBlobContainer blobContainer = 
            _myBlobStorageService.GetCloudBlobContainer();

        List&amp;lt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&amp;gt; blobs = &lt;span style="color: blue;"&gt;new&lt;/span&gt; List&amp;lt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&amp;gt;();

        &lt;span style="color: green;"&gt;// Loop over blobs within the container and output the URI to each of them&lt;/span&gt;
        &lt;span style="color: blue;"&gt;foreach&lt;/span&gt; (&lt;span style="color: blue;"&gt;var&lt;/span&gt; blobItem &lt;span style="color: blue;"&gt;in&lt;/span&gt; blobContainer.ListBlobs())
            blobs.Add(blobItem.Uri.ToString());

        &lt;span style="color: blue;"&gt;return&lt;/span&gt; View(blobs);
    }

    [HttpPost]
    [ActionName(&lt;span style="color: #a31515;"&gt;"index"&lt;/span&gt;)]
    &lt;span style="color: blue;"&gt;public&lt;/span&gt; ActionResult Index_post(HttpPostedFileBase fileBase) {

        &lt;span style="color: blue;"&gt;if&lt;/span&gt; (fileBase.ContentLength &amp;gt; 0) {

            &lt;span style="color: green;"&gt;// Retrieve a reference to a container &lt;/span&gt;
            CloudBlobContainer blobContainer = 
                _myBlobStorageService.GetCloudBlobContainer();

            CloudBlob blob = 
                blobContainer.GetBlobReference(fileBase.FileName);
            
            &lt;span style="color: green;"&gt;// Create or overwrite the "myblob" blob with contents from a local file&lt;/span&gt;
            blob.UploadFromStream(fileBase.InputStream);

        }

        &lt;span style="color: blue;"&gt;return&lt;/span&gt; RedirectToAction(&lt;span style="color: #a31515;"&gt;"index"&lt;/span&gt;);
    }   
}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;And the view code is simple as well:&lt;/p&gt;
&lt;div class="code-wrapper border-shadow-1"&gt;
&lt;div style="background-color: white; color: black;"&gt;
&lt;pre&gt;@model List&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;string&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;           
@{
    ViewBag.Title = "My Cloudy Album";
}

&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;h2&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;My Cloudy Album&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515;"&gt;h2&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;

@foreach (var item in Model) {
    &lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;img&lt;/span&gt; &lt;span style="color: red;"&gt;src&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"@item"&lt;/span&gt; &lt;span style="color: red;"&gt;width&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"200"&lt;/span&gt; &lt;span style="color: red;"&gt;height&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"100"&lt;/span&gt; &lt;span style="color: blue;"&gt;/&amp;gt;&lt;/span&gt;
}

@using (Html.BeginForm("index", "home",  
    FormMethod.Post, new { enctype = "multipart/form-data" })) {
    
    &lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;input&lt;/span&gt; &lt;span style="color: red;"&gt;type&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"file"&lt;/span&gt; &lt;span style="color: red;"&gt;name&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"fileBase"&lt;/span&gt; &lt;span style="color: blue;"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;p&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;
        &lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;input&lt;/span&gt; &lt;span style="color: red;"&gt;type&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"submit"&lt;/span&gt; &lt;span style="color: red;"&gt;value&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"Upload"&lt;/span&gt; &lt;span style="color: blue;"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515;"&gt;p&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;    
}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Now we are all set and go to go.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;But (a big but), you need to run Visual Studio with admin rights in order to run the emulator. Otherwise, you won&amp;rsquo;t be able to.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.tugberkugurlu.com/Content/Images/UploadedByAuthors/wlw/Windows-Azure-Blob-Storage-and-A.NET-MVC_FA59/image_10.png"&gt;&lt;img height="217" width="311" src="http://www.tugberkugurlu.com/Content/Images/UploadedByAuthors/wlw/Windows-Azure-Blob-Storage-and-A.NET-MVC_FA59/image_10.png" alt="image" border="0" title="image" style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;When we hit CTRL + F5, VS will create a cloud deployment package and deploy it to emulator and we will see the emulator starting up:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.tugberkugurlu.com/Content/Images/UploadedByAuthors/wlw/Windows-Azure-Blob-Storage-and-A.NET-MVC_FA59/image_11.png"&gt;&lt;img height="41" width="247" src="http://www.tugberkugurlu.com/Content/Images/UploadedByAuthors/wlw/Windows-Azure-Blob-Storage-and-A.NET-MVC_FA59/image_11.png" alt="image" border="0" title="image" style="padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Now we are cloudy!&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.tugberkugurlu.com/Content/Images/UploadedByAuthors/wlw/Windows-Azure-Blob-Storage-and-A.NET-MVC_FA59/image_12.png"&gt;&lt;img height="358" width="644" src="http://www.tugberkugurlu.com/Content/Images/UploadedByAuthors/wlw/Windows-Azure-Blob-Storage-and-A.NET-MVC_FA59/image_12.png" alt="image" border="0" title="image" style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;When we choose a picture and upload it, we should be able to see it after we got back.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.tugberkugurlu.com/Content/Images/UploadedByAuthors/wlw/Windows-Azure-Blob-Storage-and-A.NET-MVC_FA59/image_13.png"&gt;&lt;img height="358" width="644" src="http://www.tugberkugurlu.com/Content/Images/UploadedByAuthors/wlw/Windows-Azure-Blob-Storage-and-A.NET-MVC_FA59/image_13.png" alt="image" border="0" title="image" style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;One more thing to prove that we really run inside the emulator is to right click the emulator icon, choose Show Storage Emulator UI:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.tugberkugurlu.com/Content/Images/UploadedByAuthors/wlw/Windows-Azure-Blob-Storage-and-A.NET-MVC_FA59/image_14.png"&gt;&lt;img height="191" width="244" src="http://www.tugberkugurlu.com/Content/Images/UploadedByAuthors/wlw/Windows-Azure-Blob-Storage-and-A.NET-MVC_FA59/image_14.png" alt="image" border="0" title="image" style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;This will bring up the Storage Emulator as below:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.tugberkugurlu.com/Content/Images/UploadedByAuthors/wlw/Windows-Azure-Blob-Storage-and-A.NET-MVC_FA59/image_15.png"&gt;&lt;img height="209" width="607" src="http://www.tugberkugurlu.com/Content/Images/UploadedByAuthors/wlw/Windows-Azure-Blob-Storage-and-A.NET-MVC_FA59/image_15.png" alt="image" border="0" title="image" style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;On the right hand side, there is a reset button. Click it to reset the emulator and go back to your page and refresh. You will see that all the pictures that you have uploaded are gone now.&lt;/p&gt;
&lt;p&gt;That&amp;rsquo;s all for now &lt;img src="http://www.tugberkugurlu.com/Content/Images/UploadedByAuthors/wlw/0310308e59b3_10C04/wlEmoticon-smile.png" alt="Winking smile" class="wlEmoticon wlEmoticon-winkingsmile" style="border-style: none;" /&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Additional Resources&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a target="_blank" href="http://www.windowsazure.com" title="http://www.windowsazure.com"&gt;Windows Azure Web Site&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://blogs.msdn.com/b/windowsazurestorage/"&gt;Windows Azure Storage Team Blog&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;&lt;a target="_blank" href="http://www.windowsazure.com/en-us/develop/net/how-to-guides/blob-storage/" title="http://www.windowsazure.com/en-us/develop/net/how-to-guides/blob-storage/"&gt;How to Use the Blob Storage Service&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/b97v4zCFjZWyX-_pVJwUqU07eR8/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/b97v4zCFjZWyX-_pVJwUqU07eR8/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/b97v4zCFjZWyX-_pVJwUqU07eR8/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/b97v4zCFjZWyX-_pVJwUqU07eR8/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/TugberkUgurlu/~4/UNFu6fkP2DQ" height="1" width="1"/&gt;</description><pubDate>Wed, 18 Jan 2012 07:17:00 Z</pubDate><a10:updated>2012-01-18T07:17:00Z</a10:updated><a10:content type="text">&lt;p&gt;I have been digging into &lt;a target="_blank" href="http://www.windowsazure.com" title="http://www.windowsazure.com"&gt;Windows Azure&lt;/a&gt; more and more lately. I wish that it would be supported in Turkey but anyway, emulator is my cloud for now &lt;img src="http://www.tugberkugurlu.com/Content/Images/UploadedByAuthors/wlw/0310308e59b3_10C04/wlEmoticon-smile.png" alt="Smile" class="wlEmoticon wlEmoticon-smile" style="border-style: none;" /&gt; Yesterday, I thought I should note some of things down and told myself "There is no better place than your blog for that, my friend" and here I am.&lt;/p&gt;
&lt;p&gt;One feature of Windows Azure platform which I will be using is &lt;strong&gt;Blob Storage&lt;/strong&gt;. Blob storage enables you to store your unstructured data (like pictures, word docs, excel file, etc.) inside Windows Azure servers and access them over HTTP or HTTPS. &lt;a target="_blank" href="https://github.com/WindowsAzure/azure-sdk-for-net" title="https://github.com/WindowsAzure/azure-sdk-for-net"&gt;With Windows Azure .Net SDK&lt;/a&gt;, you have full control over your blobs and program against that easily. How? Let&amp;rsquo;s see.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Before starting, make sure that you have installed Windows Azure SDK for .Net which will bring down Windows Azure Tools for Microsoft Visual Studio and Windows Azure Client Libraries for .Net. You can find the information on how to install the SDK from &lt;a href="http://www.windowsazure.com/en-us/develop/net/"&gt;http://www.windowsazure.com/en-us/develop/net/&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;First thing is first. We need an ASP.NET MVC project to simulate this (but it doesn&amp;rsquo;t have to be ASP.NET MVC project). We have two options to make our application azure-cloudy:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Directly Create a Cloud Application&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Inside the new project dialog box on Visual Studio, choose Windows Azure Project as indicated below:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.tugberkugurlu.com/Content/Images/UploadedByAuthors/wlw/Windows-Azure-Blob-Storage-and-A.NET-MVC_FA59/image.png"&gt;&lt;img height="394" width="644" src="http://www.tugberkugurlu.com/Content/Images/UploadedByAuthors/wlw/Windows-Azure-Blob-Storage-and-A.NET-MVC_FA59/image.png" alt="image" border="0" title="image" style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;When you hit OK, you will see a dialog as below:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.tugberkugurlu.com/Content/Images/UploadedByAuthors/wlw/Windows-Azure-Blob-Storage-and-A.NET-MVC_FA59/image_3.png"&gt;&lt;img height="404" width="643" src="http://www.tugberkugurlu.com/Content/Images/UploadedByAuthors/wlw/Windows-Azure-Blob-Storage-and-A.NET-MVC_FA59/image_3.png" alt="image" border="0" title="image" style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;This dialog box is like a open buffet, you can choose which project you need for your application here. But we will choose ASP.NET MVC 3 Web Role and then hit OK:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.tugberkugurlu.com/Content/Images/UploadedByAuthors/wlw/Windows-Azure-Blob-Storage-and-A.NET-MVC_FA59/image_4.png"&gt;&lt;img height="404" width="643" src="http://www.tugberkugurlu.com/Content/Images/UploadedByAuthors/wlw/Windows-Azure-Blob-Storage-and-A.NET-MVC_FA59/image_4.png" alt="image" border="0" title="image" style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Then we will see a dialog box which is owned by ASP.NET MVC. From now on everything is same like it is a standard ASP.NET MVC project.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Make Your Application Azure-Cloudy Later&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Assuming that we have an existing ASP.NET MVC application and we want to run this application on Windows Azure. What we need to do is to right click on our project and choose &lt;em&gt;"Add Windows Azure Deployment Project Option" &lt;/em&gt;as below:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.tugberkugurlu.com/Content/Images/UploadedByAuthors/wlw/Windows-Azure-Blob-Storage-and-A.NET-MVC_FA59/image_5.png"&gt;&lt;img height="303" width="596" src="http://www.tugberkugurlu.com/Content/Images/UploadedByAuthors/wlw/Windows-Azure-Blob-Storage-and-A.NET-MVC_FA59/image_5.png" alt="image" border="0" title="image" style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Either way, our solution will look something like this:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.tugberkugurlu.com/Content/Images/UploadedByAuthors/wlw/Windows-Azure-Blob-Storage-and-A.NET-MVC_FA59/image_6.png"&gt;&lt;img height="431" width="324" src="http://www.tugberkugurlu.com/Content/Images/UploadedByAuthors/wlw/Windows-Azure-Blob-Storage-and-A.NET-MVC_FA59/image_6.png" alt="image" border="0" title="image" style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Configure to Work With Blob Storage&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;We have a few steps to make before we can start developing. In a real world scenario, you need a Windows Azure storage account to use the blob storage service and you can create this account from &lt;a href="http://windows.azure.com/"&gt;Windows Azure Management Portal&lt;/a&gt;. After you configure your account, you will have your access keys to that storage account which you will need on your development process.&lt;/p&gt;
&lt;p&gt;One thing to mention before going further is that you will be able to access your files through HTTP or HTTPS as motioned before and the URL for your blobs will look like this:&lt;/p&gt;
&lt;p&gt;&lt;em&gt;http://&amp;lt;storage account&amp;gt;.blob.core.windows.net/&amp;lt;container&amp;gt;/&amp;lt;blob&amp;gt;&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;There is a way to change this so that you can use your own domain. In our example, we will be reaching out our blobs through localhost. we will get to that later in this post.&lt;/p&gt;
&lt;p&gt;In order to develop locally with emulator, we do not need a storage account which means that we don&amp;rsquo;t need access keys. In order to configure it so, right click on the web role file and choose &lt;em&gt;Properties&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.tugberkugurlu.com/Content/Images/UploadedByAuthors/wlw/Windows-Azure-Blob-Storage-and-A.NET-MVC_FA59/image_7.png"&gt;&lt;img height="378" width="644" src="http://www.tugberkugurlu.com/Content/Images/UploadedByAuthors/wlw/Windows-Azure-Blob-Storage-and-A.NET-MVC_FA59/image_7.png" alt="image" border="0" title="image" style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;This action will brings up the properties windows. From there, go to &lt;em&gt;Settings&lt;/em&gt; tab and click add settings and on the new created node, select Connection String as Type and click "..." which stands right hand side.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.tugberkugurlu.com/Content/Images/UploadedByAuthors/wlw/Windows-Azure-Blob-Storage-and-A.NET-MVC_FA59/image_8.png"&gt;&lt;img height="378" width="644" src="http://www.tugberkugurlu.com/Content/Images/UploadedByAuthors/wlw/Windows-Azure-Blob-Storage-and-A.NET-MVC_FA59/image_8.png" alt="image" border="0" title="image" style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;This action will also open up a new window for you and this is where you configure your storage account. But we will choose "&lt;em&gt;Use the Windows Azure storage emulator&lt;/em&gt;" option and give this configuration a new friendly name:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.tugberkugurlu.com/Content/Images/UploadedByAuthors/wlw/Windows-Azure-Blob-Storage-and-A.NET-MVC_FA59/image_9.png"&gt;&lt;img height="351" width="644" src="http://www.tugberkugurlu.com/Content/Images/UploadedByAuthors/wlw/Windows-Azure-Blob-Storage-and-A.NET-MVC_FA59/image_9.png" alt="image" border="0" title="image" style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Those actions are not required but viewed as a best practice while working windows azure cloud projects. Also, you can use Windows Azure Storage and run your application inside your own servers. It is totally fine. So, in that case you won&amp;rsquo;t need a cloud project. What .Net SDK provides is a wrapper around Windows Azure REST APIs which makes it easy to program against.&lt;/p&gt;
&lt;p&gt;Now we are all set and finally we can write some code.&lt;/p&gt;
&lt;p&gt;First of all, we need two additional libraries to develop against Windows Azure with .Net:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Microsoft.WindowsAzure.StorageClient.dll &lt;/li&gt;
&lt;li&gt;Microsoft.WindowsAzure.ServiceRuntime.dll&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Those two will give us everything we need. For the sake of simplicity, I created a simple project which uploads images through Windows Azure Storage service and list those images on a page. In order to do that so, I created a service class (which is a standard class, nothing further than that) called &lt;strong&gt;MyBlobStorageService&lt;/strong&gt;. Let&amp;rsquo;s see the code first:&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;div class="code-wrapper border-shadow-1"&gt;
&lt;div style="background-color: white; color: black;"&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;pre&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;class&lt;/span&gt; MyBlobStorageService {

    &lt;span style="color: blue;"&gt;public&lt;/span&gt; CloudBlobContainer GetCloudBlobContainer() {

        &lt;span style="color: green;"&gt;// Retrieve storage account from connection-string&lt;/span&gt;
        CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
                RoleEnvironment.GetConfigurationSettingValue(&lt;span style="color: #a31515;"&gt;"StorageConnectionString"&lt;/span&gt;)
            );

        &lt;span style="color: green;"&gt;// Create the blob client &lt;/span&gt;
        CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

        &lt;span style="color: green;"&gt;// Retrieve a reference to a container &lt;/span&gt;
        CloudBlobContainer blobContainer = blobClient.GetContainerReference(&lt;span style="color: #a31515;"&gt;"albums"&lt;/span&gt;);

        &lt;span style="color: green;"&gt;// Create the container if it doesn't already exist&lt;/span&gt;
        &lt;span style="color: blue;"&gt;if&lt;/span&gt; (blobContainer.CreateIfNotExist()) {

            blobContainer.SetPermissions(
               &lt;span style="color: blue;"&gt;new&lt;/span&gt; BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Blob }
            );
        }

        &lt;span style="color: blue;"&gt;return&lt;/span&gt; blobContainer;
    }

}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;What this code does it fairly simple. It gets the storage account information form the connection string that we have configured and creates a &lt;strong&gt;CloudStorageAccount&lt;/strong&gt; class according to that. Then, We create a blob storage client (&lt;strong&gt;CloudBlobClient&lt;/strong&gt;) over that storage account. Finally, we create a container (&lt;strong&gt;CloudBlobContainer&lt;/strong&gt;) for our blobs and check if it exists of not. If not, then we simply create it and set the public access permission to it because we will store images inside that container and we want to display those images on our web page.&lt;/p&gt;
&lt;p&gt;In order to demonstrate this, I have two controller actions. One is for HTTP GET and the other is for HTTP POST. Here is the complete controller code:&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;div class="code-wrapper border-shadow-1"&gt;
&lt;div style="background-color: white; color: black;"&gt;
&lt;pre&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;class&lt;/span&gt; HomeController : Controller {

    &lt;span style="color: blue;"&gt;public&lt;/span&gt; MyBlobStorageService 
        _myBlobStorageService = &lt;span style="color: blue;"&gt;new&lt;/span&gt; MyBlobStorageService();

    &lt;span style="color: blue;"&gt;public&lt;/span&gt; ActionResult Index() {

        &lt;span style="color: green;"&gt;// Retrieve a reference to a container &lt;/span&gt;
        CloudBlobContainer blobContainer = 
            _myBlobStorageService.GetCloudBlobContainer();

        List&amp;lt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&amp;gt; blobs = &lt;span style="color: blue;"&gt;new&lt;/span&gt; List&amp;lt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&amp;gt;();

        &lt;span style="color: green;"&gt;// Loop over blobs within the container and output the URI to each of them&lt;/span&gt;
        &lt;span style="color: blue;"&gt;foreach&lt;/span&gt; (&lt;span style="color: blue;"&gt;var&lt;/span&gt; blobItem &lt;span style="color: blue;"&gt;in&lt;/span&gt; blobContainer.ListBlobs())
            blobs.Add(blobItem.Uri.ToString());

        &lt;span style="color: blue;"&gt;return&lt;/span&gt; View(blobs);
    }

    [HttpPost]
    [ActionName(&lt;span style="color: #a31515;"&gt;"index"&lt;/span&gt;)]
    &lt;span style="color: blue;"&gt;public&lt;/span&gt; ActionResult Index_post(HttpPostedFileBase fileBase) {

        &lt;span style="color: blue;"&gt;if&lt;/span&gt; (fileBase.ContentLength &amp;gt; 0) {

            &lt;span style="color: green;"&gt;// Retrieve a reference to a container &lt;/span&gt;
            CloudBlobContainer blobContainer = 
                _myBlobStorageService.GetCloudBlobContainer();

            CloudBlob blob = 
                blobContainer.GetBlobReference(fileBase.FileName);
            
            &lt;span style="color: green;"&gt;// Create or overwrite the "myblob" blob with contents from a local file&lt;/span&gt;
            blob.UploadFromStream(fileBase.InputStream);

        }

        &lt;span style="color: blue;"&gt;return&lt;/span&gt; RedirectToAction(&lt;span style="color: #a31515;"&gt;"index"&lt;/span&gt;);
    }   
}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;And the view code is simple as well:&lt;/p&gt;
&lt;div class="code-wrapper border-shadow-1"&gt;
&lt;div style="background-color: white; color: black;"&gt;
&lt;pre&gt;@model List&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;string&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;           
@{
    ViewBag.Title = "My Cloudy Album";
}

&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;h2&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;My Cloudy Album&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515;"&gt;h2&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;

@foreach (var item in Model) {
    &lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;img&lt;/span&gt; &lt;span style="color: red;"&gt;src&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"@item"&lt;/span&gt; &lt;span style="color: red;"&gt;width&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"200"&lt;/span&gt; &lt;span style="color: red;"&gt;height&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"100"&lt;/span&gt; &lt;span style="color: blue;"&gt;/&amp;gt;&lt;/span&gt;
}

@using (Html.BeginForm("index", "home",  
    FormMethod.Post, new { enctype = "multipart/form-data" })) {
    
    &lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;input&lt;/span&gt; &lt;span style="color: red;"&gt;type&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"file"&lt;/span&gt; &lt;span style="color: red;"&gt;name&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"fileBase"&lt;/span&gt; &lt;span style="color: blue;"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;p&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;
        &lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;input&lt;/span&gt; &lt;span style="color: red;"&gt;type&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"submit"&lt;/span&gt; &lt;span style="color: red;"&gt;value&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"Upload"&lt;/span&gt; &lt;span style="color: blue;"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515;"&gt;p&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;    
}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Now we are all set and go to go.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;But (a big but), you need to run Visual Studio with admin rights in order to run the emulator. Otherwise, you won&amp;rsquo;t be able to.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.tugberkugurlu.com/Content/Images/UploadedByAuthors/wlw/Windows-Azure-Blob-Storage-and-A.NET-MVC_FA59/image_10.png"&gt;&lt;img height="217" width="311" src="http://www.tugberkugurlu.com/Content/Images/UploadedByAuthors/wlw/Windows-Azure-Blob-Storage-and-A.NET-MVC_FA59/image_10.png" alt="image" border="0" title="image" style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;When we hit CTRL + F5, VS will create a cloud deployment package and deploy it to emulator and we will see the emulator starting up:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.tugberkugurlu.com/Content/Images/UploadedByAuthors/wlw/Windows-Azure-Blob-Storage-and-A.NET-MVC_FA59/image_11.png"&gt;&lt;img height="41" width="247" src="http://www.tugberkugurlu.com/Content/Images/UploadedByAuthors/wlw/Windows-Azure-Blob-Storage-and-A.NET-MVC_FA59/image_11.png" alt="image" border="0" title="image" style="padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Now we are cloudy!&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.tugberkugurlu.com/Content/Images/UploadedByAuthors/wlw/Windows-Azure-Blob-Storage-and-A.NET-MVC_FA59/image_12.png"&gt;&lt;img height="358" width="644" src="http://www.tugberkugurlu.com/Content/Images/UploadedByAuthors/wlw/Windows-Azure-Blob-Storage-and-A.NET-MVC_FA59/image_12.png" alt="image" border="0" title="image" style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;When we choose a picture and upload it, we should be able to see it after we got back.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.tugberkugurlu.com/Content/Images/UploadedByAuthors/wlw/Windows-Azure-Blob-Storage-and-A.NET-MVC_FA59/image_13.png"&gt;&lt;img height="358" width="644" src="http://www.tugberkugurlu.com/Content/Images/UploadedByAuthors/wlw/Windows-Azure-Blob-Storage-and-A.NET-MVC_FA59/image_13.png" alt="image" border="0" title="image" style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;One more thing to prove that we really run inside the emulator is to right click the emulator icon, choose Show Storage Emulator UI:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.tugberkugurlu.com/Content/Images/UploadedByAuthors/wlw/Windows-Azure-Blob-Storage-and-A.NET-MVC_FA59/image_14.png"&gt;&lt;img height="191" width="244" src="http://www.tugberkugurlu.com/Content/Images/UploadedByAuthors/wlw/Windows-Azure-Blob-Storage-and-A.NET-MVC_FA59/image_14.png" alt="image" border="0" title="image" style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;This will bring up the Storage Emulator as below:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.tugberkugurlu.com/Content/Images/UploadedByAuthors/wlw/Windows-Azure-Blob-Storage-and-A.NET-MVC_FA59/image_15.png"&gt;&lt;img height="209" width="607" src="http://www.tugberkugurlu.com/Content/Images/UploadedByAuthors/wlw/Windows-Azure-Blob-Storage-and-A.NET-MVC_FA59/image_15.png" alt="image" border="0" title="image" style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;On the right hand side, there is a reset button. Click it to reset the emulator and go back to your page and refresh. You will see that all the pictures that you have uploaded are gone now.&lt;/p&gt;
&lt;p&gt;That&amp;rsquo;s all for now &lt;img src="http://www.tugberkugurlu.com/Content/Images/UploadedByAuthors/wlw/0310308e59b3_10C04/wlEmoticon-smile.png" alt="Winking smile" class="wlEmoticon wlEmoticon-winkingsmile" style="border-style: none;" /&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Additional Resources&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a target="_blank" href="http://www.windowsazure.com" title="http://www.windowsazure.com"&gt;Windows Azure Web Site&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://blogs.msdn.com/b/windowsazurestorage/"&gt;Windows Azure Storage Team Blog&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;&lt;a target="_blank" href="http://www.windowsazure.com/en-us/develop/net/how-to-guides/blob-storage/" title="http://www.windowsazure.com/en-us/develop/net/how-to-guides/blob-storage/"&gt;How to Use the Blob Storage Service&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</a10:content><feedburner:origLink>http://tugberkugurlu.com/81</feedburner:origLink></item><item><guid isPermaLink="false">http://tugberkugurlu.com/80</guid><link>http://feedproxy.google.com/~r/TugberkUgurlu/~3/BIWjIfi3it4/80</link><title>Entity Framework DbContext Generic Repository Implementation Is Now On Nuget and GitHub</title><description>&lt;p&gt;Couple of weeks ago, I have done two blog post on &lt;a href="http://www.tugberkugurlu.com/archive/generic-repository-pattern-entity-framework-asp-net-mvc-and-unit-testing-triangle"&gt;Generic Repository Pattern - Entity Framework, ASP.NET MVC and Unit Testing Triangle&lt;/a&gt; and &lt;a href="http://www.tugberkugurlu.com/archive/how-to-work-with-generic-repositories-on-asp-net-mvc-and-unit-testing-them-by-mocking"&gt;How to Work With Generic Repositories on ASP.NET MVC and Unit Testing Them By Mocking&lt;/a&gt; and I have put up the &lt;a href="https://github.com/tugberkugurlu/GenericRepoWebApp"&gt;https://github.com/tugberkugurlu/GenericRepoWebApp&lt;/a&gt; up on &lt;a target="_blank" href="http://github.com" title="http://github.com"&gt;GitHub&lt;/a&gt; as well.&lt;/p&gt;
&lt;p&gt;Now, the Generic Repository implementation has its own project separately: &lt;a href="https://github.com/tugberkugurlu/GenericRepository"&gt;https://github.com/tugberkugurlu/GenericRepository&lt;/a&gt;. This is a simple project and has not much inside it but it is nice to have something static so that it can be improved along the way.&lt;/p&gt;
&lt;p&gt;Here is a brief intro:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;This little project contains a Generic Repository infrastructure for .NET applications and also a DbContext Implementation of that.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;You can find more information about the main idea of this pattern and the usage on ASP.NET MVC along with Unit Testing by visiting the below links:&lt;/em&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;l&gt;&lt;a href="http://www.tugberkugurlu.com/archive/generic-repository-pattern-entity-framework-asp-net-mvc-and-unit-testing-triangle"&gt;&lt;em&gt;Generic Repository Pattern - Entity Framework, ASP.NET MVC and Unit Testing Triangle&lt;/em&gt;&lt;/a&gt; &lt;l&gt;&lt;a href="http://www.tugberkugurlu.com/archive/how-to-work-with-generic-repositories-on-asp-net-mvc-and-unit-testing-them-by-mocking"&gt;&lt;em&gt;How to Work With Generic Repositories on ASP.NET MVC and Unit Testing Them By Mocking&lt;/em&gt;&lt;/a&gt;&lt;/l&gt;&lt;/l&gt; 
&lt;/ul&gt;
&lt;h3&gt;How to Install&lt;/h3&gt;
&lt;p&gt;&lt;em&gt;You can directly install this little project from &lt;/em&gt;&lt;a href="http://nuget.org/"&gt;&lt;em&gt;Nuget&lt;/em&gt;&lt;/a&gt;&lt;em&gt;. There are two packages:&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&lt;a target="_blank" href="https://nuget.org/packages/GenericRepository"&gt;&lt;strong&gt;&lt;em&gt;GenericRepository&lt;/em&gt;&lt;/strong&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.tugberkugurlu.com/Content/Images/UploadedByAuthors/wlw/ff3107405744_AEFB/image.png"&gt;&lt;img height="74" width="744" src="http://www.tugberkugurlu.com/Content/Images/UploadedByAuthors/wlw/ff3107405744_AEFB/image.png" alt="image" title="image" style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border-image: initial; border: 0px initial initial;" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Generic Repository Infrastructure For .NET Applications&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;a href="https://nuget.org/packages/GenericRepository.EF"&gt;&lt;em&gt;GenericRepository.EF&lt;/em&gt;&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.tugberkugurlu.com/Content/Images/UploadedByAuthors/wlw/ff3107405744_AEFB/image_3.png"&gt;&lt;img height="76" width="749" src="http://www.tugberkugurlu.com/Content/Images/UploadedByAuthors/wlw/ff3107405744_AEFB/image_3.png" alt="image" title="image" style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border-image: initial; border: 0px initial initial;" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Generic Repository DbContext Implementation&lt;/em&gt;&lt;/p&gt;
&lt;h3&gt;Releases&lt;/h3&gt;
&lt;p&gt;&lt;em&gt;Under the &lt;/em&gt;&lt;a href="https://github.com/tugberkugurlu/GenericRepository"&gt;&lt;em&gt;master&lt;/em&gt;&lt;/a&gt;&lt;em&gt; branch, you can find the latest stable release of this project.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Also, you can see the most up-to-date project under &lt;/em&gt;&lt;a href="https://github.com/tugberkugurlu/GenericRepository/tree/develop"&gt;&lt;em&gt;develop&lt;/em&gt;&lt;/a&gt;&lt;em&gt; branch. Repository under develop branch is possibly the unstable version of the project.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;You can also find all the releases under &lt;/em&gt;&lt;a href="https://github.com/tugberkugurlu/GenericRepository/tags"&gt;&lt;em&gt;Tags&lt;/em&gt;&lt;/a&gt;&lt;em&gt; section.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;I await for your PRs :)&lt;/p&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/KWEzqdgYVKSR4WZi6nr0pXLX06o/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/KWEzqdgYVKSR4WZi6nr0pXLX06o/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/KWEzqdgYVKSR4WZi6nr0pXLX06o/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/KWEzqdgYVKSR4WZi6nr0pXLX06o/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/TugberkUgurlu/~4/BIWjIfi3it4" height="1" width="1"/&gt;</description><pubDate>Sun, 15 Jan 2012 08:41:00 Z</pubDate><a10:updated>2012-01-15T08:41:00Z</a10:updated><a10:content type="text">&lt;p&gt;Couple of weeks ago, I have done two blog post on &lt;a href="http://www.tugberkugurlu.com/archive/generic-repository-pattern-entity-framework-asp-net-mvc-and-unit-testing-triangle"&gt;Generic Repository Pattern - Entity Framework, ASP.NET MVC and Unit Testing Triangle&lt;/a&gt; and &lt;a href="http://www.tugberkugurlu.com/archive/how-to-work-with-generic-repositories-on-asp-net-mvc-and-unit-testing-them-by-mocking"&gt;How to Work With Generic Repositories on ASP.NET MVC and Unit Testing Them By Mocking&lt;/a&gt; and I have put up the &lt;a href="https://github.com/tugberkugurlu/GenericRepoWebApp"&gt;https://github.com/tugberkugurlu/GenericRepoWebApp&lt;/a&gt; up on &lt;a target="_blank" href="http://github.com" title="http://github.com"&gt;GitHub&lt;/a&gt; as well.&lt;/p&gt;
&lt;p&gt;Now, the Generic Repository implementation has its own project separately: &lt;a href="https://github.com/tugberkugurlu/GenericRepository"&gt;https://github.com/tugberkugurlu/GenericRepository&lt;/a&gt;. This is a simple project and has not much inside it but it is nice to have something static so that it can be improved along the way.&lt;/p&gt;
&lt;p&gt;Here is a brief intro:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;This little project contains a Generic Repository infrastructure for .NET applications and also a DbContext Implementation of that.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;You can find more information about the main idea of this pattern and the usage on ASP.NET MVC along with Unit Testing by visiting the below links:&lt;/em&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;l&gt;&lt;a href="http://www.tugberkugurlu.com/archive/generic-repository-pattern-entity-framework-asp-net-mvc-and-unit-testing-triangle"&gt;&lt;em&gt;Generic Repository Pattern - Entity Framework, ASP.NET MVC and Unit Testing Triangle&lt;/em&gt;&lt;/a&gt; &lt;l&gt;&lt;a href="http://www.tugberkugurlu.com/archive/how-to-work-with-generic-repositories-on-asp-net-mvc-and-unit-testing-them-by-mocking"&gt;&lt;em&gt;How to Work With Generic Repositories on ASP.NET MVC and Unit Testing Them By Mocking&lt;/em&gt;&lt;/a&gt;&lt;/l&gt;&lt;/l&gt; 
&lt;/ul&gt;
&lt;h3&gt;How to Install&lt;/h3&gt;
&lt;p&gt;&lt;em&gt;You can directly install this little project from &lt;/em&gt;&lt;a href="http://nuget.org/"&gt;&lt;em&gt;Nuget&lt;/em&gt;&lt;/a&gt;&lt;em&gt;. There are two packages:&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&lt;a target="_blank" href="https://nuget.org/packages/GenericRepository"&gt;&lt;strong&gt;&lt;em&gt;GenericRepository&lt;/em&gt;&lt;/strong&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.tugberkugurlu.com/Content/Images/UploadedByAuthors/wlw/ff3107405744_AEFB/image.png"&gt;&lt;img height="74" width="744" src="http://www.tugberkugurlu.com/Content/Images/UploadedByAuthors/wlw/ff3107405744_AEFB/image.png" alt="image" title="image" style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border-image: initial; border: 0px initial initial;" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Generic Repository Infrastructure For .NET Applications&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;a href="https://nuget.org/packages/GenericRepository.EF"&gt;&lt;em&gt;GenericRepository.EF&lt;/em&gt;&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.tugberkugurlu.com/Content/Images/UploadedByAuthors/wlw/ff3107405744_AEFB/image_3.png"&gt;&lt;img height="76" width="749" src="http://www.tugberkugurlu.com/Content/Images/UploadedByAuthors/wlw/ff3107405744_AEFB/image_3.png" alt="image" title="image" style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border-image: initial; border: 0px initial initial;" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Generic Repository DbContext Implementation&lt;/em&gt;&lt;/p&gt;
&lt;h3&gt;Releases&lt;/h3&gt;
&lt;p&gt;&lt;em&gt;Under the &lt;/em&gt;&lt;a href="https://github.com/tugberkugurlu/GenericRepository"&gt;&lt;em&gt;master&lt;/em&gt;&lt;/a&gt;&lt;em&gt; branch, you can find the latest stable release of this project.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Also, you can see the most up-to-date project under &lt;/em&gt;&lt;a href="https://github.com/tugberkugurlu/GenericRepository/tree/develop"&gt;&lt;em&gt;develop&lt;/em&gt;&lt;/a&gt;&lt;em&gt; branch. Repository under develop branch is possibly the unstable version of the project.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;You can also find all the releases under &lt;/em&gt;&lt;a href="https://github.com/tugberkugurlu/GenericRepository/tags"&gt;&lt;em&gt;Tags&lt;/em&gt;&lt;/a&gt;&lt;em&gt; section.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;I await for your PRs :)&lt;/p&gt;</a10:content><feedburner:origLink>http://tugberkugurlu.com/80</feedburner:origLink></item><item><guid isPermaLink="false">http://tugberkugurlu.com/79</guid><link>http://feedproxy.google.com/~r/TugberkUgurlu/~3/pNxBdKj2IWM/79</link><title>ASP.NET MVC Code Review #2 - A Way of Working with Html Select Element (AKA DropDownList) In ASP.NET MVC</title><description>&lt;p&gt;This is the #2 of the series of blog posts which is about some core scenarios on &lt;a href="http://asp.net/mvc"&gt;ASP.NET MVC&lt;/a&gt;. In this one, code review #2, I will try to show you the best way of working with &lt;strong&gt;Html Select element &lt;/strong&gt;(AKA &lt;strong&gt;DropDownList&lt;/strong&gt;) in ASP.NET MVC. Here is the code:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Repository Class which generates the dummy data for the demo:&lt;/strong&gt;&lt;/p&gt;
&lt;div class="code-wrapper border-shadow-1"&gt;
&lt;div style="background-color: white; color: black;"&gt;
&lt;pre&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;class&lt;/span&gt; ProductCategory {

    &lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;int&lt;/span&gt; CategoryId { &lt;span style="color: blue;"&gt;get&lt;/span&gt;; &lt;span style="color: blue;"&gt;set&lt;/span&gt;; }
    &lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;string&lt;/span&gt; CategoryName { &lt;span style="color: blue;"&gt;get&lt;/span&gt;; &lt;span style="color: blue;"&gt;set&lt;/span&gt;; }
}

&lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;class&lt;/span&gt; ProductCategoryRepo {

    &lt;span style="color: blue;"&gt;public&lt;/span&gt; List&amp;lt;ProductCategory&amp;gt; GetAll() {

        List&amp;lt;ProductCategory&amp;gt; categories = &lt;span style="color: blue;"&gt;new&lt;/span&gt; List&amp;lt;ProductCategory&amp;gt;();

        &lt;span style="color: blue;"&gt;for&lt;/span&gt; (&lt;span style="color: blue;"&gt;int&lt;/span&gt; i = 1; i &amp;lt;= 10; i++) {

            categories.Add(&lt;span style="color: blue;"&gt;new&lt;/span&gt; ProductCategory { 
                CategoryId = i,
                CategoryName = &lt;span style="color: blue;"&gt;string&lt;/span&gt;.Format(&lt;span style="color: #a31515;"&gt;"Category {0}"&lt;/span&gt;, i)
            });
        }

        &lt;span style="color: blue;"&gt;return&lt;/span&gt; categories;
    }
}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;Controller:&lt;/strong&gt;&lt;/p&gt;
&lt;div class="code-wrapper border-shadow-1"&gt;
&lt;div style="background-color: white; color: black;"&gt;
&lt;pre&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;class&lt;/span&gt; SampleController : Controller {

    &lt;span style="color: blue;"&gt;private&lt;/span&gt; &lt;span style="color: blue;"&gt;readonly&lt;/span&gt; ProductCategoryRepo productCategoryRepo = 
         &lt;span style="color: blue;"&gt;new&lt;/span&gt; ProductCategoryRepo();

    &lt;span style="color: blue;"&gt;public&lt;/span&gt; ActionResult Index() {

        registerProductCategorySelectListViewBag();
        &lt;span style="color: blue;"&gt;return&lt;/span&gt; View();
    }

    &lt;span style="color: blue;"&gt;private&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; registerProductCategorySelectListViewBag() {

        ViewBag.ProductCategorySelectList = 
            productCategoryRepo.GetAll().Select(
                c =&amp;gt; &lt;span style="color: blue;"&gt;new&lt;/span&gt; SelectListItem { 
                    Text = c.CategoryName,
                    Value = c.CategoryId.ToString()
                }
            );
    }
}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;View:&lt;/strong&gt;&lt;/p&gt;
&lt;div class="code-wrapper border-shadow-1"&gt;
&lt;div style="background-color: white; color: black;"&gt;
&lt;pre&gt;@{
    ViewBag.Title = &lt;span style="color: #a31515;"&gt;"HTML Select List Sample"&lt;/span&gt;;
}

&amp;lt;h2&amp;gt;HTML Select List Sample&amp;lt;/h2&amp;gt;

&amp;lt;p&amp;gt;
    &amp;lt;strong&amp;gt;Product Categories:&amp;lt;/strong&amp;gt;
&amp;lt;/p&amp;gt;
&amp;lt;p&amp;gt;
    @Html.DropDownList(
        &lt;span style="color: #a31515;"&gt;"ProductCategoryId"&lt;/span&gt;, 
        (IEnumerable&amp;lt;SelectListItem&amp;gt;)ViewBag.ProductCategorySelectList
    )
&amp;lt;/p&amp;gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Let&amp;rsquo;s see what it generates for us:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Output HTML of Select element:&lt;/strong&gt;&lt;/p&gt;
&lt;div class="code-wrapper border-shadow-1"&gt;
&lt;div style="background-color: white; color: black;"&gt;
&lt;pre&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;select&lt;/span&gt; &lt;span style="color: red;"&gt;id&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"ProductCategoryId"&lt;/span&gt; &lt;span style="color: red;"&gt;name&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"ProductCategoryId"&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;
    &lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;option&lt;/span&gt; &lt;span style="color: red;"&gt;value&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"1"&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;Category 1&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515;"&gt;option&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;
    &lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;option&lt;/span&gt; &lt;span style="color: red;"&gt;value&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"2"&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;Category 2&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515;"&gt;option&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;
    &lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;option&lt;/span&gt; &lt;span style="color: red;"&gt;value&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"3"&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;Category 3&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515;"&gt;option&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;
    &lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;option&lt;/span&gt; &lt;span style="color: red;"&gt;value&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"4"&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;Category 4&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515;"&gt;option&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;
    &lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;option&lt;/span&gt; &lt;span style="color: red;"&gt;value&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"5"&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;Category 5&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515;"&gt;option&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;
    &lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;option&lt;/span&gt; &lt;span style="color: red;"&gt;value&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"6"&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;Category 6&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515;"&gt;option&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;
    &lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;option&lt;/span&gt; &lt;span style="color: red;"&gt;value&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"7"&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;Category 7&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515;"&gt;option&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;
    &lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;option&lt;/span&gt; &lt;span style="color: red;"&gt;value&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"8"&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;Category 8&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515;"&gt;option&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;
    &lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;option&lt;/span&gt; &lt;span style="color: red;"&gt;value&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"9"&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;Category 9&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515;"&gt;option&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;
    &lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;option&lt;/span&gt; &lt;span style="color: red;"&gt;value&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"10"&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;Category 10&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515;"&gt;option&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;
&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515;"&gt;select&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Just like what I expected. On the controller, you see that I newed up &lt;a target="_blank" href="http://msdn.microsoft.com/en-us/library/system.web.mvc.selectlistitem(v=VS.98).aspx" title="http://msdn.microsoft.com/en-us/library/system.web.mvc.selectlistitem(v=VS.98).aspx"&gt;SelectListItem&lt;/a&gt; classes and pass them to the view through the &lt;a target="_blank" href="http://msdn.microsoft.com/en-us/library/system.web.mvc.viewdatadictionary(v=VS.98).aspx" title="http://msdn.microsoft.com/en-us/library/system.web.mvc.viewdatadictionary(v=VS.98).aspx"&gt;ViewDataDictionary&lt;/a&gt;. SelectListItem class works nicely with &lt;a target="_blank" href="http://msdn.microsoft.com/en-us/library/dd492738(v=VS.98).aspx" title="http://msdn.microsoft.com/en-us/library/dd492738(v=VS.98).aspx"&gt;DropDownList&lt;/a&gt; and &lt;a target="_blank" href="http://msdn.microsoft.com/en-us/library/ee703462(v=VS.98).aspx" title="http://msdn.microsoft.com/en-us/library/ee703462(v=VS.98).aspx"&gt;DropDownListFor&lt;/a&gt; Html helpers within ASP.NET MVC as you see inside the view code above.&lt;/p&gt;
&lt;p&gt;So, don&amp;rsquo;t try to reinvent the wheel and don&amp;rsquo;t get into for and foreach loops in order just to create a select element. SelectListItem has been put inside the library just for this purpose and leverage it.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/0O3SR5BnUEe806ZQ5Uxj-0b3UhI/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/0O3SR5BnUEe806ZQ5Uxj-0b3UhI/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/0O3SR5BnUEe806ZQ5Uxj-0b3UhI/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/0O3SR5BnUEe806ZQ5Uxj-0b3UhI/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/TugberkUgurlu/~4/pNxBdKj2IWM" height="1" width="1"/&gt;</description><pubDate>Fri, 30 Dec 2011 14:28:00 Z</pubDate><a10:updated>2011-12-30T14:28:00Z</a10:updated><a10:content type="text">&lt;p&gt;This is the #2 of the series of blog posts which is about some core scenarios on &lt;a href="http://asp.net/mvc"&gt;ASP.NET MVC&lt;/a&gt;. In this one, code review #2, I will try to show you the best way of working with &lt;strong&gt;Html Select element &lt;/strong&gt;(AKA &lt;strong&gt;DropDownList&lt;/strong&gt;) in ASP.NET MVC. Here is the code:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Repository Class which generates the dummy data for the demo:&lt;/strong&gt;&lt;/p&gt;
&lt;div class="code-wrapper border-shadow-1"&gt;
&lt;div style="background-color: white; color: black;"&gt;
&lt;pre&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;class&lt;/span&gt; ProductCategory {

    &lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;int&lt;/span&gt; CategoryId { &lt;span style="color: blue;"&gt;get&lt;/span&gt;; &lt;span style="color: blue;"&gt;set&lt;/span&gt;; }
    &lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;string&lt;/span&gt; CategoryName { &lt;span style="color: blue;"&gt;get&lt;/span&gt;; &lt;span style="color: blue;"&gt;set&lt;/span&gt;; }
}

&lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;class&lt;/span&gt; ProductCategoryRepo {

    &lt;span style="color: blue;"&gt;public&lt;/span&gt; List&amp;lt;ProductCategory&amp;gt; GetAll() {

        List&amp;lt;ProductCategory&amp;gt; categories = &lt;span style="color: blue;"&gt;new&lt;/span&gt; List&amp;lt;ProductCategory&amp;gt;();

        &lt;span style="color: blue;"&gt;for&lt;/span&gt; (&lt;span style="color: blue;"&gt;int&lt;/span&gt; i = 1; i &amp;lt;= 10; i++) {

            categories.Add(&lt;span style="color: blue;"&gt;new&lt;/span&gt; ProductCategory { 
                CategoryId = i,
                CategoryName = &lt;span style="color: blue;"&gt;string&lt;/span&gt;.Format(&lt;span style="color: #a31515;"&gt;"Category {0}"&lt;/span&gt;, i)
            });
        }

        &lt;span style="color: blue;"&gt;return&lt;/span&gt; categories;
    }
}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;Controller:&lt;/strong&gt;&lt;/p&gt;
&lt;div class="code-wrapper border-shadow-1"&gt;
&lt;div style="background-color: white; color: black;"&gt;
&lt;pre&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;class&lt;/span&gt; SampleController : Controller {

    &lt;span style="color: blue;"&gt;private&lt;/span&gt; &lt;span style="color: blue;"&gt;readonly&lt;/span&gt; ProductCategoryRepo productCategoryRepo = 
         &lt;span style="color: blue;"&gt;new&lt;/span&gt; ProductCategoryRepo();

    &lt;span style="color: blue;"&gt;public&lt;/span&gt; ActionResult Index() {

        registerProductCategorySelectListViewBag();
        &lt;span style="color: blue;"&gt;return&lt;/span&gt; View();
    }

    &lt;span style="color: blue;"&gt;private&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; registerProductCategorySelectListViewBag() {

        ViewBag.ProductCategorySelectList = 
            productCategoryRepo.GetAll().Select(
                c =&amp;gt; &lt;span style="color: blue;"&gt;new&lt;/span&gt; SelectListItem { 
                    Text = c.CategoryName,
                    Value = c.CategoryId.ToString()
                }
            );
    }
}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;View:&lt;/strong&gt;&lt;/p&gt;
&lt;div class="code-wrapper border-shadow-1"&gt;
&lt;div style="background-color: white; color: black;"&gt;
&lt;pre&gt;@{
    ViewBag.Title = &lt;span style="color: #a31515;"&gt;"HTML Select List Sample"&lt;/span&gt;;
}

&amp;lt;h2&amp;gt;HTML Select List Sample&amp;lt;/h2&amp;gt;

&amp;lt;p&amp;gt;
    &amp;lt;strong&amp;gt;Product Categories:&amp;lt;/strong&amp;gt;
&amp;lt;/p&amp;gt;
&amp;lt;p&amp;gt;
    @Html.DropDownList(
        &lt;span style="color: #a31515;"&gt;"ProductCategoryId"&lt;/span&gt;, 
        (IEnumerable&amp;lt;SelectListItem&amp;gt;)ViewBag.ProductCategorySelectList
    )
&amp;lt;/p&amp;gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Let&amp;rsquo;s see what it generates for us:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Output HTML of Select element:&lt;/strong&gt;&lt;/p&gt;
&lt;div class="code-wrapper border-shadow-1"&gt;
&lt;div style="background-color: white; color: black;"&gt;
&lt;pre&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;select&lt;/span&gt; &lt;span style="color: red;"&gt;id&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"ProductCategoryId"&lt;/span&gt; &lt;span style="color: red;"&gt;name&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"ProductCategoryId"&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;
    &lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;option&lt;/span&gt; &lt;span style="color: red;"&gt;value&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"1"&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;Category 1&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515;"&gt;option&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;
    &lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;option&lt;/span&gt; &lt;span style="color: red;"&gt;value&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"2"&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;Category 2&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515;"&gt;option&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;
    &lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;option&lt;/span&gt; &lt;span style="color: red;"&gt;value&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"3"&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;Category 3&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515;"&gt;option&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;
    &lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;option&lt;/span&gt; &lt;span style="color: red;"&gt;value&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"4"&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;Category 4&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515;"&gt;option&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;
    &lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;option&lt;/span&gt; &lt;span style="color: red;"&gt;value&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"5"&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;Category 5&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515;"&gt;option&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;
    &lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;option&lt;/span&gt; &lt;span style="color: red;"&gt;value&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"6"&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;Category 6&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515;"&gt;option&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;
    &lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;option&lt;/span&gt; &lt;span style="color: red;"&gt;value&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"7"&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;Category 7&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515;"&gt;option&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;
    &lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;option&lt;/span&gt; &lt;span style="color: red;"&gt;value&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"8"&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;Category 8&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515;"&gt;option&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;
    &lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;option&lt;/span&gt; &lt;span style="color: red;"&gt;value&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"9"&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;Category 9&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515;"&gt;option&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;
    &lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;option&lt;/span&gt; &lt;span style="color: red;"&gt;value&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"10"&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;Category 10&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515;"&gt;option&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;
&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515;"&gt;select&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Just like what I expected. On the controller, you see that I newed up &lt;a target="_blank" href="http://msdn.microsoft.com/en-us/library/system.web.mvc.selectlistitem(v=VS.98).aspx" title="http://msdn.microsoft.com/en-us/library/system.web.mvc.selectlistitem(v=VS.98).aspx"&gt;SelectListItem&lt;/a&gt; classes and pass them to the view through the &lt;a target="_blank" href="http://msdn.microsoft.com/en-us/library/system.web.mvc.viewdatadictionary(v=VS.98).aspx" title="http://msdn.microsoft.com/en-us/library/system.web.mvc.viewdatadictionary(v=VS.98).aspx"&gt;ViewDataDictionary&lt;/a&gt;. SelectListItem class works nicely with &lt;a target="_blank" href="http://msdn.microsoft.com/en-us/library/dd492738(v=VS.98).aspx" title="http://msdn.microsoft.com/en-us/library/dd492738(v=VS.98).aspx"&gt;DropDownList&lt;/a&gt; and &lt;a target="_blank" href="http://msdn.microsoft.com/en-us/library/ee703462(v=VS.98).aspx" title="http://msdn.microsoft.com/en-us/library/ee703462(v=VS.98).aspx"&gt;DropDownListFor&lt;/a&gt; Html helpers within ASP.NET MVC as you see inside the view code above.&lt;/p&gt;
&lt;p&gt;So, don&amp;rsquo;t try to reinvent the wheel and don&amp;rsquo;t get into for and foreach loops in order just to create a select element. SelectListItem has been put inside the library just for this purpose and leverage it.&lt;/p&gt;</a10:content><feedburner:origLink>http://tugberkugurlu.com/79</feedburner:origLink></item><item><guid isPermaLink="false">http://tugberkugurlu.com/78</guid><link>http://feedproxy.google.com/~r/TugberkUgurlu/~3/yJjQTxddSJo/78</link><title>ASP.NET MVC Code Review #1 - File Upload With HttpPostedFileBase Class</title><description>&lt;p&gt;Today, I decided to change my blogging style a little bit. I always try to write posts which contains detailed information about the subject but I realized that it prevents me from writing much more useful things. So, from now on, I will drop a bunch of code on the screen and talk about that briefly.&lt;/p&gt;
&lt;p&gt;This is the beginning of the series of blog posts which is about some core scenarios on &lt;a title="http://asp.net/mvc" href="http://asp.net/mvc" target="_blank"&gt;ASP.NET MVC&lt;/a&gt;. In this one, code review #1, I will give you an example on how to get file upload functionality working in ASP.NET MVC. Here is the code:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Controller:&lt;/strong&gt;&lt;/p&gt;
&lt;div class="code-wrapper border-shadow-1"&gt;
&lt;div style="background-color: white; color: black;"&gt;
&lt;pre&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;class&lt;/span&gt; SampleController : Controller {

    &lt;span style="color: blue;"&gt;public&lt;/span&gt; ActionResult Index() {
        &lt;span style="color: blue;"&gt;return&lt;/span&gt; View();
    }

    [ActionName(&lt;span style="color: #a31515;"&gt;"Index"&lt;/span&gt;)]
    [ValidateAntiForgeryToken, HttpPost]
    &lt;span style="color: blue;"&gt;public&lt;/span&gt; ActionResult Index_post(HttpPostedFileBase File) {

        &lt;span style="color: green;"&gt;//Check if the file is not null and content length is bigger than 0&lt;/span&gt;
        &lt;span style="color: blue;"&gt;if&lt;/span&gt; (File != &lt;span style="color: blue;"&gt;null&lt;/span&gt; &amp;amp;&amp;amp; File.ContentLength &amp;gt; 0) {

            &lt;span style="color: green;"&gt;//Check if folder is there&lt;/span&gt;
            &lt;span style="color: blue;"&gt;if&lt;/span&gt;(!System.IO.Directory.Exists(Server.MapPath(&lt;span style="color: #a31515;"&gt;"~/Content/PostedFiles"&lt;/span&gt;)))
                System.IO.Directory.CreateDirectory(
                    Server.MapPath(&lt;span style="color: #a31515;"&gt;"~/Content/PostedFiles"&lt;/span&gt;)
                );

            &lt;span style="color: green;"&gt;//Set the full path&lt;/span&gt;
            &lt;span style="color: blue;"&gt;string&lt;/span&gt; path = System.IO.Path.Combine(
                Server.MapPath(&lt;span style="color: #a31515;"&gt;"~/Content/PostedFiles"&lt;/span&gt;),
                System.IO.Path.GetFileName(File.FileName)
            );

            &lt;span style="color: green;"&gt;//Save the thing&lt;/span&gt;
            File.SaveAs(path);

            TempData[&lt;span style="color: #a31515;"&gt;"Result"&lt;/span&gt;] = &lt;span style="color: #a31515;"&gt;"File created successfully!"&lt;/span&gt;;
        }

        &lt;span style="color: green;"&gt;//RedirectToAction so that we can get rid of so-called "Form Resubmission"&lt;/span&gt;
        &lt;span style="color: blue;"&gt;return&lt;/span&gt; RedirectToAction(&lt;span style="color: #a31515;"&gt;"Index"&lt;/span&gt;);
    }

}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;View:&lt;/strong&gt;&lt;/p&gt;
&lt;div class="code-wrapper border-shadow-1"&gt;
&lt;div style="background-color: white; color: black;"&gt;
&lt;pre&gt;@{
    ViewBag.Title = &lt;span style="color: #a31515;"&gt;"File Upload Sample"&lt;/span&gt;;
}
&amp;lt;h2&amp;gt;File Upload Sample&amp;lt;/h2&amp;gt;

@&lt;span style="color: blue;"&gt;if&lt;/span&gt; (TempData[&lt;span style="color: #a31515;"&gt;"Result"&lt;/span&gt;] != &lt;span style="color: blue;"&gt;null&lt;/span&gt;) { 
    &amp;lt;ul&amp;gt;
        &amp;lt;li&amp;gt;@TempData[&lt;span style="color: #a31515;"&gt;"Result"&lt;/span&gt;]&amp;lt;/li&amp;gt;
    &amp;lt;/ul&amp;gt;
}

@&lt;span style="color: blue;"&gt;using&lt;/span&gt; (Html.BeginForm(&lt;span style="color: #a31515;"&gt;"index"&lt;/span&gt;, &lt;span style="color: #a31515;"&gt;"sample"&lt;/span&gt;, 
    FormMethod.Post, &lt;span style="color: blue;"&gt;new&lt;/span&gt; { enctype = &lt;span style="color: #a31515;"&gt;"multipart/form-data"&lt;/span&gt; })) {
 
    @Html.AntiForgeryToken()
    
    &amp;lt;input type=&lt;span style="color: #a31515;"&gt;"file"&lt;/span&gt; name=&lt;span style="color: #a31515;"&gt;"File"&lt;/span&gt; /&amp;gt;
    &amp;lt;p&amp;gt;
        &amp;lt;input type=&lt;span style="color: #a31515;"&gt;"submit"&lt;/span&gt; value=&lt;span style="color: #a31515;"&gt;"Upload"&lt;/span&gt; /&amp;gt;
    &amp;lt;/p&amp;gt;
}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Actually, code explains itself nicely here but I see one thing to worth pointing out here. &lt;a title="http://msdn.microsoft.com/en-us/library/system.web.mvc.httppostedfilebasemodelbinder(v=VS.98).aspx" href="http://msdn.microsoft.com/en-us/library/system.web.mvc.httppostedfilebasemodelbinder(v=VS.98).aspx" target="_blank"&gt;HttpPostedFileBaseModelBinder&lt;/a&gt; class from System.Web.Mvc namespace is the class which binds a model to a posted file. So, the parameter (which is type of &lt;a title="http://msdn.microsoft.com/en-us/library/system.web.httppostedfilebase.aspx" href="http://msdn.microsoft.com/en-us/library/system.web.httppostedfilebase.aspx" target="_blank"&gt;HttpPostedFileBase&lt;/a&gt;) of the Index_post method receives the posted file.&lt;/p&gt;
&lt;p&gt;Pretty neat stuff. Hope you enjoy it.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/CLte2wPnDttmxHvvyKUhB-kXTG0/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/CLte2wPnDttmxHvvyKUhB-kXTG0/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/CLte2wPnDttmxHvvyKUhB-kXTG0/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/CLte2wPnDttmxHvvyKUhB-kXTG0/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/TugberkUgurlu/~4/yJjQTxddSJo" height="1" width="1"/&gt;</description><pubDate>Thu, 29 Dec 2011 21:25:00 Z</pubDate><a10:updated>2011-12-29T21:25:00Z</a10:updated><a10:content type="text">&lt;p&gt;Today, I decided to change my blogging style a little bit. I always try to write posts which contains detailed information about the subject but I realized that it prevents me from writing much more useful things. So, from now on, I will drop a bunch of code on the screen and talk about that briefly.&lt;/p&gt;
&lt;p&gt;This is the beginning of the series of blog posts which is about some core scenarios on &lt;a title="http://asp.net/mvc" href="http://asp.net/mvc" target="_blank"&gt;ASP.NET MVC&lt;/a&gt;. In this one, code review #1, I will give you an example on how to get file upload functionality working in ASP.NET MVC. Here is the code:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Controller:&lt;/strong&gt;&lt;/p&gt;
&lt;div class="code-wrapper border-shadow-1"&gt;
&lt;div style="background-color: white; color: black;"&gt;
&lt;pre&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;class&lt;/span&gt; SampleController : Controller {

    &lt;span style="color: blue;"&gt;public&lt;/span&gt; ActionResult Index() {
        &lt;span style="color: blue;"&gt;return&lt;/span&gt; View();
    }

    [ActionName(&lt;span style="color: #a31515;"&gt;"Index"&lt;/span&gt;)]
    [ValidateAntiForgeryToken, HttpPost]
    &lt;span style="color: blue;"&gt;public&lt;/span&gt; ActionResult Index_post(HttpPostedFileBase File) {

        &lt;span style="color: green;"&gt;//Check if the file is not null and content length is bigger than 0&lt;/span&gt;
        &lt;span style="color: blue;"&gt;if&lt;/span&gt; (File != &lt;span style="color: blue;"&gt;null&lt;/span&gt; &amp;amp;&amp;amp; File.ContentLength &amp;gt; 0) {

            &lt;span style="color: green;"&gt;//Check if folder is there&lt;/span&gt;
            &lt;span style="color: blue;"&gt;if&lt;/span&gt;(!System.IO.Directory.Exists(Server.MapPath(&lt;span style="color: #a31515;"&gt;"~/Content/PostedFiles"&lt;/span&gt;)))
                System.IO.Directory.CreateDirectory(
                    Server.MapPath(&lt;span style="color: #a31515;"&gt;"~/Content/PostedFiles"&lt;/span&gt;)
                );

            &lt;span style="color: green;"&gt;//Set the full path&lt;/span&gt;
            &lt;span style="color: blue;"&gt;string&lt;/span&gt; path = System.IO.Path.Combine(
                Server.MapPath(&lt;span style="color: #a31515;"&gt;"~/Content/PostedFiles"&lt;/span&gt;),
                System.IO.Path.GetFileName(File.FileName)
            );

            &lt;span style="color: green;"&gt;//Save the thing&lt;/span&gt;
            File.SaveAs(path);

            TempData[&lt;span style="color: #a31515;"&gt;"Result"&lt;/span&gt;] = &lt;span style="color: #a31515;"&gt;"File created successfully!"&lt;/span&gt;;
        }

        &lt;span style="color: green;"&gt;//RedirectToAction so that we can get rid of so-called "Form Resubmission"&lt;/span&gt;
        &lt;span style="color: blue;"&gt;return&lt;/span&gt; RedirectToAction(&lt;span style="color: #a31515;"&gt;"Index"&lt;/span&gt;);
    }

}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;View:&lt;/strong&gt;&lt;/p&gt;
&lt;div class="code-wrapper border-shadow-1"&gt;
&lt;div style="background-color: white; color: black;"&gt;
&lt;pre&gt;@{
    ViewBag.Title = &lt;span style="color: #a31515;"&gt;"File Upload Sample"&lt;/span&gt;;
}
&amp;lt;h2&amp;gt;File Upload Sample&amp;lt;/h2&amp;gt;

@&lt;span style="color: blue;"&gt;if&lt;/span&gt; (TempData[&lt;span style="color: #a31515;"&gt;"Result"&lt;/span&gt;] != &lt;span style="color: blue;"&gt;null&lt;/span&gt;) { 
    &amp;lt;ul&amp;gt;
        &amp;lt;li&amp;gt;@TempData[&lt;span style="color: #a31515;"&gt;"Result"&lt;/span&gt;]&amp;lt;/li&amp;gt;
    &amp;lt;/ul&amp;gt;
}

@&lt;span style="color: blue;"&gt;using&lt;/span&gt; (Html.BeginForm(&lt;span style="color: #a31515;"&gt;"index"&lt;/span&gt;, &lt;span style="color: #a31515;"&gt;"sample"&lt;/span&gt;, 
    FormMethod.Post, &lt;span style="color: blue;"&gt;new&lt;/span&gt; { enctype = &lt;span style="color: #a31515;"&gt;"multipart/form-data"&lt;/span&gt; })) {
 
    @Html.AntiForgeryToken()
    
    &amp;lt;input type=&lt;span style="color: #a31515;"&gt;"file"&lt;/span&gt; name=&lt;span style="color: #a31515;"&gt;"File"&lt;/span&gt; /&amp;gt;
    &amp;lt;p&amp;gt;
        &amp;lt;input type=&lt;span style="color: #a31515;"&gt;"submit"&lt;/span&gt; value=&lt;span style="color: #a31515;"&gt;"Upload"&lt;/span&gt; /&amp;gt;
    &amp;lt;/p&amp;gt;
}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Actually, code explains itself nicely here but I see one thing to worth pointing out here. &lt;a title="http://msdn.microsoft.com/en-us/library/system.web.mvc.httppostedfilebasemodelbinder(v=VS.98).aspx" href="http://msdn.microsoft.com/en-us/library/system.web.mvc.httppostedfilebasemodelbinder(v=VS.98).aspx" target="_blank"&gt;HttpPostedFileBaseModelBinder&lt;/a&gt; class from System.Web.Mvc namespace is the class which binds a model to a posted file. So, the parameter (which is type of &lt;a title="http://msdn.microsoft.com/en-us/library/system.web.httppostedfilebase.aspx" href="http://msdn.microsoft.com/en-us/library/system.web.httppostedfilebase.aspx" target="_blank"&gt;HttpPostedFileBase&lt;/a&gt;) of the Index_post method receives the posted file.&lt;/p&gt;
&lt;p&gt;Pretty neat stuff. Hope you enjoy it.&lt;/p&gt;</a10:content><feedburner:origLink>http://tugberkugurlu.com/78</feedburner:origLink></item><item><guid isPermaLink="false">http://tugberkugurlu.com/75</guid><link>http://feedproxy.google.com/~r/TugberkUgurlu/~3/-eTf2aAYjn8/75</link><title>How to Work With Generic Repositories on ASP.NET MVC and Unit Testing Them By Mocking</title><description>&lt;p&gt;&lt;a href="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/How-to-Wor.NET-MVC-and-Unit-Testing-Them_FE36/image.png"&gt;&lt;img height="244" width="155" src="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/How-to-Wor.NET-MVC-and-Unit-Testing-Them_FE36/image_thumb.png" align="left" alt="image" border="0" title="image" style="background-image: none; margin: 0px 15px 10px 0px; padding-left: 0px; padding-right: 0px; display: inline; float: left; padding-top: 0px; border: 0px;" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;I have blogged about &lt;a target="_blank" href="http://www.tugberkugurlu.com/archive/generic-repository-pattern-entity-framework-asp-net-mvc-and-unit-testing-triangle" title="http://www.tugberkugurlu.com/archive/generic-repository-pattern-entity-framework-asp-net-mvc-and-unit-testing-triangle"&gt;Generic Repository Pattern - Entity Framework, ASP.NET MVC and Unit Testing Triangle&lt;/a&gt; but that blog post only contains how we can implement that pattern on our DAL (Data Access Layer) project. Now, let&amp;rsquo;s see how it fits into our &lt;a target="_blank" href="http://asp.net/mvc" title="http://asp.net/mvc"&gt;ASP.NET MVC&lt;/a&gt; application.&lt;/p&gt;
&lt;p&gt;I created a sample project which basically has CRUD operations for &lt;strong&gt;Foo&lt;/strong&gt; class and I put the complete code of the project on &lt;a target="_blank" href="https://github.com" title="https://github.com"&gt;GitHub&lt;/a&gt;. &lt;a target="_blank" href="https://github.com/tugberkugurlu/GenericRepoWebApp" title="https://github.com/tugberkugurlu/GenericRepoWebApp"&gt;https://github.com/tugberkugurlu/GenericRepoWebApp&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;So, in the previous blog post we have created our DAL project and repository classes based on our generic repository. I have created an ASP.NET MVC 3 Web Application project and a Visual Studio Test Project besides the DAL project. On the left had side, you can see how the project structure looks like inside solution explorer.&lt;/p&gt;
&lt;p&gt;When we think about the project, we have repository classes which implements &lt;strong&gt;GenericRepositroy&amp;lt;C, T&amp;gt;&lt;/strong&gt; abstract class and individual repository interfaces. So, we need a way to inject our concrete repository classes into our ASP.NET MVC application so that we make our application loosely coupled which means unit testing friendly.&lt;/p&gt;
&lt;p&gt;Fortunately, ASP.NET MVC has been built with unit testing in mind so it makes dependency injection very easy with &lt;a target="_blank" href="http://msdn.microsoft.com/en-us/library/system.web.mvc.dependencyresolver(v=vs.98).aspx" title="http://msdn.microsoft.com/en-us/library/system.web.mvc.dependencyresolver(v=vs.98).aspx"&gt;DependencyResolver&lt;/a&gt; class. DependencyResolver class Provides a registration point for dependency resolvers that implement &lt;a target="_blank" href="http://msdn.microsoft.com/en-us/library/system.web.mvc.idependencyresolver(v=vs.98).aspx" title="http://msdn.microsoft.com/en-us/library/system.web.mvc.idependencyresolver(v=vs.98).aspx"&gt;IDependencyResolver&lt;/a&gt; or the Common Service Locator IServiceLocator interface. But we won&amp;rsquo;t be dealing with that class at all. Instead, we will use a third party dependency injector called &lt;a target="_blank" href="http://ninject.org/" title="http://ninject.org/"&gt;Ninject&lt;/a&gt;. Ninject also has a package which benefits from DependencyResolver of ASP.NET MVC. We will use &lt;a target="_blank" href="http://nuget.org" title="http://nuget.org"&gt;Nuget&lt;/a&gt; to bring down Ninject.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/How-to-Wor.NET-MVC-and-Unit-Testing-Them_FE36/image_3.png"&gt;&lt;img height="67" width="640" src="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/How-to-Wor.NET-MVC-and-Unit-Testing-Them_FE36/image_thumb_3.png" alt="image" border="0" title="image" style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;After we install the package, we will see a folder named App_Start added to our project. Inside that folder, open up the NinjectMVC3.cs file and go to RegisterServices method. In our case, here what we do:&lt;/p&gt;
&lt;div class="code-wrapper border-shadow-1"&gt;
&lt;div style="background-color: white; color: black;"&gt;
&lt;pre&gt;&lt;span style="color: gray;"&gt;///&lt;/span&gt; &lt;span style="color: gray;"&gt;&amp;lt;summary&amp;gt;&lt;/span&gt;
&lt;span style="color: gray;"&gt;///&lt;/span&gt;&lt;span style="color: green;"&gt; Load your modules or register your services here!&lt;/span&gt;
&lt;span style="color: gray;"&gt;///&lt;/span&gt; &lt;span style="color: gray;"&gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;
&lt;span style="color: gray;"&gt;///&lt;/span&gt; &lt;span style="color: gray;"&gt;&amp;lt;param name="kernel"&amp;gt;&lt;/span&gt;&lt;span style="color: green;"&gt;The kernel.&amp;lt;/param&amp;gt;&lt;/span&gt;
&lt;span style="color: blue;"&gt;private&lt;/span&gt; &lt;span style="color: blue;"&gt;static&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; RegisterServices(IKernel kernel) {

    kernel.Bind&amp;lt;IFooRepository&amp;gt;().To&amp;lt;FooRepository&amp;gt;();
    kernel.Bind&amp;lt;IBarRepository&amp;gt;().To&amp;lt;BarReposiltory&amp;gt;();
}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;This does some stuff behind the scenes and I won&amp;rsquo;t go into details here but I really recommend you to go and take a look at series of blog posts which &lt;a target="_blank" href="http://bradwilson.typepad.com" title="http://bradwilson.typepad.com"&gt;Brad Wilson&lt;/a&gt; has done on &lt;a target="_blank" href="http://bradwilson.typepad.com/blog/2010/07/service-location-pt1-introduction.html" title="http://bradwilson.typepad.com/blog/2010/07/service-location-pt1-introduction.html"&gt;ASP.NET MVC 3 Service Location&lt;/a&gt;. However, if we try to explain it with simplest words, Ninject news up the controller classes with parameters which we specify. In this case, if a controller constructor accepts a parameter which is type of IFooRepository, Ninject will give it FooRepository class and news it up. We will see why this is useful on unit testing stage.&lt;/p&gt;
&lt;p&gt;When we look inside the RegisterServices method, we don&amp;rsquo;t see neither GenericRepository nor IGenericRepository because the way we implement them enables them to work behind the scenes.&lt;/p&gt;
&lt;p&gt;As for the implementation, here how the controller looks like:&lt;/p&gt;
&lt;div class="code-wrapper border-shadow-1"&gt;
&lt;div style="background-color: white; color: black;"&gt;
&lt;pre&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;class&lt;/span&gt; FooController : Controller {

    &lt;span style="color: blue;"&gt;private&lt;/span&gt; &lt;span style="color: blue;"&gt;readonly&lt;/span&gt; IFooRepository _fooRepo;
    
    &lt;span style="color: blue;"&gt;public&lt;/span&gt; FooController(IFooRepository fooRepo) {
        _fooRepo = fooRepo;
    }

    &lt;span style="color: blue;"&gt;public&lt;/span&gt; ViewResult Index() {

        &lt;span style="color: blue;"&gt;var&lt;/span&gt; model = _fooRepo.GetAll();
        &lt;span style="color: blue;"&gt;return&lt;/span&gt; View(model);
    }

    &lt;span style="color: blue;"&gt;public&lt;/span&gt; ActionResult Details(&lt;span style="color: blue;"&gt;int&lt;/span&gt; id) {

        &lt;span style="color: blue;"&gt;var&lt;/span&gt; model = _fooRepo.GetSingle(id);
        &lt;span style="color: blue;"&gt;if&lt;/span&gt; (model == &lt;span style="color: blue;"&gt;null&lt;/span&gt;)
            &lt;span style="color: blue;"&gt;return&lt;/span&gt; HttpNotFound();

        &lt;span style="color: blue;"&gt;return&lt;/span&gt; View(model);
    }

    &lt;span style="color: blue;"&gt;public&lt;/span&gt; ActionResult Edit(&lt;span style="color: blue;"&gt;int&lt;/span&gt; id) {

        &lt;span style="color: blue;"&gt;var&lt;/span&gt; model = _fooRepo.GetSingle(id);
        &lt;span style="color: blue;"&gt;if&lt;/span&gt; (model == &lt;span style="color: blue;"&gt;null&lt;/span&gt;)
            &lt;span style="color: blue;"&gt;return&lt;/span&gt; HttpNotFound();

        &lt;span style="color: blue;"&gt;return&lt;/span&gt; View(model);
    }

    [ActionName(&lt;span style="color: #a31515;"&gt;"Edit"&lt;/span&gt;), HttpPost]
    &lt;span style="color: blue;"&gt;public&lt;/span&gt; ActionResult Edit_post(Foo foo) {

        &lt;span style="color: blue;"&gt;if&lt;/span&gt; (ModelState.IsValid) {

            &lt;span style="color: blue;"&gt;try&lt;/span&gt; {
                _fooRepo.Edit(foo);
                _fooRepo.Save();

                &lt;span style="color: blue;"&gt;return&lt;/span&gt; RedirectToAction(&lt;span style="color: #a31515;"&gt;"details"&lt;/span&gt;, &lt;span style="color: blue;"&gt;new&lt;/span&gt; { id = foo.FooId });

            } &lt;span style="color: blue;"&gt;catch&lt;/span&gt; (Exception ex) {
                ModelState.AddModelError(&lt;span style="color: blue;"&gt;string&lt;/span&gt;.Empty, &lt;span style="color: #a31515;"&gt;"Something went wrong. Message: "&lt;/span&gt; + ex.Message);
            }
        }

        &lt;span style="color: green;"&gt;//If we come here, something went wrong. Return it back.&lt;/span&gt;
        &lt;span style="color: blue;"&gt;return&lt;/span&gt; View(foo);
    }

    &lt;span style="color: blue;"&gt;public&lt;/span&gt; ActionResult Create() {

        &lt;span style="color: blue;"&gt;return&lt;/span&gt; View();
    }

    [ActionName(&lt;span style="color: #a31515;"&gt;"Create"&lt;/span&gt;), HttpPost]
    &lt;span style="color: blue;"&gt;public&lt;/span&gt; ActionResult Create_post(Foo foo) {

        &lt;span style="color: blue;"&gt;if&lt;/span&gt; (ModelState.IsValid) {

            &lt;span style="color: blue;"&gt;try&lt;/span&gt; {
                _fooRepo.Add(foo);
                _fooRepo.Save();

                &lt;span style="color: blue;"&gt;return&lt;/span&gt; RedirectToAction(&lt;span style="color: #a31515;"&gt;"details"&lt;/span&gt;, &lt;span style="color: blue;"&gt;new&lt;/span&gt; { id = foo.FooId });

            } &lt;span style="color: blue;"&gt;catch&lt;/span&gt; (Exception ex) {
                ModelState.AddModelError(&lt;span style="color: blue;"&gt;string&lt;/span&gt;.Empty, &lt;span style="color: #a31515;"&gt;"Something went wrong. Message: "&lt;/span&gt; + ex.Message);
            }
        }

        &lt;span style="color: green;"&gt;//If we come here, something went wrong. Return it back.&lt;/span&gt;
        &lt;span style="color: blue;"&gt;return&lt;/span&gt; View(foo);

    }

    &lt;span style="color: blue;"&gt;public&lt;/span&gt; ActionResult Delete(&lt;span style="color: blue;"&gt;int&lt;/span&gt; id) {

        &lt;span style="color: blue;"&gt;var&lt;/span&gt; model = _fooRepo.GetSingle(id);
        &lt;span style="color: blue;"&gt;if&lt;/span&gt; (model == &lt;span style="color: blue;"&gt;null&lt;/span&gt;)
            &lt;span style="color: blue;"&gt;return&lt;/span&gt; HttpNotFound();

        &lt;span style="color: blue;"&gt;return&lt;/span&gt; View(model);
    }

    [ActionName(&lt;span style="color: #a31515;"&gt;"Delete"&lt;/span&gt;), HttpPost]
    &lt;span style="color: blue;"&gt;public&lt;/span&gt; ActionResult Delete_post(&lt;span style="color: blue;"&gt;int&lt;/span&gt; id) {

        &lt;span style="color: blue;"&gt;var&lt;/span&gt; model = _fooRepo.GetSingle(id);
        &lt;span style="color: blue;"&gt;if&lt;/span&gt; (model == &lt;span style="color: blue;"&gt;null&lt;/span&gt;)
            &lt;span style="color: blue;"&gt;return&lt;/span&gt; HttpNotFound();

        _fooRepo.Delete(model);
        _fooRepo.Save();

        &lt;span style="color: blue;"&gt;return&lt;/span&gt; RedirectToAction(&lt;span style="color: #a31515;"&gt;"Index"&lt;/span&gt;);
    }
}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;All of the action methods do some very basic stuff but one thing to notice here is the below code:&lt;/p&gt;
&lt;div class="code-wrapper border-shadow-1"&gt;
&lt;div style="background-color: white; color: black;"&gt;
&lt;pre&gt;    &lt;span style="color: blue;"&gt;private&lt;/span&gt; &lt;span style="color: blue;"&gt;readonly&lt;/span&gt; IFooRepository _fooRepo;
    
    &lt;span style="color: blue;"&gt;public&lt;/span&gt; FooController(IFooRepository fooRepo) {
        _fooRepo = fooRepo;
    }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;As I mentioned before, controller constructor accepts a parameter which is type of IFooRepository and inside the constructor method, we expose the parameter for internal use of that controller class.&lt;/p&gt;
&lt;p&gt;I have some views which corresponds to each action method and they work as expected.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Unit Testing&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;So, how do we unit test that controller without connecting to our database? When we think theoretically, what we need is a fake repository which implements IFooRepository interface so that we can pass that fake repository into our controller as a constructor parameter. Pay attention here that we still has no need for neither generic repository interface nor generic repository abstract class. We just need to fake FooRepository interface with fake data.&lt;/p&gt;
&lt;p&gt;We will do this by mocking and creating &lt;a target="_blank" href="http://en.wikipedia.org/wiki/Mock_object" title="http://en.wikipedia.org/wiki/Mock_object"&gt;mock objects&lt;/a&gt;. In order to do that, we will benefit from an awesome library called &lt;a target="_blank" href="http://code.google.com/p/moq" title="http://code.google.com/p/moq"&gt;Moq&lt;/a&gt;.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;As you can see inside the project on GitHub, I didn&amp;rsquo;t use NuGet to bring down the Moq because I tried and it failed over and over again. So, I put that inside the lib folder under the root directory and reference it from there.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;After you reference the Moq library inside the Test application, create a class named FooControllerTest.cs. Here how it should look like at first:&lt;/p&gt;
&lt;div class="code-wrapper border-shadow-1"&gt;
&lt;div style="background-color: white; color: black;"&gt;
&lt;pre&gt;[TestClass]
&lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;class&lt;/span&gt; FooControllerTest {

}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;So empty. We will start to fill it in with a mock of IFooReporsitory. Below, you can see the complete code which enables that:&lt;/p&gt;
&lt;div class="code-wrapper border-shadow-1"&gt;
&lt;div style="background-color: white; color: black;"&gt;
&lt;pre&gt;[TestClass]
&lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;class&lt;/span&gt; FooControllerTest {

    &lt;span style="color: blue;"&gt;private&lt;/span&gt; IFooRepository fooRepo;

    [TestInitialize]
    &lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; Initialize() {

        &lt;span style="color: green;"&gt;//Mock repository creation&lt;/span&gt;
        Mock&amp;lt;IFooRepository&amp;gt; mock = &lt;span style="color: blue;"&gt;new&lt;/span&gt; Mock&amp;lt;IFooRepository&amp;gt;();
        mock.Setup(m =&amp;gt; m.GetAll()).Returns(&lt;span style="color: blue;"&gt;new&lt;/span&gt;[] { 
            &lt;span style="color: blue;"&gt;new&lt;/span&gt; Foo { FooId = 1, FooName = &lt;span style="color: #a31515;"&gt;"Fake Foo 1"&lt;/span&gt; },
            &lt;span style="color: blue;"&gt;new&lt;/span&gt; Foo { FooId = 2, FooName = &lt;span style="color: #a31515;"&gt;"Fake Foo 2"&lt;/span&gt; },
            &lt;span style="color: blue;"&gt;new&lt;/span&gt; Foo { FooId = 3, FooName = &lt;span style="color: #a31515;"&gt;"Fake Foo 3"&lt;/span&gt; },
            &lt;span style="color: blue;"&gt;new&lt;/span&gt; Foo { FooId = 4, FooName = &lt;span style="color: #a31515;"&gt;"Fake Foo 4"&lt;/span&gt; }
        }.AsQueryable());

        mock.Setup(m =&amp;gt; 
            m.GetSingle(
                It.Is&amp;lt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&amp;gt;(i =&amp;gt; 
                    i == 1 || i == 2 || i == 3 || i == 4
                )
            )
        ).Returns&amp;lt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&amp;gt;(r =&amp;gt; &lt;span style="color: blue;"&gt;new&lt;/span&gt; Foo { 
            FooId = r,
            FooName = &lt;span style="color: blue;"&gt;string&lt;/span&gt;.Format(&lt;span style="color: #a31515;"&gt;"Fake Foo {0}"&lt;/span&gt;, r)
        });

        fooRepo = mock.Object;
    }

}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;This Initialize method will run before all of the test methods run so we can work inside that method in order to mock our object.&lt;/p&gt;
&lt;p&gt;In here, first I have setup a mock for &lt;strong&gt;GetAll&lt;/strong&gt; method result and it returns 4 instances or Foo class.&lt;/p&gt;
&lt;p&gt;Second, I do the same thing for &lt;strong&gt;GetSingle&lt;/strong&gt; method. It looks a little different because it accepts a parameter type of &lt;strong&gt;int&lt;/strong&gt;. What I am telling there is that: there are 4 instances I have here and if the parameter matches one of those instances, it will returns a Foo class which is a type of &lt;strong&gt;GetSingle&lt;/strong&gt; method returns.&lt;/p&gt;
&lt;p&gt;Lastly, I expose the mock object for internal use for the test class.&lt;/p&gt;
&lt;p&gt;Now we have an IFooRepository instance we can work with. We have completed the hard part and now we can start writing our unit tests. Here is some of possible unit tests that we can have:&lt;/p&gt;
&lt;div class="code-wrapper border-shadow-1"&gt;
&lt;div style="background-color: white; color: black;"&gt;
&lt;pre&gt;[TestClass]
&lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;class&lt;/span&gt; FooControllerTest {

    &lt;span style="color: blue;"&gt;private&lt;/span&gt; IFooRepository fooRepo;

    [TestInitialize]
    &lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; Initialize() {

        &lt;span style="color: green;"&gt;//Mock repository creation&lt;/span&gt;
        Mock&amp;lt;IFooRepository&amp;gt; mock = &lt;span style="color: blue;"&gt;new&lt;/span&gt; Mock&amp;lt;IFooRepository&amp;gt;();
        mock.Setup(m =&amp;gt; m.GetAll()).Returns(&lt;span style="color: blue;"&gt;new&lt;/span&gt;[] { 
            &lt;span style="color: blue;"&gt;new&lt;/span&gt; Foo { FooId = 1, FooName = &lt;span style="color: #a31515;"&gt;"Fake Foo 1"&lt;/span&gt; },
            &lt;span style="color: blue;"&gt;new&lt;/span&gt; Foo { FooId = 2, FooName = &lt;span style="color: #a31515;"&gt;"Fake Foo 2"&lt;/span&gt; },
            &lt;span style="color: blue;"&gt;new&lt;/span&gt; Foo { FooId = 3, FooName = &lt;span style="color: #a31515;"&gt;"Fake Foo 3"&lt;/span&gt; },
            &lt;span style="color: blue;"&gt;new&lt;/span&gt; Foo { FooId = 4, FooName = &lt;span style="color: #a31515;"&gt;"Fake Foo 4"&lt;/span&gt; }
        }.AsQueryable());

        mock.Setup(m =&amp;gt; 
            m.GetSingle(
                It.Is&amp;lt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&amp;gt;(i =&amp;gt; 
                    i == 1 || i == 2 || i == 3 || i == 4
                )
            )
        ).Returns&amp;lt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&amp;gt;(r =&amp;gt; &lt;span style="color: blue;"&gt;new&lt;/span&gt; Foo { 
            FooId = r,
            FooName = &lt;span style="color: blue;"&gt;string&lt;/span&gt;.Format(&lt;span style="color: #a31515;"&gt;"Fake Foo {0}"&lt;/span&gt;, r)
        });

        fooRepo = mock.Object;
    }

    [TestMethod]
    &lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; is_index_returns_model_type_of_iqueryable_foo() {
        
        &lt;span style="color: green;"&gt;//Arrange&lt;/span&gt;
        &lt;span style="color: green;"&gt;//Create the controller instance&lt;/span&gt;
        FooController fooController = &lt;span style="color: blue;"&gt;new&lt;/span&gt; FooController(fooRepo);

        &lt;span style="color: green;"&gt;//Act&lt;/span&gt;
        &lt;span style="color: blue;"&gt;var&lt;/span&gt; indexModel = fooController.Index().Model;

        &lt;span style="color: green;"&gt;//Assert&lt;/span&gt;
        Assert.IsInstanceOfType(indexModel, &lt;span style="color: blue;"&gt;typeof&lt;/span&gt;(IQueryable&amp;lt;Foo&amp;gt;));
    }

    [TestMethod]
    &lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; is_index_returns_iqueryable_foo_count_of_4() {

        &lt;span style="color: green;"&gt;//Arrange&lt;/span&gt;
        &lt;span style="color: green;"&gt;//Create the controller instance&lt;/span&gt;
        FooController fooController = &lt;span style="color: blue;"&gt;new&lt;/span&gt; FooController(fooRepo);

        &lt;span style="color: green;"&gt;//Act&lt;/span&gt;
        &lt;span style="color: blue;"&gt;var&lt;/span&gt; indexModel = (IQueryable&amp;lt;&lt;span style="color: blue;"&gt;object&lt;/span&gt;&amp;gt;)fooController.Index().Model;

        &lt;span style="color: green;"&gt;//Assert&lt;/span&gt;
        Assert.AreEqual&amp;lt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&amp;gt;(4, indexModel.Count());
    }

    [TestMethod]
    &lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; is_details_returns_type_of_ViewResult() {

        &lt;span style="color: green;"&gt;//Arrange&lt;/span&gt;
        &lt;span style="color: green;"&gt;//Create the controller instance&lt;/span&gt;
        FooController fooController = &lt;span style="color: blue;"&gt;new&lt;/span&gt; FooController(fooRepo);

        &lt;span style="color: green;"&gt;//Act&lt;/span&gt;
        &lt;span style="color: blue;"&gt;var&lt;/span&gt; detailsResult = fooController.Details(1);

        &lt;span style="color: green;"&gt;//Assert&lt;/span&gt;
        Assert.IsInstanceOfType(detailsResult, &lt;span style="color: blue;"&gt;typeof&lt;/span&gt;(ViewResult));
    }

    [TestMethod]
    &lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; is_details_returns_type_of_HttpNotFoundResult() { 

        &lt;span style="color: green;"&gt;//Arrange&lt;/span&gt;
        &lt;span style="color: green;"&gt;//Create the controller instance&lt;/span&gt;
        FooController fooController = &lt;span style="color: blue;"&gt;new&lt;/span&gt; FooController(fooRepo);

        &lt;span style="color: green;"&gt;//Act&lt;/span&gt;
        &lt;span style="color: blue;"&gt;var&lt;/span&gt; detailsResult = fooController.Details(5);

        &lt;span style="color: green;"&gt;//Assert&lt;/span&gt;
        Assert.IsInstanceOfType(detailsResult, &lt;span style="color: blue;"&gt;typeof&lt;/span&gt;(HttpNotFoundResult));
    }
}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;When I run the all the test, I should see all of them pass and I do:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/How-to-Wor.NET-MVC-and-Unit-Testing-Them_FE36/image_4.png"&gt;&lt;img height="196" width="644" src="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/How-to-Wor.NET-MVC-and-Unit-Testing-Them_FE36/image_thumb_4.png" alt="image" border="0" title="image" style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;I hope this blog post gives you an idea. Stay tuned for others &lt;img src="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/How-to-Wor.NET-MVC-and-Unit-Testing-Them_FE36/wlEmoticon-winkingsmile.png" alt="Winking smile" class="wlEmoticon wlEmoticon-winkingsmile" style="border-style: none;" /&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;NOTE:&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Entity Framework DbContext Generic Repository Implementation Is Now On Nuget and GitHub:&amp;nbsp;&lt;a target="_blank" href="http://www.tugberkugurlu.com/archive/entity-framework-dbcontext-generic-repository-implementation-is-now-on-nuget-and-github"&gt;http://www.tugberkugurlu.com/archive/entity-framework-dbcontext-generic-repository-implementation-is-now-on-nuget-and-github&lt;/a&gt;&lt;strong&gt;&lt;br /&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/gnx2KZFKhq2UnTKKcYVEjFZCz-0/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/gnx2KZFKhq2UnTKKcYVEjFZCz-0/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/gnx2KZFKhq2UnTKKcYVEjFZCz-0/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/gnx2KZFKhq2UnTKKcYVEjFZCz-0/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/TugberkUgurlu/~4/-eTf2aAYjn8" height="1" width="1"/&gt;</description><pubDate>Thu, 22 Dec 2011 17:05:00 Z</pubDate><a10:updated>2011-12-22T17:05:00Z</a10:updated><a10:content type="text">&lt;p&gt;&lt;a href="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/How-to-Wor.NET-MVC-and-Unit-Testing-Them_FE36/image.png"&gt;&lt;img height="244" width="155" src="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/How-to-Wor.NET-MVC-and-Unit-Testing-Them_FE36/image_thumb.png" align="left" alt="image" border="0" title="image" style="background-image: none; margin: 0px 15px 10px 0px; padding-left: 0px; padding-right: 0px; display: inline; float: left; padding-top: 0px; border: 0px;" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;I have blogged about &lt;a target="_blank" href="http://www.tugberkugurlu.com/archive/generic-repository-pattern-entity-framework-asp-net-mvc-and-unit-testing-triangle" title="http://www.tugberkugurlu.com/archive/generic-repository-pattern-entity-framework-asp-net-mvc-and-unit-testing-triangle"&gt;Generic Repository Pattern - Entity Framework, ASP.NET MVC and Unit Testing Triangle&lt;/a&gt; but that blog post only contains how we can implement that pattern on our DAL (Data Access Layer) project. Now, let&amp;rsquo;s see how it fits into our &lt;a target="_blank" href="http://asp.net/mvc" title="http://asp.net/mvc"&gt;ASP.NET MVC&lt;/a&gt; application.&lt;/p&gt;
&lt;p&gt;I created a sample project which basically has CRUD operations for &lt;strong&gt;Foo&lt;/strong&gt; class and I put the complete code of the project on &lt;a target="_blank" href="https://github.com" title="https://github.com"&gt;GitHub&lt;/a&gt;. &lt;a target="_blank" href="https://github.com/tugberkugurlu/GenericRepoWebApp" title="https://github.com/tugberkugurlu/GenericRepoWebApp"&gt;https://github.com/tugberkugurlu/GenericRepoWebApp&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;So, in the previous blog post we have created our DAL project and repository classes based on our generic repository. I have created an ASP.NET MVC 3 Web Application project and a Visual Studio Test Project besides the DAL project. On the left had side, you can see how the project structure looks like inside solution explorer.&lt;/p&gt;
&lt;p&gt;When we think about the project, we have repository classes which implements &lt;strong&gt;GenericRepositroy&amp;lt;C, T&amp;gt;&lt;/strong&gt; abstract class and individual repository interfaces. So, we need a way to inject our concrete repository classes into our ASP.NET MVC application so that we make our application loosely coupled which means unit testing friendly.&lt;/p&gt;
&lt;p&gt;Fortunately, ASP.NET MVC has been built with unit testing in mind so it makes dependency injection very easy with &lt;a target="_blank" href="http://msdn.microsoft.com/en-us/library/system.web.mvc.dependencyresolver(v=vs.98).aspx" title="http://msdn.microsoft.com/en-us/library/system.web.mvc.dependencyresolver(v=vs.98).aspx"&gt;DependencyResolver&lt;/a&gt; class. DependencyResolver class Provides a registration point for dependency resolvers that implement &lt;a target="_blank" href="http://msdn.microsoft.com/en-us/library/system.web.mvc.idependencyresolver(v=vs.98).aspx" title="http://msdn.microsoft.com/en-us/library/system.web.mvc.idependencyresolver(v=vs.98).aspx"&gt;IDependencyResolver&lt;/a&gt; or the Common Service Locator IServiceLocator interface. But we won&amp;rsquo;t be dealing with that class at all. Instead, we will use a third party dependency injector called &lt;a target="_blank" href="http://ninject.org/" title="http://ninject.org/"&gt;Ninject&lt;/a&gt;. Ninject also has a package which benefits from DependencyResolver of ASP.NET MVC. We will use &lt;a target="_blank" href="http://nuget.org" title="http://nuget.org"&gt;Nuget&lt;/a&gt; to bring down Ninject.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/How-to-Wor.NET-MVC-and-Unit-Testing-Them_FE36/image_3.png"&gt;&lt;img height="67" width="640" src="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/How-to-Wor.NET-MVC-and-Unit-Testing-Them_FE36/image_thumb_3.png" alt="image" border="0" title="image" style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;After we install the package, we will see a folder named App_Start added to our project. Inside that folder, open up the NinjectMVC3.cs file and go to RegisterServices method. In our case, here what we do:&lt;/p&gt;
&lt;div class="code-wrapper border-shadow-1"&gt;
&lt;div style="background-color: white; color: black;"&gt;
&lt;pre&gt;&lt;span style="color: gray;"&gt;///&lt;/span&gt; &lt;span style="color: gray;"&gt;&amp;lt;summary&amp;gt;&lt;/span&gt;
&lt;span style="color: gray;"&gt;///&lt;/span&gt;&lt;span style="color: green;"&gt; Load your modules or register your services here!&lt;/span&gt;
&lt;span style="color: gray;"&gt;///&lt;/span&gt; &lt;span style="color: gray;"&gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;
&lt;span style="color: gray;"&gt;///&lt;/span&gt; &lt;span style="color: gray;"&gt;&amp;lt;param name="kernel"&amp;gt;&lt;/span&gt;&lt;span style="color: green;"&gt;The kernel.&amp;lt;/param&amp;gt;&lt;/span&gt;
&lt;span style="color: blue;"&gt;private&lt;/span&gt; &lt;span style="color: blue;"&gt;static&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; RegisterServices(IKernel kernel) {

    kernel.Bind&amp;lt;IFooRepository&amp;gt;().To&amp;lt;FooRepository&amp;gt;();
    kernel.Bind&amp;lt;IBarRepository&amp;gt;().To&amp;lt;BarReposiltory&amp;gt;();
}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;This does some stuff behind the scenes and I won&amp;rsquo;t go into details here but I really recommend you to go and take a look at series of blog posts which &lt;a target="_blank" href="http://bradwilson.typepad.com" title="http://bradwilson.typepad.com"&gt;Brad Wilson&lt;/a&gt; has done on &lt;a target="_blank" href="http://bradwilson.typepad.com/blog/2010/07/service-location-pt1-introduction.html" title="http://bradwilson.typepad.com/blog/2010/07/service-location-pt1-introduction.html"&gt;ASP.NET MVC 3 Service Location&lt;/a&gt;. However, if we try to explain it with simplest words, Ninject news up the controller classes with parameters which we specify. In this case, if a controller constructor accepts a parameter which is type of IFooRepository, Ninject will give it FooRepository class and news it up. We will see why this is useful on unit testing stage.&lt;/p&gt;
&lt;p&gt;When we look inside the RegisterServices method, we don&amp;rsquo;t see neither GenericRepository nor IGenericRepository because the way we implement them enables them to work behind the scenes.&lt;/p&gt;
&lt;p&gt;As for the implementation, here how the controller looks like:&lt;/p&gt;
&lt;div class="code-wrapper border-shadow-1"&gt;
&lt;div style="background-color: white; color: black;"&gt;
&lt;pre&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;class&lt;/span&gt; FooController : Controller {

    &lt;span style="color: blue;"&gt;private&lt;/span&gt; &lt;span style="color: blue;"&gt;readonly&lt;/span&gt; IFooRepository _fooRepo;
    
    &lt;span style="color: blue;"&gt;public&lt;/span&gt; FooController(IFooRepository fooRepo) {
        _fooRepo = fooRepo;
    }

    &lt;span style="color: blue;"&gt;public&lt;/span&gt; ViewResult Index() {

        &lt;span style="color: blue;"&gt;var&lt;/span&gt; model = _fooRepo.GetAll();
        &lt;span style="color: blue;"&gt;return&lt;/span&gt; View(model);
    }

    &lt;span style="color: blue;"&gt;public&lt;/span&gt; ActionResult Details(&lt;span style="color: blue;"&gt;int&lt;/span&gt; id) {

        &lt;span style="color: blue;"&gt;var&lt;/span&gt; model = _fooRepo.GetSingle(id);
        &lt;span style="color: blue;"&gt;if&lt;/span&gt; (model == &lt;span style="color: blue;"&gt;null&lt;/span&gt;)
            &lt;span style="color: blue;"&gt;return&lt;/span&gt; HttpNotFound();

        &lt;span style="color: blue;"&gt;return&lt;/span&gt; View(model);
    }

    &lt;span style="color: blue;"&gt;public&lt;/span&gt; ActionResult Edit(&lt;span style="color: blue;"&gt;int&lt;/span&gt; id) {

        &lt;span style="color: blue;"&gt;var&lt;/span&gt; model = _fooRepo.GetSingle(id);
        &lt;span style="color: blue;"&gt;if&lt;/span&gt; (model == &lt;span style="color: blue;"&gt;null&lt;/span&gt;)
            &lt;span style="color: blue;"&gt;return&lt;/span&gt; HttpNotFound();

        &lt;span style="color: blue;"&gt;return&lt;/span&gt; View(model);
    }

    [ActionName(&lt;span style="color: #a31515;"&gt;"Edit"&lt;/span&gt;), HttpPost]
    &lt;span style="color: blue;"&gt;public&lt;/span&gt; ActionResult Edit_post(Foo foo) {

        &lt;span style="color: blue;"&gt;if&lt;/span&gt; (ModelState.IsValid) {

            &lt;span style="color: blue;"&gt;try&lt;/span&gt; {
                _fooRepo.Edit(foo);
                _fooRepo.Save();

                &lt;span style="color: blue;"&gt;return&lt;/span&gt; RedirectToAction(&lt;span style="color: #a31515;"&gt;"details"&lt;/span&gt;, &lt;span style="color: blue;"&gt;new&lt;/span&gt; { id = foo.FooId });

            } &lt;span style="color: blue;"&gt;catch&lt;/span&gt; (Exception ex) {
                ModelState.AddModelError(&lt;span style="color: blue;"&gt;string&lt;/span&gt;.Empty, &lt;span style="color: #a31515;"&gt;"Something went wrong. Message: "&lt;/span&gt; + ex.Message);
            }
        }

        &lt;span style="color: green;"&gt;//If we come here, something went wrong. Return it back.&lt;/span&gt;
        &lt;span style="color: blue;"&gt;return&lt;/span&gt; View(foo);
    }

    &lt;span style="color: blue;"&gt;public&lt;/span&gt; ActionResult Create() {

        &lt;span style="color: blue;"&gt;return&lt;/span&gt; View();
    }

    [ActionName(&lt;span style="color: #a31515;"&gt;"Create"&lt;/span&gt;), HttpPost]
    &lt;span style="color: blue;"&gt;public&lt;/span&gt; ActionResult Create_post(Foo foo) {

        &lt;span style="color: blue;"&gt;if&lt;/span&gt; (ModelState.IsValid) {

            &lt;span style="color: blue;"&gt;try&lt;/span&gt; {
                _fooRepo.Add(foo);
                _fooRepo.Save();

                &lt;span style="color: blue;"&gt;return&lt;/span&gt; RedirectToAction(&lt;span style="color: #a31515;"&gt;"details"&lt;/span&gt;, &lt;span style="color: blue;"&gt;new&lt;/span&gt; { id = foo.FooId });

            } &lt;span style="color: blue;"&gt;catch&lt;/span&gt; (Exception ex) {
                ModelState.AddModelError(&lt;span style="color: blue;"&gt;string&lt;/span&gt;.Empty, &lt;span style="color: #a31515;"&gt;"Something went wrong. Message: "&lt;/span&gt; + ex.Message);
            }
        }

        &lt;span style="color: green;"&gt;//If we come here, something went wrong. Return it back.&lt;/span&gt;
        &lt;span style="color: blue;"&gt;return&lt;/span&gt; View(foo);

    }

    &lt;span style="color: blue;"&gt;public&lt;/span&gt; ActionResult Delete(&lt;span style="color: blue;"&gt;int&lt;/span&gt; id) {

        &lt;span style="color: blue;"&gt;var&lt;/span&gt; model = _fooRepo.GetSingle(id);
        &lt;span style="color: blue;"&gt;if&lt;/span&gt; (model == &lt;span style="color: blue;"&gt;null&lt;/span&gt;)
            &lt;span style="color: blue;"&gt;return&lt;/span&gt; HttpNotFound();

        &lt;span style="color: blue;"&gt;return&lt;/span&gt; View(model);
    }

    [ActionName(&lt;span style="color: #a31515;"&gt;"Delete"&lt;/span&gt;), HttpPost]
    &lt;span style="color: blue;"&gt;public&lt;/span&gt; ActionResult Delete_post(&lt;span style="color: blue;"&gt;int&lt;/span&gt; id) {

        &lt;span style="color: blue;"&gt;var&lt;/span&gt; model = _fooRepo.GetSingle(id);
        &lt;span style="color: blue;"&gt;if&lt;/span&gt; (model == &lt;span style="color: blue;"&gt;null&lt;/span&gt;)
            &lt;span style="color: blue;"&gt;return&lt;/span&gt; HttpNotFound();

        _fooRepo.Delete(model);
        _fooRepo.Save();

        &lt;span style="color: blue;"&gt;return&lt;/span&gt; RedirectToAction(&lt;span style="color: #a31515;"&gt;"Index"&lt;/span&gt;);
    }
}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;All of the action methods do some very basic stuff but one thing to notice here is the below code:&lt;/p&gt;
&lt;div class="code-wrapper border-shadow-1"&gt;
&lt;div style="background-color: white; color: black;"&gt;
&lt;pre&gt;    &lt;span style="color: blue;"&gt;private&lt;/span&gt; &lt;span style="color: blue;"&gt;readonly&lt;/span&gt; IFooRepository _fooRepo;
    
    &lt;span style="color: blue;"&gt;public&lt;/span&gt; FooController(IFooRepository fooRepo) {
        _fooRepo = fooRepo;
    }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;As I mentioned before, controller constructor accepts a parameter which is type of IFooRepository and inside the constructor method, we expose the parameter for internal use of that controller class.&lt;/p&gt;
&lt;p&gt;I have some views which corresponds to each action method and they work as expected.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Unit Testing&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;So, how do we unit test that controller without connecting to our database? When we think theoretically, what we need is a fake repository which implements IFooRepository interface so that we can pass that fake repository into our controller as a constructor parameter. Pay attention here that we still has no need for neither generic repository interface nor generic repository abstract class. We just need to fake FooRepository interface with fake data.&lt;/p&gt;
&lt;p&gt;We will do this by mocking and creating &lt;a target="_blank" href="http://en.wikipedia.org/wiki/Mock_object" title="http://en.wikipedia.org/wiki/Mock_object"&gt;mock objects&lt;/a&gt;. In order to do that, we will benefit from an awesome library called &lt;a target="_blank" href="http://code.google.com/p/moq" title="http://code.google.com/p/moq"&gt;Moq&lt;/a&gt;.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;As you can see inside the project on GitHub, I didn&amp;rsquo;t use NuGet to bring down the Moq because I tried and it failed over and over again. So, I put that inside the lib folder under the root directory and reference it from there.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;After you reference the Moq library inside the Test application, create a class named FooControllerTest.cs. Here how it should look like at first:&lt;/p&gt;
&lt;div class="code-wrapper border-shadow-1"&gt;
&lt;div style="background-color: white; color: black;"&gt;
&lt;pre&gt;[TestClass]
&lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;class&lt;/span&gt; FooControllerTest {

}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;So empty. We will start to fill it in with a mock of IFooReporsitory. Below, you can see the complete code which enables that:&lt;/p&gt;
&lt;div class="code-wrapper border-shadow-1"&gt;
&lt;div style="background-color: white; color: black;"&gt;
&lt;pre&gt;[TestClass]
&lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;class&lt;/span&gt; FooControllerTest {

    &lt;span style="color: blue;"&gt;private&lt;/span&gt; IFooRepository fooRepo;

    [TestInitialize]
    &lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; Initialize() {

        &lt;span style="color: green;"&gt;//Mock repository creation&lt;/span&gt;
        Mock&amp;lt;IFooRepository&amp;gt; mock = &lt;span style="color: blue;"&gt;new&lt;/span&gt; Mock&amp;lt;IFooRepository&amp;gt;();
        mock.Setup(m =&amp;gt; m.GetAll()).Returns(&lt;span style="color: blue;"&gt;new&lt;/span&gt;[] { 
            &lt;span style="color: blue;"&gt;new&lt;/span&gt; Foo { FooId = 1, FooName = &lt;span style="color: #a31515;"&gt;"Fake Foo 1"&lt;/span&gt; },
            &lt;span style="color: blue;"&gt;new&lt;/span&gt; Foo { FooId = 2, FooName = &lt;span style="color: #a31515;"&gt;"Fake Foo 2"&lt;/span&gt; },
            &lt;span style="color: blue;"&gt;new&lt;/span&gt; Foo { FooId = 3, FooName = &lt;span style="color: #a31515;"&gt;"Fake Foo 3"&lt;/span&gt; },
            &lt;span style="color: blue;"&gt;new&lt;/span&gt; Foo { FooId = 4, FooName = &lt;span style="color: #a31515;"&gt;"Fake Foo 4"&lt;/span&gt; }
        }.AsQueryable());

        mock.Setup(m =&amp;gt; 
            m.GetSingle(
                It.Is&amp;lt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&amp;gt;(i =&amp;gt; 
                    i == 1 || i == 2 || i == 3 || i == 4
                )
            )
        ).Returns&amp;lt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&amp;gt;(r =&amp;gt; &lt;span style="color: blue;"&gt;new&lt;/span&gt; Foo { 
            FooId = r,
            FooName = &lt;span style="color: blue;"&gt;string&lt;/span&gt;.Format(&lt;span style="color: #a31515;"&gt;"Fake Foo {0}"&lt;/span&gt;, r)
        });

        fooRepo = mock.Object;
    }

}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;This Initialize method will run before all of the test methods run so we can work inside that method in order to mock our object.&lt;/p&gt;
&lt;p&gt;In here, first I have setup a mock for &lt;strong&gt;GetAll&lt;/strong&gt; method result and it returns 4 instances or Foo class.&lt;/p&gt;
&lt;p&gt;Second, I do the same thing for &lt;strong&gt;GetSingle&lt;/strong&gt; method. It looks a little different because it accepts a parameter type of &lt;strong&gt;int&lt;/strong&gt;. What I am telling there is that: there are 4 instances I have here and if the parameter matches one of those instances, it will returns a Foo class which is a type of &lt;strong&gt;GetSingle&lt;/strong&gt; method returns.&lt;/p&gt;
&lt;p&gt;Lastly, I expose the mock object for internal use for the test class.&lt;/p&gt;
&lt;p&gt;Now we have an IFooRepository instance we can work with. We have completed the hard part and now we can start writing our unit tests. Here is some of possible unit tests that we can have:&lt;/p&gt;
&lt;div class="code-wrapper border-shadow-1"&gt;
&lt;div style="background-color: white; color: black;"&gt;
&lt;pre&gt;[TestClass]
&lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;class&lt;/span&gt; FooControllerTest {

    &lt;span style="color: blue;"&gt;private&lt;/span&gt; IFooRepository fooRepo;

    [TestInitialize]
    &lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; Initialize() {

        &lt;span style="color: green;"&gt;//Mock repository creation&lt;/span&gt;
        Mock&amp;lt;IFooRepository&amp;gt; mock = &lt;span style="color: blue;"&gt;new&lt;/span&gt; Mock&amp;lt;IFooRepository&amp;gt;();
        mock.Setup(m =&amp;gt; m.GetAll()).Returns(&lt;span style="color: blue;"&gt;new&lt;/span&gt;[] { 
            &lt;span style="color: blue;"&gt;new&lt;/span&gt; Foo { FooId = 1, FooName = &lt;span style="color: #a31515;"&gt;"Fake Foo 1"&lt;/span&gt; },
            &lt;span style="color: blue;"&gt;new&lt;/span&gt; Foo { FooId = 2, FooName = &lt;span style="color: #a31515;"&gt;"Fake Foo 2"&lt;/span&gt; },
            &lt;span style="color: blue;"&gt;new&lt;/span&gt; Foo { FooId = 3, FooName = &lt;span style="color: #a31515;"&gt;"Fake Foo 3"&lt;/span&gt; },
            &lt;span style="color: blue;"&gt;new&lt;/span&gt; Foo { FooId = 4, FooName = &lt;span style="color: #a31515;"&gt;"Fake Foo 4"&lt;/span&gt; }
        }.AsQueryable());

        mock.Setup(m =&amp;gt; 
            m.GetSingle(
                It.Is&amp;lt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&amp;gt;(i =&amp;gt; 
                    i == 1 || i == 2 || i == 3 || i == 4
                )
            )
        ).Returns&amp;lt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&amp;gt;(r =&amp;gt; &lt;span style="color: blue;"&gt;new&lt;/span&gt; Foo { 
            FooId = r,
            FooName = &lt;span style="color: blue;"&gt;string&lt;/span&gt;.Format(&lt;span style="color: #a31515;"&gt;"Fake Foo {0}"&lt;/span&gt;, r)
        });

        fooRepo = mock.Object;
    }

    [TestMethod]
    &lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; is_index_returns_model_type_of_iqueryable_foo() {
        
        &lt;span style="color: green;"&gt;//Arrange&lt;/span&gt;
        &lt;span style="color: green;"&gt;//Create the controller instance&lt;/span&gt;
        FooController fooController = &lt;span style="color: blue;"&gt;new&lt;/span&gt; FooController(fooRepo);

        &lt;span style="color: green;"&gt;//Act&lt;/span&gt;
        &lt;span style="color: blue;"&gt;var&lt;/span&gt; indexModel = fooController.Index().Model;

        &lt;span style="color: green;"&gt;//Assert&lt;/span&gt;
        Assert.IsInstanceOfType(indexModel, &lt;span style="color: blue;"&gt;typeof&lt;/span&gt;(IQueryable&amp;lt;Foo&amp;gt;));
    }

    [TestMethod]
    &lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; is_index_returns_iqueryable_foo_count_of_4() {

        &lt;span style="color: green;"&gt;//Arrange&lt;/span&gt;
        &lt;span style="color: green;"&gt;//Create the controller instance&lt;/span&gt;
        FooController fooController = &lt;span style="color: blue;"&gt;new&lt;/span&gt; FooController(fooRepo);

        &lt;span style="color: green;"&gt;//Act&lt;/span&gt;
        &lt;span style="color: blue;"&gt;var&lt;/span&gt; indexModel = (IQueryable&amp;lt;&lt;span style="color: blue;"&gt;object&lt;/span&gt;&amp;gt;)fooController.Index().Model;

        &lt;span style="color: green;"&gt;//Assert&lt;/span&gt;
        Assert.AreEqual&amp;lt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&amp;gt;(4, indexModel.Count());
    }

    [TestMethod]
    &lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; is_details_returns_type_of_ViewResult() {

        &lt;span style="color: green;"&gt;//Arrange&lt;/span&gt;
        &lt;span style="color: green;"&gt;//Create the controller instance&lt;/span&gt;
        FooController fooController = &lt;span style="color: blue;"&gt;new&lt;/span&gt; FooController(fooRepo);

        &lt;span style="color: green;"&gt;//Act&lt;/span&gt;
        &lt;span style="color: blue;"&gt;var&lt;/span&gt; detailsResult = fooController.Details(1);

        &lt;span style="color: green;"&gt;//Assert&lt;/span&gt;
        Assert.IsInstanceOfType(detailsResult, &lt;span style="color: blue;"&gt;typeof&lt;/span&gt;(ViewResult));
    }

    [TestMethod]
    &lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; is_details_returns_type_of_HttpNotFoundResult() { 

        &lt;span style="color: green;"&gt;//Arrange&lt;/span&gt;
        &lt;span style="color: green;"&gt;//Create the controller instance&lt;/span&gt;
        FooController fooController = &lt;span style="color: blue;"&gt;new&lt;/span&gt; FooController(fooRepo);

        &lt;span style="color: green;"&gt;//Act&lt;/span&gt;
        &lt;span style="color: blue;"&gt;var&lt;/span&gt; detailsResult = fooController.Details(5);

        &lt;span style="color: green;"&gt;//Assert&lt;/span&gt;
        Assert.IsInstanceOfType(detailsResult, &lt;span style="color: blue;"&gt;typeof&lt;/span&gt;(HttpNotFoundResult));
    }
}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;When I run the all the test, I should see all of them pass and I do:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/How-to-Wor.NET-MVC-and-Unit-Testing-Them_FE36/image_4.png"&gt;&lt;img height="196" width="644" src="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/How-to-Wor.NET-MVC-and-Unit-Testing-Them_FE36/image_thumb_4.png" alt="image" border="0" title="image" style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;I hope this blog post gives you an idea. Stay tuned for others &lt;img src="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/How-to-Wor.NET-MVC-and-Unit-Testing-Them_FE36/wlEmoticon-winkingsmile.png" alt="Winking smile" class="wlEmoticon wlEmoticon-winkingsmile" style="border-style: none;" /&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;NOTE:&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Entity Framework DbContext Generic Repository Implementation Is Now On Nuget and GitHub:&amp;nbsp;&lt;a target="_blank" href="http://www.tugberkugurlu.com/archive/entity-framework-dbcontext-generic-repository-implementation-is-now-on-nuget-and-github"&gt;http://www.tugberkugurlu.com/archive/entity-framework-dbcontext-generic-repository-implementation-is-now-on-nuget-and-github&lt;/a&gt;&lt;strong&gt;&lt;br /&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;</a10:content><feedburner:origLink>http://tugberkugurlu.com/75</feedburner:origLink></item><item><guid isPermaLink="false">http://tugberkugurlu.com/74</guid><link>http://feedproxy.google.com/~r/TugberkUgurlu/~3/JqWgl_lANIM/74</link><title>Generic Repository Pattern - Entity Framework, ASP.NET MVC and Unit Testing Triangle</title><description>&lt;p&gt;&lt;strong&gt;DRY&lt;/strong&gt;: &lt;a title="http://en.wikipedia.org/wiki/Don't_repeat_yourself" href="http://en.wikipedia.org/wiki/Don't_repeat_yourself" target="_blank"&gt;Don&amp;rsquo;t repeat yourself&lt;/a&gt; which is a &lt;strong&gt;principle&lt;/strong&gt; of software development aimed at reducing repetition of information of all kinds, especially useful in multi-tier architectures. That&amp;rsquo;s what &lt;a title="http://en.wikipedia.org" href="http://en.wikipedia.org" target="_blank"&gt;Wikipedia&lt;/a&gt; says. In my words, if you are writing same code twice, follow these steps:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Step back. &lt;/li&gt;
&lt;li&gt;Sit. &lt;/li&gt;
&lt;li&gt;Think about it and dwell on that. &lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;That&amp;rsquo;s what I have done for repository classes on my DAL projects. I nearly all the time use Entity Framework to reach out my database and I create repositories in order to query and manipulate data inside that database. There are some specific methods which I use for every single repository. As you can assume, those are &lt;em&gt;FindBy&lt;/em&gt;, &lt;em&gt;Add&lt;/em&gt;, &lt;em&gt;Edit&lt;/em&gt;, &lt;em&gt;Delete&lt;/em&gt;, &lt;em&gt;Save&lt;/em&gt;. Let&amp;rsquo;s see on code what is my story here.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;First approach (worst approach)&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;At first, long time ago, I have been creating all the single methods for each interface. For example below one is one of my repository interfaces:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;I am giving examples here with EF 4.2 but I was following this approach with EF 4 which does not contain DbContext class.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;div class="code-wrapper border-shadow-1"&gt;
&lt;div style="background-color: white; color: black;"&gt;
&lt;pre&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;interface&lt;/span&gt; IFooRepository {
        
    IQueryable&amp;lt;Foo&amp;gt; GetAll();
    Foo GetSingle(int fooId);
    IQueryable&amp;lt;Foo&amp;gt; FindBy(Expression&amp;lt;Func&amp;lt;Foo, &lt;span style="color: blue;"&gt;bool&lt;/span&gt;&amp;gt;&amp;gt; predicate);
    &lt;span style="color: blue;"&gt;void&lt;/span&gt; Add(Foo entity);
    &lt;span style="color: blue;"&gt;void&lt;/span&gt; Delete(Foo entity);
    &lt;span style="color: blue;"&gt;void&lt;/span&gt; Edit(Foo entity);
    &lt;span style="color: blue;"&gt;void&lt;/span&gt; Save();
}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;This repo is for &lt;strong&gt;Foo&lt;/strong&gt; class I have (imaginary). Let's see the implementation for &lt;strong&gt;Bar&lt;/strong&gt; class.&lt;/p&gt;
&lt;div class="code-wrapper border-shadow-1"&gt;
&lt;div style="background-color: white; color: black;"&gt;
&lt;pre&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;interface&lt;/span&gt; IBarRepository {
    
    IQueryable&amp;lt;Bar&amp;gt; GetAll();
    Bar GetSingle(&lt;span style="color: blue;"&gt;int&lt;/span&gt; barId);
    IQueryable&amp;lt;Bar&amp;gt; FindBy(Expression&amp;lt;Func&amp;lt;Bar, &lt;span style="color: blue;"&gt;bool&lt;/span&gt;&amp;gt;&amp;gt; predicate);
    &lt;span style="color: blue;"&gt;void&lt;/span&gt; Add(Bar entity);
    &lt;span style="color: blue;"&gt;void&lt;/span&gt; Delete(Bar entity);
    &lt;span style="color: blue;"&gt;void&lt;/span&gt; Edit(Bar entity);
    &lt;span style="color: blue;"&gt;void&lt;/span&gt; Save();
}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Implementation nearly exactly the same here. Here is also an example of implementing one of these interfaces:&lt;/p&gt;
&lt;div class="code-wrapper border-shadow-1"&gt;
&lt;div style="background-color: white; color: black;"&gt;
&lt;pre&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;class&lt;/span&gt; FooRepository : IFooRepository {

    &lt;span style="color: blue;"&gt;private&lt;/span&gt; readonly FooBarEntities context = &lt;span style="color: blue;"&gt;new&lt;/span&gt; FooBarEntities();

    &lt;span style="color: blue;"&gt;public&lt;/span&gt; IQueryable&amp;lt;Foo&amp;gt; GetAll() {

        IQueryable&amp;lt;Foo&amp;gt; query = context.Foos;
        &lt;span style="color: blue;"&gt;return&lt;/span&gt; query;
    }

    &lt;span style="color: blue;"&gt;public&lt;/span&gt; Foo GetSingle(&lt;span style="color: blue;"&gt;int&lt;/span&gt; fooId) {

        &lt;span style="color: blue;"&gt;var&lt;/span&gt; query = &lt;span style="color: blue;"&gt;this&lt;/span&gt;.GetAll().FirstOrDefault(x =&amp;gt; x.FooId == fooId);
        &lt;span style="color: blue;"&gt;return&lt;/span&gt; query;
    }

    &lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; Add(Foo entity) {

        context.Foos.Add(entity);
    }

    &lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; Delete(Foo entity) {

        context.Foos.Remove(entity);
    }

    &lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; Edit(Foo entity) {

        context.Entry&amp;lt;Foo&amp;gt;(entity).State = System.Data.EntityState.Modified;
    }

    &lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; Save() {

        context.SaveChanges();
    }
}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Also imagine this implementation for BarRepository as well. Indeed, there would be probably more repository classes for your project. After playing like that for a while I decided to do something different which still sucked but better.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;A better approach but still sucks&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;I created a generic interface which saves me a lot of keystrokes. Here how it looks like:&lt;/p&gt;
&lt;div class="code-wrapper border-shadow-1"&gt;
&lt;div style="background-color: white; color: black;"&gt;
&lt;pre&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;interface&lt;/span&gt; IGenericRepository&amp;lt;T&amp;gt; &lt;span style="color: blue;"&gt;where&lt;/span&gt; T : &lt;span style="color: blue;"&gt;class&lt;/span&gt; {
    
    IQueryable&amp;lt;T&amp;gt; GetAll();
    IQueryable&amp;lt;T&amp;gt; FindBy(Expression&amp;lt;Func&amp;lt;T, &lt;span style="color: blue;"&gt;bool&lt;/span&gt;&amp;gt;&amp;gt; predicate);
    &lt;span style="color: blue;"&gt;void&lt;/span&gt; Add(T entity);
    &lt;span style="color: blue;"&gt;void&lt;/span&gt; Delete(T entity);
    &lt;span style="color: blue;"&gt;void&lt;/span&gt; Edit(T entity);
    &lt;span style="color: blue;"&gt;void&lt;/span&gt; Save();
}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;And how I implemented in on repository interfaces:&lt;/p&gt;
&lt;div class="code-wrapper border-shadow-1"&gt;
&lt;div style="background-color: white; color: black;"&gt;
&lt;pre&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;interface&lt;/span&gt; IFooRepository : IGenericRepository&amp;lt;Foo&amp;gt; {
    
    Foo GetSingle(&lt;span style="color: blue;"&gt;int&lt;/span&gt; fooId);
}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class="code-wrapper border-shadow-1"&gt;
&lt;div style="background-color: white; color: black;"&gt;
&lt;pre&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;interface&lt;/span&gt; IBarRepository : IGenericRepository&amp;lt;Bar&amp;gt; {
    
    Bar GetSingle(&lt;span style="color: blue;"&gt;int&lt;/span&gt; barId);
}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;You can see that I only needed to implement &lt;strong&gt;GetSingle&lt;/strong&gt; method here and others come with &lt;strong&gt;IGenericRepositoy&amp;lt;T&amp;gt;&lt;/strong&gt; interface.&lt;/p&gt;
&lt;p&gt;Where I implement these repository interfaces to my concrete classes, I still need to go over all the methods and create them individually. The repository class looked like as the same. So it leads me to a final solution which is the best one I can come up with so far.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Best approach&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The generic interface I have created is still legitimate and usable here. In fact, I won&amp;rsquo;t touch the repository interfaces at all. What I did here first is to create an abstract class which implements IGenericReposity&amp;lt;T&amp;gt; interface but also accepts another type parameter defined in a generic declaration which is a type of DbConetxt class. Here is how it looks like:&lt;/p&gt;
&lt;div class="code-wrapper border-shadow-1"&gt;
&lt;div style="background-color: white; color: black;"&gt;
&lt;pre&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;abstract&lt;/span&gt; &lt;span style="color: blue;"&gt;class&lt;/span&gt; GenericRepository&amp;lt;C, T&amp;gt; : 
    IGenericRepository&amp;lt;T&amp;gt; &lt;span style="color: blue;"&gt;where&lt;/span&gt; T : &lt;span style="color: blue;"&gt;class&lt;/span&gt; &lt;span style="color: blue;"&gt;where&lt;/span&gt; C : DbContext, &lt;span style="color: blue;"&gt;new&lt;/span&gt;() {

    &lt;span style="color: blue;"&gt;private&lt;/span&gt; C _entities = &lt;span style="color: blue;"&gt;new&lt;/span&gt; C();
    &lt;span style="color: blue;"&gt;public&lt;/span&gt; C Context {

        &lt;span style="color: blue;"&gt;get&lt;/span&gt; { &lt;span style="color: blue;"&gt;return&lt;/span&gt; _entities; }
        &lt;span style="color: blue;"&gt;set&lt;/span&gt; { _entities = value; }
    }

    &lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;virtual&lt;/span&gt; IQueryable&amp;lt;T&amp;gt; GetAll() {

        IQueryable&amp;lt;T&amp;gt; query = _entities.Set&amp;lt;T&amp;gt;();
        &lt;span style="color: blue;"&gt;return&lt;/span&gt; query;
    }

    &lt;span style="color: blue;"&gt;public&lt;/span&gt; IQueryable&amp;lt;T&amp;gt; FindBy(System.Linq.Expressions.Expression&amp;lt;Func&amp;lt;T, &lt;span style="color: blue;"&gt;bool&lt;/span&gt;&amp;gt;&amp;gt; predicate) {

        IQueryable&amp;lt;T&amp;gt; query = _entities.Set&amp;lt;T&amp;gt;().Where(predicate);
        &lt;span style="color: blue;"&gt;return&lt;/span&gt; query;
    }

    &lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;virtual&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; Add(T entity) {
        _entities.Set&amp;lt;T&amp;gt;().Add(entity);
    }

    &lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;virtual&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; Delete(T entity) {
        _entities.Set&amp;lt;T&amp;gt;().Remove(entity);
    }

    &lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;virtual&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; Edit(T entity) {
        _entities.Entry(entity).State = System.Data.EntityState.Modified;
    }

    &lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;virtual&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; Save() {
        _entities.SaveChanges();
    }
}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;This is so nice because of some factors I like:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;This implements so basic and ordinary methods&lt;/li&gt;
&lt;li&gt;If necessary, those methods can be overridden because each method is virtual.&lt;/li&gt;
&lt;li&gt;As we newed up the DbContext class here and expose it public with a public property, we have flexibility of extend the individual repositories for our needs.&lt;/li&gt;
&lt;li&gt;As we only implement this abstract class only to our repository classes, it won&amp;rsquo;t effect unit testing at all. DbContext is not in the picture in terms of unit testing.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;So, when we need to implement these changes to our concrete repository classes, we will end up with following result:&lt;/p&gt;
&lt;div class="code-wrapper border-shadow-1"&gt;
&lt;div style="background-color: white; color: black;"&gt;
&lt;pre&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;class&lt;/span&gt; FooRepository :
    GenericRepository&amp;lt;FooBarEntities, Foo&amp;gt;, IFooRepository {

    &lt;span style="color: blue;"&gt;public&lt;/span&gt; Foo GetSingle(&lt;span style="color: blue;"&gt;int&lt;/span&gt; fooId) {

        &lt;span style="color: blue;"&gt;var&lt;/span&gt; query = GetAll().FirstOrDefault(x =&amp;gt; x.FooId == fooId);
        &lt;span style="color: blue;"&gt;return&lt;/span&gt; query;
    }
}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class="code-wrapper border-shadow-1"&gt;
&lt;div style="background-color: white; color: black;"&gt;
&lt;pre&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;class&lt;/span&gt; BarReposiltory : 
    GenericRepository&amp;lt;FooBarEntities, Bar&amp;gt;, IBarRepository  {

    &lt;span style="color: blue;"&gt;public&lt;/span&gt; Bar GetSingle(&lt;span style="color: blue;"&gt;int&lt;/span&gt; barId) {

        &lt;span style="color: blue;"&gt;var&lt;/span&gt; query = Context.Bars.FirstOrDefault(x =&amp;gt; x.BarId == barId);
        &lt;span style="color: blue;"&gt;return&lt;/span&gt; query;
    }
}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Very nice and clean. Inside &lt;strong&gt;BarRepository&lt;/strong&gt; &lt;strong&gt;GetSingle&lt;/strong&gt; method, as you see I use &lt;strong&gt;Context&lt;/strong&gt; property of &lt;strong&gt;GenericRepository&amp;lt;C, T&amp;gt;&lt;/strong&gt; abstract class to access an instance of DbContext.&lt;/p&gt;
&lt;p&gt;So, how the things work inside our &lt;a title="http://asp.net/mvc" href="http://asp.net/mvc" target="_blank"&gt;ASP.NET MVC&lt;/a&gt; project? this is another story but no so complicated. I will continue right from here on my next post.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;UPDATE:&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Here is the next post:&lt;/p&gt;
&lt;p&gt;&lt;a target="_blank" title="http://www.tugberkugurlu.com/archive/how-to-work-with-generic-repositories-on-asp-net-mvc-and-unit-testing-them-by-mocking" href="http://www.tugberkugurlu.com/archive/how-to-work-with-generic-repositories-on-asp-net-mvc-and-unit-testing-them-by-mocking"&gt;How to Work With Generic Repositories on ASP.NET MVC and Unit Testing Them By Mocking&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/0eNOjlp7BtPypGrU1pyYO_yFFJM/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/0eNOjlp7BtPypGrU1pyYO_yFFJM/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/0eNOjlp7BtPypGrU1pyYO_yFFJM/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/0eNOjlp7BtPypGrU1pyYO_yFFJM/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/TugberkUgurlu/~4/JqWgl_lANIM" height="1" width="1"/&gt;</description><pubDate>Thu, 22 Dec 2011 09:07:00 Z</pubDate><a10:updated>2011-12-22T09:07:00Z</a10:updated><a10:content type="text">&lt;p&gt;&lt;strong&gt;DRY&lt;/strong&gt;: &lt;a title="http://en.wikipedia.org/wiki/Don't_repeat_yourself" href="http://en.wikipedia.org/wiki/Don't_repeat_yourself" target="_blank"&gt;Don&amp;rsquo;t repeat yourself&lt;/a&gt; which is a &lt;strong&gt;principle&lt;/strong&gt; of software development aimed at reducing repetition of information of all kinds, especially useful in multi-tier architectures. That&amp;rsquo;s what &lt;a title="http://en.wikipedia.org" href="http://en.wikipedia.org" target="_blank"&gt;Wikipedia&lt;/a&gt; says. In my words, if you are writing same code twice, follow these steps:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Step back. &lt;/li&gt;
&lt;li&gt;Sit. &lt;/li&gt;
&lt;li&gt;Think about it and dwell on that. &lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;That&amp;rsquo;s what I have done for repository classes on my DAL projects. I nearly all the time use Entity Framework to reach out my database and I create repositories in order to query and manipulate data inside that database. There are some specific methods which I use for every single repository. As you can assume, those are &lt;em&gt;FindBy&lt;/em&gt;, &lt;em&gt;Add&lt;/em&gt;, &lt;em&gt;Edit&lt;/em&gt;, &lt;em&gt;Delete&lt;/em&gt;, &lt;em&gt;Save&lt;/em&gt;. Let&amp;rsquo;s see on code what is my story here.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;First approach (worst approach)&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;At first, long time ago, I have been creating all the single methods for each interface. For example below one is one of my repository interfaces:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;I am giving examples here with EF 4.2 but I was following this approach with EF 4 which does not contain DbContext class.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;div class="code-wrapper border-shadow-1"&gt;
&lt;div style="background-color: white; color: black;"&gt;
&lt;pre&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;interface&lt;/span&gt; IFooRepository {
        
    IQueryable&amp;lt;Foo&amp;gt; GetAll();
    Foo GetSingle(int fooId);
    IQueryable&amp;lt;Foo&amp;gt; FindBy(Expression&amp;lt;Func&amp;lt;Foo, &lt;span style="color: blue;"&gt;bool&lt;/span&gt;&amp;gt;&amp;gt; predicate);
    &lt;span style="color: blue;"&gt;void&lt;/span&gt; Add(Foo entity);
    &lt;span style="color: blue;"&gt;void&lt;/span&gt; Delete(Foo entity);
    &lt;span style="color: blue;"&gt;void&lt;/span&gt; Edit(Foo entity);
    &lt;span style="color: blue;"&gt;void&lt;/span&gt; Save();
}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;This repo is for &lt;strong&gt;Foo&lt;/strong&gt; class I have (imaginary). Let's see the implementation for &lt;strong&gt;Bar&lt;/strong&gt; class.&lt;/p&gt;
&lt;div class="code-wrapper border-shadow-1"&gt;
&lt;div style="background-color: white; color: black;"&gt;
&lt;pre&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;interface&lt;/span&gt; IBarRepository {
    
    IQueryable&amp;lt;Bar&amp;gt; GetAll();
    Bar GetSingle(&lt;span style="color: blue;"&gt;int&lt;/span&gt; barId);
    IQueryable&amp;lt;Bar&amp;gt; FindBy(Expression&amp;lt;Func&amp;lt;Bar, &lt;span style="color: blue;"&gt;bool&lt;/span&gt;&amp;gt;&amp;gt; predicate);
    &lt;span style="color: blue;"&gt;void&lt;/span&gt; Add(Bar entity);
    &lt;span style="color: blue;"&gt;void&lt;/span&gt; Delete(Bar entity);
    &lt;span style="color: blue;"&gt;void&lt;/span&gt; Edit(Bar entity);
    &lt;span style="color: blue;"&gt;void&lt;/span&gt; Save();
}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Implementation nearly exactly the same here. Here is also an example of implementing one of these interfaces:&lt;/p&gt;
&lt;div class="code-wrapper border-shadow-1"&gt;
&lt;div style="background-color: white; color: black;"&gt;
&lt;pre&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;class&lt;/span&gt; FooRepository : IFooRepository {

    &lt;span style="color: blue;"&gt;private&lt;/span&gt; readonly FooBarEntities context = &lt;span style="color: blue;"&gt;new&lt;/span&gt; FooBarEntities();

    &lt;span style="color: blue;"&gt;public&lt;/span&gt; IQueryable&amp;lt;Foo&amp;gt; GetAll() {

        IQueryable&amp;lt;Foo&amp;gt; query = context.Foos;
        &lt;span style="color: blue;"&gt;return&lt;/span&gt; query;
    }

    &lt;span style="color: blue;"&gt;public&lt;/span&gt; Foo GetSingle(&lt;span style="color: blue;"&gt;int&lt;/span&gt; fooId) {

        &lt;span style="color: blue;"&gt;var&lt;/span&gt; query = &lt;span style="color: blue;"&gt;this&lt;/span&gt;.GetAll().FirstOrDefault(x =&amp;gt; x.FooId == fooId);
        &lt;span style="color: blue;"&gt;return&lt;/span&gt; query;
    }

    &lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; Add(Foo entity) {

        context.Foos.Add(entity);
    }

    &lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; Delete(Foo entity) {

        context.Foos.Remove(entity);
    }

    &lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; Edit(Foo entity) {

        context.Entry&amp;lt;Foo&amp;gt;(entity).State = System.Data.EntityState.Modified;
    }

    &lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; Save() {

        context.SaveChanges();
    }
}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Also imagine this implementation for BarRepository as well. Indeed, there would be probably more repository classes for your project. After playing like that for a while I decided to do something different which still sucked but better.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;A better approach but still sucks&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;I created a generic interface which saves me a lot of keystrokes. Here how it looks like:&lt;/p&gt;
&lt;div class="code-wrapper border-shadow-1"&gt;
&lt;div style="background-color: white; color: black;"&gt;
&lt;pre&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;interface&lt;/span&gt; IGenericRepository&amp;lt;T&amp;gt; &lt;span style="color: blue;"&gt;where&lt;/span&gt; T : &lt;span style="color: blue;"&gt;class&lt;/span&gt; {
    
    IQueryable&amp;lt;T&amp;gt; GetAll();
    IQueryable&amp;lt;T&amp;gt; FindBy(Expression&amp;lt;Func&amp;lt;T, &lt;span style="color: blue;"&gt;bool&lt;/span&gt;&amp;gt;&amp;gt; predicate);
    &lt;span style="color: blue;"&gt;void&lt;/span&gt; Add(T entity);
    &lt;span style="color: blue;"&gt;void&lt;/span&gt; Delete(T entity);
    &lt;span style="color: blue;"&gt;void&lt;/span&gt; Edit(T entity);
    &lt;span style="color: blue;"&gt;void&lt;/span&gt; Save();
}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;And how I implemented in on repository interfaces:&lt;/p&gt;
&lt;div class="code-wrapper border-shadow-1"&gt;
&lt;div style="background-color: white; color: black;"&gt;
&lt;pre&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;interface&lt;/span&gt; IFooRepository : IGenericRepository&amp;lt;Foo&amp;gt; {
    
    Foo GetSingle(&lt;span style="color: blue;"&gt;int&lt;/span&gt; fooId);
}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class="code-wrapper border-shadow-1"&gt;
&lt;div style="background-color: white; color: black;"&gt;
&lt;pre&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;interface&lt;/span&gt; IBarRepository : IGenericRepository&amp;lt;Bar&amp;gt; {
    
    Bar GetSingle(&lt;span style="color: blue;"&gt;int&lt;/span&gt; barId);
}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;You can see that I only needed to implement &lt;strong&gt;GetSingle&lt;/strong&gt; method here and others come with &lt;strong&gt;IGenericRepositoy&amp;lt;T&amp;gt;&lt;/strong&gt; interface.&lt;/p&gt;
&lt;p&gt;Where I implement these repository interfaces to my concrete classes, I still need to go over all the methods and create them individually. The repository class looked like as the same. So it leads me to a final solution which is the best one I can come up with so far.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Best approach&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The generic interface I have created is still legitimate and usable here. In fact, I won&amp;rsquo;t touch the repository interfaces at all. What I did here first is to create an abstract class which implements IGenericReposity&amp;lt;T&amp;gt; interface but also accepts another type parameter defined in a generic declaration which is a type of DbConetxt class. Here is how it looks like:&lt;/p&gt;
&lt;div class="code-wrapper border-shadow-1"&gt;
&lt;div style="background-color: white; color: black;"&gt;
&lt;pre&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;abstract&lt;/span&gt; &lt;span style="color: blue;"&gt;class&lt;/span&gt; GenericRepository&amp;lt;C, T&amp;gt; : 
    IGenericRepository&amp;lt;T&amp;gt; &lt;span style="color: blue;"&gt;where&lt;/span&gt; T : &lt;span style="color: blue;"&gt;class&lt;/span&gt; &lt;span style="color: blue;"&gt;where&lt;/span&gt; C : DbContext, &lt;span style="color: blue;"&gt;new&lt;/span&gt;() {

    &lt;span style="color: blue;"&gt;private&lt;/span&gt; C _entities = &lt;span style="color: blue;"&gt;new&lt;/span&gt; C();
    &lt;span style="color: blue;"&gt;public&lt;/span&gt; C Context {

        &lt;span style="color: blue;"&gt;get&lt;/span&gt; { &lt;span style="color: blue;"&gt;return&lt;/span&gt; _entities; }
        &lt;span style="color: blue;"&gt;set&lt;/span&gt; { _entities = value; }
    }

    &lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;virtual&lt;/span&gt; IQueryable&amp;lt;T&amp;gt; GetAll() {

        IQueryable&amp;lt;T&amp;gt; query = _entities.Set&amp;lt;T&amp;gt;();
        &lt;span style="color: blue;"&gt;return&lt;/span&gt; query;
    }

    &lt;span style="color: blue;"&gt;public&lt;/span&gt; IQueryable&amp;lt;T&amp;gt; FindBy(System.Linq.Expressions.Expression&amp;lt;Func&amp;lt;T, &lt;span style="color: blue;"&gt;bool&lt;/span&gt;&amp;gt;&amp;gt; predicate) {

        IQueryable&amp;lt;T&amp;gt; query = _entities.Set&amp;lt;T&amp;gt;().Where(predicate);
        &lt;span style="color: blue;"&gt;return&lt;/span&gt; query;
    }

    &lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;virtual&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; Add(T entity) {
        _entities.Set&amp;lt;T&amp;gt;().Add(entity);
    }

    &lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;virtual&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; Delete(T entity) {
        _entities.Set&amp;lt;T&amp;gt;().Remove(entity);
    }

    &lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;virtual&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; Edit(T entity) {
        _entities.Entry(entity).State = System.Data.EntityState.Modified;
    }

    &lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;virtual&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; Save() {
        _entities.SaveChanges();
    }
}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;This is so nice because of some factors I like:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;This implements so basic and ordinary methods&lt;/li&gt;
&lt;li&gt;If necessary, those methods can be overridden because each method is virtual.&lt;/li&gt;
&lt;li&gt;As we newed up the DbContext class here and expose it public with a public property, we have flexibility of extend the individual repositories for our needs.&lt;/li&gt;
&lt;li&gt;As we only implement this abstract class only to our repository classes, it won&amp;rsquo;t effect unit testing at all. DbContext is not in the picture in terms of unit testing.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;So, when we need to implement these changes to our concrete repository classes, we will end up with following result:&lt;/p&gt;
&lt;div class="code-wrapper border-shadow-1"&gt;
&lt;div style="background-color: white; color: black;"&gt;
&lt;pre&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;class&lt;/span&gt; FooRepository :
    GenericRepository&amp;lt;FooBarEntities, Foo&amp;gt;, IFooRepository {

    &lt;span style="color: blue;"&gt;public&lt;/span&gt; Foo GetSingle(&lt;span style="color: blue;"&gt;int&lt;/span&gt; fooId) {

        &lt;span style="color: blue;"&gt;var&lt;/span&gt; query = GetAll().FirstOrDefault(x =&amp;gt; x.FooId == fooId);
        &lt;span style="color: blue;"&gt;return&lt;/span&gt; query;
    }
}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class="code-wrapper border-shadow-1"&gt;
&lt;div style="background-color: white; color: black;"&gt;
&lt;pre&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;class&lt;/span&gt; BarReposiltory : 
    GenericRepository&amp;lt;FooBarEntities, Bar&amp;gt;, IBarRepository  {

    &lt;span style="color: blue;"&gt;public&lt;/span&gt; Bar GetSingle(&lt;span style="color: blue;"&gt;int&lt;/span&gt; barId) {

        &lt;span style="color: blue;"&gt;var&lt;/span&gt; query = Context.Bars.FirstOrDefault(x =&amp;gt; x.BarId == barId);
        &lt;span style="color: blue;"&gt;return&lt;/span&gt; query;
    }
}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Very nice and clean. Inside &lt;strong&gt;BarRepository&lt;/strong&gt; &lt;strong&gt;GetSingle&lt;/strong&gt; method, as you see I use &lt;strong&gt;Context&lt;/strong&gt; property of &lt;strong&gt;GenericRepository&amp;lt;C, T&amp;gt;&lt;/strong&gt; abstract class to access an instance of DbContext.&lt;/p&gt;
&lt;p&gt;So, how the things work inside our &lt;a title="http://asp.net/mvc" href="http://asp.net/mvc" target="_blank"&gt;ASP.NET MVC&lt;/a&gt; project? this is another story but no so complicated. I will continue right from here on my next post.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;UPDATE:&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Here is the next post:&lt;/p&gt;
&lt;p&gt;&lt;a target="_blank" title="http://www.tugberkugurlu.com/archive/how-to-work-with-generic-repositories-on-asp-net-mvc-and-unit-testing-them-by-mocking" href="http://www.tugberkugurlu.com/archive/how-to-work-with-generic-repositories-on-asp-net-mvc-and-unit-testing-them-by-mocking"&gt;How to Work With Generic Repositories on ASP.NET MVC and Unit Testing Them By Mocking&lt;/a&gt;&lt;/p&gt;</a10:content><feedburner:origLink>http://tugberkugurlu.com/74</feedburner:origLink></item><item><guid isPermaLink="false">http://tugberkugurlu.com/73</guid><link>http://feedproxy.google.com/~r/TugberkUgurlu/~3/-_6kVnOEUSU/73</link><title>Make The Visual Studio Extensions Compatible With Visual Studio 11 Developer Preview</title><description>&lt;p&gt;&lt;a href="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/da94e67e689d_F189/works-on-my-machine-seal-of-approval.png"&gt;&lt;img style="background-image: none; margin: 0px 15px 10px 0px; padding-left: 0px; padding-right: 0px; display: inline; float: left; padding-top: 0px; border: 0px;" title="works-on-my-machine-seal-of-approval" border="0" alt="works-on-my-machine-seal-of-approval" align="left" src="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/da94e67e689d_F189/works-on-my-machine-seal-of-approval_thumb.png" width="200" height="193" /&gt;&lt;/a&gt;Do you use &lt;a title="http://msdn.microsoft.com/en-us/vstudio/hh127353" href="http://msdn.microsoft.com/en-us/vstudio/hh127353" target="_blank"&gt;Visual Studio 11 Developer Preview&lt;/a&gt; and miss your extensions? Well, I do! Especially, &lt;a title="http://visualstudiogallery.msdn.microsoft.com/7812b04c-db36-4817-8a84-e73c452410a2" href="http://visualstudiogallery.msdn.microsoft.com/7812b04c-db36-4817-8a84-e73c452410a2" target="_blank"&gt;ADO.NET C# DbContext Generator&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;As you know, for now Visual Studio extensions are not compatible with Visual Studio 11 Developer Preview.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/da94e67e689d_F189/image.png"&gt;&lt;img style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" title="image" border="0" alt="image" src="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/da94e67e689d_F189/image_thumb.png" width="244" height="97" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;This is normal because it is &lt;strong&gt;Developer Preview&lt;/strong&gt;. Even, I opened up a thread for that on &lt;a title="http://stackoverflow.com" href="http://stackoverflow.com" target="_blank"&gt;Stackoverflow&lt;/a&gt;: &lt;a href="http://stackoverflow.com/questions/7726121/visual-studio-extensions-and-visual-studio-11-dev-preview" target="_blank"&gt;Visual Studio Extensions and Visual Studio 11 Dev. Preview&lt;/a&gt; and &lt;a title="http://stackoverflow.com/users/629083/betty" href="http://stackoverflow.com/users/629083/betty" target="_blank"&gt;@betty&lt;/a&gt; replied to my post after a long time with &lt;a title="http://stackoverflow.com/a/8335636/463785" href="http://stackoverflow.com/a/8335636/463785" target="_blank"&gt;an amazing answer&lt;/a&gt;. I never developed a Visual Studio extension before and I am not familiar with the drill so I thought it is not possible to tweak the extensions so that they can be compatible with Visual Studio 11 Developer Preview but it turns out that it is as easy as pie. How so? Here are the steps:&lt;/p&gt;
&lt;p&gt;Oww, by the way, this thing &lt;strong&gt;works on my machine &lt;/strong&gt;and I am not guarantee that it will work on yours as well. Even worse, it can completely screw your machine up. So, take the risk here and don&amp;rsquo;t swear to me.&lt;/p&gt;
&lt;p&gt;I also assume that the &lt;a title="http://visualstudiogallery.msdn.microsoft.com/7812b04c-db36-4817-8a84-e73c452410a2" href="http://visualstudiogallery.msdn.microsoft.com/7812b04c-db36-4817-8a84-e73c452410a2" target="_blank"&gt;ADO.NET C# DbContext Generator&lt;/a&gt; does not integrate with Visual Studio that much so there was no big compatibility problems but I won&amp;rsquo;t try this for the extensions like &lt;a title="http://visualstudiogallery.msdn.microsoft.com/d491911d-97f3-4cf6-87b0-6a2882120acf?SRC=Home" href="http://visualstudiogallery.msdn.microsoft.com/d491911d-97f3-4cf6-87b0-6a2882120acf?SRC=Home" target="_blank"&gt;VSCommands 2010&lt;/a&gt;, &lt;a title="http://visualstudiogallery.msdn.microsoft.com/6ed4c78f-a23e-49ad-b5fd-369af0c2107f?SRC=Home" href="http://visualstudiogallery.msdn.microsoft.com/6ed4c78f-a23e-49ad-b5fd-369af0c2107f?SRC=Home" target="_blank"&gt;Web Essentials&lt;/a&gt;, etc. If you try, please let me know how it went. Anyway, here is the drill:&lt;/p&gt;
&lt;p&gt;First of all, download the extension from &lt;a title="http://visualstudiogallery.msdn.microsoft.com" href="http://visualstudiogallery.msdn.microsoft.com" target="_blank"&gt;Visual Studio Gallery&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/da94e67e689d_F189/image_3.png"&gt;&lt;img style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" title="image" border="0" alt="image" src="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/da94e67e689d_F189/image_thumb_3.png" width="644" height="396" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;I am not sure if you are aware of it but vsix files are actually zip files just like &lt;a title="http://nuget.org" href="http://nuget.org" target="_blank"&gt;NuGet&lt;/a&gt; package files (nupkg) and MS Word files (docx). So, change the extension of the file we have downloaded from vsix to zip:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/da94e67e689d_F189/image_4.png"&gt;&lt;img style="background-image: none; margin: 0px 15px 0px 0px; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" title="image" border="0" alt="image" src="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/da94e67e689d_F189/image_thumb_4.png" width="244" height="150" /&gt;&lt;/a&gt;&amp;nbsp;&lt;a href="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/da94e67e689d_F189/image_5.png"&gt;&lt;img style="background-image: none; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" title="image" border="0" alt="image" src="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/da94e67e689d_F189/image_thumb_5.png" width="244" height="150" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Unzip the zip file and you will see a file called &lt;strong&gt;extension.vsixmanifest &lt;/strong&gt;under root folder. No, it is not a zip file, too &lt;img style="border-style: none;" class="wlEmoticon wlEmoticon-openmouthedsmile" alt="Open-mouthed smile" src="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/da94e67e689d_F189/wlEmoticon-openmouthedsmile.png" /&gt; It is an xml file though. Open it up with a text editor and find the node called &lt;strong&gt;SupportedProducts&lt;/strong&gt;:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/da94e67e689d_F189/image_6.png"&gt;&lt;img style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" title="image" border="0" alt="image" src="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/da94e67e689d_F189/image_thumb_6.png" width="644" height="357" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Just change the value of version attribute of VisualStudio node to &lt;strong&gt;11.0 &lt;/strong&gt;and save it.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/da94e67e689d_F189/image_7.png"&gt;&lt;img style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" title="image" border="0" alt="image" src="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/da94e67e689d_F189/image_thumb_7.png" width="644" height="357" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;This is actually enough for the extension compatibility standpoint but &lt;strong&gt;ADO.NET C# DbContext Generator&lt;/strong&gt; extension has some dependencies which are specific to Visual Studio 2010 (not sure on that though). So,we have one more step to go as well.&lt;/p&gt;
&lt;p&gt;Navigate to &lt;strong&gt;/T/code/&lt;/strong&gt; folder and you will see a zip file there:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/da94e67e689d_F189/image_8.png"&gt;&lt;img style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" title="image" border="0" alt="image" src="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/da94e67e689d_F189/image_thumb_8.png" width="644" height="393" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Unzip it:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/da94e67e689d_F189/image_9.png"&gt;&lt;img style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" title="image" border="0" alt="image" src="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/da94e67e689d_F189/image_thumb_9.png" width="644" height="393" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Open up the file called &lt;strong&gt;ADONETArtifactGenerator_DbContextCSharp.vstemplate &lt;/strong&gt;with a text editor this time and you will notice a node like below:&lt;/p&gt;
&lt;div class="code-wrapper border-shadow-1"&gt;
&lt;div style="background-color: white; color: black;"&gt;
&lt;pre&gt;  &lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;WizardExtension&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;
    &lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;Assembly&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;Microsoft.Data.Entity.Design, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515;"&gt;Assembly&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;
    &lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;FullClassName&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;Microsoft.Data.Entity.Design.VisualStudio.ModelWizard.AddArtifactGeneratorWizard&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515;"&gt;FullClassName&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;
  &lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515;"&gt;WizardExtension&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Change this one with the below code:&lt;/p&gt;
&lt;div class="code-wrapper border-shadow-1"&gt;
&lt;div style="background-color: white; color: black;"&gt;
&lt;pre&gt;  &lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;WizardExtension&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;
    &lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;Assembly&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;Microsoft.Data.Entity.Design, Version=11.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515;"&gt;Assembly&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;
    &lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;FullClassName&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;Microsoft.Data.Entity.Design.VisualStudio.ModelWizard.AddArtifactGeneratorWizard&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515;"&gt;FullClassName&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;
  &lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515;"&gt;WizardExtension&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Don&amp;rsquo;t ask me why because I have no clue. I assumed that it was a versioning issue and it works this way. Remember, this is developer preview. Not even alpha or beta so don&amp;rsquo;t look for logic that much.&lt;/p&gt;
&lt;p&gt;Save the file and zip it up again.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/da94e67e689d_F189/image_10.png"&gt;&lt;img style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" title="image" border="0" alt="image" src="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/da94e67e689d_F189/image_thumb_10.png" width="644" height="368" /&gt;&lt;/a&gt;,&lt;/p&gt;
&lt;p&gt;Don&amp;rsquo;t forget to delete other files. You just need that zip file inside the &lt;strong&gt;code&lt;/strong&gt; folder.&lt;/p&gt;
&lt;p&gt;Lastly, go under the root and zip all the files:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/da94e67e689d_F189/image_11.png"&gt;&lt;img style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" title="image" border="0" alt="image" src="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/da94e67e689d_F189/image_thumb_11.png" width="644" height="366" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/da94e67e689d_F189/image_12.png"&gt;&lt;img style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" title="image" border="0" alt="image" src="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/da94e67e689d_F189/image_thumb_12.png" width="644" height="365" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Now we have a zip file again. Change the extension from zip to vsix and double click on that&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/da94e67e689d_F189/image_13.png"&gt;&lt;img style="background-image: none; margin: 0px 15px 0px 0px; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" title="image" border="0" alt="image" src="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/da94e67e689d_F189/image_thumb_13.png" width="244" height="150" /&gt;&lt;/a&gt;&lt;a href="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/da94e67e689d_F189/image_14.png"&gt;&lt;img style="background-image: none; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" title="image" border="0" alt="image" src="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/da94e67e689d_F189/image_thumb_14.png" width="244" height="150" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Now we get it working smoothly:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/da94e67e689d_F189/image_15.png"&gt;&lt;img style="background-image: none; margin: 0px 15px 0px 0px; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" title="image" border="0" alt="image" src="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/da94e67e689d_F189/image_thumb_15.png" width="244" height="169" /&gt;&lt;/a&gt;&lt;a href="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/da94e67e689d_F189/image_16.png"&gt;&lt;img style="background-image: none; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" title="image" border="0" alt="image" src="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/da94e67e689d_F189/image_thumb_16.png" width="244" height="169" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Now when you want to continue you way with database first approach, you can follow the steps which are explained here:&lt;/p&gt;
&lt;p&gt;&lt;a title="http://blogs.msdn.com/b/adonet/archive/2011/11/01/ef-4-2-released.aspx" href="http://blogs.msdn.com/b/adonet/archive/2011/11/01/ef-4-2-released.aspx" target="_blank"&gt;http://blogs.msdn.com/b/adonet/archive/2011/11/01/ef-4-2-released.aspx&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Here is the quote from that blog post:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;Model First &amp;amp; Database First Templates&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;The templates for using the DbContext API with Model First and Database First are now available under the &amp;ldquo;Online Templates&amp;rdquo; tab when &amp;ldquo;Right-Click &amp;ndash;&amp;gt; Add Code Generation Item&amp;hellip;&amp;rdquo; is selected on the EF Designer.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Instead of navigating to "Online Templates" tab, you will find the code gen. file under the "Code" tab:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/da94e67e689d_F189/image_17.png"&gt;&lt;img style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" title="image" border="0" alt="image" src="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/da94e67e689d_F189/image_thumb_17.png" width="644" height="418" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Admit it, we totally nailed that thing &lt;img style="border-style: none;" class="wlEmoticon wlEmoticon-winkingsmile" alt="Winking smile" src="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/da94e67e689d_F189/wlEmoticon-winkingsmile.png" /&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/ecedCtEuUXvn7u9WAEIkzp40rDY/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/ecedCtEuUXvn7u9WAEIkzp40rDY/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/ecedCtEuUXvn7u9WAEIkzp40rDY/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/ecedCtEuUXvn7u9WAEIkzp40rDY/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/TugberkUgurlu/~4/-_6kVnOEUSU" height="1" width="1"/&gt;</description><pubDate>Wed, 07 Dec 2011 14:05:00 Z</pubDate><a10:updated>2011-12-07T14:05:00Z</a10:updated><a10:content type="text">&lt;p&gt;&lt;a href="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/da94e67e689d_F189/works-on-my-machine-seal-of-approval.png"&gt;&lt;img style="background-image: none; margin: 0px 15px 10px 0px; padding-left: 0px; padding-right: 0px; display: inline; float: left; padding-top: 0px; border: 0px;" title="works-on-my-machine-seal-of-approval" border="0" alt="works-on-my-machine-seal-of-approval" align="left" src="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/da94e67e689d_F189/works-on-my-machine-seal-of-approval_thumb.png" width="200" height="193" /&gt;&lt;/a&gt;Do you use &lt;a title="http://msdn.microsoft.com/en-us/vstudio/hh127353" href="http://msdn.microsoft.com/en-us/vstudio/hh127353" target="_blank"&gt;Visual Studio 11 Developer Preview&lt;/a&gt; and miss your extensions? Well, I do! Especially, &lt;a title="http://visualstudiogallery.msdn.microsoft.com/7812b04c-db36-4817-8a84-e73c452410a2" href="http://visualstudiogallery.msdn.microsoft.com/7812b04c-db36-4817-8a84-e73c452410a2" target="_blank"&gt;ADO.NET C# DbContext Generator&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;As you know, for now Visual Studio extensions are not compatible with Visual Studio 11 Developer Preview.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/da94e67e689d_F189/image.png"&gt;&lt;img style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" title="image" border="0" alt="image" src="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/da94e67e689d_F189/image_thumb.png" width="244" height="97" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;This is normal because it is &lt;strong&gt;Developer Preview&lt;/strong&gt;. Even, I opened up a thread for that on &lt;a title="http://stackoverflow.com" href="http://stackoverflow.com" target="_blank"&gt;Stackoverflow&lt;/a&gt;: &lt;a href="http://stackoverflow.com/questions/7726121/visual-studio-extensions-and-visual-studio-11-dev-preview" target="_blank"&gt;Visual Studio Extensions and Visual Studio 11 Dev. Preview&lt;/a&gt; and &lt;a title="http://stackoverflow.com/users/629083/betty" href="http://stackoverflow.com/users/629083/betty" target="_blank"&gt;@betty&lt;/a&gt; replied to my post after a long time with &lt;a title="http://stackoverflow.com/a/8335636/463785" href="http://stackoverflow.com/a/8335636/463785" target="_blank"&gt;an amazing answer&lt;/a&gt;. I never developed a Visual Studio extension before and I am not familiar with the drill so I thought it is not possible to tweak the extensions so that they can be compatible with Visual Studio 11 Developer Preview but it turns out that it is as easy as pie. How so? Here are the steps:&lt;/p&gt;
&lt;p&gt;Oww, by the way, this thing &lt;strong&gt;works on my machine &lt;/strong&gt;and I am not guarantee that it will work on yours as well. Even worse, it can completely screw your machine up. So, take the risk here and don&amp;rsquo;t swear to me.&lt;/p&gt;
&lt;p&gt;I also assume that the &lt;a title="http://visualstudiogallery.msdn.microsoft.com/7812b04c-db36-4817-8a84-e73c452410a2" href="http://visualstudiogallery.msdn.microsoft.com/7812b04c-db36-4817-8a84-e73c452410a2" target="_blank"&gt;ADO.NET C# DbContext Generator&lt;/a&gt; does not integrate with Visual Studio that much so there was no big compatibility problems but I won&amp;rsquo;t try this for the extensions like &lt;a title="http://visualstudiogallery.msdn.microsoft.com/d491911d-97f3-4cf6-87b0-6a2882120acf?SRC=Home" href="http://visualstudiogallery.msdn.microsoft.com/d491911d-97f3-4cf6-87b0-6a2882120acf?SRC=Home" target="_blank"&gt;VSCommands 2010&lt;/a&gt;, &lt;a title="http://visualstudiogallery.msdn.microsoft.com/6ed4c78f-a23e-49ad-b5fd-369af0c2107f?SRC=Home" href="http://visualstudiogallery.msdn.microsoft.com/6ed4c78f-a23e-49ad-b5fd-369af0c2107f?SRC=Home" target="_blank"&gt;Web Essentials&lt;/a&gt;, etc. If you try, please let me know how it went. Anyway, here is the drill:&lt;/p&gt;
&lt;p&gt;First of all, download the extension from &lt;a title="http://visualstudiogallery.msdn.microsoft.com" href="http://visualstudiogallery.msdn.microsoft.com" target="_blank"&gt;Visual Studio Gallery&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/da94e67e689d_F189/image_3.png"&gt;&lt;img style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" title="image" border="0" alt="image" src="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/da94e67e689d_F189/image_thumb_3.png" width="644" height="396" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;I am not sure if you are aware of it but vsix files are actually zip files just like &lt;a title="http://nuget.org" href="http://nuget.org" target="_blank"&gt;NuGet&lt;/a&gt; package files (nupkg) and MS Word files (docx). So, change the extension of the file we have downloaded from vsix to zip:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/da94e67e689d_F189/image_4.png"&gt;&lt;img style="background-image: none; margin: 0px 15px 0px 0px; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" title="image" border="0" alt="image" src="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/da94e67e689d_F189/image_thumb_4.png" width="244" height="150" /&gt;&lt;/a&gt;&amp;nbsp;&lt;a href="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/da94e67e689d_F189/image_5.png"&gt;&lt;img style="background-image: none; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" title="image" border="0" alt="image" src="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/da94e67e689d_F189/image_thumb_5.png" width="244" height="150" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Unzip the zip file and you will see a file called &lt;strong&gt;extension.vsixmanifest &lt;/strong&gt;under root folder. No, it is not a zip file, too &lt;img style="border-style: none;" class="wlEmoticon wlEmoticon-openmouthedsmile" alt="Open-mouthed smile" src="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/da94e67e689d_F189/wlEmoticon-openmouthedsmile.png" /&gt; It is an xml file though. Open it up with a text editor and find the node called &lt;strong&gt;SupportedProducts&lt;/strong&gt;:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/da94e67e689d_F189/image_6.png"&gt;&lt;img style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" title="image" border="0" alt="image" src="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/da94e67e689d_F189/image_thumb_6.png" width="644" height="357" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Just change the value of version attribute of VisualStudio node to &lt;strong&gt;11.0 &lt;/strong&gt;and save it.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/da94e67e689d_F189/image_7.png"&gt;&lt;img style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" title="image" border="0" alt="image" src="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/da94e67e689d_F189/image_thumb_7.png" width="644" height="357" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;This is actually enough for the extension compatibility standpoint but &lt;strong&gt;ADO.NET C# DbContext Generator&lt;/strong&gt; extension has some dependencies which are specific to Visual Studio 2010 (not sure on that though). So,we have one more step to go as well.&lt;/p&gt;
&lt;p&gt;Navigate to &lt;strong&gt;/T/code/&lt;/strong&gt; folder and you will see a zip file there:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/da94e67e689d_F189/image_8.png"&gt;&lt;img style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" title="image" border="0" alt="image" src="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/da94e67e689d_F189/image_thumb_8.png" width="644" height="393" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Unzip it:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/da94e67e689d_F189/image_9.png"&gt;&lt;img style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" title="image" border="0" alt="image" src="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/da94e67e689d_F189/image_thumb_9.png" width="644" height="393" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Open up the file called &lt;strong&gt;ADONETArtifactGenerator_DbContextCSharp.vstemplate &lt;/strong&gt;with a text editor this time and you will notice a node like below:&lt;/p&gt;
&lt;div class="code-wrapper border-shadow-1"&gt;
&lt;div style="background-color: white; color: black;"&gt;
&lt;pre&gt;  &lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;WizardExtension&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;
    &lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;Assembly&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;Microsoft.Data.Entity.Design, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515;"&gt;Assembly&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;
    &lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;FullClassName&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;Microsoft.Data.Entity.Design.VisualStudio.ModelWizard.AddArtifactGeneratorWizard&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515;"&gt;FullClassName&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;
  &lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515;"&gt;WizardExtension&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Change this one with the below code:&lt;/p&gt;
&lt;div class="code-wrapper border-shadow-1"&gt;
&lt;div style="background-color: white; color: black;"&gt;
&lt;pre&gt;  &lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;WizardExtension&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;
    &lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;Assembly&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;Microsoft.Data.Entity.Design, Version=11.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515;"&gt;Assembly&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;
    &lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;FullClassName&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;Microsoft.Data.Entity.Design.VisualStudio.ModelWizard.AddArtifactGeneratorWizard&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515;"&gt;FullClassName&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;
  &lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515;"&gt;WizardExtension&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Don&amp;rsquo;t ask me why because I have no clue. I assumed that it was a versioning issue and it works this way. Remember, this is developer preview. Not even alpha or beta so don&amp;rsquo;t look for logic that much.&lt;/p&gt;
&lt;p&gt;Save the file and zip it up again.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/da94e67e689d_F189/image_10.png"&gt;&lt;img style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" title="image" border="0" alt="image" src="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/da94e67e689d_F189/image_thumb_10.png" width="644" height="368" /&gt;&lt;/a&gt;,&lt;/p&gt;
&lt;p&gt;Don&amp;rsquo;t forget to delete other files. You just need that zip file inside the &lt;strong&gt;code&lt;/strong&gt; folder.&lt;/p&gt;
&lt;p&gt;Lastly, go under the root and zip all the files:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/da94e67e689d_F189/image_11.png"&gt;&lt;img style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" title="image" border="0" alt="image" src="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/da94e67e689d_F189/image_thumb_11.png" width="644" height="366" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/da94e67e689d_F189/image_12.png"&gt;&lt;img style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" title="image" border="0" alt="image" src="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/da94e67e689d_F189/image_thumb_12.png" width="644" height="365" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Now we have a zip file again. Change the extension from zip to vsix and double click on that&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/da94e67e689d_F189/image_13.png"&gt;&lt;img style="background-image: none; margin: 0px 15px 0px 0px; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" title="image" border="0" alt="image" src="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/da94e67e689d_F189/image_thumb_13.png" width="244" height="150" /&gt;&lt;/a&gt;&lt;a href="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/da94e67e689d_F189/image_14.png"&gt;&lt;img style="background-image: none; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" title="image" border="0" alt="image" src="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/da94e67e689d_F189/image_thumb_14.png" width="244" height="150" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Now we get it working smoothly:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/da94e67e689d_F189/image_15.png"&gt;&lt;img style="background-image: none; margin: 0px 15px 0px 0px; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" title="image" border="0" alt="image" src="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/da94e67e689d_F189/image_thumb_15.png" width="244" height="169" /&gt;&lt;/a&gt;&lt;a href="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/da94e67e689d_F189/image_16.png"&gt;&lt;img style="background-image: none; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" title="image" border="0" alt="image" src="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/da94e67e689d_F189/image_thumb_16.png" width="244" height="169" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Now when you want to continue you way with database first approach, you can follow the steps which are explained here:&lt;/p&gt;
&lt;p&gt;&lt;a title="http://blogs.msdn.com/b/adonet/archive/2011/11/01/ef-4-2-released.aspx" href="http://blogs.msdn.com/b/adonet/archive/2011/11/01/ef-4-2-released.aspx" target="_blank"&gt;http://blogs.msdn.com/b/adonet/archive/2011/11/01/ef-4-2-released.aspx&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Here is the quote from that blog post:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;Model First &amp;amp; Database First Templates&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;The templates for using the DbContext API with Model First and Database First are now available under the &amp;ldquo;Online Templates&amp;rdquo; tab when &amp;ldquo;Right-Click &amp;ndash;&amp;gt; Add Code Generation Item&amp;hellip;&amp;rdquo; is selected on the EF Designer.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Instead of navigating to "Online Templates" tab, you will find the code gen. file under the "Code" tab:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/da94e67e689d_F189/image_17.png"&gt;&lt;img style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" title="image" border="0" alt="image" src="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/da94e67e689d_F189/image_thumb_17.png" width="644" height="418" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Admit it, we totally nailed that thing &lt;img style="border-style: none;" class="wlEmoticon wlEmoticon-winkingsmile" alt="Winking smile" src="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/da94e67e689d_F189/wlEmoticon-winkingsmile.png" /&gt;&lt;/p&gt;</a10:content><feedburner:origLink>http://tugberkugurlu.com/73</feedburner:origLink></item><item><guid isPermaLink="false">http://tugberkugurlu.com/72</guid><link>http://feedproxy.google.com/~r/TugberkUgurlu/~3/0nYzOnIuEwQ/72</link><title>Don't Shit All Over The Company You Just Left</title><description>&lt;p&gt;&lt;a href="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/cf4619a3b962_13A62/images.jpg"&gt;&lt;img style="background-image: none; margin: 0px 15px 10px 0px; padding-left: 0px; padding-right: 0px; display: inline; float: left; padding-top: 0px; border: 0px;" title="images" border="0" alt="images" align="left" src="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/cf4619a3b962_13A62/images_thumb.jpg" width="229" height="229" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p align="left"&gt;A few days ago, &lt;a title="http://haacked.com" href="http://haacked.com" target="_blank"&gt;Phil Haack&lt;/a&gt; made an announcement which wasn&amp;rsquo;t a kind of announcement that we had heard from Mr. Haack. It wasn&amp;rsquo;t a new product release or new version of &lt;a title="http://asp.net/mvc" href="http://asp.net/mvc" target="_blank"&gt;ASP.NET MVC&lt;/a&gt; or &lt;a title="http://nuget.org" href="http://nuget.org" target="_blank"&gt;NuGet&lt;/a&gt;. It wasn&amp;rsquo;t the birthday of one of his children either. It was the announcement which tells us a very bad news. It was telling that &lt;a title="http://haacked.com/archive/2011/11/28/departing-microsoft.aspx" href="http://haacked.com/archive/2011/11/28/departing-microsoft.aspx" target="_blank"&gt;he won&amp;rsquo;t be working on behalf of Microsoft after the 5th of December 2011&lt;/a&gt;.&lt;/p&gt;
&lt;p align="left"&gt;The blog post was sad for me and at the same time it was shocking as well. But none of those are the reason why I write about it here. I have no idea what happened and drew him away from Microsoft but I can tell that the blog post he has written is exemplary.&lt;/p&gt;
&lt;p align="left"&gt;&lt;strong&gt;Phil Haack respects the place he worked and he didn&amp;rsquo;t shit all over the company. &lt;/strong&gt;This should be always the case even if you experienced the hell there.&lt;/p&gt;
&lt;p align="left"&gt;If you work on a company for a particular period of time and realize that this company is a shitty place after that period of time, you have a serious problem in my opinion. This behavior is no different than seeing your ex-wife / ex-husband as a monster, enemy after a divorce.&lt;/p&gt;
&lt;p align="left"&gt;Why am I writing this? I honestly don&amp;rsquo;t know. But &lt;a title="http://www.misfitgeek.com/2011/09/whats-wrong-with-microsoft/" href="http://www.misfitgeek.com/2011/09/whats-wrong-with-microsoft/" target="_blank"&gt;when I saw the blog post of Joe Stanger about the Microsoft&lt;/a&gt;, I was really sad that a person like him can be a person like that. &lt;a title="http://www.misfitgeek.com/2011/09/my-last-day-at-the-evil-empire-%E2%80%93-free-from-the-borg-collective/" href="http://www.misfitgeek.com/2011/09/my-last-day-at-the-evil-empire-%E2%80%93-free-from-the-borg-collective/" target="_blank"&gt;The announcement of his leave was a little harsh as well&lt;/a&gt;.&amp;nbsp; The title of the first blog post I referenced is 'What&amp;rsquo;s wrong with Microsoft?'. The company name is not important here. It could be Apple, Starbucks or The Jim&amp;rsquo;s Coffee House. The point here is that: this company was perfect when the employee was working there and it suddenly turned out a shitty place just after the employee left. Maybe it was a shitty place for a while when the employee worked there and he knew that but I, as a person in public, have no idea about that. &lt;em&gt;If you don&amp;rsquo;t have guts to tell the same thing &lt;strong&gt;publicly&lt;/strong&gt; when you work there, keep it to yourself for your entire life. &lt;/em&gt;No matter what happened, that place is the company you have worked for. At some point, maybe the place you have represented for.&lt;/p&gt;
&lt;p align="left"&gt;So, don&amp;rsquo;t be a slave of your angers. Don&amp;rsquo;t go public without thinking by using the words you will possibly regret. Also, don&amp;rsquo;t forget this is just a random dude&amp;rsquo;s opinion on the internet.&lt;/p&gt;
&lt;p align="left"&gt;Oww, I almost forgot. I know that you are not dying but we, at least I, will miss you anyway &lt;a title="http://twitter.com/Haacked" href="http://twitter.com/Haacked" target="_blank"&gt;@Haacked&lt;/a&gt;!&lt;/p&gt;
&lt;p align="left"&gt;&lt;a href="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/cf4619a3b962_13A62/58679_433381146629_516786629_5719811_7566226_n.jpg"&gt;&lt;img style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" title="58679_433381146629_516786629_5719811_7566226_n" border="0" alt="58679_433381146629_516786629_5719811_7566226_n" src="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/cf4619a3b962_13A62/58679_433381146629_516786629_5719811_7566226_n_thumb.jpg" width="644" height="484" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/iB0dSNyDJ7w9NY8a7a4jlOp-b1M/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/iB0dSNyDJ7w9NY8a7a4jlOp-b1M/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/iB0dSNyDJ7w9NY8a7a4jlOp-b1M/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/iB0dSNyDJ7w9NY8a7a4jlOp-b1M/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/TugberkUgurlu/~4/0nYzOnIuEwQ" height="1" width="1"/&gt;</description><pubDate>Sat, 03 Dec 2011 19:29:00 Z</pubDate><a10:updated>2011-12-03T19:29:00Z</a10:updated><a10:content type="text">&lt;p&gt;&lt;a href="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/cf4619a3b962_13A62/images.jpg"&gt;&lt;img style="background-image: none; margin: 0px 15px 10px 0px; padding-left: 0px; padding-right: 0px; display: inline; float: left; padding-top: 0px; border: 0px;" title="images" border="0" alt="images" align="left" src="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/cf4619a3b962_13A62/images_thumb.jpg" width="229" height="229" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p align="left"&gt;A few days ago, &lt;a title="http://haacked.com" href="http://haacked.com" target="_blank"&gt;Phil Haack&lt;/a&gt; made an announcement which wasn&amp;rsquo;t a kind of announcement that we had heard from Mr. Haack. It wasn&amp;rsquo;t a new product release or new version of &lt;a title="http://asp.net/mvc" href="http://asp.net/mvc" target="_blank"&gt;ASP.NET MVC&lt;/a&gt; or &lt;a title="http://nuget.org" href="http://nuget.org" target="_blank"&gt;NuGet&lt;/a&gt;. It wasn&amp;rsquo;t the birthday of one of his children either. It was the announcement which tells us a very bad news. It was telling that &lt;a title="http://haacked.com/archive/2011/11/28/departing-microsoft.aspx" href="http://haacked.com/archive/2011/11/28/departing-microsoft.aspx" target="_blank"&gt;he won&amp;rsquo;t be working on behalf of Microsoft after the 5th of December 2011&lt;/a&gt;.&lt;/p&gt;
&lt;p align="left"&gt;The blog post was sad for me and at the same time it was shocking as well. But none of those are the reason why I write about it here. I have no idea what happened and drew him away from Microsoft but I can tell that the blog post he has written is exemplary.&lt;/p&gt;
&lt;p align="left"&gt;&lt;strong&gt;Phil Haack respects the place he worked and he didn&amp;rsquo;t shit all over the company. &lt;/strong&gt;This should be always the case even if you experienced the hell there.&lt;/p&gt;
&lt;p align="left"&gt;If you work on a company for a particular period of time and realize that this company is a shitty place after that period of time, you have a serious problem in my opinion. This behavior is no different than seeing your ex-wife / ex-husband as a monster, enemy after a divorce.&lt;/p&gt;
&lt;p align="left"&gt;Why am I writing this? I honestly don&amp;rsquo;t know. But &lt;a title="http://www.misfitgeek.com/2011/09/whats-wrong-with-microsoft/" href="http://www.misfitgeek.com/2011/09/whats-wrong-with-microsoft/" target="_blank"&gt;when I saw the blog post of Joe Stanger about the Microsoft&lt;/a&gt;, I was really sad that a person like him can be a person like that. &lt;a title="http://www.misfitgeek.com/2011/09/my-last-day-at-the-evil-empire-%E2%80%93-free-from-the-borg-collective/" href="http://www.misfitgeek.com/2011/09/my-last-day-at-the-evil-empire-%E2%80%93-free-from-the-borg-collective/" target="_blank"&gt;The announcement of his leave was a little harsh as well&lt;/a&gt;.&amp;nbsp; The title of the first blog post I referenced is 'What&amp;rsquo;s wrong with Microsoft?'. The company name is not important here. It could be Apple, Starbucks or The Jim&amp;rsquo;s Coffee House. The point here is that: this company was perfect when the employee was working there and it suddenly turned out a shitty place just after the employee left. Maybe it was a shitty place for a while when the employee worked there and he knew that but I, as a person in public, have no idea about that. &lt;em&gt;If you don&amp;rsquo;t have guts to tell the same thing &lt;strong&gt;publicly&lt;/strong&gt; when you work there, keep it to yourself for your entire life. &lt;/em&gt;No matter what happened, that place is the company you have worked for. At some point, maybe the place you have represented for.&lt;/p&gt;
&lt;p align="left"&gt;So, don&amp;rsquo;t be a slave of your angers. Don&amp;rsquo;t go public without thinking by using the words you will possibly regret. Also, don&amp;rsquo;t forget this is just a random dude&amp;rsquo;s opinion on the internet.&lt;/p&gt;
&lt;p align="left"&gt;Oww, I almost forgot. I know that you are not dying but we, at least I, will miss you anyway &lt;a title="http://twitter.com/Haacked" href="http://twitter.com/Haacked" target="_blank"&gt;@Haacked&lt;/a&gt;!&lt;/p&gt;
&lt;p align="left"&gt;&lt;a href="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/cf4619a3b962_13A62/58679_433381146629_516786629_5719811_7566226_n.jpg"&gt;&lt;img style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" title="58679_433381146629_516786629_5719811_7566226_n" border="0" alt="58679_433381146629_516786629_5719811_7566226_n" src="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/cf4619a3b962_13A62/58679_433381146629_516786629_5719811_7566226_n_thumb.jpg" width="644" height="484" /&gt;&lt;/a&gt;&lt;/p&gt;</a10:content><feedburner:origLink>http://tugberkugurlu.com/72</feedburner:origLink></item><item><guid isPermaLink="false">http://tugberkugurlu.com/71</guid><link>http://feedproxy.google.com/~r/TugberkUgurlu/~3/qNERZo6CjW4/71</link><title>WCF Web API Plays Nice With ELMAH - A Quick Introduction to WCF Web API HttpErrorHandler</title><description>&lt;p&gt;&lt;a href="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/c64ad9c94e51_D270/image.png"&gt;&lt;img height="165" width="244" src="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/c64ad9c94e51_D270/image_thumb.png" align="right" alt="image" border="0" title="image" style="background-image: none; margin: 0px 0px 10px 15px; padding-left: 0px; padding-right: 0px; display: inline; float: right; padding-top: 0px; border: 0px;" /&gt;&lt;/a&gt;How many times you face an error message like this one when you are working with WCF? Many times I guess.&lt;/p&gt;
&lt;p&gt;On WCF Web API, this is not a big deal on the development stage because Web API runs on core ASP.NET and debugging is not a big deal (maybe it is not a big deal on WCF as well but it always for me, anybody knows a good debugging scenario on WCF, please let me know).&lt;/p&gt;
&lt;p&gt;But when you expose your data and your customers starts to consume your service, you will pull your hairs when you see this screen.&lt;/p&gt;
&lt;p&gt;WCF Web API has been built extensibility and testability in mind so it is real easy to plug things into the framework. One of the extensibility points is &lt;strong&gt;ErrorHandlers&lt;/strong&gt; and in this quick blog post I will show you how to handle error nicely on WCF Web API.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;If you haven&amp;rsquo;t seen my previous blog post on &lt;a target="_blank" href="http://www.tugberkugurlu.com/archive/introduction-to-wcf-web-api-new-rest-face-ofnet" title="http://www.tugberkugurlu.com/archive/introduction-to-wcf-web-api-new-rest-face-ofnet"&gt;Introduction to WCF Web API - New REST Face of .NET&lt;/a&gt;, I encourage you to check that out. What I do here will be addition to that.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;When somebody tells me the words &amp;ldquo;Error Handling&amp;rdquo; and &amp;ldquo;.NET&amp;rdquo; in the same sentence, I tell him/her &lt;a target="_blank" href="http://code.google.com/p/elmah/" title="http://code.google.com/p/elmah/"&gt;ELMAH&lt;/a&gt; in response. &lt;em&gt;ELMAH (Error Logging Modules and Handlers) is is an application-wide error logging facility that is completely pluggable. It can be dynamically added to a running &lt;/em&gt;&lt;a href="http://www.asp.net/"&gt;&lt;em&gt;ASP.NET&lt;/em&gt;&lt;/a&gt;&lt;em&gt; web application, or even all ASP.NET web applications on a machine, without any need for re-compilation or re-deployment. &lt;/em&gt;These are the official words.&lt;/p&gt;
&lt;p&gt;In order to integrate ELMAH with our Web API application, we need to bring down the ELMAH via &lt;a target="_blank" href="http://nuget.org" title="http://nuget.org"&gt;NuGet&lt;/a&gt; as we always do for open source libraries.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;a target="_blank" href="http://hanselman.com/" title="http://hanselman.com/"&gt;Scott Hanselman&lt;/a&gt; has a great blog post on how ELMAH gets into your application via NuGet Package Manager. You should check that out if you are interested:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.hanselman.com/blog/IntroducingNuGetPackageManagementForNETAnotherPieceOfTheWebStack.aspx"&gt;http://www.hanselman.com/blog/IntroducingNuGetPackageManagementForNETAnotherPieceOfTheWebStack.aspx&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Open up you PMC and type &lt;em&gt;&lt;strong&gt;Install-Package ELMAH&lt;/strong&gt;&lt;/em&gt;:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/c64ad9c94e51_D270/image_3.png"&gt;&lt;img height="253" width="644" src="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/c64ad9c94e51_D270/image_thumb_3.png" alt="image" border="0" title="image" style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;After you install the package successfully, it doesn&amp;rsquo;t need any extra configuration to work but you would probably want to secure your error logging page. Check it out how on &lt;a target="_blank" href="http://haacked.com" title="http://haacked.com"&gt;Phil Haack&lt;/a&gt;&amp;rsquo;s blog post: &lt;a href="http://haacked.com/archive/2007/07/24/securely-implement-elmah-for-plug-and-play-error-logging.aspx"&gt;http://haacked.com/archive/2007/07/24/securely-implement-elmah-for-plug-and-play-error-logging.aspx&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;When you go to http://localhost:{port_number}/elmah.axd, you will see the error list page:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/c64ad9c94e51_D270/image_4.png"&gt;&lt;img height="433" width="644" src="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/c64ad9c94e51_D270/image_thumb_4.png" alt="image" border="0" title="image" style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;I added a new method for PeopleApi in order to be able to reach single person data:&lt;/p&gt;
&lt;div class="code-wrapper border-shadow-1"&gt;
&lt;div style="background-color: white; color: black;"&gt;
&lt;pre&gt;[WebGet(UriTemplate = &lt;span style="color: #a31515;"&gt;"{id}"&lt;/span&gt;)]
&lt;span style="color: blue;"&gt;public&lt;/span&gt; HttpResponseMessage&amp;lt;Person&amp;gt; GetSingle(&lt;span style="color: blue;"&gt;int&lt;/span&gt; id) {

    &lt;span style="color: blue;"&gt;var&lt;/span&gt; person = _repo.GetSingle(id);

    &lt;span style="color: blue;"&gt;if&lt;/span&gt; (person == &lt;span style="color: blue;"&gt;null&lt;/span&gt;) {
        &lt;span style="color: blue;"&gt;var&lt;/span&gt; response = &lt;span style="color: blue;"&gt;new&lt;/span&gt; HttpResponseMessage();
        response.StatusCode = HttpStatusCode.NotFound;
        response.Content = &lt;span style="color: blue;"&gt;new&lt;/span&gt; StringContent(&lt;span style="color: #a31515;"&gt;"Country not found"&lt;/span&gt;);
        &lt;span style="color: blue;"&gt;throw&lt;/span&gt; &lt;span style="color: blue;"&gt;new&lt;/span&gt; HttpResponseException(response);
    }

    &lt;span style="color: blue;"&gt;var&lt;/span&gt; personResponse = &lt;span style="color: blue;"&gt;new&lt;/span&gt; HttpResponseMessage&amp;lt;Models.Person&amp;gt;(person);
    personResponse.Content.Headers.Expires = &lt;span style="color: blue;"&gt;new&lt;/span&gt; DateTimeOffset(DateTime.Now.AddHours(6));
    &lt;span style="color: blue;"&gt;return&lt;/span&gt; personResponse;
}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;It is a simple method. It returns a single Person data wrapped up inside &lt;a target="_blank" href="http://msdn.microsoft.com/en-us/library/system.net.http.httpresponsemessage(v=vs.110).aspx" title="http://msdn.microsoft.com/en-us/library/system.net.http.httpresponsemessage(v=vs.110).aspx"&gt;HttpResponseMessage&lt;/a&gt; if there is one and returns 404 if there is no person with the given id value.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/c64ad9c94e51_D270/image_5.png"&gt;&lt;img height="165" width="244" src="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/c64ad9c94e51_D270/image_thumb_5.png" alt="image" border="0" title="image" style="background-image: none; margin: 0px 15px 0px 0px; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" /&gt;&lt;/a&gt;&lt;a href="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/c64ad9c94e51_D270/image_6.png"&gt;&lt;img height="165" width="244" src="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/c64ad9c94e51_D270/image_thumb_6.png" alt="image" border="0" title="image" style="background-image: none; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Let&amp;rsquo;s send a string value instead of int32 and see what happens:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/c64ad9c94e51_D270/image_7.png"&gt;&lt;img height="433" width="644" src="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/c64ad9c94e51_D270/image_thumb_7.png" alt="image" border="0" title="image" style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;It is an error so ELMAH should have logged this, right? It didn&amp;rsquo;t because WCF Web API handles exceptions on its own but good news is you can get in there and plug your own stuff into it.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Disclaimer: &lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;I am still a newbie on WCF Web API and learning it day by day. Also, the framework is at the preview stage (not even Alpha) so the things I explain and show might be not entirely the best case scenarios.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;In WCF Web API preview 5, you can use the &lt;strong&gt;ErrorHandler&lt;/strong&gt;, which is the recommended way to do error handling. In order to implement your own error handler, your class needs to derived from HttpErrorHandler class. Here is the implementation I use:&lt;/p&gt;
&lt;div class="code-wrapper border-shadow-1"&gt;
&lt;div style="background-color: white; color: black;"&gt;
&lt;pre&gt;&lt;span style="color: blue;"&gt;using&lt;/span&gt; System;
&lt;span style="color: blue;"&gt;using&lt;/span&gt; System.Collections.Generic;
&lt;span style="color: blue;"&gt;using&lt;/span&gt; System.Linq;
&lt;span style="color: blue;"&gt;using&lt;/span&gt; System.Net;
&lt;span style="color: blue;"&gt;using&lt;/span&gt; System.Net.Http;
&lt;span style="color: blue;"&gt;using&lt;/span&gt; System.Web;
&lt;span style="color: blue;"&gt;using&lt;/span&gt; Microsoft.ApplicationServer.Http.Dispatcher;

&lt;span style="color: blue;"&gt;namespace&lt;/span&gt; VeryFirstWcfWebAPI.Handlers {

    &lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;class&lt;/span&gt; GlobalErrorHandler : HttpErrorHandler {

        &lt;span style="color: blue;"&gt;protected&lt;/span&gt; &lt;span style="color: blue;"&gt;override&lt;/span&gt; &lt;span style="color: blue;"&gt;bool&lt;/span&gt; OnTryProvideResponse(Exception exception, &lt;span style="color: blue;"&gt;ref&lt;/span&gt; System.Net.Http.HttpResponseMessage message) {

            &lt;span style="color: blue;"&gt;if&lt;/span&gt;(exception != &lt;span style="color: blue;"&gt;null&lt;/span&gt;) &lt;span style="color: green;"&gt;// Notify ELMAH&lt;/span&gt;
                Elmah.ErrorSignal.FromCurrentContext().Raise(exception);

            message = &lt;span style="color: blue;"&gt;new&lt;/span&gt; HttpResponseMessage {
                StatusCode = HttpStatusCode.InternalServerError
            };

            &lt;span style="color: blue;"&gt;return&lt;/span&gt; &lt;span style="color: blue;"&gt;true&lt;/span&gt;;
        }
    }
}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Pretty straight forward implementation. The last step is to register this handler. We will do that inside the &lt;strong&gt;Global.asax&lt;/strong&gt; file as below:&lt;/p&gt;
&lt;div class="code-wrapper border-shadow-1"&gt;
&lt;div style="background-color: white; color: black;"&gt;
&lt;pre&gt;&lt;span style="color: blue;"&gt;protected&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; Application_Start(&lt;span style="color: blue;"&gt;object&lt;/span&gt; sender, EventArgs e) {

    RouteTable.Routes.SetDefaultHttpConfiguration(&lt;span style="color: blue;"&gt;new&lt;/span&gt; Microsoft.ApplicationServer.Http.WebApiConfiguration() { 
        CreateInstance = (serviceType, context, request) =&amp;gt; GetKernel().Get(serviceType),
        ErrorHandlers = (handlers, endpoint, description) =&amp;gt; handlers.Add(&lt;span style="color: blue;"&gt;new&lt;/span&gt; GlobalErrorHandler())
    });

    RouteTable.Routes.MapServiceRoute&amp;lt;People.PeopleApi&amp;gt;(&lt;span style="color: #a31515;"&gt;"Api/People"&lt;/span&gt;);
}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Now, hit your service and cause an error. After that go back to elmah.axd page to see what we got there:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/c64ad9c94e51_D270/image_8.png"&gt;&lt;img height="433" width="644" src="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/c64ad9c94e51_D270/image_thumb_8.png" alt="image" border="0" title="image" style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/c64ad9c94e51_D270/image_9.png"&gt;&lt;img height="433" width="644" src="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/c64ad9c94e51_D270/image_thumb_9.png" alt="image" border="0" title="image" style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;We totally nailed it! Now, you can configure ELMAH to send you an e-mail when an error occurred or you can log the error inside an XML file, SQL Server Database, wherever you what.&lt;/p&gt;
&lt;p&gt;As I said at the end of my previous post, there is so much to cover about WCF Web API. I hope I will blog about it more.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;You can find the full code on &lt;a title="https://github.com" href="https://github.com" target="_blank"&gt;GitHub&lt;/a&gt;: &lt;a href="https://github.com/tugberkugurlu/VeryFirstWcfWebAPI" target="_blank"&gt;https://github.com/tugberkugurlu/VeryFirstWcfWebAPI&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/gN30rt7Pj6ZVnhPCvJxtIDpjX7U/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/gN30rt7Pj6ZVnhPCvJxtIDpjX7U/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/gN30rt7Pj6ZVnhPCvJxtIDpjX7U/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/gN30rt7Pj6ZVnhPCvJxtIDpjX7U/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/TugberkUgurlu/~4/qNERZo6CjW4" height="1" width="1"/&gt;</description><pubDate>Tue, 22 Nov 2011 12:09:00 Z</pubDate><a10:updated>2011-11-22T12:09:00Z</a10:updated><a10:content type="text">&lt;p&gt;&lt;a href="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/c64ad9c94e51_D270/image.png"&gt;&lt;img height="165" width="244" src="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/c64ad9c94e51_D270/image_thumb.png" align="right" alt="image" border="0" title="image" style="background-image: none; margin: 0px 0px 10px 15px; padding-left: 0px; padding-right: 0px; display: inline; float: right; padding-top: 0px; border: 0px;" /&gt;&lt;/a&gt;How many times you face an error message like this one when you are working with WCF? Many times I guess.&lt;/p&gt;
&lt;p&gt;On WCF Web API, this is not a big deal on the development stage because Web API runs on core ASP.NET and debugging is not a big deal (maybe it is not a big deal on WCF as well but it always for me, anybody knows a good debugging scenario on WCF, please let me know).&lt;/p&gt;
&lt;p&gt;But when you expose your data and your customers starts to consume your service, you will pull your hairs when you see this screen.&lt;/p&gt;
&lt;p&gt;WCF Web API has been built extensibility and testability in mind so it is real easy to plug things into the framework. One of the extensibility points is &lt;strong&gt;ErrorHandlers&lt;/strong&gt; and in this quick blog post I will show you how to handle error nicely on WCF Web API.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;If you haven&amp;rsquo;t seen my previous blog post on &lt;a target="_blank" href="http://www.tugberkugurlu.com/archive/introduction-to-wcf-web-api-new-rest-face-ofnet" title="http://www.tugberkugurlu.com/archive/introduction-to-wcf-web-api-new-rest-face-ofnet"&gt;Introduction to WCF Web API - New REST Face of .NET&lt;/a&gt;, I encourage you to check that out. What I do here will be addition to that.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;When somebody tells me the words &amp;ldquo;Error Handling&amp;rdquo; and &amp;ldquo;.NET&amp;rdquo; in the same sentence, I tell him/her &lt;a target="_blank" href="http://code.google.com/p/elmah/" title="http://code.google.com/p/elmah/"&gt;ELMAH&lt;/a&gt; in response. &lt;em&gt;ELMAH (Error Logging Modules and Handlers) is is an application-wide error logging facility that is completely pluggable. It can be dynamically added to a running &lt;/em&gt;&lt;a href="http://www.asp.net/"&gt;&lt;em&gt;ASP.NET&lt;/em&gt;&lt;/a&gt;&lt;em&gt; web application, or even all ASP.NET web applications on a machine, without any need for re-compilation or re-deployment. &lt;/em&gt;These are the official words.&lt;/p&gt;
&lt;p&gt;In order to integrate ELMAH with our Web API application, we need to bring down the ELMAH via &lt;a target="_blank" href="http://nuget.org" title="http://nuget.org"&gt;NuGet&lt;/a&gt; as we always do for open source libraries.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;a target="_blank" href="http://hanselman.com/" title="http://hanselman.com/"&gt;Scott Hanselman&lt;/a&gt; has a great blog post on how ELMAH gets into your application via NuGet Package Manager. You should check that out if you are interested:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.hanselman.com/blog/IntroducingNuGetPackageManagementForNETAnotherPieceOfTheWebStack.aspx"&gt;http://www.hanselman.com/blog/IntroducingNuGetPackageManagementForNETAnotherPieceOfTheWebStack.aspx&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Open up you PMC and type &lt;em&gt;&lt;strong&gt;Install-Package ELMAH&lt;/strong&gt;&lt;/em&gt;:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/c64ad9c94e51_D270/image_3.png"&gt;&lt;img height="253" width="644" src="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/c64ad9c94e51_D270/image_thumb_3.png" alt="image" border="0" title="image" style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;After you install the package successfully, it doesn&amp;rsquo;t need any extra configuration to work but you would probably want to secure your error logging page. Check it out how on &lt;a target="_blank" href="http://haacked.com" title="http://haacked.com"&gt;Phil Haack&lt;/a&gt;&amp;rsquo;s blog post: &lt;a href="http://haacked.com/archive/2007/07/24/securely-implement-elmah-for-plug-and-play-error-logging.aspx"&gt;http://haacked.com/archive/2007/07/24/securely-implement-elmah-for-plug-and-play-error-logging.aspx&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;When you go to http://localhost:{port_number}/elmah.axd, you will see the error list page:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/c64ad9c94e51_D270/image_4.png"&gt;&lt;img height="433" width="644" src="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/c64ad9c94e51_D270/image_thumb_4.png" alt="image" border="0" title="image" style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;I added a new method for PeopleApi in order to be able to reach single person data:&lt;/p&gt;
&lt;div class="code-wrapper border-shadow-1"&gt;
&lt;div style="background-color: white; color: black;"&gt;
&lt;pre&gt;[WebGet(UriTemplate = &lt;span style="color: #a31515;"&gt;"{id}"&lt;/span&gt;)]
&lt;span style="color: blue;"&gt;public&lt;/span&gt; HttpResponseMessage&amp;lt;Person&amp;gt; GetSingle(&lt;span style="color: blue;"&gt;int&lt;/span&gt; id) {

    &lt;span style="color: blue;"&gt;var&lt;/span&gt; person = _repo.GetSingle(id);

    &lt;span style="color: blue;"&gt;if&lt;/span&gt; (person == &lt;span style="color: blue;"&gt;null&lt;/span&gt;) {
        &lt;span style="color: blue;"&gt;var&lt;/span&gt; response = &lt;span style="color: blue;"&gt;new&lt;/span&gt; HttpResponseMessage();
        response.StatusCode = HttpStatusCode.NotFound;
        response.Content = &lt;span style="color: blue;"&gt;new&lt;/span&gt; StringContent(&lt;span style="color: #a31515;"&gt;"Country not found"&lt;/span&gt;);
        &lt;span style="color: blue;"&gt;throw&lt;/span&gt; &lt;span style="color: blue;"&gt;new&lt;/span&gt; HttpResponseException(response);
    }

    &lt;span style="color: blue;"&gt;var&lt;/span&gt; personResponse = &lt;span style="color: blue;"&gt;new&lt;/span&gt; HttpResponseMessage&amp;lt;Models.Person&amp;gt;(person);
    personResponse.Content.Headers.Expires = &lt;span style="color: blue;"&gt;new&lt;/span&gt; DateTimeOffset(DateTime.Now.AddHours(6));
    &lt;span style="color: blue;"&gt;return&lt;/span&gt; personResponse;
}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;It is a simple method. It returns a single Person data wrapped up inside &lt;a target="_blank" href="http://msdn.microsoft.com/en-us/library/system.net.http.httpresponsemessage(v=vs.110).aspx" title="http://msdn.microsoft.com/en-us/library/system.net.http.httpresponsemessage(v=vs.110).aspx"&gt;HttpResponseMessage&lt;/a&gt; if there is one and returns 404 if there is no person with the given id value.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/c64ad9c94e51_D270/image_5.png"&gt;&lt;img height="165" width="244" src="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/c64ad9c94e51_D270/image_thumb_5.png" alt="image" border="0" title="image" style="background-image: none; margin: 0px 15px 0px 0px; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" /&gt;&lt;/a&gt;&lt;a href="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/c64ad9c94e51_D270/image_6.png"&gt;&lt;img height="165" width="244" src="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/c64ad9c94e51_D270/image_thumb_6.png" alt="image" border="0" title="image" style="background-image: none; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Let&amp;rsquo;s send a string value instead of int32 and see what happens:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/c64ad9c94e51_D270/image_7.png"&gt;&lt;img height="433" width="644" src="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/c64ad9c94e51_D270/image_thumb_7.png" alt="image" border="0" title="image" style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;It is an error so ELMAH should have logged this, right? It didn&amp;rsquo;t because WCF Web API handles exceptions on its own but good news is you can get in there and plug your own stuff into it.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Disclaimer: &lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;I am still a newbie on WCF Web API and learning it day by day. Also, the framework is at the preview stage (not even Alpha) so the things I explain and show might be not entirely the best case scenarios.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;In WCF Web API preview 5, you can use the &lt;strong&gt;ErrorHandler&lt;/strong&gt;, which is the recommended way to do error handling. In order to implement your own error handler, your class needs to derived from HttpErrorHandler class. Here is the implementation I use:&lt;/p&gt;
&lt;div class="code-wrapper border-shadow-1"&gt;
&lt;div style="background-color: white; color: black;"&gt;
&lt;pre&gt;&lt;span style="color: blue;"&gt;using&lt;/span&gt; System;
&lt;span style="color: blue;"&gt;using&lt;/span&gt; System.Collections.Generic;
&lt;span style="color: blue;"&gt;using&lt;/span&gt; System.Linq;
&lt;span style="color: blue;"&gt;using&lt;/span&gt; System.Net;
&lt;span style="color: blue;"&gt;using&lt;/span&gt; System.Net.Http;
&lt;span style="color: blue;"&gt;using&lt;/span&gt; System.Web;
&lt;span style="color: blue;"&gt;using&lt;/span&gt; Microsoft.ApplicationServer.Http.Dispatcher;

&lt;span style="color: blue;"&gt;namespace&lt;/span&gt; VeryFirstWcfWebAPI.Handlers {

    &lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;class&lt;/span&gt; GlobalErrorHandler : HttpErrorHandler {

        &lt;span style="color: blue;"&gt;protected&lt;/span&gt; &lt;span style="color: blue;"&gt;override&lt;/span&gt; &lt;span style="color: blue;"&gt;bool&lt;/span&gt; OnTryProvideResponse(Exception exception, &lt;span style="color: blue;"&gt;ref&lt;/span&gt; System.Net.Http.HttpResponseMessage message) {

            &lt;span style="color: blue;"&gt;if&lt;/span&gt;(exception != &lt;span style="color: blue;"&gt;null&lt;/span&gt;) &lt;span style="color: green;"&gt;// Notify ELMAH&lt;/span&gt;
                Elmah.ErrorSignal.FromCurrentContext().Raise(exception);

            message = &lt;span style="color: blue;"&gt;new&lt;/span&gt; HttpResponseMessage {
                StatusCode = HttpStatusCode.InternalServerError
            };

            &lt;span style="color: blue;"&gt;return&lt;/span&gt; &lt;span style="color: blue;"&gt;true&lt;/span&gt;;
        }
    }
}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Pretty straight forward implementation. The last step is to register this handler. We will do that inside the &lt;strong&gt;Global.asax&lt;/strong&gt; file as below:&lt;/p&gt;
&lt;div class="code-wrapper border-shadow-1"&gt;
&lt;div style="background-color: white; color: black;"&gt;
&lt;pre&gt;&lt;span style="color: blue;"&gt;protected&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; Application_Start(&lt;span style="color: blue;"&gt;object&lt;/span&gt; sender, EventArgs e) {

    RouteTable.Routes.SetDefaultHttpConfiguration(&lt;span style="color: blue;"&gt;new&lt;/span&gt; Microsoft.ApplicationServer.Http.WebApiConfiguration() { 
        CreateInstance = (serviceType, context, request) =&amp;gt; GetKernel().Get(serviceType),
        ErrorHandlers = (handlers, endpoint, description) =&amp;gt; handlers.Add(&lt;span style="color: blue;"&gt;new&lt;/span&gt; GlobalErrorHandler())
    });

    RouteTable.Routes.MapServiceRoute&amp;lt;People.PeopleApi&amp;gt;(&lt;span style="color: #a31515;"&gt;"Api/People"&lt;/span&gt;);
}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Now, hit your service and cause an error. After that go back to elmah.axd page to see what we got there:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/c64ad9c94e51_D270/image_8.png"&gt;&lt;img height="433" width="644" src="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/c64ad9c94e51_D270/image_thumb_8.png" alt="image" border="0" title="image" style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/c64ad9c94e51_D270/image_9.png"&gt;&lt;img height="433" width="644" src="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/c64ad9c94e51_D270/image_thumb_9.png" alt="image" border="0" title="image" style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;We totally nailed it! Now, you can configure ELMAH to send you an e-mail when an error occurred or you can log the error inside an XML file, SQL Server Database, wherever you what.&lt;/p&gt;
&lt;p&gt;As I said at the end of my previous post, there is so much to cover about WCF Web API. I hope I will blog about it more.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;You can find the full code on &lt;a title="https://github.com" href="https://github.com" target="_blank"&gt;GitHub&lt;/a&gt;: &lt;a href="https://github.com/tugberkugurlu/VeryFirstWcfWebAPI" target="_blank"&gt;https://github.com/tugberkugurlu/VeryFirstWcfWebAPI&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;</a10:content><feedburner:origLink>http://tugberkugurlu.com/71</feedburner:origLink></item><item><guid isPermaLink="false">http://tugberkugurlu.com/70</guid><link>http://feedproxy.google.com/~r/TugberkUgurlu/~3/K6c53NXTteM/70</link><title>Introduction to WCF Web API - New REST Face of .NET</title><description>&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;24 February 2012&lt;/strong&gt;&lt;/p&gt;
WCF Web API is now &lt;a href="http://asp.net/web-api" title="http://asp.net/web-api"&gt;ASP.NET Web API&lt;/a&gt;&amp;nbsp;and has changed a lot. The beta version is now available.&amp;nbsp;For more information: &lt;a href="http://www.tugberkugurlu.com/archive/getting-started-with-asp-net-web-api-tutorials-videos-samples" title="http://www.tugberkugurlu.com/archive/getting-started-with-asp-net-web-api-tutorials-videos-samples"&gt;Getting Started With ASP.NET Web API - Tutorials, Videos, Samples&lt;/a&gt;.&lt;/blockquote&gt;
&lt;p&gt;&lt;a href="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/ec9739380e27_9F4D/image.png"&gt;&lt;img style="background-image: none; margin: 0px 15px 10px 0px; padding-left: 0px; padding-right: 0px; display: inline; float: left; padding-top: 0px; border-width: 0px;" title="image" border="0" alt="image" align="left" src="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/ec9739380e27_9F4D/image_thumb.png" width="244" height="166" /&gt;&lt;/a&gt;Microsoft Web Platform is evolving. I mean really evolving. &lt;a title="http://hanselman.com/" href="http://hanselman.com/" target="_blank"&gt;Scott Hanselman&lt;/a&gt;, &lt;a title="http://haacked.com" href="http://haacked.com" target="_blank"&gt;Phil Haack&lt;/a&gt;, &lt;a title="http://blogs.msdn.com/gblock" href="http://blogs.msdn.com/gblock" target="_blank"&gt;Glenn Block&lt;/a&gt; and &lt;a title="http://damianedwards.wordpress.com" href="http://damianedwards.wordpress.com" target="_blank"&gt;Damian Edwards&lt;/a&gt; are the main actors for this evolution.&lt;/p&gt;
&lt;p&gt;One of the biggest frustration we had as web developers was to face with the endless configurations with WCF. I mean, WCF is great but hasn&amp;rsquo;t been embraced the REST since the &lt;a title="http://wcf.codeplex.com/wikipage?title=WCF%20HTTP" href="http://wcf.codeplex.com/wikipage?title=WCF%20HTTP" target="_blank"&gt;WCF Web API&lt;/a&gt; framework. There was something called REST Starter Kit but it ended up dead.&lt;/p&gt;
&lt;p&gt;Yes, WCF Web API. WCF Web API is the new way of exposing and consuming APIs over http in .NET. The unofficial (not sure, maybe the official one) slogan of WCF Web API is this:&lt;/p&gt;
&lt;p&gt;&lt;em&gt;"Making REST a first class citizen in .NET"&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;I stole (or quoted would be nicer) the real sentences which explain the WCF Web API from the official WCF Web API page (I hope they don&amp;rsquo;t mind):&lt;/p&gt;
&lt;blockquote&gt;
&lt;h4&gt;What is WCF Web API?&lt;/h4&gt;
&lt;p&gt;&lt;em&gt;Applications are continually evolving to expose their functionality over the web for example social services like &lt;/em&gt;&lt;a href="http://www.flickr.com/"&gt;&lt;em&gt;Flickr&lt;/em&gt;&lt;/a&gt;&lt;em&gt;, &lt;/em&gt;&lt;a href="http://www.twitter.com/"&gt;&lt;em&gt;Twitter&lt;/em&gt;&lt;/a&gt;&lt;em&gt; and &lt;/em&gt;&lt;a href="http://www.facebook.com/"&gt;&lt;em&gt;Facebook&lt;/em&gt;&lt;/a&gt;&lt;em&gt;. Aside from social applications, organizations are also looking to surface their core enterprise business functionality to an ever expanding array of client platforms. WCF Web API allows developers to expose their applications, data and services to the web directly over &lt;/em&gt;&lt;a href="http://en.wikipedia.org/wiki/HTTP"&gt;&lt;em&gt;HTTP&lt;/em&gt;&lt;/a&gt;&lt;em&gt;. This allows developers to fully harness the richness of the HTTP as an application layer protocol. Applications can communicate with a very broad set of clients whether they be browsers, mobile devices, desktop applications or other backend services. They can also take advantage of the caching and proxy infrastructure of the web through providing proper control and entity headers. We are designing specifically to support applications built with a &lt;/em&gt;&lt;a href="http://en.wikipedia.org/wiki/Representational_State_Transfer"&gt;&lt;em&gt;RESTful&lt;/em&gt;&lt;/a&gt;&lt;em&gt; architecture style though it does not force developers to use REST. The benefits of REST for your applications include discoverability, evolvability and scalability.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;The project is still at the preview stage and we are swimming in the dark sea. There are lots of rumors going around about it and most of them are nearly certain to be true. One of them is that WCF Web API and &lt;a title="http://asp.net/mvc" href="http://asp.net/mvc" target="_blank"&gt;ASP.NEt MVC&lt;/a&gt; will be blood brothers in near future. In plain English, they will be merged together. This will be exciting and we won&amp;rsquo;t feel ourselves in a fork in the road when we need to pick ASP.NET MVC or WCF Web API in order to expose our data over http.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Cut the crap and show me the code&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Well, when I first saw the WCF Web API, I told myself that this&amp;rsquo;s it! Why I told that? Because it is extremely easy to get started and going from there. I am interested in WCF as well but its configuration is endless so I haven&amp;rsquo;t been able to develop a decent project with WCF so far (maybe the problem is me, who knows!). Be careful here though, WCF is not gone! It is still the way to go with for SOAP based services.&lt;/p&gt;
&lt;p&gt;Let&amp;rsquo;s see how we get started developing a &lt;strong&gt;Web API&lt;/strong&gt; with &lt;strong&gt;WCF Web API &lt;/strong&gt;(this sentence is like a poem &lt;img style="border-style: none;" class="wlEmoticon wlEmoticon-smile" alt="Smile" src="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/ec9739380e27_9F4D/wlEmoticon-smile.png" /&gt;).&lt;/p&gt;
&lt;p&gt;First of all, open up your VS and create a new &lt;strong&gt;ASP.NET Empty Web Application&lt;/strong&gt; (this one is real &lt;strong&gt;empty&lt;/strong&gt; guys unlike &lt;strong&gt;ASP.NET MVC 3 Empty Web Application&lt;/strong&gt;):&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/ec9739380e27_9F4D/image_3.png"&gt;&lt;img style="background-image: none; margin: 0px 15px 0px 0px; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" title="image" border="0" alt="image" src="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/ec9739380e27_9F4D/image_thumb_3.png" width="244" height="159" /&gt;&lt;/a&gt;&lt;a href="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/ec9739380e27_9F4D/image_4.png"&gt;&lt;img style="background-image: none; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" title="image" border="0" alt="image" src="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/ec9739380e27_9F4D/image_thumb_4.png" width="244" height="160" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;I told you it is real empty. Anyway, let&amp;rsquo;s stick to the point here&lt;img style="border-style: none;" class="wlEmoticon wlEmoticon-smile" alt="Smile" src="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/ec9739380e27_9F4D/wlEmoticon-smile.png" /&gt; Now, bring up the &lt;a title="http://nuget.org" href="http://nuget.org" target="_blank"&gt;NuGet&lt;/a&gt; PMC (&lt;strong&gt;P&lt;/strong&gt;ackage &lt;strong&gt;M&lt;/strong&gt;anager &lt;strong&gt;C&lt;/strong&gt;onsole) and install the package called &lt;strong&gt;WebApi.All&lt;/strong&gt;:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/ec9739380e27_9F4D/image_5.png"&gt;&lt;img style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" title="image" border="0" alt="image" src="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/ec9739380e27_9F4D/image_thumb_5.png" width="640" height="75" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/ec9739380e27_9F4D/image_6.png"&gt;&lt;img style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" title="image" border="0" alt="image" src="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/ec9739380e27_9F4D/image_thumb_6.png" width="644" height="238" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Let&amp;rsquo;s look what we got:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/ec9739380e27_9F4D/image_7.png"&gt;&lt;img style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" title="image" border="0" alt="image" src="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/ec9739380e27_9F4D/image_thumb_7.png" width="644" height="187" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;All the packages we have pulled is individual packages. For example, if you are consuming web services on the server side, &lt;strong&gt;HttpClient&lt;/strong&gt; package will help you a lot. I think all of those packages will be baked-in for .NET 4.5 so we will see a lot samples for those.&lt;/p&gt;
&lt;p&gt;One thing that I looked when I first bring up this package is Web.Config file because I wondered how giant it was. It comes to me as a shock and it will for you, too:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/ec9739380e27_9F4D/image_8.png"&gt;&lt;img style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" title="image" border="0" alt="image" src="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/ec9739380e27_9F4D/image_thumb_8.png" width="644" height="229" /&gt;&lt;/a&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;3 lines of code which is special to WCF Web API. This is awesome. So, where do we configure the stuff. First of all, if you would like to get started you do not need to make any configuration. We will see how in a minute. Web API comes with default configuration and this can be overridden in any steps. You can set your default configuration. One of your APIs needs different configuration? Don&amp;rsquo;t change the default one. Configure it separately. So, it is really a &lt;strong&gt;convention&lt;/strong&gt;. Best part is that it is all done with code, I mean inside &lt;strong&gt;Global.asax&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;For the sake of this demo, I created a dummy data to play with. It is really simple as follows (I will put up the source code online, you can check out what is in there):&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/ec9739380e27_9F4D/image_9.png"&gt;&lt;img style="background-image: none; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" title="image" border="0" alt="image" src="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/ec9739380e27_9F4D/image_thumb_9.png" width="220" height="244" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;I implemented the repository pattern here with an interface. I did that because I would like to show you how easy is to get going along with Dependency Injection (DI) here.&lt;/p&gt;
&lt;p&gt;In order to create our API, we need to create a separate class. I put it under &lt;strong&gt;People&lt;/strong&gt; folder and named it &lt;strong&gt;PeopleApi &lt;/strong&gt;but where it stands and what name it carries don&amp;rsquo;t matter here.&lt;/p&gt;
&lt;p&gt;I would like to go with the simplest approach firstly. Here is how &lt;strong&gt;PeopleApi&lt;/strong&gt; class looks like:&lt;/p&gt;
&lt;div class="code-wrapper border-shadow-1"&gt;
&lt;div style="background-color: white; color: black;"&gt;
&lt;pre&gt;&lt;span style="color: blue;"&gt;using&lt;/span&gt; System;
&lt;span style="color: blue;"&gt;using&lt;/span&gt; System.Collections.Generic;
&lt;span style="color: blue;"&gt;using&lt;/span&gt; System.Linq;
&lt;span style="color: blue;"&gt;using&lt;/span&gt; System.Net.Http;
&lt;span style="color: blue;"&gt;using&lt;/span&gt; System.ServiceModel;
&lt;span style="color: blue;"&gt;using&lt;/span&gt; System.ServiceModel.Web;
&lt;span style="color: blue;"&gt;using&lt;/span&gt; System.Web;
&lt;span style="color: blue;"&gt;using&lt;/span&gt; VeryFirstWcfWebAPI.People.Infrastructure;
&lt;span style="color: blue;"&gt;using&lt;/span&gt; VeryFirstWcfWebAPI.People.Models;

&lt;span style="color: blue;"&gt;namespace&lt;/span&gt; VeryFirstWcfWebAPI.People {

    [ServiceContract]
    &lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;class&lt;/span&gt; PeopleApi {

        &lt;span style="color: blue;"&gt;private&lt;/span&gt; &lt;span style="color: blue;"&gt;readonly&lt;/span&gt; IPeopleRepository _repo = &lt;span style="color: blue;"&gt;new&lt;/span&gt; PeopleRepository();

        [WebGet]
        &lt;span style="color: blue;"&gt;public&lt;/span&gt; HttpResponseMessage&amp;lt;IQueryable&amp;lt;Person&amp;gt;&amp;gt; Get() {

            &lt;span style="color: blue;"&gt;var&lt;/span&gt; model = _repo.GetAll();

            &lt;span style="color: blue;"&gt;var&lt;/span&gt; responseMessage = &lt;span style="color: blue;"&gt;new&lt;/span&gt; HttpResponseMessage&amp;lt;IQueryable&amp;lt;Person&amp;gt;&amp;gt;(model);
            responseMessage.Content.Headers.Expires = &lt;span style="color: blue;"&gt;new&lt;/span&gt; DateTimeOffset(DateTime.Now.AddHours(6));

            &lt;span style="color: blue;"&gt;return&lt;/span&gt; responseMessage;
        }

    }
}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;When we observe this class a little bit carefully, we see some staff going on there:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;You API class needs to be annotated with &lt;a title="http://msdn.microsoft.com/en-us/library/system.servicemodel.servicecontractattribute.aspx" href="http://msdn.microsoft.com/en-us/library/system.servicemodel.servicecontractattribute.aspx" target="_blank"&gt;ServiceContractAttribute&lt;/a&gt;. This is must to do (&lt;a title="http://twitter.com/gblock" href="http://twitter.com/gblock" target="_blank"&gt;@gblock&lt;/a&gt; said at the //Build conference that we are still in love with attributes but we are trying to get rid of them...).&lt;/li&gt;
&lt;li&gt;The methods inside your class needs to have some special attributes like &lt;a title="http://msdn.microsoft.com/en-us/library/system.servicemodel.web.webgetattribute.aspx" href="http://msdn.microsoft.com/en-us/library/system.servicemodel.web.webgetattribute.aspx" target="_blank"&gt;WebGetAttribute&lt;/a&gt; and &lt;a title="http://msdn.microsoft.com/en-us/library/system.servicemodel.web.webinvokeattribute.aspx" href="http://msdn.microsoft.com/en-us/library/system.servicemodel.web.webinvokeattribute.aspx" target="_blank"&gt;WebInvokeAttribute&lt;/a&gt;. If you put them without &lt;a title="http://msdn.microsoft.com/en-us/library/system.servicemodel.web.webgetattribute.uritemplate.aspx" href="http://msdn.microsoft.com/en-us/library/system.servicemodel.web.webgetattribute.uritemplate.aspx" target="_blank"&gt;UriTemplate&lt;/a&gt; property, it assumes that the method is for root of the URL.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Also, another thing to notice here is we are returning our model by wrapping it up with &lt;a title="http://msdn.microsoft.com/en-us/library/system.net.http.httpresponsemessage(v=vs.110).aspx" href="http://msdn.microsoft.com/en-us/library/system.net.http.httpresponsemessage(v=vs.110).aspx" target="_blank"&gt;HttpResponseMessage&lt;/a&gt; class. You don&amp;rsquo;t have to do that. You can just return your object but if you need to add some special headers or response message code, it is nice way to do it that way as we set here our expires header.&lt;/p&gt;
&lt;p&gt;As I mentioned before, there is no configuration at all in the web.config but we still need to do some configuration to tell the system to figure out what to do.&lt;/p&gt;
&lt;p&gt;Add a Global Application Class under the root of your project:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/ec9739380e27_9F4D/image_10.png"&gt;&lt;img style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" title="image" border="0" alt="image" src="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/ec9739380e27_9F4D/image_thumb_10.png" width="644" height="418" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Inside the Application_Start method, here is our initial set up to get going:&lt;/p&gt;
&lt;div class="code-wrapper border-shadow-1"&gt;
&lt;div style="background-color: white; color: black;"&gt;
&lt;pre&gt;&lt;span style="color: blue;"&gt;using&lt;/span&gt; System;
&lt;span style="color: blue;"&gt;using&lt;/span&gt; System.Collections.Generic;
&lt;span style="color: blue;"&gt;using&lt;/span&gt; System.Linq;
&lt;span style="color: blue;"&gt;using&lt;/span&gt; System.Web;
&lt;span style="color: blue;"&gt;using&lt;/span&gt; System.Web.Routing;
&lt;span style="color: blue;"&gt;using&lt;/span&gt; System.Web.Security;
&lt;span style="color: blue;"&gt;using&lt;/span&gt; System.Web.SessionState;

&lt;span style="color: blue;"&gt;namespace&lt;/span&gt; VeryFirstWcfWebAPI {

    &lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;class&lt;/span&gt; Global : System.Web.HttpApplication {

        &lt;span style="color: blue;"&gt;protected&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; Application_Start(&lt;span style="color: blue;"&gt;object&lt;/span&gt; sender, EventArgs e) {

            RouteTable.Routes.SetDefaultHttpConfiguration(&lt;span style="color: blue;"&gt;new&lt;/span&gt; Microsoft.ApplicationServer.Http.WebApiConfiguration() { 
            });

            RouteTable.Routes.MapServiceRoute&amp;lt;People.PeopleApi&amp;gt;(&lt;span style="color: #a31515;"&gt;"Api/People"&lt;/span&gt;);
        }

    }
}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;What we do here is so simple:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;We are initializing the WCF Web API with default configuration.&lt;/li&gt;
&lt;li&gt;We specifically register our API with a base URL structure as route prefix.&lt;/li&gt;
&lt;/ul&gt;
&lt;blockquote&gt;
&lt;p&gt;As we have folder called &lt;strong&gt;People&lt;/strong&gt; under the root of out application, if you put &lt;strong&gt;People&lt;/strong&gt; as route prefix there, you will see that your API won&amp;rsquo;t work and you will get 404. I am nearly sure that it is related to routing.&lt;/p&gt;
&lt;p&gt;I haven&amp;rsquo;t figured out how to solve this issue and I tried to Ignore that folder but it didn&amp;rsquo;t work either.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;We are ready to run:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/ec9739380e27_9F4D/image_11.png"&gt;&lt;img style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" title="image" border="0" alt="image" src="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/ec9739380e27_9F4D/image_thumb_11.png" width="644" height="433" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;This is good for a start. We didn&amp;rsquo;t suffer much. Now, it is time for us to think of the possible enhancements.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Dependency Injection and IoC Container Support&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;As you probably noticed, I "newed" up the repository inside our API class:&lt;/p&gt;
&lt;div class="code-wrapper border-shadow-1"&gt;
&lt;div style="background-color: white; color: black;"&gt;
&lt;pre&gt;&lt;span style="color: blue;"&gt;private&lt;/span&gt; &lt;span style="color: blue;"&gt;readonly&lt;/span&gt; IPeopleRepository _repo = &lt;span style="color: blue;"&gt;new&lt;/span&gt; PeopleRepository();&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;This thing made my application tightly-coupled and it is not good. Here I have a static data resource and it is not much of a problem but if we had database related data structure here, this would make it hard for us to unit test our application.&lt;/p&gt;
&lt;p&gt;In order to get around for this, we need to figure out a way to new up the resource outside of our context and WCF Web API offers really good extensibility point here. I won&amp;rsquo;t really extend and customize our configuration much here in order to stick with the basics but I will probably blog about that either.&lt;/p&gt;
&lt;p&gt;First of all, let&amp;rsquo;s make our API class a little DI friendly:&lt;/p&gt;
&lt;div class="code-wrapper border-shadow-1"&gt;
&lt;div style="background-color: white; color: black;"&gt;
&lt;pre&gt;    [ServiceContract]
    &lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;class&lt;/span&gt; PeopleApi {

        &lt;span style="color: blue;"&gt;private&lt;/span&gt; &lt;span style="color: blue;"&gt;readonly&lt;/span&gt; IPeopleRepository _repo;

        &lt;span style="color: blue;"&gt;public&lt;/span&gt; PeopleApi(IPeopleRepository repo) {
            _repo = repo;
        }

        [WebGet]
        &lt;span style="color: blue;"&gt;public&lt;/span&gt; HttpResponseMessage&amp;lt;IQueryable&amp;lt;Person&amp;gt;&amp;gt; Get() {

            &lt;span style="color: blue;"&gt;var&lt;/span&gt; model = _repo.GetAll();

            &lt;span style="color: blue;"&gt;var&lt;/span&gt; responseMessage = &lt;span style="color: blue;"&gt;new&lt;/span&gt; HttpResponseMessage&amp;lt;IQueryable&amp;lt;Person&amp;gt;&amp;gt;(model);
            responseMessage.Content.Headers.Expires = &lt;span style="color: blue;"&gt;new&lt;/span&gt; DateTimeOffset(DateTime.Now.AddHours(6));

            &lt;span style="color: blue;"&gt;return&lt;/span&gt; responseMessage;
        }

    }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;What we do here enables someone else to provide the resource so our API class is now loosely-coupled. I would like to see what happens when we run the app like this:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/ec9739380e27_9F4D/image_12.png"&gt;&lt;img style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" title="image" border="0" alt="image" src="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/ec9739380e27_9F4D/image_thumb_12.png" width="644" height="433" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;We got an error:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;span color="#ff0000" style="color: #ff0000;"&gt;&lt;strong&gt;The service type provided could not be loaded as a service because it does not have a default (parameter-less) constructor. To fix the problem, add a default constructor to the type, or pass an instance of the type to the host.&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;The system needs a parameter-less constructor as default. Let&amp;rsquo;s see how we get around with this.&lt;/p&gt;
&lt;p&gt;Now, we need to provide those resources outside of our context but where? Let&amp;rsquo;s first bring down a IoC container. I am fan of Ninject so I will use it here as well:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/ec9739380e27_9F4D/image_13.png"&gt;&lt;img style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" title="image" border="0" alt="image" src="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/ec9739380e27_9F4D/image_thumb_13.png" width="644" height="191" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;After we install Ninject through NuGet, I added the following code inside my Global.asax file:&lt;/p&gt;
&lt;div class="code-wrapper border-shadow-1"&gt;
&lt;div style="background-color: white; color: black;"&gt;
&lt;pre&gt;&lt;span style="color: blue;"&gt;private&lt;/span&gt; IKernel GetKernel() { 
    
    IKernel kernel = &lt;span style="color: blue;"&gt;new&lt;/span&gt; StandardKernel();

    kernel.Bind&amp;lt;People.Infrastructure.IPeopleRepository&amp;gt;().
        To&amp;lt;People.Models.PeopleRepository&amp;gt;();

    &lt;span style="color: blue;"&gt;return&lt;/span&gt; kernel;
}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;This will provide us the resources we need. Now, we need to tell WCF Web API to use this provider to create instances. Believe it or not, it is extremely easy to do that. Remember our default configuration object, WebApiConfiguration class? We will register our IoC container there:&lt;/p&gt;
&lt;div class="code-wrapper border-shadow-1"&gt;
&lt;div style="background-color: white; color: black;"&gt;
&lt;pre&gt;RouteTable.Routes.SetDefaultHttpConfiguration(&lt;span style="color: blue;"&gt;new&lt;/span&gt; Microsoft.ApplicationServer.Http.WebApiConfiguration() { 
    CreateInstance = (serviceType, context, request) =&amp;gt; GetKernel().Get(serviceType)
});&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;We are passing a delegate here for CreateInstance property of our WebApiConfiguration class. When we run the application, we should see it working:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/ec9739380e27_9F4D/image_14.png"&gt;&lt;img style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" title="image" border="0" alt="image" src="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/ec9739380e27_9F4D/image_thumb_14.png" width="644" height="433" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;So nice to do something like that without much effort.&lt;/p&gt;
&lt;p&gt;There is so much to show but I think this is enough for an intro (which I write on the stage of preview 5, I am little late &lt;img style="border-style: none;" class="wlEmoticon wlEmoticon-confusedsmile" alt="Confused smile" src="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/ec9739380e27_9F4D/wlEmoticon-confusedsmile.png" /&gt;). I am sure that you get the idea here.&lt;/p&gt;
&lt;p&gt;You can find the full code on &lt;a title="https://github.com" href="https://github.com" target="_blank"&gt;GitHub&lt;/a&gt;: &lt;a href="https://github.com/tugberkugurlu/VeryFirstWcfWebAPI" target="_blank"&gt;https://github.com/tugberkugurlu/VeryFirstWcfWebAPI&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/lnQUsNVgNTqslXMV9vfKex04XCM/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/lnQUsNVgNTqslXMV9vfKex04XCM/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/lnQUsNVgNTqslXMV9vfKex04XCM/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/lnQUsNVgNTqslXMV9vfKex04XCM/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/TugberkUgurlu/~4/K6c53NXTteM" height="1" width="1"/&gt;</description><pubDate>Mon, 21 Nov 2011 08:09:00 Z</pubDate><a10:updated>2011-11-21T08:09:00Z</a10:updated><a10:content type="text">&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;24 February 2012&lt;/strong&gt;&lt;/p&gt;
WCF Web API is now &lt;a href="http://asp.net/web-api" title="http://asp.net/web-api"&gt;ASP.NET Web API&lt;/a&gt;&amp;nbsp;and has changed a lot. The beta version is now available.&amp;nbsp;For more information: &lt;a href="http://www.tugberkugurlu.com/archive/getting-started-with-asp-net-web-api-tutorials-videos-samples" title="http://www.tugberkugurlu.com/archive/getting-started-with-asp-net-web-api-tutorials-videos-samples"&gt;Getting Started With ASP.NET Web API - Tutorials, Videos, Samples&lt;/a&gt;.&lt;/blockquote&gt;
&lt;p&gt;&lt;a href="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/ec9739380e27_9F4D/image.png"&gt;&lt;img style="background-image: none; margin: 0px 15px 10px 0px; padding-left: 0px; padding-right: 0px; display: inline; float: left; padding-top: 0px; border-width: 0px;" title="image" border="0" alt="image" align="left" src="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/ec9739380e27_9F4D/image_thumb.png" width="244" height="166" /&gt;&lt;/a&gt;Microsoft Web Platform is evolving. I mean really evolving. &lt;a title="http://hanselman.com/" href="http://hanselman.com/" target="_blank"&gt;Scott Hanselman&lt;/a&gt;, &lt;a title="http://haacked.com" href="http://haacked.com" target="_blank"&gt;Phil Haack&lt;/a&gt;, &lt;a title="http://blogs.msdn.com/gblock" href="http://blogs.msdn.com/gblock" target="_blank"&gt;Glenn Block&lt;/a&gt; and &lt;a title="http://damianedwards.wordpress.com" href="http://damianedwards.wordpress.com" target="_blank"&gt;Damian Edwards&lt;/a&gt; are the main actors for this evolution.&lt;/p&gt;
&lt;p&gt;One of the biggest frustration we had as web developers was to face with the endless configurations with WCF. I mean, WCF is great but hasn&amp;rsquo;t been embraced the REST since the &lt;a title="http://wcf.codeplex.com/wikipage?title=WCF%20HTTP" href="http://wcf.codeplex.com/wikipage?title=WCF%20HTTP" target="_blank"&gt;WCF Web API&lt;/a&gt; framework. There was something called REST Starter Kit but it ended up dead.&lt;/p&gt;
&lt;p&gt;Yes, WCF Web API. WCF Web API is the new way of exposing and consuming APIs over http in .NET. The unofficial (not sure, maybe the official one) slogan of WCF Web API is this:&lt;/p&gt;
&lt;p&gt;&lt;em&gt;"Making REST a first class citizen in .NET"&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;I stole (or quoted would be nicer) the real sentences which explain the WCF Web API from the official WCF Web API page (I hope they don&amp;rsquo;t mind):&lt;/p&gt;
&lt;blockquote&gt;
&lt;h4&gt;What is WCF Web API?&lt;/h4&gt;
&lt;p&gt;&lt;em&gt;Applications are continually evolving to expose their functionality over the web for example social services like &lt;/em&gt;&lt;a href="http://www.flickr.com/"&gt;&lt;em&gt;Flickr&lt;/em&gt;&lt;/a&gt;&lt;em&gt;, &lt;/em&gt;&lt;a href="http://www.twitter.com/"&gt;&lt;em&gt;Twitter&lt;/em&gt;&lt;/a&gt;&lt;em&gt; and &lt;/em&gt;&lt;a href="http://www.facebook.com/"&gt;&lt;em&gt;Facebook&lt;/em&gt;&lt;/a&gt;&lt;em&gt;. Aside from social applications, organizations are also looking to surface their core enterprise business functionality to an ever expanding array of client platforms. WCF Web API allows developers to expose their applications, data and services to the web directly over &lt;/em&gt;&lt;a href="http://en.wikipedia.org/wiki/HTTP"&gt;&lt;em&gt;HTTP&lt;/em&gt;&lt;/a&gt;&lt;em&gt;. This allows developers to fully harness the richness of the HTTP as an application layer protocol. Applications can communicate with a very broad set of clients whether they be browsers, mobile devices, desktop applications or other backend services. They can also take advantage of the caching and proxy infrastructure of the web through providing proper control and entity headers. We are designing specifically to support applications built with a &lt;/em&gt;&lt;a href="http://en.wikipedia.org/wiki/Representational_State_Transfer"&gt;&lt;em&gt;RESTful&lt;/em&gt;&lt;/a&gt;&lt;em&gt; architecture style though it does not force developers to use REST. The benefits of REST for your applications include discoverability, evolvability and scalability.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;The project is still at the preview stage and we are swimming in the dark sea. There are lots of rumors going around about it and most of them are nearly certain to be true. One of them is that WCF Web API and &lt;a title="http://asp.net/mvc" href="http://asp.net/mvc" target="_blank"&gt;ASP.NEt MVC&lt;/a&gt; will be blood brothers in near future. In plain English, they will be merged together. This will be exciting and we won&amp;rsquo;t feel ourselves in a fork in the road when we need to pick ASP.NET MVC or WCF Web API in order to expose our data over http.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Cut the crap and show me the code&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Well, when I first saw the WCF Web API, I told myself that this&amp;rsquo;s it! Why I told that? Because it is extremely easy to get started and going from there. I am interested in WCF as well but its configuration is endless so I haven&amp;rsquo;t been able to develop a decent project with WCF so far (maybe the problem is me, who knows!). Be careful here though, WCF is not gone! It is still the way to go with for SOAP based services.&lt;/p&gt;
&lt;p&gt;Let&amp;rsquo;s see how we get started developing a &lt;strong&gt;Web API&lt;/strong&gt; with &lt;strong&gt;WCF Web API &lt;/strong&gt;(this sentence is like a poem &lt;img style="border-style: none;" class="wlEmoticon wlEmoticon-smile" alt="Smile" src="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/ec9739380e27_9F4D/wlEmoticon-smile.png" /&gt;).&lt;/p&gt;
&lt;p&gt;First of all, open up your VS and create a new &lt;strong&gt;ASP.NET Empty Web Application&lt;/strong&gt; (this one is real &lt;strong&gt;empty&lt;/strong&gt; guys unlike &lt;strong&gt;ASP.NET MVC 3 Empty Web Application&lt;/strong&gt;):&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/ec9739380e27_9F4D/image_3.png"&gt;&lt;img style="background-image: none; margin: 0px 15px 0px 0px; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" title="image" border="0" alt="image" src="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/ec9739380e27_9F4D/image_thumb_3.png" width="244" height="159" /&gt;&lt;/a&gt;&lt;a href="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/ec9739380e27_9F4D/image_4.png"&gt;&lt;img style="background-image: none; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" title="image" border="0" alt="image" src="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/ec9739380e27_9F4D/image_thumb_4.png" width="244" height="160" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;I told you it is real empty. Anyway, let&amp;rsquo;s stick to the point here&lt;img style="border-style: none;" class="wlEmoticon wlEmoticon-smile" alt="Smile" src="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/ec9739380e27_9F4D/wlEmoticon-smile.png" /&gt; Now, bring up the &lt;a title="http://nuget.org" href="http://nuget.org" target="_blank"&gt;NuGet&lt;/a&gt; PMC (&lt;strong&gt;P&lt;/strong&gt;ackage &lt;strong&gt;M&lt;/strong&gt;anager &lt;strong&gt;C&lt;/strong&gt;onsole) and install the package called &lt;strong&gt;WebApi.All&lt;/strong&gt;:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/ec9739380e27_9F4D/image_5.png"&gt;&lt;img style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" title="image" border="0" alt="image" src="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/ec9739380e27_9F4D/image_thumb_5.png" width="640" height="75" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/ec9739380e27_9F4D/image_6.png"&gt;&lt;img style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" title="image" border="0" alt="image" src="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/ec9739380e27_9F4D/image_thumb_6.png" width="644" height="238" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Let&amp;rsquo;s look what we got:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/ec9739380e27_9F4D/image_7.png"&gt;&lt;img style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" title="image" border="0" alt="image" src="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/ec9739380e27_9F4D/image_thumb_7.png" width="644" height="187" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;All the packages we have pulled is individual packages. For example, if you are consuming web services on the server side, &lt;strong&gt;HttpClient&lt;/strong&gt; package will help you a lot. I think all of those packages will be baked-in for .NET 4.5 so we will see a lot samples for those.&lt;/p&gt;
&lt;p&gt;One thing that I looked when I first bring up this package is Web.Config file because I wondered how giant it was. It comes to me as a shock and it will for you, too:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/ec9739380e27_9F4D/image_8.png"&gt;&lt;img style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" title="image" border="0" alt="image" src="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/ec9739380e27_9F4D/image_thumb_8.png" width="644" height="229" /&gt;&lt;/a&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;3 lines of code which is special to WCF Web API. This is awesome. So, where do we configure the stuff. First of all, if you would like to get started you do not need to make any configuration. We will see how in a minute. Web API comes with default configuration and this can be overridden in any steps. You can set your default configuration. One of your APIs needs different configuration? Don&amp;rsquo;t change the default one. Configure it separately. So, it is really a &lt;strong&gt;convention&lt;/strong&gt;. Best part is that it is all done with code, I mean inside &lt;strong&gt;Global.asax&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;For the sake of this demo, I created a dummy data to play with. It is really simple as follows (I will put up the source code online, you can check out what is in there):&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/ec9739380e27_9F4D/image_9.png"&gt;&lt;img style="background-image: none; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" title="image" border="0" alt="image" src="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/ec9739380e27_9F4D/image_thumb_9.png" width="220" height="244" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;I implemented the repository pattern here with an interface. I did that because I would like to show you how easy is to get going along with Dependency Injection (DI) here.&lt;/p&gt;
&lt;p&gt;In order to create our API, we need to create a separate class. I put it under &lt;strong&gt;People&lt;/strong&gt; folder and named it &lt;strong&gt;PeopleApi &lt;/strong&gt;but where it stands and what name it carries don&amp;rsquo;t matter here.&lt;/p&gt;
&lt;p&gt;I would like to go with the simplest approach firstly. Here is how &lt;strong&gt;PeopleApi&lt;/strong&gt; class looks like:&lt;/p&gt;
&lt;div class="code-wrapper border-shadow-1"&gt;
&lt;div style="background-color: white; color: black;"&gt;
&lt;pre&gt;&lt;span style="color: blue;"&gt;using&lt;/span&gt; System;
&lt;span style="color: blue;"&gt;using&lt;/span&gt; System.Collections.Generic;
&lt;span style="color: blue;"&gt;using&lt;/span&gt; System.Linq;
&lt;span style="color: blue;"&gt;using&lt;/span&gt; System.Net.Http;
&lt;span style="color: blue;"&gt;using&lt;/span&gt; System.ServiceModel;
&lt;span style="color: blue;"&gt;using&lt;/span&gt; System.ServiceModel.Web;
&lt;span style="color: blue;"&gt;using&lt;/span&gt; System.Web;
&lt;span style="color: blue;"&gt;using&lt;/span&gt; VeryFirstWcfWebAPI.People.Infrastructure;
&lt;span style="color: blue;"&gt;using&lt;/span&gt; VeryFirstWcfWebAPI.People.Models;

&lt;span style="color: blue;"&gt;namespace&lt;/span&gt; VeryFirstWcfWebAPI.People {

    [ServiceContract]
    &lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;class&lt;/span&gt; PeopleApi {

        &lt;span style="color: blue;"&gt;private&lt;/span&gt; &lt;span style="color: blue;"&gt;readonly&lt;/span&gt; IPeopleRepository _repo = &lt;span style="color: blue;"&gt;new&lt;/span&gt; PeopleRepository();

        [WebGet]
        &lt;span style="color: blue;"&gt;public&lt;/span&gt; HttpResponseMessage&amp;lt;IQueryable&amp;lt;Person&amp;gt;&amp;gt; Get() {

            &lt;span style="color: blue;"&gt;var&lt;/span&gt; model = _repo.GetAll();

            &lt;span style="color: blue;"&gt;var&lt;/span&gt; responseMessage = &lt;span style="color: blue;"&gt;new&lt;/span&gt; HttpResponseMessage&amp;lt;IQueryable&amp;lt;Person&amp;gt;&amp;gt;(model);
            responseMessage.Content.Headers.Expires = &lt;span style="color: blue;"&gt;new&lt;/span&gt; DateTimeOffset(DateTime.Now.AddHours(6));

            &lt;span style="color: blue;"&gt;return&lt;/span&gt; responseMessage;
        }

    }
}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;When we observe this class a little bit carefully, we see some staff going on there:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;You API class needs to be annotated with &lt;a title="http://msdn.microsoft.com/en-us/library/system.servicemodel.servicecontractattribute.aspx" href="http://msdn.microsoft.com/en-us/library/system.servicemodel.servicecontractattribute.aspx" target="_blank"&gt;ServiceContractAttribute&lt;/a&gt;. This is must to do (&lt;a title="http://twitter.com/gblock" href="http://twitter.com/gblock" target="_blank"&gt;@gblock&lt;/a&gt; said at the //Build conference that we are still in love with attributes but we are trying to get rid of them...).&lt;/li&gt;
&lt;li&gt;The methods inside your class needs to have some special attributes like &lt;a title="http://msdn.microsoft.com/en-us/library/system.servicemodel.web.webgetattribute.aspx" href="http://msdn.microsoft.com/en-us/library/system.servicemodel.web.webgetattribute.aspx" target="_blank"&gt;WebGetAttribute&lt;/a&gt; and &lt;a title="http://msdn.microsoft.com/en-us/library/system.servicemodel.web.webinvokeattribute.aspx" href="http://msdn.microsoft.com/en-us/library/system.servicemodel.web.webinvokeattribute.aspx" target="_blank"&gt;WebInvokeAttribute&lt;/a&gt;. If you put them without &lt;a title="http://msdn.microsoft.com/en-us/library/system.servicemodel.web.webgetattribute.uritemplate.aspx" href="http://msdn.microsoft.com/en-us/library/system.servicemodel.web.webgetattribute.uritemplate.aspx" target="_blank"&gt;UriTemplate&lt;/a&gt; property, it assumes that the method is for root of the URL.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Also, another thing to notice here is we are returning our model by wrapping it up with &lt;a title="http://msdn.microsoft.com/en-us/library/system.net.http.httpresponsemessage(v=vs.110).aspx" href="http://msdn.microsoft.com/en-us/library/system.net.http.httpresponsemessage(v=vs.110).aspx" target="_blank"&gt;HttpResponseMessage&lt;/a&gt; class. You don&amp;rsquo;t have to do that. You can just return your object but if you need to add some special headers or response message code, it is nice way to do it that way as we set here our expires header.&lt;/p&gt;
&lt;p&gt;As I mentioned before, there is no configuration at all in the web.config but we still need to do some configuration to tell the system to figure out what to do.&lt;/p&gt;
&lt;p&gt;Add a Global Application Class under the root of your project:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/ec9739380e27_9F4D/image_10.png"&gt;&lt;img style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" title="image" border="0" alt="image" src="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/ec9739380e27_9F4D/image_thumb_10.png" width="644" height="418" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Inside the Application_Start method, here is our initial set up to get going:&lt;/p&gt;
&lt;div class="code-wrapper border-shadow-1"&gt;
&lt;div style="background-color: white; color: black;"&gt;
&lt;pre&gt;&lt;span style="color: blue;"&gt;using&lt;/span&gt; System;
&lt;span style="color: blue;"&gt;using&lt;/span&gt; System.Collections.Generic;
&lt;span style="color: blue;"&gt;using&lt;/span&gt; System.Linq;
&lt;span style="color: blue;"&gt;using&lt;/span&gt; System.Web;
&lt;span style="color: blue;"&gt;using&lt;/span&gt; System.Web.Routing;
&lt;span style="color: blue;"&gt;using&lt;/span&gt; System.Web.Security;
&lt;span style="color: blue;"&gt;using&lt;/span&gt; System.Web.SessionState;

&lt;span style="color: blue;"&gt;namespace&lt;/span&gt; VeryFirstWcfWebAPI {

    &lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;class&lt;/span&gt; Global : System.Web.HttpApplication {

        &lt;span style="color: blue;"&gt;protected&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; Application_Start(&lt;span style="color: blue;"&gt;object&lt;/span&gt; sender, EventArgs e) {

            RouteTable.Routes.SetDefaultHttpConfiguration(&lt;span style="color: blue;"&gt;new&lt;/span&gt; Microsoft.ApplicationServer.Http.WebApiConfiguration() { 
            });

            RouteTable.Routes.MapServiceRoute&amp;lt;People.PeopleApi&amp;gt;(&lt;span style="color: #a31515;"&gt;"Api/People"&lt;/span&gt;);
        }

    }
}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;What we do here is so simple:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;We are initializing the WCF Web API with default configuration.&lt;/li&gt;
&lt;li&gt;We specifically register our API with a base URL structure as route prefix.&lt;/li&gt;
&lt;/ul&gt;
&lt;blockquote&gt;
&lt;p&gt;As we have folder called &lt;strong&gt;People&lt;/strong&gt; under the root of out application, if you put &lt;strong&gt;People&lt;/strong&gt; as route prefix there, you will see that your API won&amp;rsquo;t work and you will get 404. I am nearly sure that it is related to routing.&lt;/p&gt;
&lt;p&gt;I haven&amp;rsquo;t figured out how to solve this issue and I tried to Ignore that folder but it didn&amp;rsquo;t work either.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;We are ready to run:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/ec9739380e27_9F4D/image_11.png"&gt;&lt;img style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" title="image" border="0" alt="image" src="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/ec9739380e27_9F4D/image_thumb_11.png" width="644" height="433" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;This is good for a start. We didn&amp;rsquo;t suffer much. Now, it is time for us to think of the possible enhancements.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Dependency Injection and IoC Container Support&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;As you probably noticed, I "newed" up the repository inside our API class:&lt;/p&gt;
&lt;div class="code-wrapper border-shadow-1"&gt;
&lt;div style="background-color: white; color: black;"&gt;
&lt;pre&gt;&lt;span style="color: blue;"&gt;private&lt;/span&gt; &lt;span style="color: blue;"&gt;readonly&lt;/span&gt; IPeopleRepository _repo = &lt;span style="color: blue;"&gt;new&lt;/span&gt; PeopleRepository();&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;This thing made my application tightly-coupled and it is not good. Here I have a static data resource and it is not much of a problem but if we had database related data structure here, this would make it hard for us to unit test our application.&lt;/p&gt;
&lt;p&gt;In order to get around for this, we need to figure out a way to new up the resource outside of our context and WCF Web API offers really good extensibility point here. I won&amp;rsquo;t really extend and customize our configuration much here in order to stick with the basics but I will probably blog about that either.&lt;/p&gt;
&lt;p&gt;First of all, let&amp;rsquo;s make our API class a little DI friendly:&lt;/p&gt;
&lt;div class="code-wrapper border-shadow-1"&gt;
&lt;div style="background-color: white; color: black;"&gt;
&lt;pre&gt;    [ServiceContract]
    &lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;class&lt;/span&gt; PeopleApi {

        &lt;span style="color: blue;"&gt;private&lt;/span&gt; &lt;span style="color: blue;"&gt;readonly&lt;/span&gt; IPeopleRepository _repo;

        &lt;span style="color: blue;"&gt;public&lt;/span&gt; PeopleApi(IPeopleRepository repo) {
            _repo = repo;
        }

        [WebGet]
        &lt;span style="color: blue;"&gt;public&lt;/span&gt; HttpResponseMessage&amp;lt;IQueryable&amp;lt;Person&amp;gt;&amp;gt; Get() {

            &lt;span style="color: blue;"&gt;var&lt;/span&gt; model = _repo.GetAll();

            &lt;span style="color: blue;"&gt;var&lt;/span&gt; responseMessage = &lt;span style="color: blue;"&gt;new&lt;/span&gt; HttpResponseMessage&amp;lt;IQueryable&amp;lt;Person&amp;gt;&amp;gt;(model);
            responseMessage.Content.Headers.Expires = &lt;span style="color: blue;"&gt;new&lt;/span&gt; DateTimeOffset(DateTime.Now.AddHours(6));

            &lt;span style="color: blue;"&gt;return&lt;/span&gt; responseMessage;
        }

    }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;What we do here enables someone else to provide the resource so our API class is now loosely-coupled. I would like to see what happens when we run the app like this:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/ec9739380e27_9F4D/image_12.png"&gt;&lt;img style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" title="image" border="0" alt="image" src="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/ec9739380e27_9F4D/image_thumb_12.png" width="644" height="433" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;We got an error:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;span color="#ff0000" style="color: #ff0000;"&gt;&lt;strong&gt;The service type provided could not be loaded as a service because it does not have a default (parameter-less) constructor. To fix the problem, add a default constructor to the type, or pass an instance of the type to the host.&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;The system needs a parameter-less constructor as default. Let&amp;rsquo;s see how we get around with this.&lt;/p&gt;
&lt;p&gt;Now, we need to provide those resources outside of our context but where? Let&amp;rsquo;s first bring down a IoC container. I am fan of Ninject so I will use it here as well:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/ec9739380e27_9F4D/image_13.png"&gt;&lt;img style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" title="image" border="0" alt="image" src="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/ec9739380e27_9F4D/image_thumb_13.png" width="644" height="191" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;After we install Ninject through NuGet, I added the following code inside my Global.asax file:&lt;/p&gt;
&lt;div class="code-wrapper border-shadow-1"&gt;
&lt;div style="background-color: white; color: black;"&gt;
&lt;pre&gt;&lt;span style="color: blue;"&gt;private&lt;/span&gt; IKernel GetKernel() { 
    
    IKernel kernel = &lt;span style="color: blue;"&gt;new&lt;/span&gt; StandardKernel();

    kernel.Bind&amp;lt;People.Infrastructure.IPeopleRepository&amp;gt;().
        To&amp;lt;People.Models.PeopleRepository&amp;gt;();

    &lt;span style="color: blue;"&gt;return&lt;/span&gt; kernel;
}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;This will provide us the resources we need. Now, we need to tell WCF Web API to use this provider to create instances. Believe it or not, it is extremely easy to do that. Remember our default configuration object, WebApiConfiguration class? We will register our IoC container there:&lt;/p&gt;
&lt;div class="code-wrapper border-shadow-1"&gt;
&lt;div style="background-color: white; color: black;"&gt;
&lt;pre&gt;RouteTable.Routes.SetDefaultHttpConfiguration(&lt;span style="color: blue;"&gt;new&lt;/span&gt; Microsoft.ApplicationServer.Http.WebApiConfiguration() { 
    CreateInstance = (serviceType, context, request) =&amp;gt; GetKernel().Get(serviceType)
});&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;We are passing a delegate here for CreateInstance property of our WebApiConfiguration class. When we run the application, we should see it working:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/ec9739380e27_9F4D/image_14.png"&gt;&lt;img style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" title="image" border="0" alt="image" src="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/ec9739380e27_9F4D/image_thumb_14.png" width="644" height="433" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;So nice to do something like that without much effort.&lt;/p&gt;
&lt;p&gt;There is so much to show but I think this is enough for an intro (which I write on the stage of preview 5, I am little late &lt;img style="border-style: none;" class="wlEmoticon wlEmoticon-confusedsmile" alt="Confused smile" src="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/ec9739380e27_9F4D/wlEmoticon-confusedsmile.png" /&gt;). I am sure that you get the idea here.&lt;/p&gt;
&lt;p&gt;You can find the full code on &lt;a title="https://github.com" href="https://github.com" target="_blank"&gt;GitHub&lt;/a&gt;: &lt;a href="https://github.com/tugberkugurlu/VeryFirstWcfWebAPI" target="_blank"&gt;https://github.com/tugberkugurlu/VeryFirstWcfWebAPI&lt;/a&gt;&lt;/p&gt;</a10:content><feedburner:origLink>http://tugberkugurlu.com/70</feedburner:origLink></item><item><guid isPermaLink="false">http://tugberkugurlu.com/69</guid><link>http://feedproxy.google.com/~r/TugberkUgurlu/~3/bUlQ2BvAPPU/69</link><title>ASP.NET MVC Server Side Remote Validation</title><description>&lt;p&gt;&lt;a href="http://tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/de656a5ad88d_11FE4/go-to-server-and-come-back.png"&gt;&lt;img height="183" width="244" src="http://tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/de656a5ad88d_11FE4/go-to-server-and-come-back_thumb.png" align="left" alt="go-to-server-and-come-back" border="0" title="go-to-server-and-come-back" style="background-image: none; margin: 0px 15px 15px 0px; padding-left: 0px; padding-right: 0px; display: inline; float: left; padding-top: 0px; border-width: 0px;" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;For nearly 15 days, I have been poking around on the internet, books, etc. for anything related to &lt;a target="_blank" href="http://asp.net/mvc" title="http://asp.net/mvc"&gt;ASP.NET MVC&lt;/a&gt; validation. I have to say that validation features in ASP.NET MVC framework is really outstanding.&lt;/p&gt;
&lt;p&gt;I have blogged about &lt;a target="_blank" href="http://www.tugberkugurlu.com/archive/check-instantly-if-username-exists-asp-net-mvc-remote-validation" title="http://www.tugberkugurlu.com/archive/check-instantly-if-username-exists-asp-net-mvc-remote-validation"&gt;Remote Validation&lt;/a&gt; twice so far: &lt;a target="_blank" href="http://www.tugberkugurlu.com/archive/check-instantly-if-username-exists-asp-net-mvc-remote-validation" title="http://www.tugberkugurlu.com/archive/check-instantly-if-username-exists-asp-net-mvc-remote-validation"&gt;Check Instantly If Username Exists - ASP.NET MVC Remote Validation&lt;/a&gt; and &lt;a target="_blank" href="http://www.tugberkugurlu.com/archive/asp-net-mvc-remote-validation-for-multiple-fields-with-additionalfields-property" title="http://www.tugberkugurlu.com/archive/asp-net-mvc-remote-validation-for-multiple-fields-with-additionalfields-property"&gt;ASP.NET MVC Remote Validation For Multiple Fields With AdditionalFields Property&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Remote validation is one of the useful stuff which is baked into MVC framework. It is easy to implement this feature manually with &lt;a target="_blank" href="http://jquery.com/" title="http://jquery.com/"&gt;JQuery&lt;/a&gt; but it is kind of nice to have it out of the box. So, thanks a lot ASP.NET MVC team.&lt;/p&gt;
&lt;p&gt;One thing which ASP.NET MVC Remove Validation is missing is no support for server side validation of the chosen property. There is probably a good reason not to support it out of the box though.&lt;/p&gt;
&lt;p&gt;So, I decided to build one for myself. When I implemented it, I thought it worked fine and I should blog about it. So, I am writing this &lt;strong&gt;short &lt;/strong&gt;blog post. But, on the other hand I am also sure that no one should use this on production at least for now&lt;img src="http://tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/de656a5ad88d_11FE4/wlEmoticon-smile.png" alt="Smile" class="wlEmoticon wlEmoticon-smile" style="border-style: none;" /&gt; I don&amp;rsquo;t know why but it feels like it is not stable. Maybe you can use it if you see a positive comment from an MSFT person on this post.&lt;/p&gt;
&lt;p&gt;Here how it works:&lt;/p&gt;
&lt;p&gt;First of all, we need to use Nuget here to bring down a very small package called &lt;strong&gt;&lt;a target="_blank" href="http://nuget.org/List/Packages/TugberkUg.MVC" title="http://nuget.org/List/Packages/TugberkUg.MVC"&gt;TugberkUg.MVC&lt;/a&gt;&lt;/strong&gt; which will have the necessary stuff for server side remote validation to work.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/222f24832a52_124A3/image_4.png"&gt;&lt;img original="http://tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/222f24832a52_124A3/image_thumb_4.png" height="75" width="640" src="http://tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/222f24832a52_124A3/image_thumb_4.png" alt="image" border="0" title="image" style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border-width: 0px;" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;After that, here how we can use it:&lt;/p&gt;
&lt;div class="code-wrapper border-shadow-1"&gt;
&lt;div style="background-color: white; color: black;"&gt;
&lt;pre&gt;  [Required]
  [Display(Name = &lt;span style="color: #a31515;"&gt;"User name"&lt;/span&gt;)]
  [Remote(&lt;span style="color: #a31515;"&gt;"doesUserNameExist"&lt;/span&gt;, &lt;span style="color: #a31515;"&gt;"Account"&lt;/span&gt;, HttpMethod = &lt;span style="color: #a31515;"&gt;"POST"&lt;/span&gt;, ErrorMessage = &lt;span style="color: #a31515;"&gt;"User name already exists. Please enter a different user name."&lt;/span&gt;)]
  [ServerSideRemote(&lt;span style="color: #a31515;"&gt;"Account"&lt;/span&gt;, &lt;span style="color: #a31515;"&gt;"doesUserNameExistGet"&lt;/span&gt;)]
  &lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;string&lt;/span&gt; UserName { &lt;span style="color: blue;"&gt;get&lt;/span&gt;; &lt;span style="color: blue;"&gt;set&lt;/span&gt;; }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;blockquote&gt;
&lt;p&gt;You do not need to use &lt;a target="_blank" href="http://www.tugberkugurlu.com/archive/check-instantly-if-username-exists-asp-net-mvc-remote-validation" title="http://www.tugberkugurlu.com/archive/check-instantly-if-username-exists-asp-net-mvc-remote-validation"&gt;ASP.NET MVC Remote validation&lt;/a&gt; along with ServerSideRemote validation but in a real world scenario, we probably would use both.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;As you can see, there is not much to specify but of course, you get the &lt;span style="text-decoration: line-through;"&gt;options&lt;/span&gt; properties of &lt;a target="_blank" href="http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.validationattribute.aspx" title="http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.validationattribute.aspx"&gt;ValidationAttribute&lt;/a&gt; class along with it. there is not much to specify there because I have built it for like 30 minutes. So, spare me for that.&lt;/p&gt;
&lt;p&gt;When we look at our controller action which holds the validation logic, we will see that it is not much different that Remote validation&amp;rsquo;s:&lt;/p&gt;
&lt;div class="code-wrapper border-shadow-1"&gt;
&lt;div style="background-color: white; color: black;"&gt;
&lt;pre&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt; JsonResult doesUserNameExistGet(&lt;span style="color: blue;"&gt;string&lt;/span&gt; term) {

    &lt;span style="color: blue;"&gt;var&lt;/span&gt; user = Membership.GetUser(term);

    &lt;span style="color: blue;"&gt;return&lt;/span&gt; Json(user == &lt;span style="color: blue;"&gt;null&lt;/span&gt;, JsonRequestBehavior.AllowGet);
}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;When I fire up the register page, I am getting the following screen :&lt;/p&gt;
&lt;p&gt;&lt;a href="http://tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/de656a5ad88d_11FE4/image.png"&gt;&lt;img height="398" width="644" src="http://tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/de656a5ad88d_11FE4/image_thumb.png" alt="image" border="0" title="image" style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;I have excluded the JavaScript files for client side validation because we do not want to see client validation working. I already have a user registered whose user name is &lt;strong&gt;User1&lt;/strong&gt;. When I completed the fields, I am getting no warning:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/de656a5ad88d_11FE4/image_3.png"&gt;&lt;img height="441" width="644" src="http://tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/de656a5ad88d_11FE4/image_thumb_3.png" alt="image" border="0" title="image" style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Let&amp;rsquo;s push the &lt;em&gt;Register&lt;/em&gt; button :&lt;/p&gt;
&lt;p&gt;&lt;a href="http://tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/de656a5ad88d_11FE4/image_4.png"&gt;&lt;img height="441" width="644" src="http://tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/de656a5ad88d_11FE4/image_thumb_4.png" alt="image" border="0" title="image" style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Bingo! We got it working. Let&amp;rsquo;s try a legitimate one:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/de656a5ad88d_11FE4/image_5.png"&gt;&lt;img height="441" width="644" src="http://tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/de656a5ad88d_11FE4/image_thumb_5.png" alt="image" border="0" title="image" style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;That worked as well.&lt;/p&gt;
&lt;p&gt;I also put the sample project on &lt;a href="https://github.com/"&gt;GitHub&lt;/a&gt; so you can get the working sample code if you want :&lt;/p&gt;
&lt;p&gt;&lt;a target="_blank" href="https://github.com/tugberkugurlu/MvcRemoteValidationSample" title="https://github.com/tugberkugurlu/MvcRemoteValidationSample"&gt;https://github.com/tugberkugurlu/MvcRemoteValidationSample&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Also, you can find the whole &lt;a target="_blank" href="http://nuget.org/List/Packages/TugberkUg.MVC" title="http://nuget.org/List/Packages/TugberkUg.MVC"&gt;TugberkUg.MVC&lt;/a&gt; package&amp;rsquo;s code on &lt;a target="_blank" href="https://bitbucket.org" title="https://bitbucket.org"&gt;BitBucket&lt;/a&gt;:&lt;/p&gt;
&lt;p&gt;&lt;a target="_blank" href="https://bitbucket.org/tugberk/tugberkug.mvc/src" title="https://bitbucket.org/tugberk/tugberkug.mvc/src"&gt;https://bitbucket.org/tugberk/tugberkug.mvc/src&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/7KCxP_3ahIYYqQv6tzHz8mzEEsY/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/7KCxP_3ahIYYqQv6tzHz8mzEEsY/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/7KCxP_3ahIYYqQv6tzHz8mzEEsY/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/7KCxP_3ahIYYqQv6tzHz8mzEEsY/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/TugberkUgurlu/~4/bUlQ2BvAPPU" height="1" width="1"/&gt;</description><pubDate>Sun, 06 Nov 2011 17:10:00 Z</pubDate><a10:updated>2011-11-06T17:10:00Z</a10:updated><a10:content type="text">&lt;p&gt;&lt;a href="http://tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/de656a5ad88d_11FE4/go-to-server-and-come-back.png"&gt;&lt;img height="183" width="244" src="http://tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/de656a5ad88d_11FE4/go-to-server-and-come-back_thumb.png" align="left" alt="go-to-server-and-come-back" border="0" title="go-to-server-and-come-back" style="background-image: none; margin: 0px 15px 15px 0px; padding-left: 0px; padding-right: 0px; display: inline; float: left; padding-top: 0px; border-width: 0px;" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;For nearly 15 days, I have been poking around on the internet, books, etc. for anything related to &lt;a target="_blank" href="http://asp.net/mvc" title="http://asp.net/mvc"&gt;ASP.NET MVC&lt;/a&gt; validation. I have to say that validation features in ASP.NET MVC framework is really outstanding.&lt;/p&gt;
&lt;p&gt;I have blogged about &lt;a target="_blank" href="http://www.tugberkugurlu.com/archive/check-instantly-if-username-exists-asp-net-mvc-remote-validation" title="http://www.tugberkugurlu.com/archive/check-instantly-if-username-exists-asp-net-mvc-remote-validation"&gt;Remote Validation&lt;/a&gt; twice so far: &lt;a target="_blank" href="http://www.tugberkugurlu.com/archive/check-instantly-if-username-exists-asp-net-mvc-remote-validation" title="http://www.tugberkugurlu.com/archive/check-instantly-if-username-exists-asp-net-mvc-remote-validation"&gt;Check Instantly If Username Exists - ASP.NET MVC Remote Validation&lt;/a&gt; and &lt;a target="_blank" href="http://www.tugberkugurlu.com/archive/asp-net-mvc-remote-validation-for-multiple-fields-with-additionalfields-property" title="http://www.tugberkugurlu.com/archive/asp-net-mvc-remote-validation-for-multiple-fields-with-additionalfields-property"&gt;ASP.NET MVC Remote Validation For Multiple Fields With AdditionalFields Property&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Remote validation is one of the useful stuff which is baked into MVC framework. It is easy to implement this feature manually with &lt;a target="_blank" href="http://jquery.com/" title="http://jquery.com/"&gt;JQuery&lt;/a&gt; but it is kind of nice to have it out of the box. So, thanks a lot ASP.NET MVC team.&lt;/p&gt;
&lt;p&gt;One thing which ASP.NET MVC Remove Validation is missing is no support for server side validation of the chosen property. There is probably a good reason not to support it out of the box though.&lt;/p&gt;
&lt;p&gt;So, I decided to build one for myself. When I implemented it, I thought it worked fine and I should blog about it. So, I am writing this &lt;strong&gt;short &lt;/strong&gt;blog post. But, on the other hand I am also sure that no one should use this on production at least for now&lt;img src="http://tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/de656a5ad88d_11FE4/wlEmoticon-smile.png" alt="Smile" class="wlEmoticon wlEmoticon-smile" style="border-style: none;" /&gt; I don&amp;rsquo;t know why but it feels like it is not stable. Maybe you can use it if you see a positive comment from an MSFT person on this post.&lt;/p&gt;
&lt;p&gt;Here how it works:&lt;/p&gt;
&lt;p&gt;First of all, we need to use Nuget here to bring down a very small package called &lt;strong&gt;&lt;a target="_blank" href="http://nuget.org/List/Packages/TugberkUg.MVC" title="http://nuget.org/List/Packages/TugberkUg.MVC"&gt;TugberkUg.MVC&lt;/a&gt;&lt;/strong&gt; which will have the necessary stuff for server side remote validation to work.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/222f24832a52_124A3/image_4.png"&gt;&lt;img original="http://tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/222f24832a52_124A3/image_thumb_4.png" height="75" width="640" src="http://tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/222f24832a52_124A3/image_thumb_4.png" alt="image" border="0" title="image" style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border-width: 0px;" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;After that, here how we can use it:&lt;/p&gt;
&lt;div class="code-wrapper border-shadow-1"&gt;
&lt;div style="background-color: white; color: black;"&gt;
&lt;pre&gt;  [Required]
  [Display(Name = &lt;span style="color: #a31515;"&gt;"User name"&lt;/span&gt;)]
  [Remote(&lt;span style="color: #a31515;"&gt;"doesUserNameExist"&lt;/span&gt;, &lt;span style="color: #a31515;"&gt;"Account"&lt;/span&gt;, HttpMethod = &lt;span style="color: #a31515;"&gt;"POST"&lt;/span&gt;, ErrorMessage = &lt;span style="color: #a31515;"&gt;"User name already exists. Please enter a different user name."&lt;/span&gt;)]
  [ServerSideRemote(&lt;span style="color: #a31515;"&gt;"Account"&lt;/span&gt;, &lt;span style="color: #a31515;"&gt;"doesUserNameExistGet"&lt;/span&gt;)]
  &lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;string&lt;/span&gt; UserName { &lt;span style="color: blue;"&gt;get&lt;/span&gt;; &lt;span style="color: blue;"&gt;set&lt;/span&gt;; }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;blockquote&gt;
&lt;p&gt;You do not need to use &lt;a target="_blank" href="http://www.tugberkugurlu.com/archive/check-instantly-if-username-exists-asp-net-mvc-remote-validation" title="http://www.tugberkugurlu.com/archive/check-instantly-if-username-exists-asp-net-mvc-remote-validation"&gt;ASP.NET MVC Remote validation&lt;/a&gt; along with ServerSideRemote validation but in a real world scenario, we probably would use both.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;As you can see, there is not much to specify but of course, you get the &lt;span style="text-decoration: line-through;"&gt;options&lt;/span&gt; properties of &lt;a target="_blank" href="http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.validationattribute.aspx" title="http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.validationattribute.aspx"&gt;ValidationAttribute&lt;/a&gt; class along with it. there is not much to specify there because I have built it for like 30 minutes. So, spare me for that.&lt;/p&gt;
&lt;p&gt;When we look at our controller action which holds the validation logic, we will see that it is not much different that Remote validation&amp;rsquo;s:&lt;/p&gt;
&lt;div class="code-wrapper border-shadow-1"&gt;
&lt;div style="background-color: white; color: black;"&gt;
&lt;pre&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt; JsonResult doesUserNameExistGet(&lt;span style="color: blue;"&gt;string&lt;/span&gt; term) {

    &lt;span style="color: blue;"&gt;var&lt;/span&gt; user = Membership.GetUser(term);

    &lt;span style="color: blue;"&gt;return&lt;/span&gt; Json(user == &lt;span style="color: blue;"&gt;null&lt;/span&gt;, JsonRequestBehavior.AllowGet);
}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;When I fire up the register page, I am getting the following screen :&lt;/p&gt;
&lt;p&gt;&lt;a href="http://tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/de656a5ad88d_11FE4/image.png"&gt;&lt;img height="398" width="644" src="http://tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/de656a5ad88d_11FE4/image_thumb.png" alt="image" border="0" title="image" style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;I have excluded the JavaScript files for client side validation because we do not want to see client validation working. I already have a user registered whose user name is &lt;strong&gt;User1&lt;/strong&gt;. When I completed the fields, I am getting no warning:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/de656a5ad88d_11FE4/image_3.png"&gt;&lt;img height="441" width="644" src="http://tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/de656a5ad88d_11FE4/image_thumb_3.png" alt="image" border="0" title="image" style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Let&amp;rsquo;s push the &lt;em&gt;Register&lt;/em&gt; button :&lt;/p&gt;
&lt;p&gt;&lt;a href="http://tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/de656a5ad88d_11FE4/image_4.png"&gt;&lt;img height="441" width="644" src="http://tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/de656a5ad88d_11FE4/image_thumb_4.png" alt="image" border="0" title="image" style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Bingo! We got it working. Let&amp;rsquo;s try a legitimate one:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/de656a5ad88d_11FE4/image_5.png"&gt;&lt;img height="441" width="644" src="http://tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/de656a5ad88d_11FE4/image_thumb_5.png" alt="image" border="0" title="image" style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;That worked as well.&lt;/p&gt;
&lt;p&gt;I also put the sample project on &lt;a href="https://github.com/"&gt;GitHub&lt;/a&gt; so you can get the working sample code if you want :&lt;/p&gt;
&lt;p&gt;&lt;a target="_blank" href="https://github.com/tugberkugurlu/MvcRemoteValidationSample" title="https://github.com/tugberkugurlu/MvcRemoteValidationSample"&gt;https://github.com/tugberkugurlu/MvcRemoteValidationSample&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Also, you can find the whole &lt;a target="_blank" href="http://nuget.org/List/Packages/TugberkUg.MVC" title="http://nuget.org/List/Packages/TugberkUg.MVC"&gt;TugberkUg.MVC&lt;/a&gt; package&amp;rsquo;s code on &lt;a target="_blank" href="https://bitbucket.org" title="https://bitbucket.org"&gt;BitBucket&lt;/a&gt;:&lt;/p&gt;
&lt;p&gt;&lt;a target="_blank" href="https://bitbucket.org/tugberk/tugberkug.mvc/src" title="https://bitbucket.org/tugberk/tugberkug.mvc/src"&gt;https://bitbucket.org/tugberk/tugberkug.mvc/src&lt;/a&gt;&lt;/p&gt;</a10:content><feedburner:origLink>http://tugberkugurlu.com/69</feedburner:origLink></item><item><guid isPermaLink="false">http://tugberkugurlu.com/68</guid><link>http://feedproxy.google.com/~r/TugberkUgurlu/~3/Y1Ph4AWQf5s/68</link><title>Donut Hole Caching In ASP.NET MVC by Using Child Actions and OutputCacheAttribute</title><description>&lt;p&gt;&lt;a href="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/Donout_991E/homer-and-donut.jpg"&gt;&lt;img style="background-image: none; margin: 0px 0px 15px 15px; padding-left: 0px; padding-right: 0px; display: inline; float: right; padding-top: 0px; border: 0px;" title="homer-and-donut" border="0" alt="homer-and-donut" align="right" src="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/Donout_991E/homer-and-donut_thumb.jpg" width="174" height="244" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;One of the common issues of web applications is performance and serving the same content over and over again for hours, days, even months is certainly effecting the performance of our web applications. This is where &lt;strong&gt;server side caching &lt;/strong&gt;comes in handy. But sometimes the whole page we are scripting on the server side is going to be static for hours and sometimes some portion of it.&lt;/p&gt;
&lt;p&gt;The first one, caching the whole page, is sort of easy with almost any web development frameworks. The second one, caching a portion of your web page, is the tricky one.&lt;/p&gt;
&lt;p&gt;&lt;a title="http://haacked.com" href="http://haacked.com" target="_blank"&gt;Phil Haack&lt;/a&gt; blogged about &lt;a title="http://haacked.com/archive/2009/05/12/donut-hole-caching.aspx" href="http://haacked.com/archive/2009/05/12/donut-hole-caching.aspx" target="_blank"&gt;Donut Hole Caching in ASP.NET MVC&lt;/a&gt; a while back but it is a little outdated. In this quick blog post, I will try to show you the easiest way of implementing this feature with &lt;a title="http://asp.net/mvc" href="http://asp.net/mvc" target="_blank"&gt;ASP.NET MVC&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Use Cases&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;So, where might we need this Donut Hole Caching thing? For example, if you are listing categories on the side bar, you probably gathering those categories from a database. Add the fact that you render this part on every single page of your web application, it is a waste of time if your category list is not so dynamic. So, you may want to cache the category list part of the page so that you don&amp;rsquo;t go to your database every single time.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;How to Do&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;In ASP.NET MVC framework, we can cache the whole controller action with &lt;a title="http://msdn.microsoft.com/en-us/library/system.web.mvc.outputcacheattribute.aspx" href="http://msdn.microsoft.com/en-us/library/system.web.mvc.outputcacheattribute.aspx" target="_blank"&gt;OutputCacheAttribute&lt;/a&gt;&amp;nbsp;&lt;em&gt;which represents an attribute that is used to mark an action method whose output will be cached&lt;/em&gt; according to &lt;a title="http://msdn.microsoft.com" href="http://msdn.microsoft.com" target="_blank"&gt;MSDN&lt;/a&gt;. This is perfect but wait a second. We do not want to cache the entire action, we just want to cache the portion of it. This is where &lt;a title="http://msdn.microsoft.com/en-us/library/system.web.mvc.html.childactionextensions.action(v=VS.98).aspx" href="http://msdn.microsoft.com/en-us/library/system.web.mvc.html.childactionextensions.action(v=VS.98).aspx" target="_blank"&gt;ChildActionExtensions.Action&lt;/a&gt; method, which &lt;em&gt;invokes a child action method and returns the result as an HTML string&lt;/em&gt;, plays a huge role. Let me show you how these parts of the framework play nice together.&lt;/p&gt;
&lt;p&gt;Have a look at the below controller action :&lt;/p&gt;
&lt;div class="code-wrapper border-shadow-1"&gt;
&lt;div style="background-color: white; color: black;"&gt;
&lt;pre&gt;[ChildActionOnly]
[OutputCache(Duration=60)]
&lt;span style="color: blue;"&gt;public&lt;/span&gt; ActionResult sampleChildAction() {

    &lt;span style="color: green;"&gt;//Put the thread at sleep for 3 seconds to see the difference.&lt;/span&gt;
    System.Threading.Thread.Sleep(3000);

    &lt;span style="color: green;"&gt;//Also pass the date time from here just to see that it is comming from here.&lt;/span&gt;
    ViewBag.DateTime = DateTime.Now.ToString(&lt;span style="color: #a31515;"&gt;"dd.MM.yyyy HH:mm.ss"&lt;/span&gt;);

    &lt;span style="color: blue;"&gt;return&lt;/span&gt; View();
}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;A simple controller action method which returns ActionResult, nothing fancy going on except for &lt;a title="http://msdn.microsoft.com/en-us/library/system.web.mvc.childactiononlyattribute.aspx" href="http://msdn.microsoft.com/en-us/library/system.web.mvc.childactiononlyattribute.aspx" target="_blank"&gt;ChildActionOnlyAttribute&lt;/a&gt; which &lt;em&gt;represents an attribute that is used to indicate that an action method should be called only as a child action. &lt;/em&gt;Let&amp;rsquo;s look at the &lt;strong&gt;sampleChildAction &lt;/strong&gt;view and I will try to explain ChildActionOnlyAttribute function after that.&lt;/p&gt;
&lt;div class="code-wrapper border-shadow-1"&gt;
&lt;div style="background-color: white; color: black;"&gt;
&lt;pre&gt;@{
    Layout = null;
}

&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;p&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;
    This portion of the web page was scripted on &lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;strong&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;@ViewBag.DateTime&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515;"&gt;strong&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt; and I will be cached for &lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;strong&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;60&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515;"&gt;strong&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt; seconds.
&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515;"&gt;p&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;This html will be a part of our web page which will be cached. It doesn&amp;rsquo;t mean anything by itself but we have created an action for this view which means that we can call this page directly from a browser. &lt;strong&gt;ChildActionOnlyAttribute&lt;/strong&gt; exactly prevent users to call this kind of actions. You do not need to implement this attribute there but it is nice to know that it is there for us.&lt;/p&gt;
&lt;p&gt;The controller action which will render the whole page is so simple as below and doesn&amp;rsquo;t require any special thing for us to implement in order caching to work.&lt;/p&gt;
&lt;div class="code-wrapper border-shadow-1"&gt;
&lt;div style="background-color: white; color: black;"&gt;
&lt;pre&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt; ActionResult Index() {

    &lt;span style="color: blue;"&gt;return&lt;/span&gt; View();
}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Let&amp;rsquo;s also look at the view implementation :&lt;/p&gt;
&lt;div class="code-wrapper border-shadow-1"&gt;
&lt;div style="background-color: white; color: black;"&gt;
&lt;pre&gt;@{
    ViewBag.Title = "Donut Hole Caching Sample";
}

&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;h2&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;Donut Hole Caching Sample&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515;"&gt;h2&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;

&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;h3&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;Cached&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515;"&gt;h3&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;

&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;div&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;
    @Html.Action("sampleChildAction", 
      new { controller = "Sample", Area = "DonutHoleCaching" }
    )
&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515;"&gt;div&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;

&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;h3&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;Normal&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515;"&gt;h3&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;

&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;div&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;
    &lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;p&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;
        This portion of the web page was scripted on &lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;strong&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;@DateTime.Now.ToString("dd.MM.yyy HH:mm.ss")&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515;"&gt;strong&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;
    &lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515;"&gt;p&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;
&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515;"&gt;div&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;What we are doing here is that rather than putting the part, which we would like to cache, directly here, we are calling it as child action. So, the framework will treat the child action as it does for normal action methods.&lt;/p&gt;
&lt;p&gt;When we first call it, we will see something like below :&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/Donout_991E/image.png"&gt;&lt;img style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" title="image" border="0" alt="image" src="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/Donout_991E/image_thumb.png" width="644" height="419" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;While I was calling this page, I was on hold for 3 seconds because we have put the thread at sleep for 3 seconds on our child action method to feel the difference as you can see on above code in order.&lt;/p&gt;
&lt;p&gt;When I make the second call, I got something like this and I wasn&amp;rsquo;t on hold for 3 seconds :&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/Donout_991E/image_3.png"&gt;&lt;img style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" title="image" border="0" alt="image" src="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/Donout_991E/image_thumb_3.png" width="644" height="419" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Did you notice the time difference? This proves that if the cache is valid, our child action method won&amp;rsquo;t be rendered again. It will serve from the cache. Awesome, ha?&lt;/p&gt;
&lt;p&gt;I decided to create an ASP.NET MVC project called &lt;a title="http://mvcmiracleworker.tugberkugurlu.com" href="http://mvcmiracleworker.tugberkugurlu.com" target="_blank"&gt;MvcMiracleWorker&lt;/a&gt; for this kind of small samples. You can find the complete source code from &lt;a title="https://github.com" href="https://github.com" target="_blank"&gt;GitHub&lt;/a&gt; : &lt;a title="http://mvcmiracleworker.tugberkugurlu.com" href="http://mvcmiracleworker.tugberkugurlu.com"&gt;&lt;a href="https://github.com/tugberkugurlu/MvcMiracleWorker"&gt;https://github.com/tugberkugurlu/MvcMiracleWorker&lt;/a&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Behave well, use ASP.NET MVC &lt;img style="border-style: none;" class="wlEmoticon wlEmoticon-winkingsmile" alt="Winking smile" src="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/Donout_991E/wlEmoticon-winkingsmile.png" /&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/ly7o9P6CuRnUbv56mBtEkCqTP4k/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/ly7o9P6CuRnUbv56mBtEkCqTP4k/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/ly7o9P6CuRnUbv56mBtEkCqTP4k/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/ly7o9P6CuRnUbv56mBtEkCqTP4k/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/TugberkUgurlu/~4/Y1Ph4AWQf5s" height="1" width="1"/&gt;</description><pubDate>Sun, 30 Oct 2011 09:40:00 Z</pubDate><a10:updated>2011-10-30T09:40:00Z</a10:updated><a10:content type="text">&lt;p&gt;&lt;a href="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/Donout_991E/homer-and-donut.jpg"&gt;&lt;img style="background-image: none; margin: 0px 0px 15px 15px; padding-left: 0px; padding-right: 0px; display: inline; float: right; padding-top: 0px; border: 0px;" title="homer-and-donut" border="0" alt="homer-and-donut" align="right" src="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/Donout_991E/homer-and-donut_thumb.jpg" width="174" height="244" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;One of the common issues of web applications is performance and serving the same content over and over again for hours, days, even months is certainly effecting the performance of our web applications. This is where &lt;strong&gt;server side caching &lt;/strong&gt;comes in handy. But sometimes the whole page we are scripting on the server side is going to be static for hours and sometimes some portion of it.&lt;/p&gt;
&lt;p&gt;The first one, caching the whole page, is sort of easy with almost any web development frameworks. The second one, caching a portion of your web page, is the tricky one.&lt;/p&gt;
&lt;p&gt;&lt;a title="http://haacked.com" href="http://haacked.com" target="_blank"&gt;Phil Haack&lt;/a&gt; blogged about &lt;a title="http://haacked.com/archive/2009/05/12/donut-hole-caching.aspx" href="http://haacked.com/archive/2009/05/12/donut-hole-caching.aspx" target="_blank"&gt;Donut Hole Caching in ASP.NET MVC&lt;/a&gt; a while back but it is a little outdated. In this quick blog post, I will try to show you the easiest way of implementing this feature with &lt;a title="http://asp.net/mvc" href="http://asp.net/mvc" target="_blank"&gt;ASP.NET MVC&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Use Cases&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;So, where might we need this Donut Hole Caching thing? For example, if you are listing categories on the side bar, you probably gathering those categories from a database. Add the fact that you render this part on every single page of your web application, it is a waste of time if your category list is not so dynamic. So, you may want to cache the category list part of the page so that you don&amp;rsquo;t go to your database every single time.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;How to Do&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;In ASP.NET MVC framework, we can cache the whole controller action with &lt;a title="http://msdn.microsoft.com/en-us/library/system.web.mvc.outputcacheattribute.aspx" href="http://msdn.microsoft.com/en-us/library/system.web.mvc.outputcacheattribute.aspx" target="_blank"&gt;OutputCacheAttribute&lt;/a&gt;&amp;nbsp;&lt;em&gt;which represents an attribute that is used to mark an action method whose output will be cached&lt;/em&gt; according to &lt;a title="http://msdn.microsoft.com" href="http://msdn.microsoft.com" target="_blank"&gt;MSDN&lt;/a&gt;. This is perfect but wait a second. We do not want to cache the entire action, we just want to cache the portion of it. This is where &lt;a title="http://msdn.microsoft.com/en-us/library/system.web.mvc.html.childactionextensions.action(v=VS.98).aspx" href="http://msdn.microsoft.com/en-us/library/system.web.mvc.html.childactionextensions.action(v=VS.98).aspx" target="_blank"&gt;ChildActionExtensions.Action&lt;/a&gt; method, which &lt;em&gt;invokes a child action method and returns the result as an HTML string&lt;/em&gt;, plays a huge role. Let me show you how these parts of the framework play nice together.&lt;/p&gt;
&lt;p&gt;Have a look at the below controller action :&lt;/p&gt;
&lt;div class="code-wrapper border-shadow-1"&gt;
&lt;div style="background-color: white; color: black;"&gt;
&lt;pre&gt;[ChildActionOnly]
[OutputCache(Duration=60)]
&lt;span style="color: blue;"&gt;public&lt;/span&gt; ActionResult sampleChildAction() {

    &lt;span style="color: green;"&gt;//Put the thread at sleep for 3 seconds to see the difference.&lt;/span&gt;
    System.Threading.Thread.Sleep(3000);

    &lt;span style="color: green;"&gt;//Also pass the date time from here just to see that it is comming from here.&lt;/span&gt;
    ViewBag.DateTime = DateTime.Now.ToString(&lt;span style="color: #a31515;"&gt;"dd.MM.yyyy HH:mm.ss"&lt;/span&gt;);

    &lt;span style="color: blue;"&gt;return&lt;/span&gt; View();
}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;A simple controller action method which returns ActionResult, nothing fancy going on except for &lt;a title="http://msdn.microsoft.com/en-us/library/system.web.mvc.childactiononlyattribute.aspx" href="http://msdn.microsoft.com/en-us/library/system.web.mvc.childactiononlyattribute.aspx" target="_blank"&gt;ChildActionOnlyAttribute&lt;/a&gt; which &lt;em&gt;represents an attribute that is used to indicate that an action method should be called only as a child action. &lt;/em&gt;Let&amp;rsquo;s look at the &lt;strong&gt;sampleChildAction &lt;/strong&gt;view and I will try to explain ChildActionOnlyAttribute function after that.&lt;/p&gt;
&lt;div class="code-wrapper border-shadow-1"&gt;
&lt;div style="background-color: white; color: black;"&gt;
&lt;pre&gt;@{
    Layout = null;
}

&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;p&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;
    This portion of the web page was scripted on &lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;strong&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;@ViewBag.DateTime&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515;"&gt;strong&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt; and I will be cached for &lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;strong&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;60&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515;"&gt;strong&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt; seconds.
&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515;"&gt;p&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;This html will be a part of our web page which will be cached. It doesn&amp;rsquo;t mean anything by itself but we have created an action for this view which means that we can call this page directly from a browser. &lt;strong&gt;ChildActionOnlyAttribute&lt;/strong&gt; exactly prevent users to call this kind of actions. You do not need to implement this attribute there but it is nice to know that it is there for us.&lt;/p&gt;
&lt;p&gt;The controller action which will render the whole page is so simple as below and doesn&amp;rsquo;t require any special thing for us to implement in order caching to work.&lt;/p&gt;
&lt;div class="code-wrapper border-shadow-1"&gt;
&lt;div style="background-color: white; color: black;"&gt;
&lt;pre&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt; ActionResult Index() {

    &lt;span style="color: blue;"&gt;return&lt;/span&gt; View();
}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Let&amp;rsquo;s also look at the view implementation :&lt;/p&gt;
&lt;div class="code-wrapper border-shadow-1"&gt;
&lt;div style="background-color: white; color: black;"&gt;
&lt;pre&gt;@{
    ViewBag.Title = "Donut Hole Caching Sample";
}

&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;h2&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;Donut Hole Caching Sample&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515;"&gt;h2&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;

&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;h3&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;Cached&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515;"&gt;h3&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;

&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;div&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;
    @Html.Action("sampleChildAction", 
      new { controller = "Sample", Area = "DonutHoleCaching" }
    )
&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515;"&gt;div&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;

&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;h3&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;Normal&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515;"&gt;h3&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;

&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;div&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;
    &lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;p&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;
        This portion of the web page was scripted on &lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;strong&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;@DateTime.Now.ToString("dd.MM.yyy HH:mm.ss")&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515;"&gt;strong&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;
    &lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515;"&gt;p&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;
&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515;"&gt;div&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;What we are doing here is that rather than putting the part, which we would like to cache, directly here, we are calling it as child action. So, the framework will treat the child action as it does for normal action methods.&lt;/p&gt;
&lt;p&gt;When we first call it, we will see something like below :&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/Donout_991E/image.png"&gt;&lt;img style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" title="image" border="0" alt="image" src="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/Donout_991E/image_thumb.png" width="644" height="419" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;While I was calling this page, I was on hold for 3 seconds because we have put the thread at sleep for 3 seconds on our child action method to feel the difference as you can see on above code in order.&lt;/p&gt;
&lt;p&gt;When I make the second call, I got something like this and I wasn&amp;rsquo;t on hold for 3 seconds :&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/Donout_991E/image_3.png"&gt;&lt;img style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" title="image" border="0" alt="image" src="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/Donout_991E/image_thumb_3.png" width="644" height="419" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Did you notice the time difference? This proves that if the cache is valid, our child action method won&amp;rsquo;t be rendered again. It will serve from the cache. Awesome, ha?&lt;/p&gt;
&lt;p&gt;I decided to create an ASP.NET MVC project called &lt;a title="http://mvcmiracleworker.tugberkugurlu.com" href="http://mvcmiracleworker.tugberkugurlu.com" target="_blank"&gt;MvcMiracleWorker&lt;/a&gt; for this kind of small samples. You can find the complete source code from &lt;a title="https://github.com" href="https://github.com" target="_blank"&gt;GitHub&lt;/a&gt; : &lt;a title="http://mvcmiracleworker.tugberkugurlu.com" href="http://mvcmiracleworker.tugberkugurlu.com"&gt;&lt;a href="https://github.com/tugberkugurlu/MvcMiracleWorker"&gt;https://github.com/tugberkugurlu/MvcMiracleWorker&lt;/a&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Behave well, use ASP.NET MVC &lt;img style="border-style: none;" class="wlEmoticon wlEmoticon-winkingsmile" alt="Winking smile" src="http://www.tugberkugurlu.com/Content/images/Uploadedbyauthors/wlw/Donout_991E/wlEmoticon-winkingsmile.png" /&gt;&lt;/p&gt;</a10:content><feedburner:origLink>http://tugberkugurlu.com/68</feedburner:origLink></item></channel></rss>

