无法将 SignalR IHubContext 注入 class

Not able to inject SignalR IHubContext into class

我是 SignalR 的新手,在尝试将 IHubContext 注入 class 时遇到了麻烦。当我尝试 运行 代码时,出现如下所示的异常。它在我尝试在 startup.cs 中注入 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;
    }
    ....

}

Program.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>();
            });
}

我遇到的异常是:

> 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 看这个。只是你让我更仔细地查看我的代码并尝试将其分解为基础知识帮助我解决了这个问题。