我正在考虑将此库升级到SignalR 2.0
https://github.com/AndersMalmgren/SignalR.EventAggregatorProxy
我希望它支持带有IAppBuilder接口的2.0 Owin管道,而不是像SignalR 1.x那样使用RouteCollection.
问题是,如何从IAppBuilder获取routecollection?我需要它来重新定制一个处理我的自定义js脚本的自定义IHttpHandler(就像SignalR注册它们的hub脚本一样)
我的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!!"); } } }
解决方法
最终版本看起来像这样,app builder扩展
public static class AppBuilderExtensions { public static void MapEventProxy<TEvent>(this IAppBuilder app) { Bootstrapper.Init<TEvent>(); app.Map("/eventAggregation/events",subApp => subApp.Use<EventScriptMiddleware<TEvent>>()); } }
public override Task Invoke(IOwinContext context) { var response = context.Response; response.ContentType = "application/javascript"; response.StatusCode = 200; if (ClientCached(context.Request,scriptBuildDate)) { response.StatusCode = 304; response.Headers["Content-Length"] = "0"; response.Body.Close(); response.Body = Stream.Null; return Task.Fromresult<Object>(null); } response.Headers["Last-Modified"] = scriptBuildDate.ToUniversalTime().ToString("r"); return response.WriteAsync(js); }
这里有完整的源代码
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。