Reader Level:
ARTICLE

A Simple Example of WCF Service

Posted by Mahak Gupta Articles | WCF with C# March 22, 2012
Here we look at an example of a WCF Service in which we create a service for applying simple calculator functions.
  • 5
  • 0
  • 230446

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 be 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

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.

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

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:

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.

 

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.

 

fig6.gif

After that the following window will be appear.

ing7.gif

After that we select the 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 Contart 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 be appear:

fig13.gif

Here we enter values for n1 and n2 and click on the Invoke Button. The result will appear as:

fig4.gif

Article Extensions
Contents added by pola satyam on Nov 21, 2012

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 be 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

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.

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

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:

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.

 

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.

 

fig6.gif

After that the following window will be appear.

ing7.gif

After that we select the 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 Contart 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 be appear:

fig13.gif

Here we enter values for n1 and n2 and click on the Invoke Button. The result will appear as:

fig4.gif

COMMENT USING

Trending up