Skip to main content
 首页 » 编程设计

c#之ASP.NET 没有适合 WebSocketManagerMiddleware 的构造函数

2024年02月05日27落叶无声

我在像这样创建 WebSocketManagerExtensions 时出错:

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 方法:

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

WebSocketManagerMiddleware 构造函数:

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

我知道我的 WebSocketManagerMiddleware 构造函数是否有问题,但我不知道。 有人知道吗?

谢谢。

请您参考如下方法:

根据错误,我猜测 WebSocketHandler 没有添加到您的启动类中的 IServicesCollection 中。将它添加到服务集合后,它应该能够按预期注入(inject)。对于从默认模板创建的 ASP.NET Core 应用程序,通常这是在 ConfigureServices 方法中完成的。