Owin com

Keywords: owin com
Description: This article demonstrates OWIN and how we can host a SignalR based application using Katana (an OWIN implementation).

Abstract: This article demonstrates OWIN and how we can host a SignalR based application using Katana (an OWIN implementation).

OWIN stands for Open Web Interface for .NET. Now if you are thinking. NET already has ASP.NET and MVC and Web API, how does this OWIN thing fit in? Is it a new tool/library/web framework, and then what is Katana?

Well OWIN is an open, industry standard or a specification and is primarily authored by two members of the ASP.NET Team - Benjamin Vanderveen and Louis Dejardin. You can compare OWIN with RACK in Ruby and WSGI from the Python world. For now let’s call it as a specification of interoperability between webhosts and frameworks, that outlines ‘some’ rules for building ‘certain’ types of .NET applications.

The Katana project by Microsoft picks up these rules and has created an implementation of the specification. So OWIN is like the blueprint whereas the Katana project uses the blueprint to create increasingly useful set of modules that adhere to the OWIN rules. Don’t worry about what those Modules do, we’ll see that soon, for now, imprint this much, OWIN is a blueprint and Katana in as implementation.

This article is published from the DNC .NET Magazine – A Free High Quality Digital Magazine for .NET professionals published once every two months. Subscribe to this eMagazine for Free

With the vague idea of what is OWIN and Katana, let’s go deeper and see what is the grand scheme of things in which OWIN and Katana fit in. After all what is the OWIN specification for?

We first take a step back and see how Web Applications work on ASP.NET today from a hosting point of view. Anyone webdev worth their salt, know that during development, you use either Cassini - the development web server, or IIS Express, the newer more robust in-process web server. But when we deploy our ASP.NET apps, it’s invariably on an IIS server. IIS is the be-all-end-all of ASP.NET Web Hosting at least from a production deployment stand point.

With IIS we know that we will get a set of extension points, but we will get things like Compression, Authentication, Static content serving etc. for free. All this making IIS one giant black box into which we immerse our ASP.NET application. We can visualize it as follows.

This looks like a pretty picture till we start thinking of using our application in scenarios without IIS. Do such scenarios exist? Well, recently we reviewed the Sitefinity CMS application and their demo was making innovative use of Cassini, they could have very well used an alternative if available. Think of application stacks like Pentaho and Jasper Reports in the Java world. They don’t depend on a preconfigured web server to work, they come bundled with a Web Server which could be Apache or could be something smaller and lighter.

Katana as we briefly mentioned above is a suite being built, that will have its own OWIN compliant implementations of the Host, Server and Middleware layers. The difference (from IIS) being it will be far more granular giving you way more control on how to structure your application. The final goal being not to replace IIS but provide a suite of functionality that can be ‘assembled’ to comprise of only the pieces that you need for your application.

Thus the above diagram can also be considered as the ‘architecture’ for Katana. Now let’s look at each of these components in details.

a. IIS/ASP.NET: Via HttpModule and HttpHandler implementations, Katana manages to implement the OWIN pipeline requirements and execute OWIN compliant modules. This is managed by installing the Microsoft.AspNet.Host.SystemWeb Nuget package in any Web Application that you may be working on. There is one caveat though, IIS acts as both the Host and the Server, so when you use the Nuget package, you have to run with this ‘limitation’ and cannot swap in another Server implementation

b. Custom Host: This is a closer to the metal option that allows users to use a Console application, Windows Process or even a WinForms application to be the host. We will look at this option in a lot more details.

c. OwinHost.exe: This is another ‘implementation’ coming out of the Katana suite. This provides a little more support in terms of bootstrapping but providing a Console Host that starts an Http Server on its own so that you can focus on configuring the middleware and building the final application.

With the hosting sorted out, let’s move to the next layer that Katana has in store, which is the Server layer.

Often the distinction of Host and Server is merged and the fact that the OWIN spec recognizes these two as separate entities, speaks to the granularity and pluggability OWIN is aiming to achieve. As a base implementation, the Katana project provides two Server implementation.

a. Microsoft.Owin.Host.SystemWeb: As mentioned above, the IIS/ASP.NET host module serves as both the Host and Server implementation under IIS/ASP.NET. As a result it’s quoted in both the categories.

b. Microsoft.Owin.Host.HttpListener: This is a more barebones implementation using the .NET Frameworks’ HttpListener class. It contains the plumbing required to open ports and channel requests to the requests in the OWIN pipeline/middleware. We will use this in our sample below.

As depicted in the diagram above, the pipeline of OWIN modules basically channel a request from the application to the server. This pipeline approach greatly modularizes the overall solution giving the end user ability to pick and choose components required. As we will see shortly, if we are building a static HTML site, then we need not include SignalR, WebAPI or Authentication. On the other hand, if we are building an MVC application, we can use framework like Fubu MVC or Nancy and ignore Web API as well as Microsoft’s MVC stack completely. Essentially, flexibility of having an HttpHost outside IIS opens up a plethora of permutations and combinations that are today possible only if you agree to bring along the overhead of a full blown Server like IIS.

At a very basic level, an OWIN Middleware Component must implement a Func that takes in an IDictionary or objects and returns a Task.

Note the class name Startup and the method Configuration and its signature. This forms a convention for Katana Hosts. Each middleware tends to implement its own set of Extension methods for Use however for a Host and Server agnostic implementation, the above method signature must be implemented correctly.

Once all required middleware has been registered, we can then build Applications on top of this stack. In the next section, we will see how we can build an application using Katana OWIN components.

SignalR’s self-host story is built on Katana. The SignalR team went ahead and built a Server and the Middleware for themselves. Today we’ll build a Self-Hosted Console app using Katana components that not only hosts SignalR server, but also static HTML pages. In the end, we’ll have a crude chat application that uses Signal, HTML and JavaScript only. No WebForms or ASP.NET MVC involved.

For the first part, we pretty much follow the instructions on SignalR’s Wiki. however as we will see, we go beyond the barebones instructions.

1. First up we get Owin Hosting: This has the WebApplication class that provides us the bootstrapping necessary to do ‘self-hosting’.

Next we send the url and the Type Startup to the WebApplication class’ Start method. This starts off the Self host Server. If your Visual Studio is not running in Administrator mode, you may get an exception here because of lack of port opening permissions. Just start Visual Studio in Administrator mode.

Note: Startup is a naming convention that OwinHosts follow. We’ll see the Startup class shortly.

Photogallery Owin com:

Using Claims in your Web App is Easier with the new OWIN Security ...

Introduccin a OWIN y Project Katana - Twisting code

Designing Evolvable Web APIs with ASP.NET

Securing a Web API with Windows Azure AD and Katana | CloudIdentity

asp.net web api - OWIN/OAuth2 3rd party login: Authentication from ...

asp.net web api - OWIN/OAuth2 3rd party login: Authentication from ...

Understanding Security Features in the SPA Template for VS2013 RC ...

WS-Federation in Microsoft OWIN Componentsa quick start ...

Using Claims in your Web App is Easier with the new OWIN Security ...

Dan Wahlin - Adding Azure Active Directory and OWIN Code into an ...

Protecting a Self-Hosted API with Microsoft.Owin.Security ...

Living and breathing the world of Microsoft: Setting up OWIN for ...

Living and breathing the world of Microsoft: Setting up OWIN for ...

Byte Rot: OWIN and Katana challenges: blues of a library developer

Appverse Web# MVC - My ASP.NET Application

WS-Federation in Microsoft OWIN Componentsa quick start ...

Un informtico en el lado del mal: OWIN y Katana: Cmo crear apps ...

Dinosaurs and Spaceships: Using self hosted WebAPI 2 with legacy ...