Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Nancy running on the Owin pipeline using HttpListener
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Owin.Hosting;
using Nancy;
using Nancy.Bootstrapper;
using Nancy.Hosting.Owin;
using Owin;
namespace NancyOwin
{
class Program
{
static void Main(string[] args)
{
var url = "http://+:8080";
using (WebApplication.Start<Startup>(url))
{
Console.WriteLine("Running on http://localhost:8080");
Console.WriteLine("Press enter to exit");
Console.ReadLine();
}
}
}
public class Startup
{
public void Configuration(IAppBuilder app)
{
app.RunNancy(); //Uses an extension method that adds Nancy at the end of an Owin pipeline
}
}
public static class NancyOwinAppBuilderExtensions
{
public static IAppBuilder RunNancy(this IAppBuilder builder)
{
return RunNancy(builder, new DefaultNancyBootstrapper());
}
public static IAppBuilder RunNancy(this IAppBuilder builder, INancyBootstrapper bootstrapper)
{
var nancyOwinHost = new NancyOwinHost(bootstrapper);
return RunNancy(builder, nancyOwinHost);
}
public static IAppBuilder RunNancy(this IAppBuilder builder, NancyOwinHost host)
{
return RunApp(builder, host.ProcessRequest);
}
private static IAppBuilder RunApp(IAppBuilder builder, Func<IDictionary<string, object>, Task> app)
{
return UseFunc(builder, next => app);
}
private static IAppBuilder UseFunc(IAppBuilder builder, Func<Func<IDictionary<string, object>, Task>, Func<IDictionary<string, object>, Task>> middleware)
{
return builder.Use((object)middleware, new object[0]);
}
}
public class SampleModule : NancyModule
{
public SampleModule()
{
Get["/"] = _ => "Hello World!";
}
}
}
Install-Package Microsoft.Owin.Host.HttpListener -Pre
Install-Package Microsoft.Owin.Hosting -pre
Install-Package Nancy.Hosting.Owin
Microsoft.Owin.Host.HttpListener version=0.18.0-alpha
Microsoft.Owin.Hosting version=0.18.0-alpha
Nancy version=0.15.3
Nancy.Hosting.Owin version=0.15.3
Owin version=1.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment