A Simple Example of WCF Service

Introduction
 
Here we look at an example of a WCF Service in which we create a service for applying simple calculator functions (like add, subtract, multiply and divide).  
 
Step 1: First we open the Visual Studio.
  • Click on File:-> New:-> Project.
  • After that, the following window will appear.
fi1.gif
 
Here we select the WCF in the Project Type and then we select the WCF Service Library in it. After that, we specify the name of this library to be Calc (which we can define in the Name Section). And then we click on the OK Button.
 
Step 2: When we click on the Ok Button, the Calc.cs will be opened. In the Solution Explorer, we delete the following files, since we want to create the service from the start.
 
fi2.gif
 
Step 3: In Calc.cs, first we create the class public and then we create it as a WCF Data Contract. For this, we first add the namespace System.Runtime.Serialization. And then we write the following code.
 
Code  
  1. using System.Runtime.Serialization;    
  2. namespace Calc {    
  3.  [DataContract]    
  4.  public class Calc {    
  5.   [DataMember]    
  6.   public double n1;    
  7.   [DataMember]    
  8.   public double n2;    
  9.  }    
  10. }  
Step 4: After that we add another class.
 
fig3.gif
 
fig14.gif
 
Here we name it ICalcService.cs.
 
Step 5: Now we declare as an interface not a class so we change the code like this.
 
Code
  1. public interface ICalcService  
  2. {}  
Here we type the following operations:
  1. public interface ICalcService  
  2. {  
  3.  double Add(double n1, double n2);  
  4.  double Subtract(double n1, double n2);  
  5.  double Multiply(double n1, double n2);  
  6.  double Divide(double n1, double n2);  
  7. }  
After that, we delare it as a Service Contract, which is in a different namespace: System.ServiceModel. Now we look at the code:
  1. using System.ServiceModel;  
  2. namespace Calc  
  3. {  
  4.  [ServiceContract]  
  5.  public interface ICalcService  
  6.  {  
  7.   [OperationContract]  
  8.   double Add(double n1, double n2);  
  9.   [OperationContract]  
  10.   double Subtract(double n1, double n2);  
  11.   [OperationContract]  
  12.   double Multiply(double n1, double n2);  
  13.   [OperationContract]  
  14.   double Divide(double n1, double n2);  
  15.  }  
  16. }  
Step 6: After that we add another class: CalcService.cs. It is an actual service implementation class, so here we can specify the ICalcService like this.
 
Code
  1. public class CalcService:ICalcService  
  2. {}  
Here we implement an interface by right-clicking and choosing the option Implement Interface Explicitly:
 
fig5.gif
 
Now we add the ServiceBeahvior like this:
 
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
 
It specifies the behavior of our service and InstanceContextMode.Single means it creates a single instace of our service. Now we write the following code in it.
  1. using System.ServiceModel;  
  2. namespace Calc  
  3. {  
  4.  [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]  
  5.  public class CalcService: ICalcService  
  6.  {  
  7.   public double Add(double n1, double n2)  
  8.   {  
  9.    return n1 + n2;  
  10.   }  
  11.   public double Subtract(double n1, double n2)  
  12.   {  
  13.    return n1 - n2;  
  14.   }  
  15.   public double Multiply(double n1, double n2)  
  16.   {  
  17.    return n1 * n2;  
  18.   }  
  19.   public double Divide(double n1, double n2)  
  20.   {  
  21.    return n1 / n2;  
  22.   }  
  23.  }  
  24. }  
Step 7: Now we try to run the program; it can not be run since it is not yet completed. Now right-click on the App.config file and select the Edit WCF Configuration option.
 
fig6.gif
 
After that, the following window will appear.
 
ing7.gif
 
After that, we select Calc.CalcService in the Configuration option:
 
ing8.gif
 
After that, we click on the Name Option:
 
img9.gif
 
After that, we click on EndPoints.
 
fig10.gif
 
And then we click on Empty Name.
 
fig11.gif
 
Here we click on Contract and again select the Calc.dll.
 
Step 8: Now we run the program.
 
fig12.gif
 
Step 9: After that, we click on Add or any other function. When we click on Add the following window will appear:
 
fig13.gif
 
Here we enter values for n1 and n2 and click on the Invoke Button. The result will appear as:
 
 


Similar Articles