大家好,在我的 ASP.NET MVC 应用程序上的 SignalR Hub 中,我尝试将“NotificationService”类注入到我的 SignalR Hub 的构造函数中。
public class ManualReconHub : Hub<IManualReconHub>
{
private INotificationService _notificationService;
public static IList<string> AllDisabledRows = new List<string>();
public static IList<string> AllNotifications = new List<string>();
public ManualReconHub(INotificationService notificationService)
{
_notificationService = notificationService;
}
这当然会破坏连接,因为根据我在文档中读到的内容,集线器的构造函数不需要任何参数。有没有一种方法可以按照我尝试的方式实现此目的,或者是通过文档中的以下方法将类注入集线器的唯一方法:
GlobalHost.DependencyResolver.Register(
typeof(ChatHub),
() => new ChatHub(new ChatMessageRepository()));
我不熟悉这种依赖注入方法,并且更愿意以相同的方式通过我的应用程序使用依赖注入。我目前的 Program.cs 如下:
builder.Services.AddScoped<IServiceCollection, ServiceCollection>();
任何有关此问题的帮助将不胜感激!
您使用了旧的已弃用版本的 SignalR
.NET Core 中包含 SignalR。你通过这样做来添加它
services.AddSignalR();
然后您可以像这样定义集线器
app.UseEndpoints(config => config.MapHub<ManualReconHub>("/ManualReconHub"));
您还可以使用像 SignalR.EventAggregatorProxy 这样的库来为您完成繁重的工作。
https://github.com/AndersMalmgren/SignalR.EventAggregatorProxy/wiki
免责声明:我是该库的作者