加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 在SignalR lib中使用SignalR 2.0 Owin管道

发布时间:2020-12-15 21:58:15 所属栏目:百科 来源:网络整理
导读:我正在考虑将此库升级到SignalR 2.0 https://github.com/AndersMalmgren/SignalR.EventAggregatorProxy 我希望它支持带有IAppBuilder接口的2.0 Owin管道,而不是像SignalR 1.x那样使用RouteCollection. 问题是,如何从IAppBuilder获取routecollection?我需要
我正在考虑将此库升级到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>>());
    }
}

在Middleware中调用方法

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读