1

[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);
            }
  • Is `Clients` a class with a static member `Group`? Could you show where Clients is instantiated. and where it is possible disposed – Roe Jan 19 '23 at 12:28
  • No. It comes with SignalR – Smarpan Sharma Jan 19 '23 at 12:30
  • have a look at [this](https://stackoverflow.com/questions/50707223/asp-net-core-signalr-clients-object-disposed-exception-when-calling-from-a-callb) post. This might be applicable here. – Roe Jan 19 '23 at 12:39
  • I tried both methods but it doesn't seems to be working – Smarpan Sharma Jan 19 '23 at 12:52
  • The await clients does not look like this from the documentation, possibly the problem - await Clients.Group(groupName).SendAsync("Send", $"{Context.ConnectionId} has left the group {groupName}."); – Frank M Jan 19 '23 at 13:13
  • 1
    You will get an ObjectDisposedException if you try to use the Hub after it's been disposed. I'm guessing you're either added `ChatHub` to DI (which you shouldn't do), or you're calling methods on the Hub manually, or you're invoking the method from a Task that can run after the Hub is disposed. – Brennan Jan 21 '23 at 05:37
  • i was doing that. I guess that was the problem – Smarpan Sharma Jan 21 '23 at 09:23

0 Answers0