Question

I am trying to add SignalR in my MVC project. I need to call a SignalR client method from my class library. I did below code

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);
            });
        }
    }

Its not working, but when i try to call an another Hub class method from WebApi its working just fine. I want to know that is it possible to call SignalR Client method from normal C# class?

Was it helpful?

Solution

How to use SignalR hub instance outside of the hubpipleline

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

You can find out more in the SignalR documentation.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top