在启动时配置通用集线器 ASP.NET Core 3
Config generic hub in startup ASP.NET Core 3
我有一个通用集线器:
public class SendRequestToUserSignalR<T, HubContext> where T : class where HubContext : Hub
{
private readonly IUserConnectionManager userConnectionManager;
private readonly IHubContext<HubContext> hubContext;
public SendRequestToUserSignalR(IUserConnectionManager userConnectionManager , IHubContext<HubContext> hubContext)
{
this.userConnectionManager = userConnectionManager;
this.hubContext = hubContext;
}
}
而且我需要在启动时设置它:
app.UseSignalR(routes =>
{
routes.MapHub<SendRequestToUserSignalR<,>>("/sendRequest");
});
但它不起作用,我得到这个错误:
Using the generic type 'SendRequestToUserSignalR' requires 2 type arguments
我该如何解决这个问题?
对我来说,你的配置完全错误。
您不需要将泛型 class 定义为 hub:
public class SendRequestToUserSignalR : Hub
{
public SendRequestToUserSignalR(...services)
{
// code goes here...
}
}
在ConfigureServices
方法中,需要启动服务:
services.AddSignalR();
在 Configure
方法中,您将该集线器映射到 UseEndpoints
方法中:
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
endpoints.MapHub<SendRequestToUserSignalR>("/sendRequest");
});
注:app.UseSignalR
方法在asp.net核心版本3.x
中已过时
我有一个通用集线器:
public class SendRequestToUserSignalR<T, HubContext> where T : class where HubContext : Hub
{
private readonly IUserConnectionManager userConnectionManager;
private readonly IHubContext<HubContext> hubContext;
public SendRequestToUserSignalR(IUserConnectionManager userConnectionManager , IHubContext<HubContext> hubContext)
{
this.userConnectionManager = userConnectionManager;
this.hubContext = hubContext;
}
}
而且我需要在启动时设置它:
app.UseSignalR(routes =>
{
routes.MapHub<SendRequestToUserSignalR<,>>("/sendRequest");
});
但它不起作用,我得到这个错误:
Using the generic type 'SendRequestToUserSignalR' requires 2 type arguments
我该如何解决这个问题?
对我来说,你的配置完全错误。
您不需要将泛型 class 定义为 hub:
public class SendRequestToUserSignalR : Hub
{
public SendRequestToUserSignalR(...services)
{
// code goes here...
}
}
在ConfigureServices
方法中,需要启动服务:
services.AddSignalR();
在 Configure
方法中,您将该集线器映射到 UseEndpoints
方法中:
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
endpoints.MapHub<SendRequestToUserSignalR>("/sendRequest");
});
注:app.UseSignalR
方法在asp.net核心版本3.x