Wcf integer serialization error

I was once caught by an Ajax call to wcf service error like that :
ExceptionDetail:
HelpLink:null,InnerException:null,
Message:Input string was not in a correct format.
StackTrace: at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)\u000d\u000a at System.Number.ParseInt32(…)

And my Ajax call is http get with url like:
http://myurl/mywebservice.svc/MyMethod?_dc=1284453444291&id=2377& anotherid=&comments=

Wcf is using json data format as such

[ServiceContract]
public class mywebservice
{
[OperationContract]
   [WebInvoke(Method = "GET", 
BodyStyle = WebMessageBodyStyle.WrappedRequest, ResponseFormat = WebMessageFormat.Json)]
  public void MyMethod (int id, int anotherid,string comments)
    {
	// ...
   }
}

So you can see here I passed 4 parameters back to my wcf
1. _dc this is timestamp field placed by extjs framework
2. Id is an integer parameter in the interface of MyMethod, value is 2377
3. Anotherid is another integer parameter in the interface of MyMethod, with no value
4. Comments is a string parameter, with no value
The problem is ‘anotherid’ declared as an integer field, but no value is passed in, and wcf json deserialization failed to convert an empty string to an integer.

In my blog part 2: wcf communication, I said that it is safe to use string, integer in your wcf interface, and not safe for Boolean field or datetime field, because these fields have dubious string representations, therefore serialization and deserialization is troublesome. So obvious integer field in wcf is not safe as well, because it does not cover null value, you would have thought why not using int? to cover this scenario?, the fact is wcf json serializer does not support int? type as well.

I solved the problem by using string type of ‘anotherid’, before manually converting it into int, does a check of string.IsNullOrEmpty (anotherid)

The conclusion is : if you are sure that integer field always has a valid value from client, you can use integer field in wcf interface, otherwise do not use int type for that field, string field is always a safe bet.

Attached is my wcf trace log, which is associated with this error, but the exception message is totally confusing, if you are having this error message, try my scenario to see if it works.

System.Runtime.Serialization.SerializationException,
mscorlib, Version=2.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089

Expecting
element ‘root’ from namespace ”.. Encountered ‘None’ with name ”,
namespace ”.
at
System.Runtime.Serialization.Json.
DataContractJsonSerializer.InternalReadObject(XmlReaderDelegator
xmlReader, Boolean verifyObjectName)…

Any one here who can understand this message?

Tags:

This entry was posted on Thursday, September 16th, 2010 at 3:40 am and is filed under ASP.NET. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Leave a Reply

*