WCF JSON Serialization error with DateTime.MinVal and UTC
2014-04-01 13:45
453 查看
I came across the following error today in is WCF JSON web service:
SerializationException: DateTime values that are greater than DateTime.MaxValue or smaller than DateTime.MinValue when converted to UTC cannot be serialized to JSON.
The solution took me a while to get my head round, so I thought I should share it. The clue was a StackOverflow post specifying
the error and its cause.
This was compounded by the fact that the WCF service was returning a rather obscure 504 error, namely:
ReadResponse() failed: The server did not return a response for this request.
The error was not being picked up and returned as a service fault, so I switched WCF tracing on. The error message was then visible in the logs:
The bottom line is that if you have a DateTime property that is not set, it will be defaulted to DateTime.MinVal when serialized. However, DateTime.MinVal does not have a DateTimeKind specified,
which is the default.
This causes a problem with the serializer because it does not treat the MinVal as UTC. If you are in a timezone + GMT (East), then this is going to cause you a problem,
and you'll get the error above. This is very nicely described by Adam Robinson:
If your time zone is GMT+1, then the UTC value of DateTime.MinValue in your time zone is going to be an hour less than DateTime.MinValue.
In my case I had a bunch of DTO objects being passed back as JSON. The serializer would hit the defaulted DateTime value and error. the solution was simple:
The alternative is to stop DateTimes being left empty. In my case an Automapper misconfiguration
on the developer's part. If you have the potential for empty DateTime properties, then maybe consider making them nullable.
http://benpowell.soup.io/post/156011793/WCF-JSON-Serialization-error-with-DateTime-MinVal
SerializationException: DateTime values that are greater than DateTime.MaxValue or smaller than DateTime.MinValue when converted to UTC cannot be serialized to JSON.
The solution took me a while to get my head round, so I thought I should share it. The clue was a StackOverflow post specifying
the error and its cause.
This was compounded by the fact that the WCF service was returning a rather obscure 504 error, namely:
ReadResponse() failed: The server did not return a response for this request.
The error was not being picked up and returned as a service fault, so I switched WCF tracing on. The error message was then visible in the logs:
<Exception> <ExceptionType>System.Runtime.Serialization.SerializationException, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</ExceptionType> <Message>DateTime values that are greater than DateTime.MaxValue or smaller than DateTime.MinValue when converted to UTC cannot be serialized to JSON.</Message>
The bottom line is that if you have a DateTime property that is not set, it will be defaulted to DateTime.MinVal when serialized. However, DateTime.MinVal does not have a DateTimeKind specified,
which is the default.
This causes a problem with the serializer because it does not treat the MinVal as UTC. If you are in a timezone + GMT (East), then this is going to cause you a problem,
and you'll get the error above. This is very nicely described by Adam Robinson:
If your time zone is GMT+1, then the UTC value of DateTime.MinValue in your time zone is going to be an hour less than DateTime.MinValue.
In my case I had a bunch of DTO objects being passed back as JSON. The serializer would hit the defaulted DateTime value and error. the solution was simple:
foreach (var dto in dtos) { dto.DateStart = DateTime.SpecifyKind(dto.DateStart, DateTimeKind.Utc); }
The alternative is to stop DateTimes being left empty. In my case an Automapper misconfiguration
on the developer's part. If you have the potential for empty DateTime properties, then maybe consider making them nullable.
http://benpowell.soup.io/post/156011793/WCF-JSON-Serialization-error-with-DateTime-MinVal
相关文章推荐
- Backbone.js入门学习笔记目录
- Backbone.js的技巧和模式
- JS禁用右键,禁用打印,防止另存为,IE浏览器识别
- 防止javascript 注入攻击 js过滤特殊字符
- 点击一下就能找到需要的js框架
- Underscore一个非常实用的JavaScript库
- 解析JSONArray为List对象
- backbone.js 初探实例 (推荐阅读)
- 【js】批量判断表单中的文本框非空
- extjs
- 菜鸟初学JSP
- js闭包~![[5]]
- JS中style.display和style.visibility之间的区别
- JavaScript什么的最有爱了
- JavaScript对象的property属性详解
- javascript事件处理程序的3个阶段
- 可笑,竟然不知道javascript的出处。
- js 面向对象式编程
- Extjs中numberfield小数位数设置
- js自运行函数