a simple example of wcf service.pdf
TRANSCRIPT
-
8/10/2019 A Simple Example of WCF Service.pdf
1/13
AskaQuestion
50 3,120
HerewelookatanexampleofaWCFServiceinwhichwecreateaserviceforapplyingsimplecalculatorfuncons.
ASimpleExampleofWCFServicePostedbyMahakGuptainArcles|WCFwithC#onMarch22,2012
Tweet 6 197560 9 3
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 NameSection). 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.
01 MobileAppDevelopment inDetails
02 HowIBecameaCSharpcornerAddict
03 DependencyInjecon(DI)andInversion
ofControl(IOC)
04 IntroducontoDesignPaerns
05 PerformingCRUDOperaonWithDapper
(OpenSourceORMFromStackOverflow)
inMVC
06 DynamicLINQQueryinC#
07 AuthencaonUsingFacebook,Google
andMicroso AccountinUniversalApps
UsingMVVM
08 DatatableinViewDataSampleinMVC
Day3
09 EffecveUseofBootstrapWithASP.Net
10 ASP.NetMVC4,EntyFrameworkand
jqGridDemoWithSample
ViewAll
TRENDINGUP
Follow @csharpcorner
Contribute
MobileAppDevelopmentinDetails
21LikeLike
Find us on Facebook
C# Corner
54,771 people like C# Corner.
Facebook social plugin
LikeLike
TECHNOLOGIES ANSWERS BLOGS VIDEOS INTERVIEWS BOOKS NEWS CHAPTERS CAREERADVICE JOBS Mukesh
mple Example of WCF Service http://www.c-sharpcorner.com/UploadFile/mahakgupta/a-simple-e
3 13/08/2014
-
8/10/2019 A Simple Example of WCF Service.pdf
2/13
For this we first add the namespace System.Runtime.Serialization. And then we write thefollowing code.
Code
using System.Runtime.Serialization;
namespaceCalc
{
[DataContract]
publicclassCalc
{
[DataMember] publicdoublen1;
[DataMember]
publicdoublen2;
}
}
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
publicinterfaceICalcService
{
TECHNOLOGIES ANSWERS BLOGS VIDEOS INTERVIEWS BOOKS NEWS CHAPTERS CAREERADVICE JOBS
mple Example of WCF Service http://www.c-sharpcorner.com/UploadFile/mahakgupta/a-simple-e
3 13/08/2014
-
8/10/2019 A Simple Example of WCF Service.pdf
3/13
Here we type the following operations:
publicinterfaceICalcService
{
doubleAdd(doublen1, doublen2);
doubleSubtract(doublen1, doublen2);
doubleMultiply(doublen1, doublen2);
doubleDivide(doublen1, doublen2);
}
After that, we delare it as a Service Contract, which is in a different namespace:
System.ServiceModel. Now we look at the code:
usingSystem.ServiceModel;
namespaceCalc
{
[ServiceContract]
publicinterfaceICalcService
{
[OperationContract]
doubleAdd(doublen1, doublen2);
[OperationContract]
doubleSubtract(doublen1, doublen2);
[OperationContract]
doubleMultiply(doublen1, doublen2);
[OperationContract]
doubleDivide(doublen1, doublen2);
}
}
Step 6: After that we add another class: CalcService.cs. It is an actual service implementation
class, so here we can specify the ICalcServicelike this.
Code
publicclassCalcService:ICalcService
{
}
Here we implement an interface by right-clicking and choosing the option Implement InterfaceExplicitly:
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.
usingSystem.ServiceModel;
namespaceCalc
{
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
publicclassCalcService:ICalcService
{
publicdoubleAdd(doublen1, doublen2)
{ returnn1 + n2;
}
publicdoubleSubtract(doublen1, doublen2)
{
returnn1 - n2;
}
publicdoubleMultiply(doublen1, doublen2)
{
returnn1 * n2;
}
publicdoubleDivide(doublen1, doublen2)
{
returnn1 / n2;
}
}
}
TECHNOLOGIES ANSWERS BLOGS VIDEOS INTERVIEWS BOOKS NEWS CHAPTERS CAREERADVICE JOBS
mple Example of WCF Service http://www.c-sharpcorner.com/UploadFile/mahakgupta/a-simple-e
3 13/08/2014
-
8/10/2019 A Simple Example of WCF Service.pdf
4/13
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:
TECHNOLOGIES ANSWERS BLOGS VIDEOS INTERVIEWS BOOKS NEWS CHAPTERS CAREERADVICE JOBS
mple Example of WCF Service http://www.c-sharpcorner.com/UploadFile/mahakgupta/a-simple-e
3 13/08/2014
-
8/10/2019 A Simple Example of WCF Service.pdf
5/13
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.
TECHNOLOGIES ANSWERS BLOGS VIDEOS INTERVIEWS BOOKS NEWS CHAPTERS CAREERADVICE JOBS
mple Example of WCF Service http://www.c-sharpcorner.com/UploadFile/mahakgupta/a-simple-e
3 13/08/2014
-
8/10/2019 A Simple Example of WCF Service.pdf
6/13
Step 9: After that we click on Add or any other function. When we click on Add the followingwindow will be appear:
Here we enter values for n1 and n2 and click on the Invoke Button. The result will appear as:
ContentsaddedbypolasatyamonNov21,2012
ArcleExtensions
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.
TECHNOLOGIES ANSWERS BLOGS VIDEOS INTERVIEWS BOOKS NEWS CHAPTERS CAREERADVICE JOBS
mple Example of WCF Service http://www.c-sharpcorner.com/UploadFile/mahakgupta/a-simple-e
3 13/08/2014
-
8/10/2019 A Simple Example of WCF Service.pdf
7/13
Here we select the WCF in the Project Type and then we select the WCF Service Library in it. Afterthat, we specify the name of this library to be Calc (which we can define in the Name Section). Andthen 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 thefollowing code.
Code
using System.Runtime.Serialization;
namespaceCalc
{
[DataContract]
publicclassCalc
{
[DataMember]
publicdoublen1;
[DataMember]
publicdoublen2;
}
}
Step 4: After that we add another class.
TECHNOLOGIES ANSWERS BLOGS VIDEOS INTERVIEWS BOOKS NEWS CHAPTERS CAREERADVICE JOBS
mple Example of WCF Service http://www.c-sharpcorner.com/UploadFile/mahakgupta/a-simple-e
3 13/08/2014
-
8/10/2019 A Simple Example of WCF Service.pdf
8/13
Here we name it ICalcService.cs.
Step 5: Now we declare as an interface not a class so we change the code like th is.
Code
publicinterfaceICalcService
{
}
Here we type the following operations:
publicinterfaceICalcService
{
doubleAdd(doublen1, doublen2);doubleSubtract(doublen1, doublen2);
doubleMultiply(doublen1, doublen2);
doubleDivide(doublen1, doublen2);
}
After that, we delare it as a Service Contract, which is in a different namespace:
System.ServiceModel. Now we look at the code:
usingSystem.ServiceModel;
namespaceCalc
{
[ServiceContract]
publicinterfaceICalcService
{
[OperationContract]
TECHNOLOGIES ANSWERS BLOGS VIDEOS INTERVIEWS BOOKS NEWS CHAPTERS CAREERADVICE JOBS
mple Example of WCF Service http://www.c-sharpcorner.com/UploadFile/mahakgupta/a-simple-e
3 13/08/2014
-
8/10/2019 A Simple Example of WCF Service.pdf
9/13
doubleSubtract(doublen1, doublen2);
[OperationContract]
doubleMultiply(doublen1, doublen2);
[OperationContract]
doubleDivide(doublen1, doublen2);
}
}
Step 6: After that we add another class: CalcService.cs. It is an actual service implementation
class, so here we can specify the ICalcServicelike this.
Code
publicclassCalcService:ICalcService
{
}
Here we implement an interface by right-clicking and choosing the option Implement InterfaceExplicitly:
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.
usingSystem.ServiceModel;
namespaceCalc
{
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
publicclassCalcService:ICalcService
{
publicdoubleAdd(doublen1, doublen2)
{
returnn1 + n2;
}
publicdoubleSubtract(doublen1, doublen2)
{returnn1 - n2;
}
publicdoubleMultiply(doublen1, doublen2)
{
returnn1 * n2;
}
publicdoubleDivide(doublen1, doublen2)
{
returnn1 / n2;
}
}
}
Step 7: Now we try to run the program; it can not be run since it is not yet completed. Nowright-click on the App.config file and select the Edit WCF Configuration option.
TECHNOLOGIES ANSWERS BLOGS VIDEOS INTERVIEWS BOOKS NEWS CHAPTERS CAREERADVICE JOBS
mple Example of WCF Service http://www.c-sharpcorner.com/UploadFile/mahakgupta/a-simple-e
3 13/08/2014
-
8/10/2019 A Simple Example of WCF Service.pdf
10/13
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.
TECHNOLOGIES ANSWERS BLOGS VIDEOS INTERVIEWS BOOKS NEWS CHAPTERS CAREERADVICE JOBS
mple Example of WCF Service http://www.c-sharpcorner.com/UploadFile/mahakgupta/a-simple-e
13 13/08/2014
-
8/10/2019 A Simple Example of WCF Service.pdf
11/13
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 followingwindow will be appear:
TECHNOLOGIES ANSWERS BLOGS VIDEOS INTERVIEWS BOOKS NEWS CHAPTERS CAREERADVICE JOBS
mple Example of WCF Service http://www.c-sharpcorner.com/UploadFile/mahakgupta/a-simple-e
13 13/08/2014
-
8/10/2019 A Simple Example of WCF Service.pdf
12/13
RELATEDARTICLES
Here we enter values for n1 and n2 and click on the Invoke Button. The result will appear as:
ASimpleDuplexServiceinWCF HosngWCFServiceinAzureandConsuming
WCFDataServiceinWindows7applicaon
WCFServiceForInserngDataIntoDatabase
UsingASP.NET
WCFApplicaonCommunicatewithConsole
Applicaon
HosngWCFServicewithnetTcpBindingin
WindowsService
SimpleWCFwebservicetoreceiveparameter
fromHTTPPOSTrequestbody
FetchingImageUsingWCFRESTService SilverlightApplicaonWithMVVM,WCFandEntyFramework
DebuggingaWCFServicefromS ilverlight IntroducontoWCFDataserviceandODATA
COMMENTS 9of9
Mar25,20120Like 0Reply PostReply
SteveBlodge
Thanksforthegreatarcle!
Dec21,20120Like 0Reply PostReply
ShaijuJanardhanan
ThankU
Mar26,20130Like 0Reply PostReply
VinodKumarChennegowda
Greatexplanaons
Jul26,20130Like 0Reply PostReply
ChandanSinhaVerygoodworkingexampleofWCF.GreatWork!Thanks.
Aug06,20130Like 0Reply PostReply
KailashChandraBehera
NiceArcle
Nov21,20130Like 0Reply PostReply
SantoshKumar
Veryeasyandnicedescripon.
Nov26,20130Like 0Reply PostReply
MahakGupta
Thanks
DarlingDoll
WithVS2013neramework4.5,anerror:AddSyncisnotsupportedintheWCFtestclientbecauseitusestype
TECHNOLOGIES ANSWERS BLOGS VIDEOS INTERVIEWS BOOKS NEWS CHAPTERS CAREERADVICE JOBS
mple Example of WCF Service http://www.c-sharpcorner.com/UploadFile/mahakgupta/a-simple-e
13 13/08/2014
-
8/10/2019 A Simple Example of WCF Service.pdf
13/13
HostedByCBeyondCloudServices
2014C#Corner.Allcontentsarecopyrightoftheirauthors.
Jul24,20140Like 0Reply PostReply
TypeyourcommenthereandpressEnterKey....
FollowComments
COMMENTUSING
Add a comment
View 7 more
Facebook social plugin
13 comments
Sudhir Patil Software Engineerat PALASH Healthcare Systems Pvt. LtdReally Good Article......Helpful for beginners .....Nice and Very Useful....
Reply Like May 17 at 3:14am
Alo k Shar ma Software Engineerat Capgemini India
ty very much .. good illustration
Reply Like January 10 at 2:57am
Mohan Gopi Software Engineerat 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 LikeAugust 8, 2013 at 12:34pm
Num Krub Programmerat Prosoft 178 followers
Recommend for everybody, this is a great article.
Reply Like January 14, 2013 at 12:54am2
M VPs MO STVIE WE D L EGENDS NOW PRI ZE S AWARD S REVIE WS SURVE Y C ERTI FI CAT IO NS D OW NL OADS
CONTACTUS PRIVACYPOLICY TERMS&CONDITIONS SITEMAP REPORTABUSE
PHOTOS CODESNIPPETS CONSULTING TRAINING STUDENTS MEMBERS MEDIAKIT ABOUTUS L INKS IDEAS
TECHNOLOGIES ANSWERS BLOGS VIDEOS INTERVIEWS BOOKS NEWS CHAPTERS CAREERADVICE JOBS
mple Example of WCF Service http://www.c-sharpcorner.com/UploadFile/mahakgupta/a-simple-e