Benjamin van der Veen

I live in Portland, Oregon. I work as a user experience designer at Emma.

Take a look at my portfolio, follow me on Twitter @bvanderveen, or email me at .

Gate 0.1.0 Released

Today I’m very pleased to announce that Gate is available on NuGet.

Gate is a utility for locating, manufacturing, and hosting OWIN web applications. Hosting adapters are available for Kayak, ASP.NET, and WCF.

Make an OWIN application with Nancy and Kayak

Nancy currently ships a NuGet package which provides OWIN support, so we’ll make a simple demo of running Nancy on Kayak. We’ll need to pull in the Kayak, Gate, and Nancy packages, as well as two adapter packages to glue them all together: Gate.Kayak and Nancy.Hosting.Owin.

Start by creating a new console application project. Grab the aforementioned NuGet packages—the easiest thing to do would be to pull in Gate.Kayak since that will bring Gate and Kayak with it, then Nancy.Hosting.Owin, which will bring Nancy along.

Then, punch these codes into your terminal:

using System;
using System.Net;
using Gate;
using Gate.Kayak;
using Kayak;
using Nancy.Hosting.Owin;
using Nancy;

namespace KayakGateNancyDemo
{
    public class Program
    {
        // plain-ol' entry point.
        public static void Main(string[] args)
        {
            var ep = new IPEndPoint(IPAddress.Any, 5500);
            Console.WriteLine("Listening on " + ep);
            KayakGate.Start(new SchedulerDelegate(), ep, Startup.Configuration);
        }
    }

    public class Startup
    {
        // called automatically when Kayak starts up.
        public static void Configuration(IAppBuilder builder)
        {
            // we'll create a very simple pipeline:

            builder
                // insert a middleware between Kayak and Nancy
                // to ensure Kayak objects are only accessed 
                // on Kayak's worker thread.
                .RescheduleCallbacks()

                // cap off the pipeline with Nancy
                .RunNancy();
        }
    }

    public class SchedulerDelegate : ISchedulerDelegate
    {
        public void OnException(IScheduler scheduler, Exception e)
        {
            // called whenever an exception occurs on Kayak's event loop.
            // this is good place for logging. here's a start:
            Console.WriteLine("Exception on scheduler");
            Console.Out.WriteStackTrace(e);
        }

        public void OnStop(IScheduler scheduler)
        {
            // called when Kayak's run loop is about to exit.
            // this is a good place for doing clean-up or other chores.
            Console.WriteLine("Scheduler is stopping.");
        }
    }

    // Your Nancy application.
    // Check out Nancy's docs for more info on how it works.
    public class MyNancyModule : NancyModule
    {
        public MyNancyModule()
        {
            Get["/"] = x =>
            {
                return "This is the root.";
            };
        }
    }

    // a bit of sugar for running Nancy ;)
    public static class NancyExtensions
    {
        public static IAppBuilder RunNancy(this IAppBuilder builder)
        {
            return builder.Run(Delegates.ToDelegate(new NancyOwinHost().ProcessRequest));
        }
    }
}

The KayakGate.Start method invokes the Startup.Configuration method, uses the user-configured IAppBuilder instance to manufacture an OWIN application delegate, and hosts it on port 5500. Visit http://localhost:5500 in your web browser and bask in the glory.

With this bit of boilerplate, it should be possible to use Kayak to host almost any application developed with Nancy.

VoilĂ , a completely open source .NET web application stack using an open interface brought to you by hackers in the community. Have fun, and let us know your thoughts on the .NET HTTP Abstractions list.


Copyright © 2011 Benjamin van der Veen. atom feed