SignalR(asp.net 核心)不发送复杂 class 对象

SignalR (asp.net core) not sending Complex class object

当我尝试从客户端发送一个对象以在复杂 class.

中表示时,SignalR 出于某种原因失败了

下面的客户端代码工作正常...

connection.send("ABC", "111", "222", { text: "text" }).then(() => console.log("sent")).catch((r) => console.log(r))

...对于下面的服务器端代码

public async Task ABC(string a, string b, User c)
{
     await Clients.All.SendAsync(RECEIVE, a, b, c);
}

public class User
{
      public string Text { get; set; }
      public string Name { get; set; }
}

以下不成立....

connection.send("ABC", "111", "222", { text: "text", jsonOptions: [] }).then(() => console.log("sent")).catch((r) => console.log(r))

...对于下面的服务器端代码

public async Task ABC(string a, string b, Post c)
{
     await Clients.All.SendAsync(RECEIVE, a, b, c);
}

public class Post
    {
        public int Id { get; set; }

        [Required] [StringLength(450, ErrorMessage = "Sender id cannot be more than 450")]
        public string SenderId { get; set; }

        [Required] [StringLength(30, ErrorMessage = "Sender name cannot be more than 30")]
        public string SenderName { get; set; }

        [StringLength(300, ErrorMessage = "Senderpic length must not be more than 300")]
        public string SenderPic { get; set; }

        public PostLocation PostLocation { get; set; }

        public int? CourseId { get; set; }
        public int ClassId { get; set; }
        public int? ChatId { get; set; }
        public int? LectureId { get; set; }

        [Required] [StringLength(2000, ErrorMessage = "Post text cannot be more than 2000")]
        public string Text { get; set; }

        [Required]
        public DateTimeOffset TimeStamp { get; set; }

        [NotMapped]
        public int Upvotes { get; set; }

        [NotMapped]
        public ICollection<string> UpvoteIds { get; set; }

        [NotMapped]
        public ICollection<string> DownvoteIds { get; set; }

        [NotMapped]
        public ICollection<string> StarredIds { get; set; }

        public PostType Type { get; set; }

        public bool IsReply { get; set; }

        [ForeignKey(nameof(FullRepliedPost))]
        public int? RepliedId { get; set; }

        [NotMapped]
        public MiniPost RepliedPost { get; set; }

        [StringLength(300, ErrorMessage = "ProfilePicturePath length must not be more than 300")]
        public string AttachmentPath { get; set; }

        public AttachmentType AttachmentType { get; set; }

        public int AnsweredPostId { get; set; }
        public Choice CorrectAnswer { get; set; }

        public string JsonOptions { get; set; }

        [NotMapped]
        public ICollection<string> Options => (ICollection<string>)
            JsonConvert.DeserializeObject(JsonOptions, typeof(ICollection<string>));

        [NotMapped]
        public ICollection<string> ASelectionIds { get; set; }
        [NotMapped]
        public ICollection<string> BSelectionIds { get; set; }
        [NotMapped]
        public ICollection<string> CSelectionIds { get; set; }
        [NotMapped]
        public ICollection<string> DSelectionIds { get; set; }
        [NotMapped]
        public ICollection<string> ESelectionIds { get; set; }

        #region navigation properties
        [JsonIgnore]
        public Post FullRepliedPost { get; set; }
        [JsonIgnore]
        public ICollection<PostVote> PostVotes { get; set; }
        [JsonIgnore]
        public ICollection<PostStar> PostStars { get; set; }
        [JsonIgnore]
        public ICollection<MultiChoice> OptionSelections { get; set; }
        [JsonIgnore] [ForeignKey(nameof(RepliedId))]
        public ICollection<Post> Replies { get; set; }
        #endregion
    }

我使用上面的 Post class 作为业务对象,这就是它具有所有这些属性的原因。我想知道这是否与失败有关。我在 Chrome 开发工具

中收到以下错误
[2020-11-07T20:19:42.768Z] Information: Connection disconnected.

当我再次尝试发送时,我收到一条错误消息,提示当 signalR 断开连接时我无法发送消息。

编辑:我在此处粘贴的代码中省略了一些内容 (jsonOptions: []),结果证明这是问题的实际原因。

原来问题完全是我的错。实体 class 中的 JsonOptions 属性 应该是一个字符串,但我从客户端发送了一个数组。用 jsonOptions: "[]" 替换 jsonOptions: [] 修复了它并且序列化很好。

无论如何,我决定只发送 Post 对象的 ID 而不是整个对象。我认为这样会更有效率。