简体   繁体   English

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

[英]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.

相关问题 如何在 ASP.NET controller 中正确注入 SignalR 集线器? - How do I correctly inject a SignalR hub in an ASP.NET controller? 访问 SignalR 集线器上下文。来自 ASP.NET 核心中 singleton 服务的项目 - Access SignalR Hub Context.Items from an singleton service in ASP.NET Core ASP .NET MVC 核心 + SignalR:从集线器重定向到 Controller - ASP .NET MVC Core + SignalR: Redirecting from a Hub to a Controller 调用SignalR Hub不适用于Asp.Net Core Web API - Invoking SignalR Hub not working for Asp.Net Core Web API ASP.NET MVC中使用SignalR Hub和Controller类的正确方法 - correct approach with SignalR Hub and Controller class in ASP.NET MVC 来自客户端的集线器方法未在ASP.NET Core SignalR中调用 - Hub method from client is not invoking in ASP.NET Core SignalR ASP.NET Core SignalR可从任何地方访问Hub方法 - ASP.NET Core SignalR acces Hub method from anywhere 如何将SignalR集线器上下文传递到ASP .NET Core 2.1上的Hangfire作业? - How can I pass a SignalR hub context to a Hangfire job on ASP .NET Core 2.1? 如何在ASP.NET Core应用程序中将记录器传递到服务器端的SignalR集线器 - How to pass logger into SignalR hub on server side in ASP.NET Core application 如何在 ASP.NET Core 2.2 中使用来自不同托管项目的共享 SignalR Hub - How to use shared SignalR Hub from different hosted project in ASP.NET Core 2.2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM