Created
July 18, 2015 12:13
-
-
Save sergeyt/d9d7ba0b35e833533d97 to your computer and use it in GitHub Desktop.
owin dynamic routing idea
This file contains hidden or 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
// 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)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sorry ... I am having a hard time understanding how to implement this ...