And*_*ers 5 c# asp.net signalr owin
我正在考虑将该库升级到SignalR 2.0
https://github.com/AndersMalmgren/SignalR.EventAggregatorProxy
我希望它通过IAppBuilder接口支持2.0 Owin管道,而不是RouteCollection像SignalR 1.x那样使用。
问题是,如何从IAppBuilder获取路由集合?我需要它来注册处理我的自定义js脚本的自定义IHttpHandler(例如SignalR注册其中心脚本)
我的lib的1.x设置看起来像这样
public static class SignalRConfig
{
public static void Register(RouteCollection routes)
{
routes.MapHubs();
routes.MapEventProxy<Contracts.Events.Event>();
}
}
Run Code Online (Sandbox Code Playgroud)
我对2.0配置的目标是这样的
public static class SignalRConfig
{
public static void Configuration(IAppBuilder app)
{
app.MapSignalR();
app.MapEventProxy<Contracts.Events.Event>();
}
}
Run Code Online (Sandbox Code Playgroud)
我的依赖于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>()));
}
}
Run Code Online (Sandbox Code Playgroud)
编辑:看起来非常复杂,无法让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>>());
}
}
}
Run Code Online (Sandbox Code Playgroud)
现在我只需要实施 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!!");
}
}
}
Run Code Online (Sandbox Code Playgroud)
最终版本如下所示,应用程序构建器扩展
public static class AppBuilderExtensions
{
public static void MapEventProxy<TEvent>(this IAppBuilder app)
{
Bootstrapper.Init<TEvent>();
app.Map("/eventAggregation/events", subApp => subApp.Use<EventScriptMiddleware<TEvent>>());
}
}
Run Code Online (Sandbox Code Playgroud)
调用中间件中的方法
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);
}
Run Code Online (Sandbox Code Playgroud)
完整源代码在这里
| 归档时间: |
|
| 查看次数: |
1865 次 |
| 最近记录: |