Saturday, April 7, 2007

Web Services with Prototype and ASP.NET Ajax

Here's a link to the handler: http://www.subtlereality.com/wsproxy.ashx.html

As I've mentioned before, I don't like using ASP.NET Ajax's client code. I like using web services because they're easy to develop, but I can't find a good SOAP library for Javascript. This makes calling web services difficult. After waiting for a while for someone to write one, I decided to take my own shot at it. My project already uses Prototype, so I decided to use those AJAX tools. It was a lot easier than I thought.

My approach was to figure out how ASP.NET Ajax called the web services and use that method. It clearly knows how to do it. I rolled up my sleeves and delved into the JavaScript library and found.. no SOAP envelope in the entire thing. I dug deeper and found that it sends POSTs with a content-type of "application/json" and the body is a JSON-serialized object. That was easy enough.

The trickiest part was the date serialization. Nikhil Kothari explains the issue in this blog post. Microsoft used "@1234@" in the Atlas betas but switched it to "\\/Date(1234)\\/" which is a lot more frustrating because you need to count slashes and try to figure out how often your string is being interpreted.

In the end, I came up with a HttpHandler that mimics the ".asmx/js" files that ASP.NET Ajax uses. You need to pass in the path to the web service and the .NET class name as arguments through the querystring. For example:


It doesn't deserialize DateTimes as the return value and you don't have as much control over it. If other people start using this, I'll start a real website for it. Feel free to ask me any questions about it.

No comments: