gpt4 book ai didi

c# - 无法将 SignalR IHubContext 注入(inject)类

转载 作者:行者123 更新时间:2023-12-04 15:16:51 27 4
gpt4 key购买 nike

我是使用 SignalR 的新手,并且在尝试将 IHubContext 注入(inject)类时遇到了麻烦。当我尝试运行代码时,出现如下所示的异常。它在我尝试在 startup.cs 中注入(inject) TimerEventHandler 的行出错这是代码:

public class TimerEventHandler : ITimerEventHandler
{
private readonly IConfiguration _config;
private readonly IHubContext<IndexHub> _hubContext;

public TimerEventHandler(IHubContext<IndexHub> hubContext,
IConfiguration config)
{
_hubContext = hubContext;
_config = config;
}
}

ITimerEventHandler
public interface ITimerEventHandler
{
Task OnTimedEvent(object source, ElapsedEventArgs e, string connectionId);
}
这是我的 Startup.cs 中的代码
public void ConfigureServices(IServiceCollection services)
{
services.AddSignalR(o =>
{
o.EnableDetailedErrors = true;
});
services.AddScoped<ITimerEventHandler, TimerEventHandler>();
}
来自 indexhub 的代码
public class IndexHub : Hub
{

private readonly IServiceBusHelper _serviceBusHelper;
private readonly ITimerEventHandler _timerEventHandler;


public IndexHub(IServiceBusHelper serviceBusHelper,
ITimerEventHandler timerEventHandler)
{
_serviceBusHelper = serviceBusHelper;
_timerEventHandler = timerEventHandler;
}
....

}
程序.cs
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}

public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
我得到的异常(exception)是:
> System.AggregateException
HResult=0x80131500
Message=Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: my.portal.Hubs.ITimerEventHandler Lifetime: Scoped ImplementationType: my.portal.Hubs.TimerEventHandler': Unable to resolve service for type 'Microsoft.AspNet.SignalR.IHubContext`1[my.portal.Hubs.IndexHub]' while attempting to activate 'my.portal.Hubs.TimerEventHandler'.)
Source=Microsoft.Extensions.DependencyInjection
StackTrace:
at Microsoft.Extensions.DependencyInjection.ServiceProvider..ctor(IEnumerable`1 serviceDescriptors, ServiceProviderOptions options)
at Microsoft.Extensions.DependencyInjection.ServiceCollectionContainerBuilderExtensions.BuildServiceProvider(IServiceCollection services, ServiceProviderOptions options)
at Microsoft.Extensions.DependencyInjection.DefaultServiceProviderFactory.CreateServiceProvider(IServiceCollection containerBuilder)
at Microsoft.Extensions.Hosting.Internal.ServiceFactoryAdapter`1.CreateServiceProvider(Object containerBuilder)
at Microsoft.Extensions.Hosting.HostBuilder.CreateServiceProvider()
at Microsoft.Extensions.Hosting.HostBuilder.Build()
at my.portal.Program.Main(String[] args) in Program.cs:line 10
有哪位高手可以帮忙解答一下吗?

最佳答案

所以我想通了!我安装了 Microsoft.AspNet.SignalR.Core 包而不是 Microsoft.AspNetCore.SignalR.Core 包。感谢@Rena 看这个。只是你让我更仔细地查看我的代码并尝试将其剥离到基础知识帮助我解决了这个问题。

关于c# - 无法将 SignalR IHubContext 注入(inject)类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64164273/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com