WCF REST Starter Kit provides easy way to create rest web services. Visual studio has project templates for rest WCF services. First you need to go download “WCF REST Starter Kit Preview 2” from codeplex. http://aspnet.codeplex.com/releases/view/24644
Then Open a new project from installed templates tab Visual C#->Web->WCF Rest Service Application.
In the global.asax file you can specify the routing with RegisterRoutes() method. By changing the first argument of the ServiceRoute:
RouteTable.Routes.Add(new ServiceRoute(“RestServiceTest”, new WebServiceHostFactory(), typeof(HelloService)));
you can deploy your rest service with the url: http://localhost:58944/RestServiceTest (port will not be same in your localhost). It uses ASP.NET routing.
OperationContract is optional in Rest web services. In the template project you will see the SampleItem.cs file.
public class SampleItem
{
public int Id { get; set; }
public string StringValue { get; set; }
}
You can return objects of this type , or a collection of this type.
Service1.cs has some unimplemented method declerations. These will guide you to implement your own services. I will give some additional examples for service methods and how they can be called by a client.
[WebGet(UriTemplate = “GetItems”)]
public List<SampleItem> GetItems() {
//your code
}
If you implement the above method in your service, it will be available at http://localhost:58944/RestServiceTest/GetItems , because we specified UriTemplate=”GetItems” in the method template.
You can also call methods with argumens. An example call for the method
public int Square(int number) { return number*number; }
is
http://localhost:58944/RestServiceTest/Square/number
Then the number you typed ine the above url will be mapped to the argument of the method. This is the beauty of rest services.
Now we will switch our topic to the type of response. WCF WebHttp Services returns an XML response as default. If you need JSON response then a little modification is needed. We need automaticFormatSelectionEnabled = true for changing the format of response. An example web.config:
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled=”true”/>
<standardEndpoints>
<webHttpEndpoint>
<!–
Configure the WCF REST service base address via the global.asax.cs file and the default endpoint
via the attributes on the <standardEndpoint> element below
–>
<standardEndpoint name=”” helpEnabled=”true” automaticFormatSelectionEnabled=”true”/>
</webHttpEndpoint>
</standardEndpoints>
</system.serviceModel>
We can switch our web service to return json format for every request or we can give that flexibility to the client.
[WebGet(UriTemplate = “GetItems?Format={format}”)]
public List<GetItems> GetItems(string format) { // your code }
If the format sepcified by the client is json we can set the reponse format to json in the method:
WebOperationContext.Current.OutgoingResponse.Format = WebMessageFormat.Json;
An Http request with correct format will be enough to call this method and it will return a json string as response. WCF REST Starter Kit Preview 2 assemblies provides a method to read json object: response.Content.ReadAsJsonDataContract<T>()
References:
http://blogs.msdn.com/b/endpoint/archive/2010/01/06/introducing-wcf-webhttp-services-in-net-4.aspx
Turkish: http://www.buraksenyurt.com/archive.aspx#WCF-WebHttp-Services



