[英]How do you get a reference to an ASP.NET Core SignalR hub context outside of a controller?
I have just started using SignalR for ASP.NET Core. 我刚刚开始将SignalR用于ASP.NET Core。 I have used SignalR for ASP.NET for a couple of years.
我已经将SignalR用于ASP.NET已有两年了。
I am using: 我在用:
Microsoft.AspNetCore.All 2.0.7
Microsoft.AspNetCore.SignalR 1.0.0-preview2-final
Microsoft.AspNetCore.SignalR.Client 1.0.0-preview2-final
It appears that in the ASP.NET Core version of SignalR I can no longer use GlobalHost or IConnectionManager to get a reference to a hub context. 似乎在SignalR的ASP.NET Core版本中,我不再可以使用GlobalHost或IConnectionManager来获取对集线器上下文的引用。 I can use DI to get a reference to the hub context in a controller without any problems.
我可以使用DI毫无问题地获得对控制器中中心背景的引用。
public BroadcastController(IHubContext<NotificationHub> hubContext)
{
_hubContext = hubContext;
}
But I need to know how to do it outside of a controller. 但是我需要知道如何在控制器外部进行操作。
You can inject IHubContext in any class other than contoller. 您可以在contoller之外的任何其他类中注入IHubContext。 Check the code snippet below:
检查以下代码段:
public class NotificationListnerExtension : INotificationListnerExtension
{
private readonly IHubContext<Notification> _notificationHubContext;
public NotificationListnerExtension(
IHubContext<Notification> notificationHubContext)
{
_notificationHubContext = notificationHubContext;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.