by Michael Hodgdon posted on February 6 2010 10:49
After writing my recent post for Consuming Cross Domain Services using jQuery, I started investigating how we could leverage the power of ajax with our flagship product for mobile solutions, MobilePipes.
The service layer for MobilePipes is written in the .NET platform using WCF. Right away I realized that we are missing support for JSON serialization. MobilePipes currently supports SOAP and POX formatting through WCF service configurations. Microsoft did a fantastic job with WCF with separating configuration from implementation so I knew that getting JSON formats in place should be a snap. One of the ways to achieve this is to define a different interface for the different services that we want to expose. SOAP formatting is a the standard Service Contract that by default is configured when you use the tools in Visual Studio.
Other formats get a bit tricky because you need to define certain attributes that instruct WCF how to behave at runtime. For instance, POX services are configured with the following configuration:

That's it! Simple as that. This configuration tells WCF to return a string from this method in the HTTP GET request. In other words, it skips SOAP formatting and just returns the string ... in the case of POX it's simply XML. Which gets me back to JSON. In order to get MobilePipes up to speed, we have created a new Service Interface with the JSON format. All we need to to is add the format that we want returned like so:

Now any clients that choose to use JSON rather than a traditional XML format can just point to the correct Service Endpoint. All of these endpoints are encapsulated in their own assembly and call out into the domain library. All of this allows us to keep our domain library for MobilePipes completely separate while providing SOAP, XML, and JSON return formats. With the recent serivce endpoint for JSON, any ajax libraries can now seamlessy call MobilePipes services.