19 October 2009




With the introduction of Windows Communication Foundation in the .NET Framework 3.0, the responsiblity of building your own web service interface protocol has been lifted from the developer. DataContracts now provide a type-specific utility for easily sending and receiving data in either XML or Javascript Object Notation (JSON). In our iPhone Apps, we prefer to use JSON since it is both less verbose than XML (meaning less data to transfer on cellular networks) and easier to parse on the iPhone side.

Setting up a JSON WCF service requires only a few simple steps.

First, create a new WCF Service Library Project in your Visual Studio solution and name it WcfServiceLibrary1. For demonstration purposes, we’ll modify the sample code generated by Visual Studio.

There are main parts to the code that is generated:

  1. The service interface definition
  2. The service implementation
  3. The data object definitions

The IService1 interface contains all of the good stuff about WCF service. All service methods are decorated with the [OperationContract] attribute.

The Service1 class contains the business logic implementation of the IService1 interface.

The CompositeType class is the custom class returned by one of the IService1 service methods. This class uses the DataContractAttribute we mentioned before. All properties decorated with [DataMember] will be serialized (including properties marked private, so be careful) according to the serialization format you specify.

How do you specify the serialization format? Well that’s the interesting part.

Let’s create a new service method in IService1 like so:

CompositeType GetTestData();

Now add the attribute that specifies the output format that we want:

   BodyStyle = WebMessageBodyStyle.Bare,
   RequestFormat = WebMessageFormat.Json,
   ResponseFormat = WebMessageFormat.Json,
   UriTemplate = "test/random"
CompositeType GetTestData();

This requires that a reference be added to the project to System.ServiceModel.Web and add a using statement for System.ServiceModel.Web. You can read more about these attributes here, but this combination of attribute values will return our data in JSON format with no wrapper.

Next, implement the GetTestData method in Service1 like this:

public CompositeType GetTestData()
  return new CompositeType();

Now we can host the service library we’ve created in a standard ASP.NET web project. So create a new ASP.NET Web Application. Now add a reference to the WcfServiceLibrary1 project that we just created. Then create a new WCF service named Service1.svc. Right click on Service1.svc and choose “View Markup”. Now we can simply configure the service to create a new instance of the Service1 class using the WebServiceHostFactory. Below is all the markup that needs to be pasted in this file:

<%@ ServiceHost Language="C#" Debug="true" Service="WcfServiceLibrary1.Service1" Factory="System.ServiceModel.Activation.WebServiceHostFactory" %>

Now it’s time to see it all in motion. Set the web application as the startup project and run it. Append Service1.svc/test/random to the path in the address bar (e.g. http://localhost:52265/Service1.svc/test/random), press enter, and take a look at the sweet JSON file that comes out, just the way the iPhone likes it.

Download the full sample source code here.

Another hot tip: Download and install the JSONView add in for Firefox to see your JSON in the browser.

blog comments powered by Disqus