Smarpan Sharma
Smarpan Sharma

Reputation: 21

"Clients" is throwing System.ObjectDisposed exception in signalr hub

[nevermind I just requested the data from the front-end and handled it from controllers. It works.No need to get data through this hub. Although I like to know how it can be done from hub.

using IdentityCore.DbLayer.Entity;
using IdentityCore.Interface;
using IdentityCore.Services.Contracts;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.SignalR;
using System.Collections.Generic;
using System.Diagnostics;

namespace IdentityCore.Hubs
{
    [Authorize]
    public class ChatHub : Hub<IChatHub>
    {
        private static List<string> online_users = new List<string>();
        private readonly IUserContract _userContract;

        public ChatHub(IUserContract userContract)
        {           
             this._userContract = userContract;
        }


        public override async Task<Task> OnConnectedAsync()
        {
            string name = Context.User.Identity.Name;
            if (!online_users.Contains(name))
            {
                online_users.Add(name);
            }

            Groups.AddToGroupAsync(Context.ConnectionId, name);
            await fetchPendingMessages();
            return base.OnConnectedAsync();
        }
        public async Task fetchPendingMessages()
        {

            List<DbLayer.Entity.TempMessageStoreEntity> messages = await _userContract.GetTempMessages(Context.User.Identity.Name);
            foreach (DbLayer.Entity.TempMessageStoreEntity message in messages)
            {
                SendMessageAsync(message.RecieverId, message.SenderId, message.SenderUsername, message.Message);
            }
        }

        public override Task OnDisconnectedAsync(Exception? exception)
        {
            online_users.Remove(Context.User.Identity.Name);
            Groups.RemoveFromGroupAsync(Context.ConnectionId, Context.User.Identity.Name);
            return base.OnDisconnectedAsync(exception);
        }

        public void Greetings()
        {

            Clients.Caller.DisplayGreeting("You are now connected with connectionId:- " + Context.ConnectionId);
        }

        public async Task SendMessageAsync(string recieverId, string senderId, string senderUsername, string message)
        {
            bool check = await _userContract.CheckIfUserExists(recieverId);
            bool check1 = await _userContract.CheckIfUserExists(senderId);
            bool check3 = online_users.Contains(recieverId);
            if (check && check1 && !check3)
            {
                await _userContract.AddTempMessage(recieverId, senderId, senderUsername, message);
            }
            if (check3)
            {
               **//This is exception area.**
                await Clients.Group(recieverId).RecievedMessage(senderId, senderUsername, message);
            }


        }

    }
}

Lets start with what this code does or suppose to do

It is supposed to send messages to other online or offline users. It is working for online to online users.No errors. but for offline users it stores data in database which is working fine and retrive data from server which is also working fine but it is throwing error on

 if (check3)
            {
               **//This is exception area.**
                await **Clients**.Group(recieverId).RecievedMessage(senderId, senderUsername, message);
            }

Upvotes: 1

Views: 101

Answers (0)

Related Questions