如何在ASP.NET Core中获取SignalR Hub上下文? [英] How to get SignalR Hub Context in a ASP.NET Core?

查看:462
本文介绍了如何在ASP.NET Core中获取SignalR Hub上下文?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用以下内容获取中心的上下文:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆