RIA Services: How dates are handled and sent across the wire

The current(July) preview of RIA Services handles DateTime objects in an strange manner. Many of silverlight developers have complained in Silverlight forums that their dates are messed up. In this post, I am listing down several observations as a reference. Note that my current settings are +6 GMT.

From 	DateTime.Kind 	 DateTime.Value        To     DateTime.Kind  DateTime.Value
---- 	-------------   ----------------       --     -------------  ---------------
Client 	Unspecified     2009-01-01 00:00     Server 	Utc         2008-12-31 18:00
Server 	Unspecified     2009-01-01 00:00     Client 	Utc         2008-12-31 18:00
Client 	Local 	        2009-01-01 00:00     Server 	Utc         2008-12-31 18:00
Server 	Local 	        2009-01-01 00:00     Client 	Utc         2008-12-31 18:00
Client 	Utc 	        2009-01-01 00:00     Server 	Utc         2009-01-01 00:00
Server 	Utc 	        2009-01-01 00:00     Client 	Utc         2009-01-01 00:00

We can see that whatever Datetime we send, RIA service converts it to UTC when received at the other end. This could be bearable up to some extent but the worst thing is that RIA domain service treats Unspecified dates as Local and performs respective time zone conversion. Also, there is a UsesUtcDateTimes overridable in DomainService class but currently it does not seem to produce any effect.

Notice that there is no such conversion when using a WCF service, so whatever datetime value and kind we send from one side is exactlyreceived the same at the other side.

Lets hope the issues are fixed in the next version of RIA services.

My sample application that I used to test the behavior of RIA Domain Service and WCF Service can be downloaded from here. Remember to rename it to .zip for extraction.


4 Responses to “RIA Services: How dates are handled and sent across the wire”

  1. Andrej Says:

    Hello Mehroz,

    wow, I’m googling and struggling with this one a few hours now. The problem seems to be the RIA ignores UsesUtcDateTimes==true at all – it should effectively ignore any .Kind==unspecified etc., if one overrides the getter for UsesUtcDateTimes :-(..

    No solution so far, I’ll try to override some RIA framework class to workaround this. And as funniest fallback: one adds 4 hours here in Switzerland :-D. Very nice solution.

    Good luck,

  2. Andrej Says:

    Update: the UsesUtcDateTimes seems to convert the UTC server fetched DateTime because the .Kind==Unspecified. But – how to enforce .Kind=Utc in the Entity Framework, that’s the question..

    Any ideas? W/out having to write strange partial class helpers etc..

    Thanks, Andrej

  3. Syed Mehroz Alam Says:

    Hi Andrej,

    To enforce DateTime.Kind = Utc in Entity Framework, the easiest solution will be to extent the generated partial dataclasses and provide an implementation for your OnDateColumnChanged() method to assign a Utc kind to the incoming date. Something like: this._dateColumn = DateTime.SpecifyKind(this._dateColumn, DateTimeKind.Utc);

    Once you tag your datetime objects with Utc kind as described above, you are going to receive the same DateTime value at your Silverlight client.

  4. Workaround for Silverlight RIA Services DateTime discrepancy between server and client | Why Can't You Code? Says:

    […] there’s a little known bug “feature” in the latest RIA Services Beta for Silverlight that involves […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: