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 What is Rust? Safe, fast, and easy software development Unlike most programming languages, Rust doesn't make you choose between speed, safety, and ease of use. Find out how Rust delivers better code with fewer compromises, and a few downsides to consider before learning Rust. By Serdar Yegulalp Nov 20, 2024 11 mins Rust Programming Languages Software Development how-to Kotlin for Java developers: Classes and coroutines Kotlin was designed to bring more flexibility and flow to programming in the JVM. Here's an in-depth look at how Kotlin makes working with classes and objects easier and introduces coroutines to modernize concurrency. By Matthew Tyson Nov 20, 2024 9 mins Java Kotlin Programming Languages analysis Azure AI Foundry tools for changes in AI applications Microsoft’s launch of Azure AI Foundry at Ignite 2024 signals a welcome shift from chatbots to agents and to using AI for business process automation. By Simon Bisson Nov 20, 2024 7 mins Microsoft Azure Generative AI Development Tools news Microsoft unveils imaging APIs for Windows Copilot Runtime Generative AI-backed APIs will allow developers to build image super resolution, image segmentation, object erase, and OCR capabilities into Windows applications. By Paul Krill Nov 19, 2024 2 mins Generative AI APIs Development Libraries and Frameworks Resources Videos