Friday, November 6, 2009

REST with WCF 3.5

Let’s walk through Step by Step

Step 1: Create a contract, and mark the contract with WebGet Attribute

//Import the required Namespace/s
using System.ServiceModel;
using System.ServiceModel.Web;

namespace RestWithWCF
{
[ServiceContract]
public interface IGreeterService
{
[OperationContract]
[WebGet(UriTemplate = "greet/{name}")]
string DoGreet(string name);
}
}

Step 2: Implement the contract for the Service

namespace RestWithWCF
{
public class GreetService : IGreeterService
{
public string DoGreet (string name)
{
return string.Format("Hello, {0} , Welcome to the world of REST.", name);
}
}
}

Step 3: Create “Host” for the service

using System;
using System.ServiceModel;
using System.ServiceModel.Web;
namespace RestWithWCF
{
class Program
{
static void Main(string[] args)
{
using (WebServiceHost serviceHost =
new WebServiceHost(typeof(GreetService)))
{
serviceHost.Open();
Console.WriteLine("Host Started...");
Console.ReadLine();
serviceHost.Close();
}
}
}
}

Step 4: Create a Configuration file to expose a webHttpBinding endpoint for the service.

















Step 5: Compile and Execute the HOST

Step 6: Open browser and hit the REST Endpoint

Step 7: Check the XML response

That’s it.