Professional Documents
Culture Documents
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
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
13/08/2014 6:54 AM
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.
13/08/2014 6:54 AM
3 of 13
TECHNOLOGIES
ANSWERS
http://www.c-sharpcorner.com/UploadFile/mahakgupta/a-simple-example...
BLOGS
VIDEOS
INTERVIEWS
BOOKS
NEWS
CHAPTERS
CAREERADVICE
JOBS
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:
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
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.
13/08/2014 6:54 AM
5 of 13
TECHNOLOGIES
ANSWERS
http://www.c-sharpcorner.com/UploadFile/mahakgupta/a-simple-example...
BLOGS
VIDEOS
INTERVIEWS
BOOKS
NEWS
CHAPTERS
CAREERADVICE
JOBS
13/08/2014 6:54 AM
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
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
8 of 13
TECHNOLOGIES
ANSWERS
http://www.c-sharpcorner.com/UploadFile/mahakgupta/a-simple-example...
BLOGS
VIDEOS
INTERVIEWS
BOOKS
NEWS
CHAPTERS
CAREERADVICE
JOBS
which
is
in
different
namespace:
using System.ServiceModel;
namespace Calc
{
[ServiceContract]
public interface ICalcService
{
[OperationContract]
13/08/2014 6:54 AM
9 of 13
TECHNOLOGIES
ANSWERS
http://www.c-sharpcorner.com/UploadFile/mahakgupta/a-simple-example...
BLOGS
VIDEOS
INTERVIEWS
BOOKS
NEWS
CHAPTERS
CAREERADVICE
JOBS
}
Here we implement an interface by right-clicking and choosing the option Implement Interface
Explicitly:
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
10 of 13
TECHNOLOGIES
ANSWERS
http://www.c-sharpcorner.com/UploadFile/mahakgupta/a-simple-example...
BLOGS
VIDEOS
INTERVIEWS
BOOKS
NEWS
CHAPTERS
CAREERADVICE
JOBS
13/08/2014 6:54 AM
11 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:
13/08/2014 6:54 AM
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
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
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