I was writing some client code that calls some .NET Web Services and getting a strange error. I wanted to post this just to warn people to watch out for the DateTime object and null values. I have two web services, one gets an array of Entity objects and the other takes an array of Entity objects. The array is being serialized into JSON. The get web method worked fine but when I tried to pass the array to the save web method I got the following error:
System.InvalidOperationException: SaveUserFavorites Web Service method name is not valid.
at System.Web.Services.Protocols.HttpServerProtocol.Initialize()
at System.Web.Services.Protocols.ServerProtocolFactory.Create(Type type, HttpContext context, HttpRequest
request, HttpResponse response, Boolean& abortProcessing)
Now what sucks about this error is it is pretty generic. It pretty much means that the Ajax class cannot find a web service method that matches. So first I double checked my method names and parameters and no problem there. I'm going to fast forward through my debugging problems because this error took me awhile. The issue is one of my properties on my Entity object was a DateTime. It seems that when I was getting the JOSN array .NET serializes it wrong and then cannot read it. I really didn't need the property so I commented it out. But I did some additional research for this post and found that null is not a valid JSON value so the parser failed.
More info - http://west-wind.net/weblog/posts/398970.aspx
Tip: Firebug is critical in debugging ajax calls.