dor*_*ver 4 c# asp.net-core asp.net-core-signalr
我是使用 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 IndexHub(IServiceBusHelper serviceBusHelper,
ITimerEventHandler timerEventHandler)
{
_serviceBusHelper = serviceBusHelper;
_timerEventHandler = timerEventHandler;
}
....
}
Run Code Online (Sandbox Code Playgroud)
程序.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>();
});
}
Run Code Online (Sandbox Code Playgroud)
我得到的例外是:
> 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
Run Code Online (Sandbox Code Playgroud)
有哪位专家可以帮忙吗?
所以我想通了!我安装了 Microsoft.AspNet.SignalR.Core 包,而不是 Microsoft.AspNetCore.SignalR.Core 包。感谢@Rena 看到这个。只是你让我更仔细地查看我的代码并尝试将其简化为基础知识帮助我解决了这个问题。
| 归档时间: |
|
| 查看次数: |
3059 次 |
| 最近记录: |