简体   繁体   English

我们可以在 ASP.NETCORE 应用程序中托管 ASP.NET SignalR v2.4.1 吗?

[英]Can we Host ASP.NET SignalR v2.4.1 in an ASP.NETCORE App?

I have a situation where my codebase is stuck in .Net 4.7.2 for now but I need to push some notifications on a Website which is built on Asp.Core 2.2.我的代码库目前停留在 .Net 4.7.2 中,但我需要在基于 Asp.Core 2.2 的网站上推送一些通知。

Across the system we use SignalR 2.4.1 but it is completely re-written in .Net Core.在整个系统中,我们使用 SignalR 2.4.1,但它完全用 .Net Core 重写。

I tried hosting it in the same app without success.我尝试在同一个应用程序中托管它,但没有成功。 Owin does not seem to be happy.欧文似乎并不高兴。

Has anyone had any success with it or has any suggestion?有没有人有任何成功或有任何建议?

There has to be a way for projects migrating from .Net to Core.必须有一种方法可以让项目从 .Net 迁移到 Core。 Thanks谢谢

Ok so after along night I got a solution to this issue.好的,一夜之间我得到了这个问题的解决方案。

First just to make my setup clear.首先只是为了让我的设置清楚。 There is an API project targetting .Net 4.7.2 which is broadcasting some messages via a SignalR 2.4.1 Hub.有一个针对 .Net 4.7.2 的 API 项目,它通过 SignalR 2.4.1 集线器广播一些消息。 There are some other Asp.Net 4.7.2 Projects consuming those Hubs which are working fine.还有一些其他的 Asp.Net 4.7.2 项目使用了那些工作正常的集线器。 And also there is a new website build in .Net Core but targetting 4.7.2 framework.还有一个新的网站构建在 .Net Core 中,但针对 4.7.2 框架。

The solution I ended up is essentially hosting an OWIN pipeline within the AspCore Pipeline.我最终得到的解决方案本质上是在 AspCore 管道中托管一个 OWIN 管道。

First I needed to install the following packages:首先,我需要安装以下软件包:

  • Microsoft.Owin微软.Owin
  • Microsoft.AspNetCore.Owin微软.AspNetCore.Owin

I also added a new extension method for the Core IApplicationBuilder interface that sets up OWIN on the same pipeline:我还为 Core IApplicationBuilder 接口添加了一个新的扩展方法,用于在同一管道上设置 OWIN:

    public static class OwinExtensions
    {
        public static IApplicationBuilder UseOwinApp(this IApplicationBuilder app, Action<IAppBuilder> configuration)
        {
            return app.UseOwin(setup => setup(next =>
            {
                IAppBuilder owinApp = new AppBuilder();

                var aspNetCoreLifetime = (IApplicationLifetime)app.ApplicationServices.GetService(typeof(IApplicationLifetime));

                var owinAppProperties = new AppProperties(owinApp.Properties)
                {
                    OnAppDisposing = aspNetCoreLifetime?.ApplicationStopping ?? CancellationToken.None,
                    DefaultApp = next
                };

                configuration(owinApp);

                return owinApp.Build<Func<IDictionary<string, object>, Task>>();
            }));
        }
    }

Then in the Startup class of the Core project, in the Configure method I was able to use my extension and register SignalR hubs to it like this:然后在 Core 项目的Startup类中,在Configure方法中,我可以使用我的扩展并向其注册 SignalR 集线器,如下所示:

Startup.cs启动文件

        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {

            ...

            app.UseOwinApp(owinApp =>
            {
                owinApp.MapSignalR();
            });

            ...
}

This way we can add more middlewares to the OWIN pipeline if we need to for whatever reasons.通过这种方式,如果出于任何原因需要,我们可以向 OWIN 管道添加更多中间件。

I hope this helps.我希望这有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM