Take advantage of WCF to build light-weight RESTful services and keep the resource URIs clean and lean WCF (Windows Communication Foundation) is a secure, reliable, and scalable messaging platform that can be used to build Web services in .Net. It provides a unified programming model for developing service oriented applications. You can use WCF to build RESTful services in .NET. REST (Representational State Transfer) is an architecture paradigm that conforms to the REST architecture principles.The REST architecture is based on the concept of resources: It uses resources to represent the state and functionality of an application. These resources are in turn identified using URIs over the HTTP protocol. Creating a WCF service In this section we will explore how we can build a RESTful service in WCF. First off, let’s create a new WCF service in Visual Studio. To do this, follow the steps outlined below. Note that to build the application illustrated in this article I have used Visual Studio 2015 although you can use Visual Studio 2012 or 2013 as well. Open Visual Studio 2015 In the File menu in the Visual Studio IDE, click on Start -> File -> New -> Project Next, select WCF from the list of the project templates displayed Select “WCF Service Application” on the right side pane Specify a name for your WCF service project and click OK to save it This would create a new WCF Service Application project in the name you specified. The project would also contain a default service for illustration purposes only. Implementing the RESTful WCF Service When working with WCF, you first need to create a service contract and then define the service operations or operation contracts in it. Typically, a WCF service comprises of the following: Service class Service contract One or more operation contracts One or more endpoints Hosting environment A ServiceContract is used to specify the operations that are available for the service client to consume. The following code snippet shows how a service contract looks like — we will modify this later to make it RESTful. [ServiceContract] public interface ICustomerService { [OperationContract] List<Customer> GetCustomerList(); } A DataContract is used to describe the data that needs to be exchanged between the service provider and the service consumer. Consider the following DataContract called Customer. [DataContract(Namespace = "")] public class Customer { [DataMember] public Int32 CustomerID { get; set; } [DataMember] public string FirstName { get; set; } [DataMember] public string LastName { get; set; } [DataMember] public String Address { get; set; } } An operation contract is used to expose a method as a service method and also transaction flow, direction of the service operation and also the fault contract(s) that may be associated. The following code snippet illustrates how you can declare a service operation using the OperationContract attribute and the use the WebInvoke attribute to specify the HTTP operation, Uri, Web message format, etc. [OperationContract] [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "GetCustomers")] List<Customer> GetCustomerList(); The following code snippet illustrates how the customer service can be made RESTful by applying the WebInvoke attribute on its service method. public interface ICustomerService { [OperationContract] [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "GetCustomers")] List<Customer> GetCustomerList(); } The CustomerService class extends the ICustomerService service contract and provides the implementation of the service operation named GetCustomerList. Here’s how the CustomerService class would look like. [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] public class CustomerService : ICustomerService { public List<Customer> GetCustomerList() { return PopulateCustomerData(); } private List<Customer> PopulateCustomerData() { List<Customer> lstCustomer = new List<Customer>(); Customer customer1 = new Customer(); customer1.CustomerID = 1; customer1.FirstName = "John"; customer1.LastName = "Meaney"; customer1.Address = "Chicago"; lstCustomer.Add(customer1); Customer customer2 = new Customer(); customer2.CustomerID = 1; customer2.FirstName = "Peter"; customer2.LastName = "Shaw"; customer2.Address = "New York"; lstCustomer.Add(customer2); return lstCustomer; } } Note that the PopulateCustomerData method is not a service method; it’s a private method that returns a list of customer records and is called from the GetCustomerList service method. The next thing you should do is configure the WCF service. To do this, you would need to specify the binding and endpoint details and also the service behavior. The following code snippet shows how the service configuration should look like for this service. <system.serviceModel> <services> <service name="IDGWCF.CustomerService" behaviorConfiguration="ServiceBehaviour"> <endpoint address ="" binding="webHttpBinding" contract="IDGWCF.ICustomerService" behaviorConfiguration="web"> </endpoint> </service> </services> <behaviors> <serviceBehaviors> <behavior name="ServiceBehaviour"> <serviceMetadata httpGetEnabled="true"/> <serviceDebug includeExceptionDetailInFaults="false"/> </behavior> </serviceBehaviors> <endpointBehaviors> <behavior name="web"> <webHttp/> </behavior> </endpointBehaviors> </behaviors> <serviceHostingEnvironment multipleSiteBindingsEnabled="true" /> </system.serviceModel> And that’s all you have to do. You can now open a Web browser and test your WCF RESTful service. Related content feature 14 great preprocessors for developers who love to code Sometimes it seems like the rules of programming are designed to make coding a chore. Here are 14 ways preprocessors can help make software development fun again. By Peter Wayner Nov 18, 2024 10 mins Development Tools Software Development feature Designing the APIs that accidentally power businesses Well-designed APIs, even those often-neglected internal APIs, make developers more productive and businesses more agile. By Jean Yang Nov 18, 2024 6 mins APIs Software Development news Spin 3.0 supports polyglot development using Wasm components Fermyon’s open source framework for building server-side WebAssembly apps allows developers to compose apps from components created with different languages. By Paul Krill Nov 18, 2024 2 mins Microservices Serverless Computing Development Libraries and Frameworks news Go language evolving for future hardware, AI workloads The Go team is working to adapt Go to large multicore systems, the latest hardware instructions, and the needs of developers of large-scale AI systems. By Paul Krill Nov 15, 2024 3 mins Google Go Generative AI Programming Languages Resources Videos