如何在 ASP.NET Core 中获取 SignalR Hub 上下文? [英] How to get SignalR Hub Context in a ASP.NET Core?
问题描述
我正在尝试使用以下方法获取集线器的上下文:
I'm trying to get the context for a hub using the following:
var hubContext = GlobalHost.ConnectionManager.GetHubContext<SomeHub>();
问题在于未定义 GlobalHost.我看到它是 SignalR.Core dll 的一部分.目前,我的项目 .json 文件中有以下依赖项:
The problem is that GlobalHost is not defined. I see it is part of the SignalR.Core dll. At the moment, I have the following in my project .json file, under dependencies:
"Microsoft.AspNet.SignalR.Server": "3.0.0-*"
如果我添加最新可用的 Core 版本:
If I add the latest available version of Core:
"Microsoft.AspNet.SignalR.Server": "3.0.0-*",
"Microsoft.AspNet.SignalR.Core" : "2.1.2"
我收到一大堆错误,因为服务器和核心存在冲突.如果我将它们更改为都使用版本3.0.0-*",则所有冲突都会消失,但无法找到 GlobalHost.如果我删除服务器,并且只使用核心版本 2.1.2,那么 GlobalHost 可以工作,但所有其他需要服务器的东西,显然都没有.
I get a whole bunch of errors because server and core are conflicting. If I change them to both use version "3.0.0-*", all the conflicts go away, but GlobalHost cannot be found. If I remove Server, and just user Core version 2.1.2 then GlobalHost works, but all the other things needing Server, obviously do not.
有什么想法吗?
推荐答案
Microsoft.AspNet.SignalR.Infrastructure.IConnectionManager
是一个 DI 注入服务,您可以通过它获取集线器上下文...对于例子:
Microsoft.AspNet.SignalR.Infrastructure.IConnectionManager
is a DI injected service through which you can get the hub context...For example:
using Microsoft.AspNet.SignalR;
using Microsoft.AspNet.SignalR.Infrastructure;
using Microsoft.AspNet.Mvc;
public class TestController : Controller
{
private IHubContext testHub;
public TestController(IConnectionManager connectionManager)
{
testHub = connectionManager.GetHubContext<TestHub>();
}
.....
这篇关于如何在 ASP.NET Core 中获取 SignalR Hub 上下文?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!