【发布时间】:2013-10-31 17:16:35
【问题描述】:
我正在考虑将此库升级到 SignalR 2.0
https://github.com/AndersMalmgren/SignalR.EventAggregatorProxy
我希望它支持带有 IAppBuilder 接口的 2.0 Owin 管道,而不是像 SignalR 1.x 那样使用 RouteCollection。
问题是,如何从 IAppBuilder 中获取 routecollection?我需要它来注册一个自定义 IHttpHandler 来处理我的自定义 js 脚本(就像 SignalR 注册他们的集线器脚本一样)
我的 lib 的 1.x 设置如下所示
public static class SignalRConfig
{
public static void Register(RouteCollection routes)
{
routes.MapHubs();
routes.MapEventProxy<Contracts.Events.Event>();
}
}
我对 2.0 的目标是这样配置的
public static class SignalRConfig
{
public static void Configuration(IAppBuilder app)
{
app.MapSignalR();
app.MapEventProxy<Contracts.Events.Event>();
}
}
我的依赖于 RouteCollection 的代码如下所示
public static class RouteCollectionExtensions
{
public static void MapEventProxy<TEvent>(this RouteCollection routes)
{
Bootstrapper.Init<TEvent>();
routes.Add(new Route(
"eventAggregation/events",
new RouteValueDictionary(),
new RouteValueDictionary() {{"controller", string.Empty}},
new EventScriptRouteHandler<TEvent>()));
}
}
编辑:让 Owin 处理请求看起来非常复杂,我可以使用 SignalR 2.0 中的辅助方法来注册路由和该路由的处理程序吗?
更新: 使用此代码看起来我在正确的轨道上
using Owin;
using SignalR.EventAggregatorProxy.Boostrap;
namespace SignalR.EventAggregatorProxy.Owin
{
public static class AppBuilderExtensions
{
public static void MapEventProxy<TEvent>(this IAppBuilder app)
{
Bootstrapper.Init<TEvent>();
app.Map("/eventAggregation/events", subApp => subApp.Use<EventScriptMiddleware<TEvent>>());
}
}
}
现在我只需要实现EventScriptMiddleware
更新:最后一块拼图,现在我只需要我的中间件来实际吐出 javacript,应该很容易
namespace SignalR.EventAggregatorProxy.Owin
{
public class EventScriptMiddleware<TEvent> : OwinMiddleware
{
public EventScriptMiddleware(OwinMiddleware next) : base(next)
{
}
public override Task Invoke(IOwinContext context)
{
return context.Response.WriteAsync("Hello world!!");
}
}
}
【问题讨论】: