h1

WCF REST 4 anonymous output

December 26, 2010

Using RESTful service is definitely the way to go. It’s practical and easier to consume. I always opt for simplicity when choosing a design approach and WCF REST template is definitely the hammer in my toolbox.

However, I came to a problem when trying to expose anonymous object as output. I would get a runtime “Request Error” whenever using object as the return type.

BACKGROUND: WCF requires strong return type which creates unnecessary complexity. It doesn’t make sense to create a return type for each of my WCF REST methods. I would rather go back to SOAP the old way. Beside WCF, I’ve always been creating REST service just using ASP.net which, in my view, is extremely easy. After a day of digging around the Google search, I found only bits and pieces of information. Then I came across System.ServiceModel.Channels.Message type which gave me an idea to create a string output inside the Message. It gives me the freedom to manipulate the output however I want.

So here are the steps to setup the project. First download the WCF REST Service template using the Extension Manager in Visual Studio 2010

Extension Manager

Just install the template provided by Microsoft

WCF REST 4 template

Create new project using the new template “WCF REST Service Application”. The template pretty much setup everything and it should give you the Hello World sample output when running it. The new template should be listed under Visual C# category. (you will notice the template is quite different from the WCF Service Application; in most of my works, I can do away with interface definitions; this makes it easier to read the codes without flipping between two different cs files)

Follow these steps to create a method to return a Message type that contains the anonymous object.

Add the following references in the Service1.cs file


using System.ServiceModel.Channels;
using System.Net;
using System.Web.Script.Serialization;

Add a new method called JSON_Anonymous()


[WebGet(UriTemplate = "/JSON")]
public Message JSON_Anonymous()
{
  WebOperationContext.Current.OutgoingResponse.Headers[HttpResponseHeader.ContentType] = "application/json";
  var ret = new
  {
    Name = "John Doe"
    ,Title = "SVP Sales and Marketing"
  };

  return WebOperationContext.Current.CreateTextResponse(ret.ToJSON());
}

I added a ToJSON extension method to the cs file as well, to help simplify my code. ToJSON Original source was from Scott Gu’s blog


public static class JSONHelper
{
  public static string ToJSON(this object obj)
  {
    JavaScriptSerializer jss = new JavaScriptSerializer();
    return jss.Serialize(obj);
  }
}

Go ahead run the program and download the JSON output

WCF REST anonymous JSON output

The output should look like this

{
    "Name": "John Doe",
    "Title": "SVP Sales and Marketing"
}

Advertisement

One comment

  1. Nice sharing…



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.