首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >SignalR InvalidDataException C#

SignalR InvalidDataException C#
EN

Stack Overflow用户
提问于 2021-04-07 18:08:04
回答 1查看 607关注 0票数 1

我正在使用WPF上的客户端和SignalR上的服务器部分来制作一个信使。问题是,我不完全理解为什么ChatHub类中的方法: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."

服务器端:

枢纽级:

代码语言:javascript
运行
复制
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("Успешно добавлен в группу!");
        }
    } 

客户端:

代码语言:javascript
运行
复制
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);
        }
    }

消息模型

代码语言:javascript
运行
复制
   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;
        }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-04-07 18:13:09

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

代码语言:javascript
运行
复制
 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;
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66991782

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档