尝试致电Clients.Caller,但无法正常工作

时间:2018-09-20 10:58:44

标签: c# asp.net-mvc signalr chat signalr-hub

你好,我正在尝试从数据库中捕获消息并将其传输给调用方,但是当我使用Clients.Caller方法时,它向我传递了类似的错误

  

不支持使用不是由HubPipeline创建的Hub实例

如果我正在使用

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

然后它什么也没回答。所以请帮助我解决这个问题。

我正在使用以下代码:

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);
        }
 }

并且类的构造函数如下:

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

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

和Controller构造函数是这样的:

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

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

0 个答案:

没有答案