使用SignalR推送数据对象很浅
问题描述:
我已经使用SignalR将应用程序安装到了所有连接的客户端。到现在为止还挺好。 Message
对象包含复杂属性Priority
。使用SignalR推送数据对象很浅
public class Message {
public long Id { get; set; }
public string Contents { get; set; }
public Priority MessagePriority { get; set; }
}
public class Priority {
public int Id { get; set; }
public string Name { get; set; }
public string SortIndex { get; set; }
}
的Message
项目推送到客户端然而浅拷贝这意味着Priority
属性是undefined
但其它性质,Id
和Contents
,设置正确。 如何确保将深层副本推送到客户端?
答
该问题实际上是由于客户端无法映射某些属性,因为属性名已大写。通过明确设置属性的名称。
[Serializable]
public class Priority {
[JsonProperty(PropertyName = "id")]
public int Id { get; set; }
[JsonProperty(PropertyName = "name")]
public string Name { get; set; }
[JsonProperty(PropertyName = "sortindex")]
public int SortIndex { get; set; }
}
我不知道它是否对您的情况有帮助,但我一直使用JSON序列化通过SignalR发送复杂对象。 – rualmar