SignalR InvalidDataException C#

SignalR InvalidDataException C#

我正在使用 WPF 上的客户端和 SignalR 上的服务器部分制作信使。问题是我不完全理解为什么 ChatHub class 中的方法调用: SendMessage s 不起作用,尽管 JoinChatGroup 起作用。

错误:Microsoft.AspNetCore.SignalR.HubException: "Failed to invoke 'SendMessages' due to an error on the server. InvalidDataException: Error binding arguments. Make sure that the types of the provided values match the types of the hub method being invoked."

服务器端:

中心 class:

public class ChatHub : Hub
    {
        public async Task SendMessages(Message message)
        {
            await Clients.Group(message.Chat_id.ToString()).SendAsync("ReceiveMessage", message);
            Console.WriteLine("Сообщение успешно отправлено!");
        }
        public async Task JoinChatGroup(string chatId)
        {
            await Groups.AddToGroupAsync(Context.ConnectionId, chatId);
            Console.WriteLine("Успешно добавлен в группу!");
        }
    } 

客户端:

public class SignalRService
    {
        private HubConnection hub = new HubConnectionBuilder()
            .WithUrl("http://localhost:5000/Chat")
            .Build();
        public event Action<Message> MessageReceived;
        public async void StartConnection()
        {
            await hub.StartAsync();
            hub.On<Message>("ReceiveMessage", (message) => MessageReceived.Invoke(message));
        }
        public async Task addToGroup(string chatId)
        {
            await hub.InvokeAsync<string>("JoinChatGroup", chatId);
        }
        public async Task SendMessage(Message message)
        {
            await hub.InvokeAsync<Message>("SendMessages", message);
        }
    }

消息模型

   public class Message
    {
        public int ID { get; set; }
        public int User_id { get; set; }
        public int Chat_id { get; set; }
        public int ContentType { get; set; }
        public byte[] Content { get; set; }
        public Message(int user_id, int chat_id, int contentType, byte[] content)
        {
            User_id = user_id;
            Chat_id = chat_id;
            ContentType = contentType;
            Content = content;
        }
        public Message(int iD, int user_id, int chat_id, int contentType, byte[] content)
        {
            ID = iD;
            User_id = user_id;
            Chat_id = chat_id;
            ContentType = contentType;
            Content = content;
        }

您的 Message class 需要一个空的构造函数来支持序列化:

 public class Message
 {
    // this constructor 
    public Message() 
    {
    }

    public int ID { get; set; }
    public int User_id { get; set; }
    public int Chat_id { get; set; }
    public int ContentType { get; set; }
    public byte[] Content { get; set; }
    public Message(int user_id, int chat_id, int contentType, byte[] content)
    {
        User_id = user_id;
        Chat_id = chat_id;
        ContentType = contentType;
        Content = content;
    }
    public Message(int iD, int user_id, int chat_id, int contentType, byte[] content)
    {
        ID = iD;
        User_id = user_id;
        Chat_id = chat_id;
        ContentType = contentType;
        Content = content;
    }
}