In my ASP.NET Core 7 MVC application, in startup.cs
hub service is injected like this:
public void ConfigureServices(IServiceCollection services)
{
// ...
services.AddScoped<IHubContext<OstupultHub>>();
}
Running the application throws an exception
System.ArgumentException HResult=0x80070057
Cannot instantiate implementation type 'Microsoft.AspNetCore.SignalR.IHubContext1[Store.Hubs.OstupultHub]' for service type 'Microsoft.AspNetCore.SignalR.IHubContext
1[Store.Hubs.OstupultHub]'Source=Microsoft.Extensions.DependencyInjection
StackTrace:
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.Populate() in /_/src/libraries/Microsoft.Extensions.DependencyInjection/src/ServiceLookup/CallSiteFactory.cs:line 68 ...
How to fix this so that hub can be used in MVC controller constructor like
public MyController(IHubContext<OstupultHub> hubContext ) { ...
Hub class is defined as
using Microsoft.AspNetCore.SignalR;
namespace Store.Hubs;
public class OstupultHub : Hub
{ ...
}
According to
https://learn.microsoft.com/en-us/aspnet/core/signalr/hubcontext?view=aspnetcore-7.0
it should work.
Currently I'm using in controller constructor IServiceProvider
public OstupultController(IServiceProvider _provider )
{
provider = _provider;
}
and then in controller
public async Task<ContentResult> ReceiveLogFile()
{
var pultHub = provider.GetRequiredService<IHubContext<OstupultHub>>();
await pultHub.Clients.All.SendAsync("sendLogFileMessage");
return new ContentResult() { Content ="sent" };
}
Looking for use service directly like other servies.
service is injected as
services.AddScoped<IHubContext<OstupultHub>>();
AFAIK you don't need to register hubs this way. Just call services.AddSignalR()
in the ConfigureServices
and MapHub<OstupultHub>(...);
in the Configure
.