您如何在控制器外部获得对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已有两年了.
I have just started using SignalR for ASP.NET Core. I have used SignalR for ASP.NET for a couple of years.
我正在使用:
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毫无问题地获得对控制器中中心背景的引用.
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. I can use DI to get a reference to the hub context in a controller without any problems.
public BroadcastController(IHubContext<NotificationHub> hubContext)
{
_hubContext = hubContext;
}
但是我需要知道如何在控制器外部进行操作.
But I need to know how to do it outside of a controller.
推荐答案
您可以在contoller之外的任何其他类中注入IHubContext.检查以下代码段:
You can inject IHubContext in any class other than contoller. Check the code snippet below:
public class NotificationListnerExtension : INotificationListnerExtension
{
private readonly IHubContext<Notification> _notificationHubContext;
public NotificationListnerExtension(
IHubContext<Notification> notificationHubContext)
{
_notificationHubContext = notificationHubContext;
}
}
这篇关于您如何在控制器外部获得对ASP.NET Core SignalR集线器上下文的引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!