Getting started with OWIN

Posted by on Sep 29, 2013 in Asp.net

OWIN

If you’re a developer in the web space, especially focusing or involved with the Microsoft stack, you’ve no doubt heard a lot about OWIN lately. OWIN, or Open Web Interface for .NET is an open-source initiative somewhat inspired by the Rack project on the Ruby platform and the increasingly popular Node.js movement. Like Rack, OWIN sets out to provide a flexible, lightweight middleware between web servers and the underlying framework components. OWIN also delivers on its promise of portability and modularity meaning applications written on top of OWIN are server and potentially even OS agnostic.

If, like me, you’ve created a simple web server using Node.js in a dozen lines of code, then self-hosted via a command line, you’ve no doubt been slightly envious as the elegance and autonomy of that solution compared to the tightly coupled MS/IIS/asp.net stack. However, you also soon probably found that a lot of the goodness asp.net gives you ‘out of the box’ goes a long way, and with OWIN, developers can potentially have the best of both worlds.

Katana

If you’re a Microsoft web developer then for every OWIN you’ve probably heard two Katana’s. Katana is Microsofts (also open-source) implementation of the OWIN specification. It can be found on Codeplex and is unsurprisingly geared to provision for the Microsoft hosts and frameworks.

An Example

Lets put this into an example.
Using Visual Studio, click File, New Project and select Console App

Creating a new console application in Visual Studio 2012

Once that’s completed, we need to grab a few packages from NuGet. From the package manager console (or via the GUI if you’d prefer), obtain the following packages:

install-package Microsoft.Owin.Hosting –IncludePreRelease
install-package Microsoft.Owin.Host.HttpListener –IncludePreRelease

Once the process completes you’ll notice a few dependencies have been imported for you via the magic of NuGet dependency management:

OWIN assembly references

Now your app is good to go. It’s quite basic:

The static WebApp class has a generic Start method which takes in a URL on which to listen and a TStartup parameter which is a class that defines our configuration for the application. As per the above, create a new class inside the project called Startup.cs as follows:

Everything should compile just fine – hit F5 to start the app. A console window should appear telling you the app has started. Next, just open your browser and go to the Uri specified in the app (in this instance http://localhost:8001). You should see the message relayed from OWIN
Hello world OWIN sample output

To give you an idea of the middleware capabilities of OWIN, stop the application and replace the startup.cs with the following:

Here we are calling an extension method UseWelcomePage().

Now, if you F5 to run the app again and refresh your browser you should see a much more pleasing page rendered:

OWIN welcome page

Although the above has barely scratched the surface of the capabilities of OWIN, I’m sure you can agree that its inherently powerful to be able to host web services out of processes such as the above and it all feels very much like a marriage between the power and functionality of asp.net and the autonomy and rawness of Node.js.

Some useful links:

Official OWIN website
Node.js
Katana
OWIN Nuget package
Rack