Monday, December 26, 2011

Creating and exposing simple REST based services in WCF 4.0

ReST (Representational State Transfer) is something which makes the web service url easy to remember by pointing a resource rather than a web it done have the page.aspx and the query string syntax. For example consider the scenario where I need to get the details of employee named “Joy”

Traditional way :
ReST way :

There is not question that the Rest URL is simple. Now lets look at how to create such a service in .Net 4.0.
  • Create a web application normally.
  • Add a WCF service to the project.
  • Right click on the .svc file and view markup . Add Factory="System.ServiceModel.Activation.WebServiceHostFactory into the <%@ ServiceHost node.
  • In the Service interface methods add [WebGet] attribute. This makes them visible to the ReST service. Set the URI template of the WebGet properly.
So above are the steps to follow. Lets now see one example Service contract.Famous DoWork method to ReST.
public interface IMyService
        string DoWork ();
The implementation is same.No change .
public class MyService : IMyService
    public string DoWork ()
        return "Yes.Our ReST service worked!!!";

All set.Hit F5 and in the browser type http://localhost:<port>/myservice.svc/dowork .You can see the output as xml.
<?xml version="1.0"?>
<string xmlns="">Yes.Our ReST service worked!!!</string>

WCF ReST return as JSON

Its little difficult for the calling party to process the xml and one more issue is the xml is heavy.So the immediate solution which comes to this scenario is the JSON format. Its easy to make our output to JSON.Just change the WebGet to specify JSON format as follows.


How to pass parameters to the WCF ReST services

Nobody is going to have only simple services as above in any of the projects. Passing arguments are necessary in WCF ReST services and its supports too using the UriTemplate property of WebGet.

int Add ( string a, string b );
Implementation as follows.
public int Add(string a, string b)
    int num1, num2;
    if (int.TryParse(a, out num1) && int.TryParse(b, out num2))
        return num1 + num2;
    throw new ArgumentException("Give numbers to add");

Look at the UriTemplate it tells how the arguments are going to arrive. So call the service appropriately.

Points to Note

The steps which I performed are very basic and this is working since the framework is 4.0 where it automatically use the WebHttpBinding and other configurations. If you are using old frameworks you may need to set the binding properly.

Another thing is the parameters here are string. So convert them accordingly. I don't thing a sample is needed to see this in your computer.

There are more scenarios in this like such as how to pass and return complex types and how to use the Http Post etc...Hope I can post later.


Blogger said...

DreamHost is ultimately the best hosting provider for any hosting plans you require.

Blogger said...

If you need your ex-girlfriend or ex-boyfriend to come crawling back to you on their knees (even if they're dating somebody else now) you have to watch this video
right away...

(VIDEO) Text Your Ex Back?