Skip to content

Instantly share code, notes, and snippets.

@sergeyt
Created July 18, 2015 12:13
Show Gist options
  • Save sergeyt/d9d7ba0b35e833533d97 to your computer and use it in GitHub Desktop.
Save sergeyt/d9d7ba0b35e833533d97 to your computer and use it in GitHub Desktop.
owin dynamic routing idea
// WARNING this gist is an example with potentially non-compilable code!
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace Owin.Routing
{
using AppFunc = Func<IDictionary<string, object>, Task>;
static class DynamicRouter
{
/// <summary>
/// Selects OWIN pipeline at runtime.
/// </summary>
/// <param name="app">holds initial owin pipeline state</param>
/// <param name="selector">function to build new owin pipeline</param>
/// <param name="routesChanged">signal to rebuild pipeline</param>
/// <returns></returns>
public static IAppBuilder UseDynamicRouting(this IAppBuilder app,
Func<IAppBuilder, IAppBuilder> selector,
IObservable<bool> routesChanged)
{
var api = selector(app);
var handler = (AppFunc) api.Build(typeof(AppFunc));
routesChanged.Subscribe(_ =>
{
api = selector(app);
handler = (AppFunc) api.Build(typeof(AppFunc));
});
return app.Use((ctx, next) => handler(ctx.Environment));
}
}
}
@unknownguy
Copy link

Sorry ... I am having a hard time understanding how to implement this ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment