Menu

OWIN Series: Part 2: Katana

In part two of this OWIN series we are going to look at katana and get
familiar with it.

What is Katana?

Remember, OWIN is simply a specification. If you need a refresher, then
please take a look at Part 1 in this series. Katana is the name given
to Microsoft’s implementation of OWIN. As with most things ASP.Net in
recent years, the Katana project is following the open-source or
source-opened standard of recent projects.

Show me “Hello World”!

We can talk about abstract concepts all day, but I prefer to dive in to
the code and get started. I wanted to start with the simplest example,
which of course is the “Hello World” project.

Create the project

Start by creating a new ASP.NET Web Application in Visual Studio.

Then choose the Empty template.

The Empty template allows you to start with a clean slate. It
creates a vanilla application where you can build it from the ground up.

Add NuGet packages

The next step is to add the required NuGet Packages. Right
click the web application and choose Manage Nuget Packages. *
Search for *Microsoft.Owin.Host.SystemWeb and click Install.

The Microsoft.Owin.Host.SystemWeb package is a piece of glue that
connects IIS to the OWIN pipeline. Under the hood, this assembly
contains an HttpModule called OwinHttpModule which is registered
dynamically. This HttpModule is responsible for starting the OWIN pipeline. OWIN bootstraps itself using the
PreApplicationStartMethod, so you don’t need to do anything else to
tell your application to use the OWIN pipeline. The code for this
bootstrapping can be found here.

At the time of writing, the current version of the Microsoft.Owin.Host.SystemWeb package is 3.0.0.

Run the application

At this point, the application will not work. But I think it’s
important to look at the error message as it helps to give a detailed
understanding of the OWIN pipeline. Press F5 to start debugging. You
should see the YSOD below:

The error message reads:

The following errors occurred while attempting to load the app.\
- No assembly found containing an OwinStartupAttribute.\
- No assembly found containing a Startup or [AssemblyName].Startup
class.\
To disable OWIN startup discovery, add the appSetting
owin:AutomaticAppStartup with a value of "false" in your web.config.\
To specify the OWIN startup Assembly, Class, or Method, add the
appSetting owin:AppStartup with the fully qualified startup class or
configuration method name in your web.config.

Looking at the error, it’s clear that the OWIN pipeline is looking for
an entry point into the application and it can’t find one. The error
message provides a number of ways of solving this problem and you can
explore these yourself. We are going to fix the error by following
convention and providing a class called “Startup”.

Create the Startup class

Right-click the root of the site and add a new class. Name the class
Startup. If you were to run the application now, you would get a very
similar YSOD as above, but it is slightly different. The message is:

The following errors occurred while attempting to load the app.\
- No assembly found containing an OwinStartupAttribute.\
- No 'Configuration' method was found in class 'HelloWorld.Startup,
HelloWorld, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.\
To disable OWIN startup discovery, add the appSetting
owin:AutomaticAppStartup with a value of "false" in your web.config.\
To specify the OWIN startup Assembly, Class, or Method, add the
appSetting owin:AppStartup with the fully qualified startup class or
configuration method name in your web.config.

You can see that it has found our startup class and is now looking for a
public method called “Configuration”. We could carry on like this
reading the error messages, but to save time I’m going to show you the
most basic requirement of the startup class.

We now have an application with a single piece of middleware in the
pipeline. All requests that come in to the server will go through the
OWIN pipeline. They will reach our middleware above and will be
returned. The request will not get passed on because there are no more
middlewares configured in our application.