Replace Globalhost.Dependency resolver in the new Asp.net Core SignalR

Viewed 390

I am migrating my current code which is in .NET framework 4.8 to new .NET Core 5.0. Following is my original Startup.cs.

    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            var container = new UnityContainer();
            var unityConfiguration = new UnityConfiguration();
            unityConfiguration.RegisterTypes(container);

            GlobalHost.DependencyResolver = new UnityDependencyResolver(container);

            GlobalHost.HubPipeline.AddModule(new SessionIdCheckModule());

            app.UseCors(new CorsOptions
            {
                PolicyProvider = new WildcardCorsPolicyProvider()
            });

            var listener = (HttpListener)app.Properties[typeof(HttpListener).FullName];
            listener.AuthenticationSchemes = AuthenticationSchemes.Ntlm | AuthenticationSchemes.Negotiate;

            //Throttling of http.sys to prevent too many requests being accepted at once.
            var maxAccepts = 25;
            var maxRequests = 25;
            var queueLimit = 25;
            var owinListener = (OwinHttpListener)app.Properties[typeof(OwinHttpListener).FullName];
            owinListener.SetRequestProcessingLimits(maxAccepts, maxRequests);
            owinListener.SetRequestQueueLimit(queueLimit);

            TurnOffWebSocket(GlobalHost.DependencyResolver);

            app.MapSignalR();
        }

        //Have to use Ajax long polling as the transport, to get around mixed content blocking in browsers.
        public void TurnOffWebSocket(IDependencyResolver resolver)
        {
            var transportManager = resolver.Resolve<ITransportManager>() as TransportManager;
            transportManager.Remove("webSockets");
            transportManager.Remove("serverSentEvents");
            transportManager.Remove("foreverFrame");
        }
    }}

Can anyone suggest on following.

  1. What is the replacement of Globalhost.Dependency resolver?
  2. I want to keep using Unity for DI and not in build DI of asp.net core. How can I achieve that ?
  3. What can be used to achieve behaviour of HubPipelineModule?
1 Answers

DI is a first class feature in ASP.NET Core, no more Globalhost, services can accept types in their constructors from DI. There are plenty of examples of how DI works.

To keep using UnityContainer you will need to replace parts of DI, there is an article that can help with that https://docs.microsoft.com/aspnet/core/migration/proper-to-2x/mvc2?view=aspnetcore-5.0#native-dependency-injection

HubPipelineModule is replaced by Hub Filters https://docs.microsoft.com/aspnet/core/signalr/hub-filters?view=aspnetcore-5.0.

Related