问题描述
我是使用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;
}
}
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
你们中的任何专家都可以帮忙吗?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)