How To Set Up A RESTful Web Service Using WCF - GET

Article By: rajiv02


What Is WCF?

RESTful Web Service Windows Communication Foundation - Learn More

Windows Communication Foundation is a programming platform and runtime system for building, configuring and deploying distributed services. It's a framework for building Service Oriented Applications. WCF is comprised of class libraries, tools, hosting facilities, and other components that are used to provide a foundation for applications that use SOA. WCF supports the features of Web Services, Remoting, MSMQ and COM+ etc.

WCF Fundamentals :

  1. Service : Exposes one or more endpoints and one or more service operations can be accessed through endpoing exposing.

  2. Address : Specifies where WCF service is hosted. It is specified as a Uniform Resource Identifier (URI). The URI schema specifies the protocol being used such as HTTP and TCP.

  3. Binding : Binding will describe how client will communicate with service. There are different protocols available for the WCF to communicate to the Client. You can mention the protocol type based on requirements.

  4. Contract : Specified what the endpoint will communicate with outside world.

What Is REST?

REST stands for REpresentational State Transfer. It is an architectural style, and an approach to communicate using HTTP verbs to map CRUD operations to HTTP methods.

R : READ   --> GET

RESTful architecture use HTTP for all CRUD operations like (Read/Create/Update/Delete) using simple HTTP verbs like (GET, POST, PUT, and DELETE).It’s simple as well as lightweight. 

For the sake of simplicity, I am going to implement only a GET request for which service will return certain types of data (i.e. BIP Media Tutorial data) in XML format. 

HTTP verbs are used as follows;

  • Create (POST) > create a new resource.
  • Read (GET) > retrieve one or many resources.
  • Update (PUT) > update an existing resource.
  • Delete (DELETE) > delete an existing resource.

Following are 5 simple steps to create your first RESTful service that returns data in XML.

  • Create a WCF Service Project.
  • Preparing data contract object model
  • Preparing service contract and its implementation
  • Configure service and behavior
  • Running service and see its results

To detailed this example I am using Visual Studio 2012 and .NET Framework 4.5

1. Create WCF Service Project

Let us create WCF project using available templates in Visual Studio.

Open Visual Studio.

From File -> New Project.

Enter Name and Location of the project.

Select WCF from left and create a new WCF Service Application.

See Below (1) screen shot and in (2) screen shot rename project files:

Create WCF Project on the BIP media Cloud

Rename files as per Below Configuration

2. Preparing Data Contract Object Model

data contract is a formal agreement between a service and a client that abstractly describes the data to be exchanged.  It is created by applying the DataContractAttribute andDataMemberAttribute attributes to the class and its members. Let us design DataContract for this now. 

Create Data Contract

Create Data Contract

3. Preparing Service Contract And Its Implementation

Service contract describes the operation that service provide. A Service can have more than one service contract but it should have at least one Service contract. It ties multiple operations together into a single functional unit. It can define service-level settings, such as the namespace of the service, relevant callback contract, and so on.

Service Contract can be defined using [ServiceContract] and [OperationContract] attribute. [ServiceContract] attribute is similar to the [WebServcie] attribute in the WebService and [OpeartionContract] is similar to the [WebMethod] in WebService. 

Let us design ServiceContract for this now.

In file “IBIPMediaTutorialService.cs” do below ServiceContract declaration.

Create Service Contract

In file “BIPMediaTutorialService.svc” do below ServiceContract method implementation.

Currently for demo purposes I have hard coded records in the code but for real time we must fetch them from database only using ADO.NET or other mechanism.

Service Contract method implementation

I have created bipMediaTutorial object and assigning values into its properties.

Service Contract method implementation

4. Configure Service And Behavior

Endpoint "is a combination of Address, Binding and Contract". We discussed about Address, Binding and Contract in above.

Web.Config chanages for WCF Restful service

5. Running Service And See Its Results

We have hosted our service in IIS but there are other available hosting as Self Hosting, WAS (Windows Activation Service) and Windows Service.

In below we have just run our project and in URL provided method name "BIPMediaTutorialList", In below screen shot it will display all the records available in the database.

Running service and see its results

Tags: , , , , , , , , , , , ,

Welcome to the healthcare-only HIPAA - GDPR compliant cloud. Exclusively hosted on a HPC environment!

Learn more or start today by choosing your secure HIPAA - GDPR compliant server's Operating System bellow and pick the package that's best for you.

BIPmd makes it simple to launch in the cloud and scale up as you grow – whether you’re running one virtual machine, thousand or more.

Windows VPS options and add ons
Linux VPS options and add ons


Looking for a custom solution?

Our technicians can provide you with the best custom-made solutionss on the market, no matter whether you're a small business or large enterprise.

Get in touch

Leave a Reply