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.
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.
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
- using System.Runtime.Serialization;
- namespace Calc {
- [DataContract]
- public class Calc {
- [DataMember]
- public double n1;
- [DataMember]
- public double n2;
- }
- }
Step 4: After that we add another class.
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
- public interface ICalcService
- {}
Here we type the following operations:
- public interface ICalcService
- {
- double Add(double n1, double n2);
- double Subtract(double n1, double n2);
- double Multiply(double n1, double n2);
- double Divide(double n1, double n2);
- }
After that, we delare it as a Service Contract, which is in a different namespace:
System.ServiceModel. Now we look at the code:
- using System.ServiceModel;
- namespace Calc
- {
- [ServiceContract]
- public interface ICalcService
- {
- [OperationContract]
- double Add(double n1, double n2);
- [OperationContract]
- double Subtract(double n1, double n2);
- [OperationContract]
- double Multiply(double n1, double n2);
- [OperationContract]
- double Divide(double n1, double n2);
- }
- }
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
- public class CalcService:ICalcService
- {}
Here we implement an interface by right-clicking and choosing the option Implement Interface Explicitly:
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.
- using System.ServiceModel;
- namespace Calc
- {
- [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
- public class CalcService: ICalcService
- {
- public double Add(double n1, double n2)
- {
- return n1 + n2;
- }
- public double Subtract(double n1, double n2)
- {
- return n1 - n2;
- }
- public double Multiply(double n1, double n2)
- {
- return n1 * n2;
- }
- public double Divide(double n1, double n2)
- {
- return n1 / n2;
- }
- }
- }
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.
After that, the following window will appear.
After that, we select Calc.CalcService in the Configuration option:
After that, we click on the Name Option:
After that, we click on EndPoints.
And then we click on Empty Name.
Here we click on Contract and again select the Calc.dll.
Step 8: Now we run the program.
Step 9: After that, we click on Add or any other function. When we click on Add the following window will appear:
Here we enter values for n1 and n2 and click on the Invoke Button. The result will appear as: