You are on page 1of 13

A Simple Example of WCF Service

1 of 13

TECHNOLOGIES

ANSWERS

http://www.c-sharpcorner.com/UploadFile/mahakgupta/a-simple-example...

BLOGS

VIDEOS

INTERVIEWS

BOOKS

NEWS

CHAPTERS

CAREERADVICE

Mukesh

JOBS

AskaQuestion

Contribute

MobileAppDevelopmentinDetails

ASimpleExampleofWCFService
PostedbyMahakGuptainAr cles|WCFwithC#onMarch22,2012

50 3,120

HerewelookatanexampleofaWCFServiceinwhichwecreateaserviceforapplyingsimple
calculatorfunc ons.

Tweet

Like

21

197560

ReaderLevel:
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.

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.

TRENDINGUP

01

MobileAppDevelopmentinDetails

02

HowIBecameaCSharpcornerAddict

03

DependencyInjec on(DI)andInversion
ofControl(IOC)

04

Introduc ontoDesignPa erns

05

PerformingCRUDOpera onWithDapper
(OpenSourceORMFromStackOverflow)
inMVC

06

DynamicLINQQueryinC#

07

Authen ca onUsingFacebook,Google
andMicroso AccountinUniversalApps
UsingMVVM

08

DatatableinViewDataSampleinMVC
Day3

09

Eec veUseofBootstrapWithASP.Net

10

ASP.NetMVC4,En tyFrameworkand
jqGridDemoWithSample
ViewAll

Follow @csharpcorner
Find us on Facebook

C# Corner
Like

54,771 people like C# Corner.

Facebook social plugin

13/08/2014 6:54 AM

A Simple Example of WCF Service

2 of 13

TECHNOLOGIES

ANSWERS

http://www.c-sharpcorner.com/UploadFile/mahakgupta/a-simple-example...

BLOGS

VIDEOS

INTERVIEWS

BOOKS

NEWS

CHAPTERS

CAREERADVICE

JOBS

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
{

13/08/2014 6:54 AM

A Simple Example of WCF Service

3 of 13

TECHNOLOGIES

ANSWERS

http://www.c-sharpcorner.com/UploadFile/mahakgupta/a-simple-example...

BLOGS

VIDEOS

INTERVIEWS

BOOKS

NEWS

CHAPTERS

CAREERADVICE

JOBS

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

13/08/2014 6:54 AM

A Simple Example of WCF Service

4 of 13

TECHNOLOGIES

ANSWERS

http://www.c-sharpcorner.com/UploadFile/mahakgupta/a-simple-example...

BLOGS

VIDEOS

INTERVIEWS

BOOKS

NEWS

CHAPTERS

CAREERADVICE

JOBS

right-click on the App.config file and select the Edit WCF Configuration option.

After that the following window will be appear.

After that we select the Calc.CalcService in the Configuration option:

After that we click on the Name Option:

13/08/2014 6:54 AM

A Simple Example of WCF Service

5 of 13

TECHNOLOGIES

ANSWERS

http://www.c-sharpcorner.com/UploadFile/mahakgupta/a-simple-example...

BLOGS

VIDEOS

INTERVIEWS

BOOKS

NEWS

CHAPTERS

CAREERADVICE

JOBS

After that we click on EndPoints.

And then we click on Empty Name.

Here we click on Contart and again select the Calc.dll.


Step 8: Now we run the program.

13/08/2014 6:54 AM

A Simple Example of WCF Service

6 of 13

TECHNOLOGIES

ANSWERS

http://www.c-sharpcorner.com/UploadFile/mahakgupta/a-simple-example...

BLOGS

VIDEOS

INTERVIEWS

BOOKS

NEWS

CHAPTERS

CAREERADVICE

JOBS

Step 9: After that we click on Add or any other function. When we click on Add the following
window will be appear:

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

Ar cleExtensions
ContentsaddedbypolasatyamonNov21,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.

13/08/2014 6:54 AM

A Simple Example of WCF Service

7 of 13

TECHNOLOGIES

ANSWERS

http://www.c-sharpcorner.com/UploadFile/mahakgupta/a-simple-example...

BLOGS

VIDEOS

INTERVIEWS

BOOKS

NEWS

CHAPTERS

CAREERADVICE

JOBS

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.

13/08/2014 6:54 AM

A Simple Example of WCF Service

8 of 13

TECHNOLOGIES

ANSWERS

http://www.c-sharpcorner.com/UploadFile/mahakgupta/a-simple-example...

BLOGS

VIDEOS

INTERVIEWS

BOOKS

NEWS

CHAPTERS

CAREERADVICE

JOBS

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,


System.ServiceModel. Now we look at the code:

which

is

in

different

namespace:

using System.ServiceModel;
namespace Calc
{
[ServiceContract]
public interface ICalcService
{
[OperationContract]

13/08/2014 6:54 AM

A Simple Example of WCF Service

9 of 13

TECHNOLOGIES

ANSWERS

http://www.c-sharpcorner.com/UploadFile/mahakgupta/a-simple-example...

BLOGS

VIDEOS

INTERVIEWS

BOOKS

NEWS

CHAPTERS

CAREERADVICE

JOBS

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.

13/08/2014 6:54 AM

A Simple Example of WCF Service

10 of 13

TECHNOLOGIES

ANSWERS

http://www.c-sharpcorner.com/UploadFile/mahakgupta/a-simple-example...

BLOGS

VIDEOS

INTERVIEWS

BOOKS

NEWS

CHAPTERS

CAREERADVICE

JOBS

After that we select the Calc.CalcService in the Configuration option:

After that we click on the Name Option:

After that we click on EndPoints.

13/08/2014 6:54 AM

A Simple Example of WCF Service

11 of 13

TECHNOLOGIES

ANSWERS

http://www.c-sharpcorner.com/UploadFile/mahakgupta/a-simple-example...

BLOGS

VIDEOS

INTERVIEWS

BOOKS

NEWS

CHAPTERS

CAREERADVICE

JOBS

And then we click on Empty Name.

Here we click on Contart 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 be appear:

13/08/2014 6:54 AM

A Simple Example of WCF Service

12 of 13

TECHNOLOGIES

ANSWERS

http://www.c-sharpcorner.com/UploadFile/mahakgupta/a-simple-example...

BLOGS

VIDEOS

INTERVIEWS

BOOKS

NEWS

CHAPTERS

CAREERADVICE

JOBS

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

RELATEDARTICLES

ASimpleDuplexServiceinWCF
WCFServiceForInser ngDataIntoDatabase
UsingASP.NET
Hos ngWCFServicewithnetTcpBindingin
WindowsService
FetchingImageUsingWCFRESTService
DebuggingaWCFServicefromSilverlight

Hos ngWCFServiceinAzureandConsuming
WCFDataServiceinWindows7applica on
WCFApplica onCommunicatewithConsole
Applica on
SimpleWCFwebservicetoreceiveparameter
fromHTTPPOSTrequestbody
SilverlightApplica onWithMVVM,WCFand
En tyFramework
Introduc ontoWCFDataserviceandODATA

COMMENTS

9 of 9

SteveBlodge
Thanksforthegreatar cle!
0Like

0Reply

PostReply

Mar25,2012

ShaijuJanardhanan
ThankU
0Like

0Reply

PostReply

Dec21,2012

VinodKumarChennegowda
Greatexplana ons
0Like

0Reply

PostReply

Mar26,2013

ChandanSinha
VerygoodworkingexampleofWCF.GreatWork!Thanks.
0Like

0Reply

PostReply

Jul26,2013

KailashChandraBehera
NiceAr cle
0Like

0Reply

PostReply

Aug06,2013

SantoshKumar
Veryeasyandnicedescrip on.
0Like

0Reply

PostReply

Nov21,2013

PostReply

Nov26,2013

MahakGupta
Thanks
0Like

0Reply

DarlingDoll
WithVS2013ne ramework4.5,anerror:AddSyncisnotsupportedintheWCFtestclientbecauseitusestype

13/08/2014 6:54 AM

A Simple Example of WCF Service

13 of 13

TECHNOLOGIES
0Like

0Reply

http://www.c-sharpcorner.com/UploadFile/mahakgupta/a-simple-example...

ANSWERS

BLOGS

VIDEOS

INTERVIEWS

BOOKS

NEWS

CHAPTERS

CAREERADVICE

JOBS

Jul24,2014

PostReply

TypeyourcommenthereandpressEnterKey....

FollowComments

COMMENTUSING
13 comments

Add a comment

Sudhir Patil Software Engineer at PALASH Healthcare Systems Pvt. Ltd


Really Good Article......Helpful for beginners .....Nice and Very Useful....
Reply Like May 17 at 3:14am
Alok Sharma Software Engineer at Capgemini India
ty very much .. good illustration
Reply Like January 10 at 2:57am
Mohan Gopi Software Engineer at HireCraft Software Pvt Ltd
simple and understandable, very usefull.
Reply Like October 8, 2013 at 12:13am
vivek_g86 (signed in using yahoo)
Very simple and useful. Thanks a lot.
Reply Like August 8, 2013 at 12:34pm
Num Krub Programmer at Prosoft 178 followers
Recommend for everybody, this is a great article.
Reply Like

2 January 14, 2013 at 12:54am


View 7 more

Facebook social plugin

MVPs

MOSTVIEWED

PHOTOS
CONTACTUS

LEGENDS

NOW

PRIZES

CODESNIPPETS

CONSULTING

PRIVACYPOLICY

TERMS&CONDITIONS

AWARDS

TRAINING
SITEMAP

REVIEWS

SURVEY

STUDENTS

CERTIFICATIONS

MEMBERS

HostedByCBeyondCloudServices

DOWNLOADS

MEDIAKIT

ABOUTUS

LINKS

IDEAS

REPORTABUSE

2014C#Corner.Allcontentsarecopyrightoftheirauthors.

13/08/2014 6:54 AM

You might also like