Create a new Class Library Project and call it “RESTService.Lib”. Add references to “System.ServiceModel” and “System.ServiceModel.Web”. Create an Interface class called IRESTDemoServices and add the definitions of the methods that represent the services offered. Our interface will offer just one service as follows:
public interface IRESTDemoServices
{
string GetClientNameById(string Id);
}
In order to tell the framework to treat this interface as a service you need to decorate it as follows:
[ServiceContract(Name = "RESTDemoServices")]
public interface IRESTDemoServices
{
[OperationContract]
string GetClientNameById(int Id);
}
Define the URL to be used to access the service (URL Routing):
public static class Routing
{
public const string GetClientRoute = "/Client/{id}";
}
The connection of the URL Route to the method in the interface is achieved by decorating the interface with an attribute as follows:
[ServiceContract(Name = "RESTDemoServices")]
public interface IRESTDemoServices
{
[OperationContract]
[WebGet(UriTemplate = Routing.GetClientRoute, BodyStyle = WebMessageBodyStyle.Bare)]
string GetClientNameById(string Id);
}
Now implement the service:
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single,
ConcurrencyMode = ConcurrencyMode.Single, IncludeExceptionDetailInFaults = true)]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class RestDemoServices:IRESTDemoServices
{
public string GetClientNameById(string Id)
{
Random r = new Random();
string ReturnString="";
int Idnum=Convert.ToInt32(id);
for (int i = 0; i < Idnum; i++)
ReturnString += char.ConvertFromUtf32(r.Next(65, 85));
return ReturnString;
}
}
And then you have to host it either in IIS or by using custom hosting (console, windows service, etc..).
Look here: http://www.progware.org/blog/post/a-simple-rest-service-in-c.aspx
Hope that helps !