繁体   English   中英

您如何在控制器外部获得对ASP.NET Core SignalR集线器上下文的引用?

[英]How do you get a reference to an ASP.NET Core SignalR hub context outside of a controller?

我刚刚开始将SignalR用于ASP.NET Core。 我已经将SignalR用于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;
}

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

您可以在contoller之外的任何其他类中注入IHubContext。 检查以下代码段:

 public class NotificationListnerExtension : INotificationListnerExtension
    {
        private readonly IHubContext<Notification> _notificationHubContext; 

        public NotificationListnerExtension(
            IHubContext<Notification> notificationHubContext)
        {
            _notificationHubContext = notificationHubContext;                
        }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM