Creating Restful Service API using WCF

Windows Communication Foundation (WCF) is a set of Dotnet technologies used to create REST based service API returning xml and json data. REST based architecture uses some common HTTP methods like get/post to interact with the API. It has many benefits like interoperability, improved scalability, efficiency etc.

We are going to create restful API using WCF 4.0. Open visual studio and create new project. Select project type WCF Service Application. Once the project is created, you can see in solution that by default WCF service and interface files are already created. Refactor to rename it as Service.svc and IService.cs.

Open the Service.svc.cs class file and write a method.

public class Service : IService
 public string GetTodayDate()
 return string.Format("You entered: {0}", DateTime.Now.ToShortDateString());

 public string GetItemValue(string id)
 return "You requested item id: " + id;

 public string GetItemValueXml(string id)
     return "You requested item id: " + id;

 public ItemDataModel GetItemData(ItemDataModel model)
 if (model == null)
 throw new ArgumentNullException("model empty");
 model.Message = "You requested item id: " + model.id;
 return model;


Open IService.cs file and add interface method.

  1. UriTemplate - is composed of two parts: a url path and a query.
  2. Method - GET/POST
  3. ResponseFormat - is set as WebMessageFormat.Json for getting json result and XML for getting xml result.
  4. BodyStyle - is set as WebMessageBodyStyle.WrappedResponse

To pass a complex object we can assign UriTemplate as simple methodName.


 public interface IService

 [WebInvoke(Method = "GET", UriTemplate = "GetItemValue/{id}", BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Json)]
 string GetItemValue(string id);

 [WebInvoke(Method = "GET", UriTemplate = "GetTodayDate", BodyStyle = WebMessageBodyStyle.WrappedResponse, ResponseFormat = WebMessageFormat.Json)]
 string GetTodayDate();

 [WebInvoke(Method = "GET", UriTemplate = "GetItemValueXml/{id}", BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Xml)]
 string GetItemValueXml(string id);
 [WebInvoke(Method = "POST", UriTemplate = "GetItemData", BodyStyle = WebMessageBodyStyle.WrappedResponse, ResponseFormat = WebMessageFormat.Json)]
 string GetItemData(ItemDataModel model);
 public class ItemDataModel
 public int id { get; set; }
 public string itemName { get; set; }
 public string Message { get; set; }

Adding configuration with two basic parts services and behaviors.
Using webHttpBinding for creating restful service.


 <service behaviorConfiguration="serviceBehavior" name="TestWcfService.Service">
 <!-- Unless fully qualified, address is relative to base address supplied above. -->
 <endpoint address="" behaviorConfiguration="web"
 binding="webHttpBinding" contract="TestWcfService.IService" /> 
 <behavior name="web">
 <webHttp helpEnabled="true"/>
 <behavior name="serviceBehavior">
 <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
 <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
 <!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
 <serviceDebug includeExceptionDetailInFaults="false"/>


Above we have set endpointBehaviors webHttp property helpEnabled to true to get help for WCF methods.

 WCFHelp.png, WCF, Asp.Net, WCF, Api,

Now we are ready to test the Service. I launch the service in internet explorer and my url is http://localhost:9142/Service.svc. Using the following url http://localhost:9142/Service.svc/GetTodayDate we will get json response as below.


 WCFJsonService.png, WCF, Asp.Net, WCF, Api,

Now using xml url http://localhost:9142/Service.svc/GetItemValueXml/4 we have xml response.

WCFXmlService.png, WCF, Asp.Net, WCF, Api,


Now using the WCF service url http://localhost:9142/Service.svc/GetItemValue/12 passing parameter id for the method GetItemValue, having method type as get.

 WCFHttpGetMethodWithParameter.png, WCF, Asp.Net, WCF, Api,

Back to top