【问题标题】:Use SignalR 2.0 Owin pipeline with my SignalR lib将 SignalR 2.0 Owin 管道与我的 SignalR 库一起使用
【发布时间】: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!!");
        }
    }
}

【问题讨论】:

    标签: c# asp.net signalr owin


    【解决方案1】:

    最终版本看起来像这样,应用构建器扩展

    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);
    }
    

    这里有完整的源代码

    https://github.com/AndersMalmgren/SignalR.EventAggregatorProxy/tree/master/SignalR.EventAggregatorProxy/Owin

    【讨论】:

      猜你喜欢
      • 2013-07-09
      • 2016-07-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-27
      • 1970-01-01
      • 2020-08-04
      • 1970-01-01
      相关资源
      最近更新 更多