WCF 4 JSON REST Service

For the last several months I have been lucky enough to work on several projects that have exposed me to the jQuery platform. I have used jQuery with ASP .NET MVC as well as with Phonegap, lately I have been making use of the AJAX calls within a distributed application. While developing the JSON based REST Services I have learned that Fiddler can be your best friend when debugging services. In the following examples I will show a WCF Rest Service and how to call it from jQuery, Fiddler and ASP.NET MVC.

Developing a WCF JSON Rest Service

In order to to develop a WCF 4 REST Service the following instructions should be followed to download and install the template. Once the template is installed use it to create a rest service. The following code is a test service that I created. There are two methods in the class a GET Method and a POST Method. The GET method returns the current date and time. The POST method takes the day of the year and returns the date of that day in the current year. The following services make use of the JSON format. 

[ServiceContract]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
public class DateTimeService
{
        [WebGet(UriTemplate = "", ResponseFormat = WebMessageFormat.Json)]
        public ServiceResponse GetCurrentDate()
        {
            return new ServiceResponse { Success = true, Date = DateTime.Now.ToString() };
        }
 
        [WebInvoke(UriTemplate = "", Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
        public ServiceResponse GetDateOfSpecificDay(ServiceRequest request)
        {
            var response = new ServiceResponse { Date = new DateTime(DateTime.Now.Year, 1, 1).AddDays(request.DayOfYear).ToString() };
            response.Success = true;
 
            return response;
        }
}

The service class makes use of two PONOs. The ServiceResponse and ServiceRequest objects allow data to be passed in and out of the services. Although these classes are very simple it is a good idea to use PONOS when working with WCF Rest Services. The PONOs allow the services to make use of the JSON parser and the framework inputs and outputs the JSON based on those objects. The code for those objects are below.

public class ServiceRequest
{
    public int DayOfYear { get; set; }
}
 
public class ServiceResponse
{
    public bool Success { get; set; }
    public string Date { get; set; }
}

Once the service code is complete one last thing has to be done to make the services accessible. A complete web.config file is no longer required to host the service, as long as you are rolling with default configurations there is no need to specify an end point as in previous versions of WCF. The only specification needed is in the Global.asax file. Within that file we specify how the service will be routed.

RouteTable.Routes.Add(new ServiceRoute("Dates", new WebServiceHostFactory(), typeof(DateTimeService)));

Now we can access this at the virtual directory /dates/. I am using the local development server. When I go to the address http://localhost:1033/Dates/ in the browser I get the current date and time.

 

WCF JSON Dialog box

In order to see the actual response we are going to use Fiddler to call the service. In the image below you will see that we are using the Request Builder feature of Fiddler. We set the url to the instance of our Date service and then hit Execute. You will then see on the left the call to the Data Service.

 

WCF JSON Dialog box

Click on the service call and then click Text View in the menu on the right. Here we will see the JSON formatted response of the GET service.

 

WCF JSON Dialog box

Now we will test the POST service. In the Request Builder select POST instead of GET and add the following parameters to the Request Headers: User-Agent: Fiddler Host: localhost:1033 Content-Type: application/json Content-Length: 17 We then added the formatted JSON that matches our ServiceRequest PONO and added it to the Request Body:
{“DayOfYear”:100}

 

WCF JSON Dialog box

We can then see the successful return of the response.

 

WCF JSON Dialog box

We now have a successful WCF Rest Service that we have tested through Fiddler. In the Part 2 and Part 3 of this series of blogs we will explore consuming the service in both ASP.NET MVC and jQuery.