首页 > 解决方案 > 在新的 Asp.net Core SignalR 中替换 Globalhost.Dependency 解析器

问题描述

我正在将 .NET Framework 4.8 中的当前代码迁移到新的 .NET Core 5.0。以下是我原来的 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");
        }
    }}

任何人都可以建议关注。

  1. Globalhost.Dependency 解析器的替代品是什么?
  2. 我想继续将 Unity 用于 DI,而不是在 asp.net 核心的构建 DI 中。我怎样才能做到这一点?
  3. 什么可以用来实现 HubPipelineModule 的行为?

标签: c#asp.net-coresignalrunity-container

解决方案


DI 是 ASP.NET Core 中的一流功能,不再是 Globalhost,服务可以在其构造函数中接受来自 DI 的类型。有很多关于 DI 如何工作的例子。

要继续使用 UnityContainer,您需要替换部分 DI,有一篇文章可以帮助解决此问题https://docs.microsoft.com/aspnet/core/migration/proper-to-2x/mvc2?view=aspnetcore- 5.0#native-dependency-injection

HubPipelineModule 被 Hub 过滤器取代https://docs.microsoft.com/aspnet/core/signalr/hub-filters?view=aspnetcore-5.0


推荐阅读