What Is WCF?
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 :
- Service : Exposes one or more endpoints and one or more service operations can be accessed through endpoing exposing.
- 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.
- 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.
- 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.
C : CREATE --> POST R : READ --> GET U : UPDATE --> PUT D : DELETE --> DELETE
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:
2. Preparing Data Contract Object Model
A 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.
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.
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.
I have created bipMediaTutorial object and assigning values into its properties.
4. Configure Service And Behavior
Endpoint "is a combination of Address, Binding and Contract". We discussed about Address, Binding and Contract in above.
5. Running Service And See Its ResultsWe 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.
Tags: Windows, create, service, .NET, contract, DataContract, project, Restful service, RESTful Web Service, service contract, ServiceContract, WCF, Windows Communication Foundation
This work is licensed under a
Creative Commons Attribution-