【问题标题】:How do you get a reference to an ASP.NET Core SignalR hub context outside of a controller?如何获取对控制器外部 ASP.NET Core SignalR 集线器上下文的引用?
【发布时间】:2018-10-15 08:53:27
【问题描述】:

我刚刚开始使用 SignalR for ASP.NET Core。我已经使用 SignalR for ASP.NET 几年了。

我正在使用:

Microsoft.AspNetCore.All 2.0.7
Microsoft.AspNetCore.SignalR 1.0.0-preview2-final
Microsoft.AspNetCore.SignalR.Client 1.0.0-preview2-final

似乎在 SignalR 的 ASP.NET Core 版本中,我无法再使用 GlobalHost 或 IConnectionManager 来获取对集线器上下文的引用。我可以使用 DI 在控制器中获取对集线器上下文的引用,而不会出现任何问题。

public BroadcastController(IHubContext<NotificationHub> hubContext)
{
_hubContext = hubContext;
}

但我需要知道如何在控制器之外进行操作。

【问题讨论】:

    标签: c# asp.net-core asp.net-core-2.0 asp.net-core-signalr


    【解决方案1】:

    您可以在控制器以外的任何类中注入 IHubContext。检查下面的代码sn-p:

     public class NotificationListnerExtension : INotificationListnerExtension
        {
            private readonly IHubContext<Notification> _notificationHubContext; 
    
            public NotificationListnerExtension(
                IHubContext<Notification> notificationHubContext)
            {
                _notificationHubContext = notificationHubContext;                
            }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-07-01
      • 2015-02-02
      • 1970-01-01
      • 2019-10-09
      • 2016-09-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多