Implementing REST Web services Part 1


I want my project to support numerous clients form factors. This is becoming even more important now the number of form factors has increased – what with servers, desktops, laptops, notebooks, net books, slates (of various sizes) and smart phones – that is before we get to OS such as Android, Windows Phone 7, iOS and that is just phones! . All have differing screen sizes but more importantly the quality of the connection to the internet differs greatly – the speed of a home or work based network is a lot faster than the wireless cell network.

Although using WCF is fairly easy because of things like the SOAP envelope the amount of information transferred over the wire is that much greater than just the data being transferred. The SOAP envelope makes connecting to a WCF service easier – because the service itself informs the client of the data format. REST allows for quicker transfer of data, but sacrifices some of the simplicity. On the other hand REST uses nothing more than HTTP and that is widely available. Anyway much of the extra complexity can be hidden behind a object class which can be implemented in any language that is appropriate.

So any way this prompted me to look at the practicalities of implementing the web services in my project in the REST format. This would then provide a light weight service that can be called from any service going.

As it happens when doing this I found another advantage. Back when I first did ASPX Web Services a page was created with a full test harness – I could test the service just by going to an address in my browser. This is particularly useful when wanting to test a rollout – I found this much more awkward with WCF – although there is a test client. In REST we get this option back – just by going to the web address associated with the call you want to test.

Getting Started

Starting the creation of a REST web service is exactly the same as with a WCF Service. Once the WCF Service is created we need to modify the setup to make it a REST service (OK possible are you NUTS moment altering a web.config. I have a User service and below is the ServiceModel tag from the web.config of the web service.

              <behavior name="webBehavior">
          <service name="MyCompanyService.User">
              <endpoint address="" behaviorConfiguration="webBehavior" binding="webHttpBinding" bindingConfiguration="" contract="MyCompanyService.IUser" />
              <endpoint address="rest" behaviorConfiguration="webBehavior" binding="webHttpBinding" bindingConfiguration="" contract="MyCompanyService.IUser" />

The principle change was the creation of a new behaviour (webBehavior) and changes to the binding to use webHttpBinding.

Now on to creating the initial service.

The first service has to do with Users. First up is the Login method so deleting the DoWork method from the interface I am going to replace it with a Login method that takes two parameters; email and password.

public interface IUser
    [WebGet(UriTemplate = "Login?email={email}&password={password}",
    String Login(String Email,String Password);

Couple of changes here from a traditional WCF service in that we give it a an extra attribute; the WebGet attribute. The first parameter here is the UriTemplate – this determines the web address that will be used to access this service. In this instance we will add ‘/login?’ to the address of the service. I have also added the ResponseFormat and set it to Xml – this is because it allows me to use serialization to transfer any object to an Xml String from the service and the client.

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class User : IUser

    public string Login(string Email, string Password)
        return String.Format("Hello {0} your password is {1}",Email,Password);

This is just for testing purposes so all it does is pass back a string containing the email and password. It is kind of a REST equivalent of ‘Hello World’. So if we now run our project.

When the project is running start a new instance of IE (or any browser) and type the following in the address bar;

What you should get is something like the following;


This shows a browser and the string passed back from the service – thereby demonstrating it works!

Out of interest checking the source of the page gives the following XML

So that is the service setup. I’m going to add to this once I have implemented something a bit more interesting than yet another ‘Hello World’


One thought on “Implementing REST Web services Part 1

  1. hi paul u really got stuff in u r all post. But please change your Wordpres Theme. I cant read some of the lines properly.

Leave a Reply

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

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

Google+ photo

You are commenting using your Google+ 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