Brief overview of Microsoft web technologies:
Before diving in, here is a timeline showing a quick history of Microsoft web technologies:
What is OWIN?
- Stands for: The Open Web Interface for .NET.
- It is a specification, not code.
- Microsoft built a project named KATANA, which follows OWIN specifications to allow some of Microsoft’s web features to reap the benefits of using OWIN.
- OWIN was inspired by Rack in the Ruby community.
- OWIN creates an abstraction between Web servers and framework components.
- It’s an open standard.
- Authored by 2 MS guys: Benjamin Vanderveen and Louis Dejardin.
The formal OWIN definition in owin.org website is very clear:
“OWIN defines a standard interface between .NET web servers and web applications. The goal of the OWIN interface is to decouple server and application, encourage the development of simple modules for .NET web development, and, by being an open standard, stimulate the open source ecosystem of .NET web development tools.”
Why the need for OWIN?
Before we go too deeply into what OWIN and KATANA are and how to use them, we will discuss what led Microsoft to developing them.
There were challenges with ASP.NET Web Forms:
- Different features in one big DLL: System.Web.dll assembly (for example, the core HTTP objects with the Web forms framework).
- ASP.NET was included as a part of the larger .NET Framework, which meant that the time between releases was longer.
- System.Web.dll itself was coupled in a few different ways to a specific Web hosting option: Internet Information Services (IIS).
These challenges alongside the rapid development in web industry encouraged Microsoft to take evolutionary steps to enhance its web technology platform.
- Two evolutionary steps: ASP.NET MVC and ASP.NET Web API. Web applications were increasingly being developed as a series of small, focused components rather than large frameworks.
- ASP.NET MVC was released as an independent download. This gave the engineering team the flexibility to deliver updates much more frequently than had previously been possible.
- ASP.NET WebAPI Another major shift in Web application development due to the shift from dynamic, server-generated to static initial markup using AJAX .
– Nugget frequently updates.
– No dependencies on System. Web can be hosted outside IIS.
So why OWIN?
What was needed was a single hosting abstraction that would enable a developer to compose an application from a variety of different components and frameworks, and then run that application on a supporting host.
Why not host on IIS only?
- Use small, lightweight web server
- Avoid IIS performance overhead
- IIS might not be available…
What is KATANA?
Katana is Microsoft’s implementation of OWIN rules.
So OWIN itself does not have any tools, libraries or anything else. It is just a specification. While both the OWIN specification and Owin.dll are community owned and community run open source efforts, the Katana project represents the set of OWIN components that, while still open source, are built and released by Microsoft.
These components include both infrastructure components, such as hosts and servers, as well as functional components, such as authentication components and bindings to frameworks such as SignalR and ASP.NET Web API.
To create an abstraction between Web servers and framework components
Understanding the main OWIN Specifications parts:
Specifications on: http://owin.org
Specifications main parts:
- Definition of Software Actors
- Request execution
- Application Delegate
- Request data, Response data, Other data
- Application Startup
- URI and Paths
- Error Handling
So, any code that follows the OWIN specification will benefit from many advantages. For example, this code will be modular and plug-able in the way that you can easily use it multiple times across many projects and within different hosts.
Definition of Software Actors:
Host: The process that hosts the other parts( IIS , Windows service, Console application). It is responsible for starting every thing up.
Server: Responsible for binding to a TCP port, constructing the environment dictionary and processing requests through an OWIN pipeline. In IIS: Host == Server
Middleware: This is just a piece of code that the request passes on the way to and from the application. They can be used to inspect & modify both the incoming requests & outgoing responses.
Pipeline of middleware: As the server passes the request to the app using AppFunc, it sends it through a pipeline of middlewares.
Application: This is the part responsible for generating the actual response.
Technically, there is no difference between application and middleware except for the fact that the application short circuits the pipeline by generating a response, whereas the middleware alters the pass, the request or response.
Web Framework: This is not part of the actors, but is a set of middleware that plugs into the pipeline and exposes their own set of APIs for developers to interact with.
Application Delegate (AppFunc):
- The primary interface in OWIN is called the application delegate or AppFunc.
- An application delegate takes the IDictionary<string, object> environment and returns a Task when it has finished processing.
- The application MUST eventually complete the returned Task, or throw an exception.
- Returning Task tell server to non block any process waiting request to finish.
- The Environment dictionary stores information about the request, the response, and any relevant server state.
- Server is responsible for initiating it, but the application is responsible for response data and response body.
- The environment dictionary MUST be non-null, mutable and MUST contain the keys listed as required in the specifications.
Request Data, Response Data, Other Data:
Here are some OWIN/KATANA training slides, which may be helpful.
- Creating an .NET WebApp project that uses OWIN/KATANA implementation and can run using IIS:
- Install-package microsoft.owin.host.systemweb so we can host it using IIS
- Create a Startup class:
- Write a simple middleware that writes a simple response as shown below and tests it:
2. Creating a self hosted WebApp and OWIN component:
- Install those two NuGet packages so you can run a simple server using the console application:
- install-package microsoft.owin.hosting
- install-package microsoft.owin.host.HttpListener
- Inside your console application main method:
- Create a new OWIN component:
- Create a new startup class and use the component inside it:
- It’s good practice to create an extension method for your component:
- And use it inside your startup class as shown:
- By running the app and browsing this URL: http://localhost:8080 we will get this result:
That’s all for now.