Search code examples
c#.netasp.net-coredependency-injectionasp.net-core-signalr

Accessing SignalR Hub instance from Outside Class in .net Core


I want to access my queueHub from another Class. I have the following Codes so far but I get this error while running the code.

System.AggregateException: 'Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: BackEnd.Model.ServerLogger Lifetime: Singleton ImplementationType: BackEnd.Model.ServerLogger': Unable to resolve service for type 'Microsoft.AspNet.SignalR.IHubContext`1[BackEnd.Hubs.QueueHub]' while attempting to activate 'BackEnd.Model.ServerLogger'.)'

This is my Hub:

using BackEnd.Model;
using Microsoft.AspNetCore.SignalR;
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;

namespace BackEnd.Hubs
{
    public class QueueHub : Hub
    {

        public async Task JoinQueue(string userToken)
        {
            QueueItem user = new QueueItem
            {
                UserToken = userToken,
                ConnectionString = Context.ConnectionId
            };

            GlobalQueueManager.Enqueue(user);

        }

        public async Task notifyUser(QueueItem user, string matchId)
        {
            await Clients.Client(user.ConnectionString).SendAsync("notifyMatchStart", matchId);
        }

        public override Task OnConnectedAsync()
        {
            string connectionId = Context.ConnectionId;
            Console.WriteLine($"{connectionId} is Connected to the server");
            return base.OnConnectedAsync();
        }

    }
}

This is my class:

using BackEnd.Hubs;
using Microsoft.AspNet.SignalR;

namespace BackEnd.Model
{
    public class ServerLogger
    {
        private readonly IHubContext<QueueHub> _queueHub;
        private readonly Timer _timer;

        public ServerLogger(IHubContext<QueueHub> queueHub)
        {
            _timer = new Timer(LogQueueCount, null, TimeSpan.Zero, TimeSpan.FromSeconds(10));
            _queueHub = queueHub;
        }

        private async void LogQueueCount(object state)
        {
            var queueCount = GlobalQueueManager.GetQueueCount();
            Console.WriteLine($"Queue Count: {queueCount}");

            if(queueCount >= 2)
            {
                List<QueueItem> matchingUsers = new List<QueueItem>();
                for(int i = 0; i< 2; i++)
                {
                    QueueItem player = GlobalQueueManager.Dequeue();
                    matchingUsers.Add(player);
                }
                foreach(QueueItem item in matchingUsers)
                {
                   await _queueHub.Clients.Client(item.ConnectionString).notifyUser(item, "tesSTR");

                }

            }
        }

    }
}

Solution

  • You should use

    using Microsoft.AspNetCore.SignalR;
    

    in your ServerLogger.cs file.


    I have reproduced the issue by using using Microsoft.AspNet.SignalR;, and face the same issue.

    enter image description here