覆盖的日期时间序列化ASP.NET参数的WebMethod [英] Override DateTime serialization for ASP.NET WebMethod parameters

查看:183
本文介绍了覆盖的日期时间序列化ASP.NET参数的WebMethod的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作在一个大code基在那里没有人关注本地时间与UTC时间清理的错误。

I am working on cleaning up a bug in a large code base where no one was paying attention to local time vs. UTC time.

我们想要的是一个全球忽略发送到从我们的ASP.NET Web服务datetime对象时区信息的一种方式。我已经得到了检索操作的解决方案。数据只在数据集回来,我可以寻找日期时间列和DateTimeMode设置为未指定。这解决我的问题的来回传递数据集内的所有数据。

What we want is a way of globally ignoring time zone information on DateTime objects sent to and from our ASP.NET web services. I've got a solution for retrieve operations. Data is only returned in datasets, and I can look for DateTime columns and set the DateTimeMode to Unspecified. That solves my problem for all data passed back and forth inside a data set.

不过datetime对象也常常直接作为参数传递到Web方法。我想去掉所有传入的时区信息。而不是通过我们的客户code搜索和使用DateTime.SpecifyKind(..)来设置所有的DateTime瓦尔为undefined,我愿意做一些全球ASP.NET覆盖的监控传入的参数并去掉时间区信息。

However DateTime objects are also often passed directly as parameters to the web methods. I'd like to strip off any incoming time zone information. Rather than searching through our client code and using DateTime.SpecifyKind(..) to set all DateTime vars to Undefined, I'd like to do some sort of global ASP.NET override to monitor incoming parameters and strip out the time zone information.

这种事可能吗?还是有另一种更简单的方式做我想做的事?

Is such a thing possible? Or is there another easier way to do what I want to do?

只是重申 - 我不关心时区,每个人都在同一个时区。但一对夫妇的用户已经严重的机器配置,错时区等,所以当他们在2008年7月1日,发送,我收到2008年6月30日22:00:00在服务器端它会自动从它转换成他们本地时间到服务器的本地时间。

Just to reiterate -- I don't care about time zones, everyone is in the same time zone. But a couple of users have machines badly configured, wrong time zones, etc. So when they send in July 1, 2008, I'm getting June 30, 2008 22:00:00 on the server side where it's automatically converting it from their local time to the server's local time.

更新:另外一个可能性是,如果它是可能使客户端上的.NET code的变化来改变的方式datetime对象一种不确定是连载

Update: One other possibility would be if it were possible to make a change on the client side .NET code to alter the way DateTime objects with Kind 'Undefined' are serialized.

推荐答案

我已经在许多应用程序,服务和在不同的平台(.NET,Java等)处理了这个频繁。请相信我,你不想pretending,你不关心的时区的长期后果。追逐很多错误,是极其困难和昂贵的修复后,你会希望你照顾。

I have dealt with this often in many applications, services, and on different platforms (.NET, Java, etc.). Please believe me that you do NOT want the long term consequences of pretending that you don't care about the time zone. After chasing lots of errors that are enormously difficult and expensive to fix, you will wish you had cared.

因此​​,而不是剥离时区,您应该抓住正确的时区或强制特定的时区。如果你能合理,得到固定提供了正确的时区不同的数据源。如果他们是在你的控制,然后迫使他们要么到服务器的本地时区或UTC。

So, rather than stripping the time zone, you should either capture the correct time zone or force a specific time zone. If you reasonably can, get the various data sources fixed to provide a correct time zone. If they are out of your control, then force them either to the server's local time zone or to UTC.

一般行业惯例是强制所有内容UTC,并设置所有生产的硬件时钟为UTC(这意味着服务器,网络设备,如路由器等)。那么你应该从UI用户的本地时区转换成/。

The general industry convention is to force everything to UTC, and to set all production hardware clocks to UTC (that means servers, network devices like routers, etc.). Then you should translate to/from the user's local time zone in the UI.

如果您现在可以正确地解决它,它可以很容易和便宜。如果有意进一步打破它,因为你认为会更便宜,那么你将没有任何借口后,当你要解开烂摊子。

If you fix it correctly now, it can be easy and cheap. If you intentionally break it further because you think that will be cheaper, then you will have no excuses later when you have to untangle the awful mess.

请注意,这是类似于用绳子常见的问题:有没有这样的东西作为纯文本(字符串缺乏一个字符编码),而且作为一个普通的(无时区),时间/日期没有这样的事情。 pretending否则是太多的痛苦和心痛,而尴尬的错误源。

Note that this is similar to the common issue with Strings: there is not such thing as plain text (a String devoid of a character encoding) and there is no such thing as a plain (no time zone) time/date. Pretending otherwise is the source of much pain and heartache, and embarrassing errors.

这篇关于覆盖的日期时间序列化ASP.NET参数的WebMethod的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆