I have been trying to learn these concepts from a long time. Read many good articles, their definitions, but could not make much out of them. Finally, I decided to write my understanding that I have got from these resources. So in this article, I will share my learning about what are
Katana and will also try to get into some basic details that makes them important concept to be learnt. So let’s start with their basic definitions first.
What is O.W.I.N. ?
O.W.I.N. stands for Open Web Interface for .Net. Note that this is just a specification and not any technology or framework. It’s official definition is quite simple and easy to understand. It says:
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.
Let’s try to understand what this definition is trying to convey here. Till now the developers mainly focused on decoupled application code by creating different layers in the applications, which interact with the use of interfaces, use design patterns, S.O.L.I.D. principles etc. But O.W.I.N. is a step above the code decoupling. It is aimed at decoupling the application and the web-server, that hosts the application. Using such kind of decoupled architecture allows:
1. Creating middleware components which can be replace/added in the application without affecting the other components in the application
2. Remove the dependency of web server to host a component, by promoting the use of self-hosting.
We will discuss about these points, later in the discussion.
What is Katana ?
Next the question comes what is
Katana and how it is related to the
OWIN ? The answer is that if
OWIN is a specification (and not any technology or framework), than
Katana is an open source project by Microsoft based on the
OWIN. One such example is the asp.net
webapi, based on the
OWIN specifications, supports the concept of self-hosting for hosting, by using the
OWIN custom hosts(we will see the custom host concept in further discussion) and eliminating the dependency on IIS for hosting.
So to conclude this part of the discussion,
OWIN is a specification and
Katana is Microsoft’s open source project which uses these specifications.
So moving on, let’s discuss in details about
OWIN. But before that, a very important point to be mentioned, which will make further understanding easier. For this, I would like to quote a very good explanation about what
OWIN is, from the link here. It says:
OWIN introduction allowed any OWIN-compatible application to talk through OWIN to a web server that had an OWIN-compatible hosting layer. Microsoft wrote Katana as one OWIN implementation that could host ASP.NET Web API, ASP.NET SignalR, and many 3rd party frameworks on top of several servers, including IIS (and IIS Express), Katana's self-host server, and custom hosts (i.e. run Katana's host in a custom app).
This means that Microsoft’s Katana project allows the use of the
OWIN specifications by providing not only new servers like Katana’s self host server, custom host server (like any windows service or console application) but also with existing web servers like IIS & IIS express.
In any normal asp.net application architecture, we have different layers categorized as host, server and the main application. In such a case,
IIS acts as the server as well as the host. But in case of any
OWIN based structure, we can have 4 different layers. Three of these layers are web server,
OWIN compatible applications like
OWIN compatible hosting layer. The 4th one is our main application which could be any application based on web-forms or mvc framework. So the structure defined above is layered as:
Let’s discuss these layers in detail now.
The Host layer: In any
OWIN specifications based application, this layer can consist of either of the following 3 acting as the host:
- IIS : This includes the use of IIS as the host for any application. In such as case,
Microsoft.Owin.Host.SystemWebis to be used as the Server. This will allow to add any
OWINcompatible component or middleware to be easily added/remove from the pipeline.
- Custom Host: This involves creating a windows service or a console application and using it to host your
OWINcompatible applications like
Webapi.An example is self-hosting the
windows application.See an example here.
- OwinHost.exe: This is an executable file named
OWIN.exe.It can be directly run and used as a host for any application.
The Server Layer: Next we have the server layer, which will listen to any incoming requests and manage the requests in pipeline. This layer can be based on:
System.Webis used when we are using IIS as the host. In that case, IIS also acts as the server. So in order to easily plug the OWIN components in the pipeline, we can use this server layer. A definition from one of the reference links defines its role as:
The Katana SystemWeb host registers an ASP.NET HttpModule and HttpHandler to intercept requests as they flow through the HTTP pipeline and send them through the user-specified OWIN pipeline.
- Microsoft.Owin.Host.HttpListener: This involves the use of .Net frameworks’
HttpListenerclass to open a port and manage the request pipeline.
Using these two layers, suppose we add a new empty application project and try to create an application based on this structure. In this case we have two different options:
- By using
Microsoft.Owin.Host.SystemWebas the server implementation, we make use of
IISas the host as well as the server.
- By using
Microsoft.Owin.Host.HttpListeneras the server implementation, we can use any custom host like windows or console application as the host or even the
OWIN.exeexecutable as the host.
Middleware Components: These are the
OWIN compatible components like
WebAPI and even static html pages
. These are added to the system as plug and play components and can be easily added/removed without affecting the other modules. When the server receives any request from the client, it is passed through these components.
Applications: This could be any of your ASP.Net, mvc or any other applications.
Now let’s try to create a sample using the above concepts. So we will use
1. Custom host in the form of a Console application.
2. Microsoft.Owin.Host.HttpListener for the server implementation.
3. WebAPI and an HTML page as the middleware components in the system. An option could have been using any mvc or asp.net page as separate application in place of the html page. Then it would have acted as the 4th layer of the system i.e. Main Application. Another important point is, using any asp.net or mvc would not have been a good option as this would than not be hosted through custom host or used the
Microsoft.Owin.Host.HttpListener server implementation. This is the reason i have highlighted it to be used as a separate application.
Start by adding a new console application. This will be the first layer or the custom hosting the system, instead of IIS. Add the references to the
Microsoft.Owin.Host.HttpListener by using the nuget package manager. This would then meet the requirement of a server implementation.
So we have the host and server in place. Next, we need to add the middleware component webapi. So we add reference to the
OWIN package, using the nuget package manager and
OWIN hosting libraries.
Next, we add another middleware component. This time, its libraries to add support for hosting html pages. Yes that’s correct. We will host html pages in a console application. So let’s see how we can do this.
Now we have all the required references in the project and just need to configure the components to be used. Before that, we add a webapi controller and an html page into a folder named
Pages. So our complete solution structure will like the following:
So now let’s configure these components one by one. First, add a simple method in webapi controller, which returns the current date-time string. This will be called from the html which we added above.
So now you can host the html page in the console application as well. Further if you tomorrow if you need to ad the
SignalR functionality, simply add the references and configure it in the
WebAPIConfig.cs file like we did for the
webapi and the static files.
Further, if we need to provide the service api to any third party, simply host that in the console application or windows service. No dependency on the IIS.
Following are some of the reference links you may find useful:
1. Basic concept of Host, servers and middleware
2. Use IIS as host and SystemWeb as the server for OWIN implementation
4. Step by step configuring components in OWIN pipeline.
Hope you will find this article helpful and enjoyed reading it. Happy coding…!!!