I'm new to using SignalR and having troubles trying to inject IHubContext into a class. I get an exception as shown below when I try to run the code. It errors out on the line where I try to inject TimerEventHandler in startup.cs Here is the code:
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);
}
And here is code from my Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddSignalR(o =>
{
o.EnableDetailedErrors = true;
});
services.AddScoped<ITimerEventHandler, TimerEventHandler>();
}
Code from 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>();
});
}
The exception I'm getting is:
> 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
Can any of you experts out there help out?
So I figured it out! I had installed the Microsoft.AspNet.SignalR.Core package instead of Microsoft.AspNetCore.SignalR.Core package. Thanks @Rena for looking at this. Just you getting me to look more closely at my code and try to strip it down to the basics helped me solve this.
User contributions licensed under CC BY-SA 3.0