Flex, DateTimes, .NET….. Why can’t we all just get along?

Recently I’ve been working on a project that has a very interesting architecture. It’s a scheduling application that has a Flex front end, C# .NET Middle tier, nHibernate DAL and Oracle database.  We’re using SOAP to transfer data back and forth to the Flex client…..

[Rant]

I’m well aware of the other options that this client could have used to pass data back and forth. Frankly, I would have been much happier with anything other than SOAP.  But alas, this is the decision the client made before I was on the project. 

<3 AMF

[/Rant] 

….. so there I was, writing a dashboard which polls to see if the data has been updated. The idea is to request data from the server, store the servers timestamp on the client, and then check for updates by sending the timestamp back to the server.

Using C#, the web method is very simple:

[WebMethod]
public StatusResponse IsUpdated(DateTime timestamp)
{
	StatusResponse response;
 
	// Query or do what you gotta to give a valid response based on timestamp
 
	response = new StatusResponse(DateTime.Now, false);
	return response;
}

In Flex I set up a test application which allows me to send requests to the server. I should mention, the client and the server are running on the same machine, which is why I am comparing local client time to server time. This is my result handler

protected function onStatusResponse(event:ResultEvent):void
{
	_lastTimestamp = event.result.Timestamp as Date;
	var now:Date = new Date();
	var diff:int = now.time - _lastTimestamp.time;
	trace("ClientTime: "+now);
	trace("ServerTime: "+event.result.Timestamp);
	trace('Difference (ms): '+diff);
}

As I’m watching my data being outputted, I was a little disturbed with the results.

– Requesting Server Time –
ClientTime: Sat Dec 6 09:09:05 GMT-0800 2008
ServerTime: Sat Dec 6 09:23:32 GMT-0800 2008
Difference (ms): -866850
– Requesting Server Time –
ClientTime: Sat Dec 6 09:09:25 GMT-0800 2008
ServerTime: Sat Dec 6 09:32:00 GMT-0800 2008
Difference (ms): -1355072
– Requesting Server Time –
ClientTime: Sat Dec 6 09:09:25 GMT-0800 2008
ServerTime: Sat Dec 6 09:10:02 GMT-0800 2008
Difference (ms): -37070

You’ll notice that the ServerTime is getting pushed forward by some random amount of time. This is a big problem! If I am polling for updates every 30 seconds, and the last timestamp I received was 10 minutes in the future, the query will always return a status of false.

So what’s going on here? Why does Flex think that my Date is actually in the future when it’s retrieved from the web services?

Naturally, I started by looking at the web service response:

 
2008-12-06T08:49:02.2292832-08:00
false

A status of false simply means that the data has not been updated. Let’s look closely at the timestamp.

2008-12-06T08:49:02.2292832-08:00

According to the w3.org xml schema, everything looks good with that string. But one thing catches my eye: ‘.’ s+ (if present) represents the fractional seconds.

With a bit of research, I quickly learned there is a big difference between milliseconds (0-999) and fractional seconds. The schema doesn’t dictate what precision the milliseconds need to be in. So the C# webservice is using a precision of up to 7 digits. This seemed highly suspect to me, but the string is technically correct.

So now I needed to dive into Flex, and figure out where the data was being corrupted.

** fast forward a week **

After quite a bit of investigating, I figured it out.

frameworks/projects/rpc/src/mx/rpc/xml/SchemaMarshaller.as

Within the SchemaMarshaller, the milliseconds are being treated as just that… milliseconds. So when C# returned fractional seconds with a precision of 7 places (2292832), the marshaller was simply just appending 2,292,832 milliseconds to my date.

I submitted a patch which solved the issue, and while I was writing this post, I noticed that the fix has already been committed into the the Flex 4 repository.

Thanks Adobe, you guys rock!

Related Posts

Comments Closed