Order cheap dedicated servers in USA.

Simple WCF and AJAX Integration

October 21st, 2008

For a long time I have used the ScriptService Attribute with classic ASMX web services to talk with the back end with JavaScript. It is how I turn a web client into an de facto smart client. But I ran into a problem where an object collection I was sending to the server just would not deserialize automatically like it has for me multiple times before. I decided now was the time to move on to WCF/AJAX since I can now freely use .NET 3.5.

I stayed with classic ASMX for a long time mostly because it worked with very little effort. The biggest trouble with WCF is that it gives you so many options which have to be set so precisely or nothing works. It is maddening and pile of documentation you have to go through to get the right answer is overwhelming. A recent video I discovered on Channel 9 showed how to meticulously make a WCF service AJAX friendly. It was a pretty extensive configuration but I found that I needed to enable web sessions (not wcf service sessions) and there was no simple way to do so, until I came across documentation that reminded me about service host factories.

Specifically, the WebScriptServiceHostFactory allows me to configure my service host to automatically be AJAX friendly. I do so by adding the Factory attribute to the service host declaration.

Using WebScriptServiceHostFactory allows me to strip all of the configuration details out of system.serviceModel in the web.config that I added to manually make it AJAX friendly. To enable back end support to use web sessions I had to set add a little to the web.config.

To enable web session support I added the AspNetCompatibilityRequirements Attribute and set it to be required.

Now the deserialization works perfectly and data is structured according to how I have set up the DataContract objects. Now I can get back to work and get this thing completed! :)

One Response to “Simple WCF and AJAX Integration”

  1. Brennan’s Blog » Blog Archive » Simple WCF and AJAX Deployed to IIS Says:

    [...] the previous post I simplified my WCF/AJAX integration. Then it came to deploying it to Windows Server 2003 and IIS6. [...]