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;
- }
- }


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
- {}
- 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);
- }
- 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);
- }
- }
Code
- public class CalcService:ICalcService
- {}

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;
- }
- }
- }

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:


Timmy CurtinPosted Jan 10, 2021, 3:45 AM EST
Does the port have to be 8731?
Sangaralingam SPosted Jul 25, 2017, 4:18 AM EDT
Good for Beginners
Ramesh PalaniappanPosted Sep 10, 2016, 5:41 AM EDT
Good one
SubashPosted Sep 8, 2016, 4:35 AM EDT
Useful one
kalu singh raoPosted Jul 3, 2016, 10:07 PM EDT
Nice...
RajaPosted Jun 28, 2016, 2:13 AM EDT
Good one..
Venugopala Rao DivvalaPosted May 3, 2016, 4:23 AM EDT
Good for Beginners
Amatya AgyeyPosted Apr 4, 2016, 4:23 AM EDT
Very Nice .. Keep it up
Ajeet MishraPosted Sep 1, 2015, 2:56 AM EDT
usefull
SharadPosted Jul 16, 2015, 11:47 PM EDT
good one...
Shalin BhavsarPosted Mar 3, 2015, 12:01 AM EST
How to call WCF in asp.net C#?which is the another setting to provide security in WCF?is there any word or PDF with example then please share it [email protected]
Partha SaradhiPosted Feb 20, 2015, 12:30 AM EST
It is good to learn programming in WCF
Shalin BhavsarPosted Dec 16, 2014, 12:12 AM EST
I am using xamarin and want to call WCF service ?
Prem VermaPosted Nov 20, 2014, 1:14 AM EST
well discription
Gaurang BhadaniPosted Oct 14, 2014, 6:25 AM EDT
good
sagar venkateshPosted Aug 26, 2014, 12:12 AM EDT
Hi Mahak, the article is very good. i have one more question regarding WCF authentication, I have developed a WCF project which contains 2 simple services. In that 1 is for login authentication and another is just for displaying a data. i have used digest authentication (DigestAuthenticationHostFactory). Now it is authenticating for that service which ever is called first. but I want only to authenticate for Login service. How can this be achieved ?Its not mandatory that login service will be called first.
Neelam ParmarPosted Jul 23, 2014, 8:41 PM EDT
Really Good One... Easy to understand for beginners... thanks a lot
Darling DollPosted Jan 1, 2014, 6:47 AM EST
With VS2013 netframework 4.5, an error: AddSync is not supported in the WCF test client because it uses type System.Threading.Tasks.Task System Double mscorlib , can you help me? Thanks. happy 2014
Mahak GuptaPosted Nov 26, 2013, 6:08 AM EST
Thanks
Santosh KumarPosted Nov 21, 2013, 4:31 AM EST
Very easy and nice description.
Former memberPosted Aug 6, 2013, 4:00 AM EDT
Nice Article
Chandan SinhaPosted Jul 26, 2013, 10:25 AM EDT
Very good working example of WCF. Great Work! Thanks.
Vinod Kumar ChennegowdaPosted Mar 26, 2013, 8:05 AM EDT
Great explanations
Shaiju JanardhananPosted Dec 20, 2012, 11:18 PM EST
Thank U
Steve BlodgettPosted Mar 25, 2012, 1:16 PM EDT
Thanks for the great article!