signalR There was an error invoking Hub method XXX

参考:https://www.cnblogs.com/yaopengfei/p/9304308.html

           https://blog.csdn.net/weichaoya/article/details/88079271

1、后端代码

public void SendSingleMsg(string receiveId, string msg)
{
	Clients.Client(receiveId).receiveMsg($"用户【{this.Context.ConnectionId}】发来消息:{msg}");
}

2、前端调用

proxy.invoke('sendSingleMsg', '123', '发送消息'); 
// 下面这句报错:There was an error invoking Hub method XXX
proxy.invoke('sendSingleMsg', { receiveid: '123', msg: '发送消息9999' }); 

可以改为下面的代码,则可以使用{ receiveid: '123', msg: '发送消息9999' }不报错。

public void SendSingleMsg(HubMessage obj)
{
	Clients.Client(obj.receiveId).receiveMsg($"用户【{this.Context.ConnectionId}】发来消息:{obj.msg}");
}

public class HubMessage
{
	public string receiveId { get; set; }
	public string msg { get; set; }
}

 

你可能感兴趣的:(C#开发)