Tuesday, June 10, 2014

Introduction to Open Web Interface for .NET (OWIN)

In the modern world of ASP.NET, these two : OWIN and Project Katana are among most popular topics which are being discussed among the community. So today, in this post I am going to write about the Open Web Interface for .NET(OWIN).

In simple OWIN is a not a framework, it’s not an API, it simply is a community-owned specification. I am repeating, do this keep in mind that OWIN is not an any kind of implementation. This specification is built on two core design goals.
  1. New components could be more easily developed and consumed (Decoupling of layers).
  2. Applications could be more easily ported between hosts and potentially entire platforms/operating systems.
By decoupling the web server from the application, OWIN makes it easier to create middleware for .NET web development. Also, OWIN makes it easier to port web applications to other hosts - for example, self-hosting in a Windows service or other process. Prior to OWIN, when you are building ASP.NET application, you are inheritably bound to IIS due to the heavy dependency on System.Web assembly. So with OWIN, does your ASP.NET application has to run on top of IIS? Not anymore, for an example you can host your ASP.NET application in a simple console application or you can run your ASP.NET application as a Windows Service.

OWIN defines a web application as a collection of  four layers, the host, the server, middleware and the actual application. When running ASP.NET MVC in IIS, IIS would be the first two parts, the host and the server, the middleware would be our IHttpModules, and the application would be our MVC application.

Ok, now we know a bit about this specification called OWIN. But how does it specify things. OWIN specification is consists of two core elements.
  1. Environment Dictionary ( IDictionary<string, object> )
  2. Application Delegate ( Func<IDictionary<string, object>, Task> )

Environment Dictionary

The environment dictionary represents the request and server state to the application. For an example As a request comes in, it breaks it down into pieces, and adds those pieces to an IDictionary<string,object>. OWIN-compatible Web server is responsible for populating the environment dictionary with data and it is then the responsibility of the application or framework components to populate or update the dictionary with additional values and write to the response body stream.

Application Delegate

The application delegate is simply an implementation of the Func delegate type where the function accepts the environment dictionary as input and returns a Task. The Task is used to signal when the HTTP application has finished processing the request.

So what is Project Katana. Let’s get to know what Katana is in my next post.

Happy Coding.