Hello everyone! First – to break the cardinal rule of posting on stale blogs – it’s been nine months!? Time flies! Okay, now that’s out of the way.

The topic of this post is OWIN – specifically the 0.2.1 refresh of the Gate reference implementation which has been released to NuGet.org.

You can test-drive these bits yourself, if you would like, from a simple C# console application. I’m assuming you have NuGet installed, right? Good! So in a new console app (HelloWorld.exe in my case) from the Package Manager Console install either Gate.Stack.Kayak.Nancy or Gate.Stack.HttpListener.Nancy. You can also right-click the project and select Manage NuGet Packages… to accomplish the same thing. The various Gate.Stack.* you will find are umbrella packages that pull in several dependencies and add a few example source files.

Package Manager Console Host Version 1.6.21215.9133

Type 'get-help NuGet' to see all available NuGet commands.

PM> Install-Package Gate.Stack.Kayak.Nancy
Attempting to resolve dependency 'Gate.Hosts.Kayak (≥ 0.2.1)'.
Attempting to resolve dependency 'Gate.Builder (≥ 0.2.1)'.
Attempting to resolve dependency 'Gate.Owin (≥ 0.2.1)'.
Attempting to resolve dependency 'Kayak (≥ 0.7.2)'.
Attempting to resolve dependency 'Gate.Adapters.Nancy (≥ 0.2.1)'.
Attempting to resolve dependency 'Nancy (≥ 0.9.0)'.
Attempting to resolve dependency 'Gate.Middleware (≥ 0.2.1)'.
Attempting to resolve dependency 'Gate (≥ 0.2.1)'.

You’ll also notice Example-Startup.cs and Example-KayakStarter.cs have been added. We can look at those in a bit – but first let’s add some code to the Program.Main method.

namespace HelloWorld
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Running on http://localhost:8080/");
            KayakStarter.Start(8080, true);
        }
    }

    public class HomeModule : Nancy.NancyModule
    {
        public HomeModule()
        {
            Get["/"] = _ => "Hello, Kayak and Nancy!";
        }
    }
}

Now you can run with F5 and browse to http://localhost:8080/ – Bam! I won’t spoil the surprise about what you should expect to see. Let’s also take a quick look at what the two example files are doing.

using System;
using System.Net;
using Kayak;

namespace HelloWorld
{
    public class KayakStarter : ISchedulerDelegate
    {
        public static void Start(int port, bool debug)
        {
            Gate.Hosts.Kayak.KayakGate.Start(
                new KayakStarter(),
                new IPEndPoint(IPAddress.Any, port),
                debug ? "HelloWorld.Startup.Debug" : "HelloWorld.Startup");
        }

        public void OnException(IScheduler scheduler, Exception e)
        {
        }

        public void OnStop(IScheduler scheduler)
        {
        }
    }
}

This is the static Start method we’re calling from Main. It’s calling the Gate host glue for Kayak, and it also implements the ISchedulerDelegate interface to be notified of a few critical events from the Kayak server. The final string argument is also interesting – it is passed to the Gate.Builder assembly and is used to discover and call the Startup method. Let’s take a look at that Startup method next.

using Gate.Adapters.Nancy;
using Gate.Middleware;
using Gate.Owin;

namespace HelloWorld
{
    public static class Startup
    {
        public static void Configuration(IAppBuilder builder)
        {
            builder
                .RunNancy();
        }

        public static void Debug(IAppBuilder builder)
        {
            builder
                .UseShowExceptions()
                .RunNancy();
        }
    }
}

Beautiful, no? :)

The IAppBuilder is the only interface defined by the OWIN reference implementation assembly (Gate.Owin.dll) and it really only has two methods named Use and Build. Your Startup method doesn’t call those directly, rather you will use the extension methods coming from Middleware and Framework Adapter assemblies you have chosen to add to your application. UseShowExceptions comes from Gate.Middleware.dll and RunNancy comes from Gate.Adapters.Nancy.

In terms of timing you can think of this as the OWIN version of the Global.asax‘s Application_Start method. The IAppBuilder itself is simply stitching together a pipeline, and is something you can think of as the moral equivalent of Ruby’s Rack, or Node’s Connect module. It’s the result of the efforts of the .NET HTTP Abstractions group endeavoring to establish a comparable standard for .NET platform.