Last active
December 11, 2015 23:28
-
-
Save HCanber/4676779 to your computer and use it in GitHub Desktop.
Nancy running on the Owin pipeline using HttpListener
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!"; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Install-Package Microsoft.Owin.Host.HttpListener -Pre | |
Install-Package Microsoft.Owin.Hosting -pre | |
Install-Package Nancy.Hosting.Owin |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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