Getting Started With OWIN, Katana, and VS2013

Tuesday, July 9, 2013

If you start almost any new ASP.NET project in the new preview of Visual Studio 2013, you’ll find a reference to the Owin package inside. OWIN (the Open Web Interface for .NET) is a specification designed to decouple web servers from the frameworks and applications they host. The OWIN goal is to provide a lightweight, modular, and portable platform for mixing and matching components, frameworks, and servers.

Katana is Microsoft’s implementation of OWIN components. The code is available on CodePlex

In a future post we can talk about how OWIN compares to System.Web, but first let’s get a simple example up and running from scratch.

In VS2013 you can start a new console mode application, then run the following commands in the package manager console:

install-package Microsoft.Owin.Hosting -IncludePreRelease

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

install-package Microsoft.Owin.Diagnostics –IncludePreRelease

install-Package Owin.Extensions -IncludePrerelease

Inside the Main entry point for the console application, we can use the WebApp class to start an HTTP listener.

static void Main(string[] args)
{
    string uri = "http://localhost:8080/";

    using (WebApp.Start<Startup>(uri))
    {
        Console.WriteLine("Started");
        Console.ReadKey();
        Console.WriteLine("Stopping");
    }
}    

The Startup class used as the generic type parameter to WebApp.Start is a class we’ll have to implement, too.

public class Startup
{
    public void Configuration(IAppBuilder app)
    {
        app.UseWelcomePage();
    }
}

IAppBuilder is an interface we can use to compose the application for Katana to host. In this setup we’ll invoke the UseWelcomePage extension method provided by Microsoft.Owin.Diagnostics. Running the console program and pointing a browser to http://localhost:8080 produces:

katana up and running

Other extension methods allow for more fine grained control over the request and response processing. For example, UseHandlerAsync from Owin.Extensions allows for a more traditional “Hello, World” response to every request.

public class Startup
{
    public void Configuration(IAppBuilder app)
    {         
        app.UseHandlerAsync((req, res) =>
        {
            res.ContentType = "text/plain";
            return res.WriteAsync("Hello, World!");
        });
    }
}

So far this doesn’t appear much different than self-hosting WebAPI in a console application, but in some future posts we’ll dig a little deeper into some examples showing the loftier goals of OWIN and Katana.


Comments
gravatar friism Thursday, July 11, 2013
I saw this and was inspired to make it run on Heroku for live testing and prototyping: http://friism.com/running-owin-katana-apps-on-heroku
gravatar David De Sloovere Thursday, July 18, 2013
I'm looking forward to your next posts in this series. From what I've seen and read, it looks like owin/katana is the future for .net development to run on. Although someone at TechEd told me IIS only takes up 1%. So hosting out of IIS doesn't seem to be worth all this effort. Running on non-Windows OS or Microcontrollers (next gen Micro Framework?) then?
gravatar scott Thursday, July 18, 2013
@David: Yes, micro frameworks. Plus. Katana can be the place for cross cutting concerns like authz and logging.
Jomit Tuesday, July 23, 2013
Interesting... Looking forward to the next posts on this topic.
gravatar Erx Sunday, July 28, 2013
i've been reading for hours on katana/owin and i'm a 15 year .net dev, still do not understand at all what is is supposed to do. ive never used webapi or signalR or any of that. I've only used asp.net mvc and webforms. They give the standard "decoupling" line (definition/description) and do not support it with an example such as "normally, without owin/katana you would want to do A, B, C but wouldn't be able to", and... "now with owin/katana you can do all 3 because it has this and because you can use this to make that happen and it provides this benefit" -NO-FRIGGEN-WHERE DOES IT ACTUALLY TELL YOU ANYTHING OF MEANING. ARRRRRRRRRRRRRRRRRRRRRRRRRRRRRRFGGGGGGGGGGGGGGGG F-ING HELL I HATE PEOPLE WHO SUCK AT DESCRIBING THINGS! The people who setup owin.org haven't even given the description above and it is them that must. i went to Microsoft, better but still not like above. So, here, we have a corkscrew that looks like a can-opener and it will decouple your fork from your dinner place and here is how you can get started using it, open project, open new file, and voila "Hello World!" - are they gonna friggen tell us what the FUK its letting us do that we were unable to do before and how its doing it (owin.org & microsux that is)... oh no, of course not, because that would involve actually thinking about how best to describe something to an audience who may not have used webapi, so instead we will just assume you have all the necessary understandings to auto-magically know what the FUK we are talking about. Apparently they forgot to say, if you are a 15 year asp.net webforms + mvc developer only (using c#, vb & javascript) then that is not enough to understand our descriptions. F-ING idiots.
Comments are now closed.
by K. Scott Allen K.Scott Allen
My Pluralsight Courses
The Podcast!