Gate 0.2.1 implementation of OWIN online at NuGet
Gate, internet, opensource, OWIN, programming, tech January 16th, 2012Hello 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.
January 18th, 2012 at 3:32 am
[...] Gate 0.2.1 implementation of OWIN online at NuGet – Louis DeJardin announces the 0.2.1 release of the Gate implementation of OWIN (the Open Web Interface for .NET). Gate is a reference implementation of the standard for connecting web servers and web applications, and is available as a NuGet Package [...]
February 9th, 2012 at 11:25 am
The package no longer works as advertised. The call to “Gate.Hosts.Kayak.KayakGate.Start” requires an AppDelegate. I was really hopeful that this would make it dirt simple to get a Nancy/Kayak/Gate app going, hopefully it’s an easy fix?
February 9th, 2012 at 11:42 am
Sure, I’ll take a look at what changed. Stay tuned…
February 9th, 2012 at 11:48 pm
Ah, yes! The dirt simpler story for getting started is a little simpler now, but works slightly differently. I’ll add a new blog post this weekend. It’s very nice.
Sorry for the inconvenience, and as always, we appreciate your support.
May 11th, 2012 at 12:51 am
Could you maybe update your blogpost? I try to host Nancy on Kayak, but I can´t find a reference implementation – just this blogpost. I have no idea how the AppDelegate works in this line “debug ? “HelloWorld.Startup.Debug” : “HelloWorld.Startup”);”.
Thank you for your time :)
May 11th, 2012 at 2:09 am
Ah, yes! Absolutely.
There is a new sample that’s coming together just now, actually.
July 13th, 2012 at 6:24 am
Hi Louis, can you point to the new example? No idea how to use AppDelegate either. Cheers.
July 13th, 2012 at 6:36 am
Think I figured it out:
AppDelegate appDelegate = Gate.Builder.AppBuilder.BuildConfiguration(builder => new Startup().Configuration(builder));
KayakGate.Start(new SchedulerDelegate(), new IPEndPoint(IPAddress.Loopback, 8001), appDelegate);
August 24th, 2012 at 12:26 pm
This gist (https://gist.github.com/2173428) show how add the AppDelegate to the KayakGate.Start:
Gate.Hosts.Kayak.KayakGate.Start(
new KayakStarter(),
new IPEndPoint(IPAddress.Any, port),
Gate.Adapters.Nancy.NancyAdapter.App());