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:

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.


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


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

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


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