WCF from MSDNWord文档下载推荐.docx
- 文档编号:17898354
- 上传时间:2022-12-12
- 格式:DOCX
- 页数:23
- 大小:25.35KB
WCF from MSDNWord文档下载推荐.docx
《WCF from MSDNWord文档下载推荐.docx》由会员分享,可在线阅读,更多相关《WCF from MSDNWord文档下载推荐.docx(23页珍藏版)》请在冰豆网上搜索。
File
menuandselect
New,
Project.Inthe
NewProject
dialog,select
VisualBasic
or
VisualC#,andchoosetheConsoleApplication
template,andnameit
Service.Usethedefault
Location.
3.ForaC#projectVisualStudiocreatesafilecalledProgram.cs.ThisclasswillcontainanemptymethodcalledMain().ForaVBproject,VisualStudiocreatesafilecalledModule1.vbwithanemptysubroutinecalledMain().Thesemethodsarerequiredforaconsoleapplicationprojecttobuildcorrectly,soyoucansafelyleavethemintheproject.
4.Changethedefault
Service
namespaceto
Microsoft.ServiceModel.Samples.Todothis,right-clicktheprojectinthe
SolutionExplorer
andselect
Properties.Makesurethatthe
Application
tabontheleftsideofthe
Properties
dialogisselected.ForaC#project,type
Microsoft.ServiceModel.Samples
intheeditboxlabeled
DefaultNamespace.ForaVBproject,type
Root
namespace.Clickthe
SaveAll
tosaveyourchanges.
5.IfyouareusingC#,changethenamespaceinthegeneratedProgram.csfileto
asshowninthefollowingexample.
C#
namespaceMicrosoft.ServiceModel.Samples
{
classProgram
{
staticvoidMain(string[]args)
}
}
IfyouareusingVB,addaNamespacestatementandanEndNamespacestatementtothegeneratedModule1.vbasshowninthefollowingexample.
VB
NamespaceMicrosoft.ServiceModel.Samples
ModuleModule1
SubMain()
EndSub
EndModule
EndNamespace
6.Addareferenceto
System.ServiceModel.dll
totheproject:
a.Inthe
SolutionExplorer,right-clickthe
References
folderundertheprojectfolderandchoose
AddReference.
Note:
Whenusingacommand-linecompiler(forexample,Csc.exeorVbc.exe),youmustalsoprovidethepathtotheassemblies.Bydefault,onacomputerrunningWindowsVistaforexample,thepathis:
"
Windows\Microsoft.NET\Framework\v4.0"
.
b.Selectthe
.NET
tabinthe
AddReference
dialogandscrolldownuntilyousee
System.ServiceModel
(version4.0.0.0),selectit,andclick
OK.
7.Adda
using
statement(Imports
inVisualBasic)forthe
namespace.
usingSystem.ServiceModel;
8.InProgram.cs(Module1.vbforVB),defineanewinterfacecalled
ICalculator
andapplythe
attributetotheinterfacewitha
Namespace
valueof"
http:
//Microsoft.ServiceModel.Samples"
.Specifyingthenamespaceexplicitlyisabestpracticebecauseitpreventsthedefaultnamespacevaluefrombeingaddedtothecontractname.
Whenusingattributestoannotateaninterfaceorclass,youcandropthe"
Attribute"
partfromtheattributename.So
becomes[ServiceContract]
inC#,or
<
ServiceContract>
inVisualBasic.
9.C#
10.VB
11.[ServiceContract(Namespace="
)]
12.publicinterfaceICalculator
13.
14.Declareamethodforeachoftheoperationsthe
contractexposes(add,subtract,multiply,anddivide)withintheinterfaceandapplytheOperationContractAttribute
attributetoeachmethodthatyouwanttoexposeaspartofthepublicWCFcontract.
[OperationContract]
doubleAdd(doublen1,doublen2);
doubleSubtract(doublen1,doublen2);
doubleMultiply(doublen1,doublen2);
doubleDivide(doublen1,doublen2);
Example
Thefollowingcodeexampleshowsabasicinterfacethatdefinesaservicecontract.
usingSystem;
//Step5:
AddtheusingstatementfortheSystem.ServiceModelnamespace
namespaceMicrosoft.ServiceModel.Samples
//Step6:
Defineaservicecontract.
[ServiceContract(Namespace="
publicinterfaceICalculator
//Step7:
Createthemethoddeclarationforthecontract.
[OperationContract]
doubleAdd(doublen1,doublen2);
doubleSubtract(doublen1,doublen2);
doubleMultiply(doublen1,doublen2);
doubleDivide(doublen1,doublen2);
Nowtheinterfaceiscreated,proceedto
ImplementaWindowsCommunicationFoundationServiceContract
toimplementtheinterface.Fortroubleshootinginformation,see
TroubleshootingtheGettingStartedTutorial.
ImplementaWindowsCommunicationFoundationServiceContract
ThisisthesecondofsixtasksrequiredtocreateabasicWindowsCommunicationFoundation(WCF)serviceandaclientthatcancalltheservice.Foranoverviewofallsixtasks,seethe
CreatingaWCFservicerequiresthatyoufirstcreatethecontract,whichisdefinedusinganinterface.Formoreinformationaboutcreatingtheinterface,see
DefineaWindowsCommunicationFoundationServiceContract.Thenextstep,showninthisexample,istoimplementtheinterface.Thisinvolvescreatingaclasscalled
CalculatorServicethatimplementstheuser-defined
interface.Thecodeusedforthistaskisprovidedintheexamplefollowingtheprocedure.
ToimplementaWCFservicecontract
1.Createanewclasscalled
CalculatorService
inthesamefilewhereyoudefinedthe
interface.The
implementsthe
interface.
publicclassCalculatorService:
ICalculator
2.Implementeachmethoddefinedinthe
interfacewithinthe
class.
publicdoubleAdd(doublen1,doublen2)
doubleresult=n1+n2;
Console.WriteLine("
ReceivedAdd({0},{1})"
n1,n2);
//Codeaddedtowriteoutputtotheconsolewindow.
Return:
{0}"
result);
returnresult;
publicdoubleSubtract(doublen1,doublen2)
doubleresult=n1-n2;
ReceivedSubtract({0},{1})"
publicdoubleMultiply(doublen1,doublen2)
doubleresult=n1*n2;
ReceivedMultiply({0},{1})"
publicdoubleDivide(doublen1,doublen2)
doubleresult=n1/n2;
ReceivedDivide({0},{1})"
Thewriteoutputcodehasbeenaddedtomaketestingconvenient.
Thefollowingcodeexampleshowsboththeinterfacethatdefinesthecontractandtheimplementationoftheinterface.
//Defineaservicecontract.
//Step1:
Createserviceclassthatimplementstheservicecontract.
publicclassCalculatorService:
//Step2:
Implementfunctionalityfortheserviceoperations.
publicdoubleAdd(doublen1,doublen2)
publicdoubleSubtract(doublen1,doublen2)
publicdoubleMultiply(doublen1,doublen2)
publicdoubleDivide(doublen1,doublen2)
Nowtheservicecontractiscreatedandimplemented.Buildthesolutiontoensuretherearenocompilationerrorsandthenproceedto
HostandRunaBasicWindowsCommunicationFoundationService
toruntheservice.Fortroubleshootinginformation,see
CompilingtheCode
Ifyouareusingacommand-linecompiler,youmustreferencethe
assembly.
IfyouareusingVisualStudio,ontheBuildmenuclickBuildSolution(orpressCTRL+SHIFT+B).
HostandRunaBasicWindows
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- WCF from MSDN
![提示](https://static.bdocx.com/images/bang_tan.gif)