Reputation: 1122
I have many Hubs and register them:
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
endpoints.MapHub<FinanceHub>("/hub/finance");
endpoints.MapHub<PartnersHub>("/hub/partners");
...
endpoints.MapHub<WarehousesHub>("/hub/warehouses");
endpoints.MapControllerRoute("default", "{controller}/{action}/{id?}");
});
I register SignalR:
services.AddSignalR(options =>
{
options.EnableDetailedErrors = true;
options.MaximumReceiveMessageSize = signalrMaxMessageLimit * 8192;
}).AddMessagePackProtocol(conf =>
{
conf.FormatterResolvers.Clear();
conf.FormatterResolvers.Add(CircularResolver.Instance);
});
They work as SignalR hubs. But when I try to resolve them in Microsoft.AspNetCore.Mvc.ControllerBase:
private IHubContext<FinanceHub> financeHub => provider.GetRequiredService<IHubContext<FinanceHub>>();
public PaymentRegisterController(
ILogger<ServiceController> logger,
IServiceProvider _provider) : base(logger)
{
provider = _provider;
}
I get an exception:
System.InvalidOperationException: No service for type 'Microsoft.AspNet.SignalR.IHubContext`1[Engy.Plantain.Procurement.Backend.Hubs.FinanceHub]' has been registered.
Where is the problem?
Upvotes: 4
Views: 1804
Reputation: 1529
I think the new way to do it in .NET 6
You can have multiple Resolvers
services.AddSignalR()
.AddMessagePackProtocol(options =>
{
options.SerializerOptions = MessagePackSerializerOptions.Standard
.WithResolver(MessagePack.Resolvers.StandardResolver.Instance)
.WithResolver(MessagePack.Resolvers.NativeDateTimeResolver.Instance)
.WithSecurity(MessagePackSecurity.UntrustedData);
});
Upvotes: 0
Reputation: 1122
As eveyone, I am sure, thought, it was my mistake. In the API-controller I was trying to resolve Microsoft.AspNet.SignalR.IHubContext instead of Microsoft.AspNetCore.SignalR.IHubContext. Now everything works perfectly.
Upvotes: 1