WCF Web API DateTimeOffset 问题
我正在使用 WCF Web API Preview 6 及其内置测试客户端来通过 Id 请求资源。该对象返回其所有数据,但“CreateDate”和“LastModifiedDate”属性除外,这两个属性的类型为 DateTimeOffset
并且为空。我尝试通过调用 DateTimeOffset.UtcNow
和 DateTimeOffset.Now
手动设置值,尽管这些值是在对象上设置的,但它们永远不会到达对象中的另一端回复。
我还通过将属性类型更改为 DateTime 并使用 DateTime.Now 手动设置其值来对此进行了测试,一切正常,我得到了看起来像 DateTimeOffset 值。
<CreateDate>2011-12-13T16:15:47.4269538+00:00</CreateDate>
<LastModifiedDate>2011-12-13T16:15:47.4269538+00:00</LastModifiedDate>
有谁知道在预览版 6 中使用 DateTimeOffset
类型是否存在问题,或者是我做错了什么?我在使用 oData 过滤字段时遇到了类似的问题(请参阅SO问题 )
我还向 WCF Web API codeplex 网站提交了关于过滤的此错误报告 问题。然而,这已经是两个多星期前的事了,还没有任何回应。
经过几个小时的测试和调试后,我已经没有这个选项了!因此,如果有人能为我提供一些有用的提示,我将不胜感激。
I am using WCF Web API Preview 6 with its inbuilt Test Client to request a resource by Id. The object returns with all its data except for the ‘CreateDate’ and ‘LastModifiedDate’ properties which are of type DateTimeOffset
and are empty. I have tried setting the values manually by calling DateTimeOffset.UtcNow
and DateTimeOffset.Now
and although the values are set on the object they never make it through to the other end in the response.
I have also tested this by changing my property types to DateTime
and manually setting their values by using DateTime.Now
and everything works fine and I get what looks like a DateTimeOffset value.
<CreateDate>2011-12-13T16:15:47.4269538+00:00</CreateDate>
<LastModifiedDate>2011-12-13T16:15:47.4269538+00:00</LastModifiedDate>
Does anybody know if there is a problem with the use of the DateTimeOffset
type in Preview 6 or is it something I am doing wrong? I have had a similar problem with filtering the fields using oData (see SO question)
I have also submitted this bug report to the WCF Web API codeplex site on the filtering issue. However that was over two weeks ago and haven’t had any response.
After many hours of testing and debugging I am running out of options on this one! So if anybody out there can provide me with some helpful hints it would be much appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这不是 Web API 问题,而是 Microsoft 序列化问题。 XmlSerializer 不处理 DateTimeOffset。我相信 TimeSpan 也有同样的问题。
只需在您的对象上实现 IXMLSerializable 并自行处理即可。
请参阅此处如何 XML 序列化 DateTimeOffset 属性?
It's not a Web API issue, it's Microsoft serialization issue. The XmlSerializer does not handle DateTimeOffset. I believe it has the same issue with TimeSpan.
Just implement IXMLSerializable on your object and handle it yourself.
See here How can I XML Serialize a DateTimeOffset Property?