Katana & Owin – Part I

Katana is a flexible set of components for building and hosting OWIN-based web applications.
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 create the ‘clasic’ Hello World! application for Katana.
Start Visual Studio 2012 then create a new Console Application.
Ctrl + Q -> Package Manager Console


for diagnostics

The source code is:

If you want to have a nice welcome screen instead of “Hello world !” just add below line of code and ignore the rest of the code from Configuration method.

Under the hood

Owin describes one main component which is the following interface:

This is a function that accepts a simple dictionary of objects, keyed by a string identifier. The function itself returns a task. The object in the dictionary in this instance will vary depending on what the key is referring to.

More often, you will see it referenced like this:

An actual implementation might look like this:

This is essentially how the “environment” or information about the HTTP context is passed around. Looking at the environment argument of this method, you could interrogate it as follows:

if a HttpRequest was being made to ‘http://localhost:8080/Content/Main.css” then the output would be:

Your Path is [/Content/Main.css]

In addition, while not part of the spec, the IAppBuilder interface is also core to the functioning of an Owin module (or ‘middleware’ in Owin speak):

The IAppBuilder interface acts as the ‘glue’ or host to bring any registered Owin compatible libraries/modules together. With support of this simple mechanism, you can now write isolated components that deal with specific parts of functionality related to Http requests. You can then chain them together to build capabilities of my Http server. You can literally chain together Owin components to form a pipeline of only the necessary features you want.
Basically you can build or use a custom host, then you can insert whatever custom modules into the Http request processing pipeline. Owin provides the specification for writing those modules that make it easy to insert into the chain.

Sample Katana component

Owin components are also known as middleware.

Wrap component in extension method and invoke it like this which resembles the Welcome page invocation.

See owin for full owin specifications.

Chaining components