【发布时间】: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