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

610 views Asked by At

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.

Across the system we use SignalR 2.4.1 but it is completely re-written in .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. Thanks

1

There are 1 answers

0
Ilias.P On

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. There are some other Asp.Net 4.7.2 Projects consuming those Hubs which are working fine. And also there is a new website build in .Net Core but targetting 4.7.2 framework.

The solution I ended up is essentially hosting an OWIN pipeline within the AspCore Pipeline.

First I needed to install the following packages:

  • Microsoft.Owin
  • Microsoft.AspNetCore.Owin

I also added a new extension method for the Core IApplicationBuilder interface that sets up OWIN on the same pipeline:

    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:

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.

I hope this helps.