Justinus Hermawan Justinus Hermawan -4 years ago 467
C# Question

ASP.NET No suitable constructor for WebSocketManagerMiddleware

I got error when created WebSocketManagerExtensions like this:

System.InvalidOperationException
A suitable constructor for type
'CustomerManagementCore.WebSocketManager.WebSocketManagerMiddleware' could not be located. Ensure the type is concrete and services are registered for all parameters of a public constructor.


MapWebSocketManager
method:

public static IApplicationBuilder MapWebSocketManager(this IApplicationBuilder app,
PathString path,
WebSocketHandler handler)
{
return app.Map(path, (_app) => _app.UseMiddleware<WebSocketManagerMiddleware>(handler));
}


WebSocketManagerMiddleware
constructor:

public WebSocketManagerMiddleware(RequestDelegate next,
WebSocketHandler webSocketHandler)
{
_next = next;
_webSocketHandler = webSocketHandler;
}


I know if there is something error with my
WebSocketManagerMiddleware
constructor, but I have no idea.
Anyone know?

Thanks.

Answer Source

From the error I would guess WebSocketHandler was not added to the IServicesCollection in your startup class. Once you add it to the services collection, it should be able to be injected as expected. Typically this is done in the ConfigureServices method for an ASP.NET Core application created from the default templates.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download