Search code examples
asp.net-coresignalrsignalrcore

How can I use SignalR in the class library, using the same instance as the webApplication project?


I am writing a core class library for SignalR. I created a class library and I added a SignalR package to that. as you know instances are different how can I use webApplication(Asp.Net-Code) project SignalR instance? Please share with me if you have a sample code thanks a lot


Solution

  • Hi I added a singleton class

    internal class HubClientsConnectedInfo
    {
        private IHubCallerClients _clients;
        private IGroupManager _groups;
    
        public void Set(IHubCallerClients clients, IGroupManager groups)
        {
            _clients ??= clients;
            _groups ??= groups;
        }
        public IHubCallerClients Clients => _clients;
        public IGroupManager Groups => _groups;
    }
    

    then i register it in Program.cs

    builder.Services.AddSingleton<HubClientsConnectedInfo>();
    

    then i fill it on BaseHub.OnConnectedAsync