2

Hello I am trying to catch messages from database and transfer to caller but when I used Clients.Caller method then it pass me error like

Using a Hub instance not created by the HubPipeline is unsupported

If I am using

var context = GlobalHost.ConnectionManager.GetHubContext<ChatHub>();
context.Clients.All.receiver(respose);

then it replying nothing. So please help me to solve out this question.

and I am using below code:

public async Task GetAllMessages(int roomid, int fromuserId, int touserId, int index, int pageSize, int broadcastid = 0)
{
        ResponseModel respose = new ResponseModel();

        try
        {
            var model = new MessageDetailModel
            {
                Index = index,
                PageSize = pageSize,
                FromUserId = fromuserId,
                ToUserId = touserId,
                ChatRoomId = roomid,
                BroadCastId = broadcastid
            };

            MesaageModel totalmessages = new MesaageModel();
            List<RecentMessage> messages = new List<RecentMessage>();

            var usermessages = await _userService.GetAllMessages(model);
            messages.AddRange(usermessages);

            foreach (var message in messages)
            {
                model.BroadCastMessageId = model.BroadCastId == 0 ? 0 : message.BroadCastMessageId;
                model.ChatRoomMessageId = model.BroadCastId == 0 ? message.ChatRoomMessageId : 0;
                List<UserDetail> userofseenby = new List<UserDetail>();
                var users = await _userService.GetMessageSeenBy(model);
                userofseenby.AddRange(users);
                message.SeenBy = userofseenby;
            }

            //totalmessages.Messages.AddRange(messages);
            totalmessages.Messages = messages;

            totalmessages.UnseenMessageCount = await _userService.GetUnSeenCount(messages.FirstOrDefault().ChatRoomId, fromuserId);

            if (messages.FirstOrDefault().IsBroadCast == true)
                totalmessages.Users = (List<UserDetail>)await _userService.GetUsersByBroadCastId(messages.FirstOrDefault().BroadCastId);
            else if (messages.FirstOrDefault().IsGroup == true)
                totalmessages.Users = (List<UserDetail>)await _userService.GetUsersByChatRoomId((int)messages.FirstOrDefault().ChatRoomId);

            respose = ResponseHelper.SuccessResponse("onAllMessagesReceived", totalmessages);

            Clients.Caller.receiver(respose); // Error catch here
        }
        catch (Exception ex)
        {
            respose.ActionCommand = "onAllMessagesReceived";
            respose = ResponseHelper.ErrorResponse(respose.ActionCommand, ex.ToString());
            Clients.Caller.receiver(respose);
        }
 }

And Constructors of class is below :

 public ChatHub()
    {
        _userService = new UserBusinessLogic();
        _chatterService = new ChatterBusinessLogic();
    }

    public ChatHub(IChatterBusinessLogic chatterService, IUserBusinessLogic userService)
    {
        _chatterService = chatterService;
        _userService = userService;
    }

And Controller constructor are like this :

  public ChatterController()
    {
        _userService = new UserBusinessLogic();
        _chatterService = new ChatterBusinessLogic();
    }

    public ChatterController(IChatterBusinessLogic chatterService, IUserBusinessLogic userService)
    {
        _chatterService = chatterService;
        _userService = userService;
    }
Hardik Dhankecha
  • 148
  • 1
  • 11
  • Please share your java script code related to signalR – TanvirArjel Sep 20 '18 at 11:32
  • Actually I am using this code in API project. So it's handling by API calling. – Hardik Dhankecha Sep 20 '18 at 12:29
  • But my main problem is that every where it's throwing an error of `Object reference not set to an instance` at Clients.client or Clients.caller function. So should I change code in any file. Please help me. – Hardik Dhankecha Sep 20 '18 at 12:31
  • https://stackoverflow.com/a/12463990 – Hina Khuman Sep 21 '18 at 06:14
  • Check the constructor https://stackoverflow.com/a/34278325 – Hina Khuman Sep 21 '18 at 06:15
  • Thank you. But I am configuring HubContext manually where I should use. like `var context = GlobalHost.ConnectionManager.GetHubContext(); context.Clients.All.receiver(respose);` but it's not configuring caller in Clients or I can not use Clients.Caller when define HubContext. – Hardik Dhankecha Sep 21 '18 at 12:26
  • And If I will define HubContext manually and define Clients.All method then it will be work for some methods but it will send message to all users. However I want to send message to caller only. – Hardik Dhankecha Sep 21 '18 at 12:27

0 Answers0