Creating Simple WCF Application Using C# and VS 2012

This article provides a detailed introduction to creating a simple WCF Service.

Introduction

 
This article provides a detailed introduction to creating a simple WCF Service. It is very simple to create a WCF Service in C#. Before continuing to describe the procedure to create a WCF service, I would like to provide a small description of WCF Services.
 

WCF Service

 
Windows Communication Foundation (WCF) is a framework for building service-oriented applications. Using WCF, you can send data as asynchronous messages from one service endpoint to another. A service endpoint can be part of a continuously available service hosted by IIS, or it can be a service hosted in an application. An endpoint can be a client of a service that requests data from a service endpoint. The messages can be as simple as a single character or word sent as XML, or as complex as a stream of binary data. For more details, follow the link: http://msdn.microsoft.com/en-us/library/ms731082.aspx.
 
Contracts are needed to create a WCF Service., There are a minimum of two contracts required to create a simple WCF Service.
 

Contracts in WCF

 
The contract is a platform-neutral and standard way of describing what the service does. There are four type of contracts that WCF supports, but you can use a minimum of two main contracts to start working in a WCF service, here I have explained only two contracts. The details of the main contract are the following:
  • Service Contracts
  • Data Contracts

1. Service Contract

 
This contract describes which operation the client can perform on the service. There are two types of Service Contracts available in the WCF Application.
 

2. Data Contract

 
This contract defines an interface for the client, in other words if you create an interface in the WCF Application and you are not using the Service Contract tag the interface then it will not be accessible to the client. It should be declared before the name of the interface.
 
For example: 
  1. [ServiceContract]  
  2. public interface IStudent  
  3. {  
  4. }  
Operation Contract
 
The Operation Contract defines which function will be accessible to the client, mean if you don't declare the operation contract before declaring your function, the function or method will not be available to the client. Declare the tag within the interface before declaring the function to be accessible for the client.
 
For example:
  1. [OperationContract]  
  2. Student Get(int ID);  
  3. [OperationContract]  
  4. string Set(Student SD);  
Data Contracts
 
There are two in the WCF service that define which data types are passed to and from the service. 
  1. DataContract

    This contract declares the simple CLR understandable (Entity) class in the WCF Service. This position of Data Contracting is before the class declaration.

    For example:
    1. [DataContract]    
    2. class Student:IStudent    
    3. {    
    4. [DataMember]    
    5. public int SudentID { get;set; }    
    6. [DataMember]    
    7. public string StudentName { get;set; }    
    8. }
  1. DataMember

    This attribute is used to define the fields of an entity (properties of class). The tag is declared before the property declaration.

    For example:
    1. [DataMember]  
    2. public int SudentID { get;set; }  
    3. [DataMember]  
    4. public string StudentName { get;set; }
The details mentioned above are about the WCF Service and Contract needed to create a Service. Now let's go to the procedure to create a simple WCF Service application.
 
In this article, I have described a simple WCF Service that stores and retrieves details of students, such as StudentName and StudentID. Let's discuss the procedure to create the same service I have created.
 

Procedure to Create a WCF Service

  1. Open Visual Studio and create a new project. The New Project Template Window will open, select the WCF option from the left side tree view items. The template related to WCF will display in the right side, then select "WCF Service Library".

    Note 1: WCF Service Supports Visual Studio 2008 and later and SQL Server 2005 and later.

    Note: It is a good practice to define the service in the Class library and refer to the class library in the Host project. Use a service class in the host project.
  2. Rename the service application to "MyServiceLibrary" and set the location, solution and solution name if necessary.
  3. Remove the Iservice, Service1 and Service classes from your project.
  4. Add an interface to your project having the name Istudent, define the Service Contract above the interface name and OperationContract or OperationContracts above the method or methods in your Interface to make it accessible to the client.
  5. Here I have declared two methods named Get and Set to retrieve and save student details.
    For example:
    1. namespace MyService  
    2. {  
    3. [ServiceContract]  
    4. interface IStudent  
    5. {  
    6. [OperationContract]  
    7. Student Get(int ID);  
    8. [OperationContract]  
    9. string Set(Student SD);  
    10. }  
    11. } 
  6. Add an Entity (SimpleCLR Understandable class) and rename it to Student; on the above classname declare the DataContract tag.
  7. Declare the properties necessary; here I have declared two properties (StudentID and StudentName) and the above on every property declare the DataMember tag.
    For example:
    1. [DataContract]  
    2. classStudent  
    3. {  
    4. [DataMember]  
    5. public int SudentID { get;set; }  
    6. [DataMember]  
    7. public string StudentName { get;set; }  
    8. }
  8. Add another class to implement the interface function/methods, in the class you can add code into the method body that will be available in the client. Rename the class name to Student or whatever depending on your needs. Implement the interface and their functions here.
  9. Only the function having the declaration Operation Contract tag above it will be accessible to the client. Or those functions that are not declared with the OperationContract tag will not be accessible to the client.
  10. Then after completing above 9 steps you need to create an EndPoint. WCF Service is a program that exposes a collection of Endpoints. Each Endpoint is a portal for communicating with the world. Three components Address, Binding and Contract belong to an End Point.
  11. The address specifies where the WCF Service is hosted and the Binding will describe how the client will communicate with the service. A Contract is a collection of operations that specify what the end point will communicate with in the outside world
  12. Open the Solution Explorer to configure the end point. Right-click on the app.config file then select "EditWCF Config".
  13. The Config window will open, on the left side expand the service folder then again expand Service1.
  14. In the service1 you will find the endpoint folder, expand that again; you will find two more end points by default. Right-click on the endpoints, click on "New ServiceEndPoint".
  15. On the right pan you will get a field of the new service endpoint. Set the name of the endpoint; I have given it "Student". Specify the address, the details of address I have already discussed. Select the binding as basic HTTP Bind.
  16. Click on the Contract; the contract browser will open, find your service library from the bin folder of your application. Open the library DLL; there you will find an interface or interfaces. Select the one whose name is in the name field of EndPoint. Then click on the "Open" button.
    Select the client folder and follow the procedure to create a client Endpoint.
  17. Up to this step you have created a WCF Library, now we will create a WCF Service Application.
  18. Right-click on the project solution then select "Add" -> "New project". In the project template windows select WCF Service application.
  19. Rename it to MyServiceApplication and remove the interface Iservice.cs. Rename the Service.svc file to MyStudentService.
  20. Expand "mystudentService"; you will get the Service1.cs file there as a child, remove "service1" because we are using a library.
  21. Add the reference of MyServicelibrary into your MyServiceApplication.
  22. Double-click on "MystudentService"; it will open, then remove the "CodeBinding" tag from "mystudentService". Modify the Service tag to "MyServicelibrary" Service then close and save.
  23. Again right-click on the web.config file and select "Edit WCF config file". Right-click on the services and select "New Service" in the right side pan. Type the name of your service. Here I have given "StudentService".
  24. Do the same thing that you have done before creating EndPoint.
Now your WCF Service is completed; you can publish it and use it in a different application.
 
Summary
 
Here we have discussed how to create a WCF service and a little description of WCF services.