小编dor*_*ver的帖子

无法将 SignalR IHubContext 注入类中

我是使用 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;            
        }
    }
  
Run Code Online (Sandbox Code Playgroud)

ITimer事件处理器

public interface ITimerEventHandler
{
    Task OnTimedEvent(object source, ElapsedEventArgs e, string connectionId);
}
Run Code Online (Sandbox Code Playgroud)

这是我的 Startup.cs 中的代码

public void ConfigureServices(IServiceCollection services)
{            
   services.AddSignalR(o =>
   {
      o.EnableDetailedErrors = true;
   });
   services.AddScoped<ITimerEventHandler, TimerEventHandler>();            
}
Run Code Online (Sandbox Code Playgroud)

代码来自indexhub

public class IndexHub : Hub
{       

    private readonly IServiceBusHelper _serviceBusHelper;
    private readonly ITimerEventHandler _timerEventHandler;
    

    public …
Run Code Online (Sandbox Code Playgroud)

c# asp.net-core asp.net-core-signalr

4
推荐指数
1
解决办法
3059
查看次数

标签 统计

asp.net-core ×1

asp.net-core-signalr ×1

c# ×1