I define my strong typed SignalR hub exactly by MS Rules
public class OutputMessages : Hub<IOutputMessages>
{...}
than inject my SignalR hub to conctoller by the same way
public class ApplicationAPIController : ControllerBase
{
public ApplicationAPIController(IHubContext<OutputMessages, IOutputMessages> _outputMessages)
{...}
}
In Startup I add SignalR service as
services.AddSignalR()
.AddHubOptions<OutputMessages>(options =>
{
options.EnableDetailedErrors = true;
})
and define endpoint
app.UseEndpoints(endpoints =>
{
endpoints.MapHub<OutputMessages>("/OutputMessages", options =>
{
options.Transports =
HttpTransportType.WebSockets |
HttpTransportType.LongPolling;
});
});
Looks good? Yes. And compilation is good. But in runTime I receive
fail: Microsoft.AspNetCore.SignalR.HubConnectionHandler[1]
Error when dispatching 'OnConnectedAsync' on hub.
System.InvalidOperationException: Unable to resolve service for type 'Microsoft.AspNetCore.SignalR.Hub`1[IOutputMessages]' while attempting to activate 'OutputMessages'.
at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.GetService(IServiceProvider sp, Type type, Type requiredBy, Boolean isDefaultParameterRequired)
at lambda_method(Closure , IServiceProvider , Object[] )
at Microsoft.AspNetCore.SignalR.Internal.DefaultHubActivator`1.Create()
at Microsoft.AspNetCore.SignalR.Internal.DefaultHubDispatcher`1.OnConnectedAsync(HubConnectionContext connection)
at Microsoft.AspNetCore.SignalR.Internal.DefaultHubDispatcher`1.OnConnectedAsync(HubConnectionContext connection)
at Microsoft.AspNetCore.SignalR.HubConnectionHandler`1.RunHubAsync(HubConnectionContext connection)
dbug: Microsoft.AspNetCore.SignalR.HubConnectionHandler[6]
OnConnectedAsync ending.
.AddHubOptions<OutputMessages>(options =>...
is correct. change that toservices.AddSignalR(o => o.EnableDetailedErrors = true);
, then try injectingIOutputMessages
the normal route:services.AddTransient<IOutputMessages, OutputMessages>();
– TurkishOutputMessages
class. You don't need any constructor sinceAddSignalR
registers the HUB on DI. Then the dependency you resolve trough DI like I answered you in previous questions. – Plea