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

Is the AppFunc the provider of the new routing?

@sergeyt
Copy link
Author

sergeyt commented Jul 20, 2015

BTW this solution should be easily implemented/ported to Web API, ASP.NET vNext. So no need to use Owin.Routing package at all.

@sergeyt
Copy link
Author

sergeyt commented Jul 20, 2015

AppFunc is alias of function which as part of OWIN standard, this function abstracts entire app from server implementation. See also MapMiddlware where IAppBuilder.Build is used.

P.S. it seems I did not understand the question, but try to give you some information.

@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