dotnet thoughts 

a dotnet developer's technical blog

How to create RESTful WCF services

In the current project, I am working with WCF services in .Net 4.0. Few days back I got a question like how can we make a WCF service RESTful. Yesterday I played around it and I could figure out how it works.

In this post I am posting how to create a RESTful time service, which will give the current system time,

http://localhost:8080/TimeService/

and the next method give current time with specified format.

http://localhost:8080/TimeService/ddMMMyyyy

As usual for creating a WCF service you need a service contract. For making WCF services you need special attributes. And you need to add reference of System.ServiceModel.Web. And if you want use custom POCO classes, you need to add the reference of System.Runtime.Serialization.

Here is the Service interface

[ServiceContract]
interface ITimeService
{
    [OperationContract]
    [WebGet(UriTemplate = "/")]
    string GetTime();

    [OperationContract]
    [WebGet(UriTemplate = "/{timeformat}")]
    string GetFormattedTime(string timeformat);
}

The WebGet attribute is enables the GET method in the service. The UriTemplate specifies the address. And all the parameters should be string, and parameter name should be match. It also support POST and PUT protocols.

And here is the implementation

class TimeService : ITimeService
{
    public string GetTime()
    {
        return DateTime.Now.ToString();
    }

    public string GetFormattedTime(string timeformat)
    {
        return DateTime.Now.ToString(timeformat);
    }
}

I am using Self Hosting for this. And here is this Service Host implementation.

static void Main(string[] args)
{
    using (ServiceHost serviceHost =
        new ServiceHost(typeof(TimeService)))
    {
        string baseUri =
            string.Format("http://{0}:8080/TimeService",
            Environment.MachineName);
        ServiceEndpoint serviceEndPoint =
            serviceHost.AddServiceEndpoint(typeof(ITimeService),
            new WebHttpBinding(), baseUri);
        serviceEndPoint.Behaviors.Add(new WebHttpBehavior());
        serviceHost.Open();
        Console.WriteLine(
            string.Format("Server available on the url - {0}",
            baseUri));
        Console.WriteLine("Press <ENTER> to stop listening.");
        Console.ReadLine();
        serviceHost.Close();
    }
}

Here is the screen shot RESTful service running.

RESTful Service Running

RESTful Service Running

And here is the the screenshot of the RESTful service accessing via Internet Explorer.

TimeService - With no parameters

TimeService - With no parameters

TimeService - With format specified on the URL

TimeService - With format specified on the URL

This following code snippet to how to access WCF RESTful service with WebClient.

using (WebClient webClient = new WebClient())
{
    using(StreamReader sr =
        new StreamReader(
            webClient.OpenRead("http://localhost:8080/TimeService/")))
    {
        Console.WriteLine(sr.ReadToEnd());
    }
}

2 Responses to “How to create RESTful WCF services”

  1. nice ……

    Comment by shiju — March 30, 2011 @ 1:24 am

  2. good ….

    Comment by shiju — March 30, 2011 @ 1:25 am

RSS feed for comments on this post. TrackBack URL

Leave a Response

*