TimeSpan

Topics: Web Api
Sep 14, 2011 at 1:43 PM

I have a POX service, based on WebAPI whose request and response consists of fairly complex object graphs. I'm testing this with XML at the moment, and it works apart from one big exceptions - TimeSpan-s don't appear to be deserialised correctly. They rest of the object is deserialised, including lists, strings, doubles, custom classes, etc, but not TimeSpan - they are set to 00:00:00. This is the XML format I'm using to denote a TimeSpan:

<StartTime>00:00:00</StartTime>

Is there a known issue with TimeSpans? Is my format (above) correct?

Thanks in advance.

Sep 14, 2011 at 4:41 PM

To add, if I have the TimeSpan as an argument of the method;

void ProcessTime(TimeSpan time) { ... }

It works, but if the argument is an object that contains a TimeSpan, it defaults to 00:00.

void ProcessTime(ObjWithTimeSpanProperty obj) { ... }

Sep 20, 2011 at 10:42 AM

This is still an issue in the latest version (WebAPI 5)

Can someone at least tell me if this is the expected behaviour, or suggest a workaround or fix?

Sep 20, 2011 at 12:26 PM

Unfortunately this isn't an issue with Web API, this is a problem with the XML Serializer.  See http://kennethxu.blogspot.com/2008/09/xmlserializer-doesn-serialize-timespan.html

If you search for timespan xml serializer you will see many people complaining about the same thing http://www.bing.com/search?q=timespan+xml+serializer

Sep 20, 2011 at 1:00 PM

This is still an issue with DataContractSerializer? I assumed that is what WebAPI uses behind the scenes?

Sep 20, 2011 at 1:47 PM

Web API uses XmlSerializer by default.  There is a sample here http://webapicontrib.codeplex.com/SourceControl/changeset/view/982e23432fd8#Samples%2fDataContractExample%2fProgram.cs that shows how to use the DataContractSerializer instead.  I don't know if this problem with TimeSpan exists in the datacontract serializer also.

Coordinator
Sep 20, 2011 at 5:00 PM
Actually in web api preview 5 you can enable dcs without needing the contrib stuff. There is a property on the formatter collection you can set to enable dcs.

Darrel, the stuff in contrib should be pulled. We should prbaby add the dcs sample I contributed to our samples.

Sent from my Windows Phone

From: DarrelMiller
Sent: 9/20/2011 6:47 AM
To: Glenn Block
Subject: Re: TimeSpan [wcf:272557]

From: DarrelMiller

Web API uses XmlSerializer by default. There is a sample here http://webapicontrib.codeplex.com/SourceControl/changeset/view/982e23432fd8#Samples%2fDataContractExample%2fProgram.cs that shows how to use the DataContractSerializer instead. I don't know if this problem with TimeSpan exists in the datacontract serializer also.

Sep 20, 2011 at 7:18 PM

Hey Glenn,

I'll pull the datacontractserializer example from WebApiContrib as I can't get it working with the new Formatters.XmlFormatter.UseDataContractSerializer property.

Darrel

Sep 21, 2011 at 11:00 PM

The issue with XmlSerializer and TimeSpan is that TimeSpan struct only has private fields and get-only properties which XmlSerializer doesn’t serialize so none of its members get serialized. The only workaround is to have a wrapper type.

DataContractSerialixer understands [Serializable] so that works fine.

What’ the problem you have with Formatters.XmlFormatter.UseDataContractSerializer?

Henrik

From: DarrelMiller [email removed]
Sent: Tuesday, September 20, 2011 12:19 PM
To: Henrik Frystyk Nielsen
Subject: Re: TimeSpan [wcf:272557]

From: DarrelMiller

Hey Glenn,

I'll pull the datacontractserializer example from WebApiContrib as I can't get it working with the new Formatters.XmlFormatter.UseDataContractSerializer property.

Darrel