【问题标题】:Dependency Injection into SignalR Hub (dotnet core 3.1)依赖注入到 SignalR Hub (dotnet core 3.1)
【发布时间】:2020-11-30 17:17:09
【问题描述】:

我正在使用 SignalR,但在让 DI 进入 SignalR 集线器时遇到问题。我以为它会使用现有的 dotnet core DI 框架,但似乎并非如此。我不断得到

System.InvalidOperationException: Unable to resolve service for type 'Comcast.Cs.Mercury.Web.Api.IHubClientHelper' while attempting to activate 'mercury_ms_auth.Hubs.AuthenticationHub'.
   at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.GetService(IServiceProvider sp, Type type, Type requiredBy, Boolean isDefaultParameterRequired)
   at lambda_method(Closure , IServiceProvider , Object[] )
   at Microsoft.AspNetCore.SignalR.Internal.DefaultHubActivator`1.Create()
   at Microsoft.AspNetCore.SignalR.Internal.DefaultHubDispatcher`1.OnConnectedAsync(HubConnectionContext connection)
   at Microsoft.AspNetCore.SignalR.Internal.DefaultHubDispatcher`1.OnConnectedAsync(HubConnectionContext connection)
   at Microsoft.AspNetCore.SignalR.HubConnectionHandler`1.RunHubAsync(HubConnectionContext connection)

我注册了单例:

            services.AddSingleton<IHubClientHelper>(new HubClientHelper(loggerFactory.CreateLogger<HubClientHelper>())
            {
                ClientConnectionType = _environment.IsDevelopment()
                    ? ClientConnectionType.ConnectionId
                    : ClientConnectionType.UserId
            });

并且我有依赖进入集线器的构造函数:

public AuthenticationHub(TelemetryClient telemetryClient, IHubClientHelper clientHelper)
            : base(telemetryClient, clientHelper)
        {
            
        }

文档表明这应该可以工作。有什么想法吗?

【问题讨论】:

  • 您需要在此处提供更多信息。甚至不清楚你得到了什么例外。
  • 是的,我的错。这就是我在凌晨 1 点发布的结果

标签: c# asp.net-core dependency-injection signalr asp.net-core-signalr


【解决方案1】:

您的问题不是很清楚,但我会尝试回答您如何正确注册集线器并使用 DI 注入它。首先,您在启动时添加您的集线器:

services.AddSignalR(hubOptions =>
{
    hubOptions.ClientTimeoutInterval = TimeSpan.FromSeconds(hostConfiguration.SignalR.ClientTimeoutInterval);
    hubOptions.HandshakeTimeout = TimeSpan.FromSeconds(hostConfiguration.SignalR.HandshakeTimeout);
    hubOptions.KeepAliveInterval = TimeSpan.FromSeconds(hostConfiguration.SignalR.KeepAliveInterval);
    hubOptions.EnableDetailedErrors = hostConfiguration.SignalR.EnableDetailedErrors;
    hubOptions.MaximumReceiveMessageSize = hostConfiguration.SignalR.MaximumReceiveMessageSize;
    hubOptions.StreamBufferCapacity = hostConfiguration.SignalR.StreamBufferCapacity;
});
app.UseSignalR(routes =>
{
    routes.MapHub<YourHub>(/yourHub);
});

所以这会将您的中心添加为transient。然后你可以像这样注入你的集线器:

private IHubContext<YourHub, IYourHub> YourHub
{
    get
    {
        return this.serviceProvider.GetRequiredService<IHubContext<YourHub, IYourHub>>();
    }
}

还有一些注意事项:

SignalR 希望为每条消息单独创建集线器。如果您希望 Hub 处于 DI 中,则需要将其添加为瞬态服务。您通常不应该将集线器从 DI 中解析出来。如果您需要在 Hub 和其他组件之间共享代码,我建议您使用 IHubContext 或将共享代码放在单独的 DI 服务中。

【讨论】:

  • 不幸的是,您的答案包含的问题多于源答案。 (1)在什么类中需要定义私有属性?在集线器?为什么? (2)通过什么方式获取this.serviceProvider?这不是测试。例如,在服务内部,我们可以获得 serviceCollection 作为 new Microsoft.Extensions.DependencyInjection.ServiceCollection(); (3) 在 IHubContext YourHub 中,您会收到 YourHub 的实例。并且由于比例,您在 YouHub 上收到了一个实例?
【解决方案2】:

发现问题。我正在使用 Unity Container,结果发现该容器没有像 OOTB DI 框架那样自动注册 ILogger。我通过使用即时窗口尝试解决 IHubClientHelper 发现了这一点,它最终告诉我它无法解决 ILogger (No public constructor is available for type Microsoft.Extensions.Logging.ILogger.)。在快速谷歌之后,我找到了this,并通过将以下内容添加到 Startup.cs 中的 ConfigureContainer 方法,我再次启动并运行。

var logFactory = new LoggerFactory();
container.AddExtension(new LoggingExtension(logFactory));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-11-09
    • 1970-01-01
    • 1970-01-01
    • 2017-07-14
    • 2020-06-04
    • 2019-01-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多