WCF 结果反序列化为键/值对列表中值类型的默认值
我有一个 WCF 服务,结果是一个自定义 TimeSeries 类,定义为:
[DataContract]
public class TimeSeries
{
[DataMember]
public string Name { get; set; }
[DataMember]
public List<KeyValuePair<DateTime, double>> Data { get; set; }
}
我的服务方法创建要返回的这些对象的数组。调试服务方法,我可以看到包含这些对象之一的数组已正确创建(它有一个名称和 37 个 vk 对数据)。使用 Fiddler,我可以看到该对象正在被序列化并发送(HTTP 响应中的数据仍然正确)。然而,当我在客户端检查结果对象并且它不正确时,问题就出现了。具体来说,我得到了一个具有正确名称的 TimeSeries 对象,以及正确的 kv 对数量,但它们包含每个 DateTime 和 double 的默认值(即 01/01/0001 12:00AM 和;0.0)。
我的客户端是 Silverlight v4,我正在使用自动生成的服务引用。该问题似乎与反序列化有关。任何人都知道为什么要这样做,我缺少什么,或者我如何解决它?
I have a WCF service and the result is a custom TimeSeries class defined as:
[DataContract]
public class TimeSeries
{
[DataMember]
public string Name { get; set; }
[DataMember]
public List<KeyValuePair<DateTime, double>> Data { get; set; }
}
My service method creates an array of these objects to return. Debugging the service method, I can see that an array containing one of these objects is created correctly (it has a name and 37 vk pairs of data). Using Fiddler, I can see that the object is being serialized and sent (the data is still correct in the HTTP response). However the problem comes when on the client I check the result object and it is incorrect. Specifically, I get a TimeSeries object with the correct name, and the correct number of of kv pairs, but they contain the default values for each DateTime and double (ie 01/01/0001 12:00AM & 0.0).
My client is Silverlight v4 and I am using an automagically generated service reference. The problem appears to be related to deserialization. Anyone have any thoughts as to why it is doing this, what I am missing, or how I can fix it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如 将键/值对列表序列化为中所述XML:
KeyValuePair 不可序列化,因为它具有只读属性
因此您需要自己的类,就像该页面上的答案所示。
As it is stated in Serializing a list of Key/Value pairs to XML:
KeyValuePair is not serializable, because it has read-only properties
So you need your own class, just like the answer on that page says.
不使用您自己的类的另一种选择是使用
Dictionary
,这似乎可以很好地序列化和反序列化。An alternative rather than using your own class is to use a
Dictionary<DateTime,double>
instead which seems to serialize and deserialize fine.