【问题标题】:Not able to inject SignalR IHubContext into class无法将 SignalR IHubContext 注入类
【发布时间】:2020-10-01 23:05:25
【问题描述】:

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

ITTimerEventHandler

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

我得到的例外是:

> 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

有哪位专家可以帮忙吗?

【问题讨论】:

  • 通过使用你提供的代码,我无法重现你的问题,它在我的项目中运行良好。你能分享你的ITimerEventHandlerIndexHub。也来自你的错误消息,你能分享你的程序.cs?
  • @Rena 感谢您的收看。我已按要求添加了代码。
  • 对不起,你提供的不是我想要的。我需要ITimerEventHandler IndexHub。不是IndexHub.js。
  • 对不起@Rena。我现在添加了 ITimerHandler 和 IndexHub。
  • 也可以在我的项目中运行良好。也许其他一些事情会影响项目。您能否创建一个可以重现该问题的新存储库?我有点困惑,您为什么要循环引用ITimerEventHandler 和 IndexHub?

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


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多