从普通的C#类调用SignalR Client方法

Rav*_*tal 2 c# asp.net-mvc signalr

我想在我的MVC项目中添加SignalR.我需要从类库中调用SignalR客户端方法.我做了以下代码

public class CommomHubManager : ICommomHubManager
    {
        readonly IHubContext Context;
        public CommomHubManager()
        {
            Context = Helpers.Providers.HubContextProvider<Hubs.Notifications>.HubContext;
        }
        public Task AddUserToGroup(string groupName)
        {
            return Task.Factory.StartNew(() => {
                Context.Clients.All.addUserToGroup(groupName);
            });
        }
    }
Run Code Online (Sandbox Code Playgroud)

它不起作用,但是当我尝试从WebApi调用另一个Hub类方法时,它的工作正常.我想知道可以从普通的C#类调用SignalR Client方法吗?

vto*_*ola 5

如何在hubpipleline之外使用SignalR hub实例

var context = GlobalHost.ConnectionManager.GetHubContext<CommomHubManager>();
context.Clients.All.Send("Admin", "stop the chat");
Run Code Online (Sandbox Code Playgroud)

您可以在SignalR文档中找到更多信息.