Sie sind auf Seite 1von 11

Page 1 of 11

Microsoft.NET2.0WebServiceandWebServiceClientTutorial

MatthewKubicina
CIS764DatabaseDesign
KansasStateUniversity

Requirements
a. Microsoft.NETFramework2.0
b. MicrosoftVisualStudio2005

Outline
Part1CreatetheVisualStudioSolution
Part2CreatetheWebService
Part3TestingtheWebService
Part4ParameterizedWebMethods
Part5CreatetheClientApplication
Part6AddtheWebReference
Part7ConsumingtheWebService
Part8TestingtheClientApplication
Part9PassingParameterstotheWebService

Page 2 of 11
Part1CreatetheVisualStudioSolution
a. OpenVisualStudio2005.
b. GototheFilemenuandthenselectNewProject.
c. Intheprojecttypelisting,clickonVisualC#.Inthetemplatessection,selectASP.NETWeb
ServiceApplication.Intheprojectsettings,fillinthefollowingpropertiesandclickOK.
Name:DateTimeWebService
Location:Default
SolutionName:WebServiceTutorial
Createdirectoryforsolution:Checked

Page 3 of 11
d. VisualStudiocreatesasolutionandawebserviceproject.Thewebservice,containedinthe
Service1.asmxfile,containsadefaultHelloWorldmethod.

e. Insolutionexplorer,deletetheService1.asmxfiletoremovethedefaultwebservicefileby
rightclickingonthefileandselectingDelete.ClickOKontheconfirmationboxthatwill
appear.

Page 4 of 11
Step2CreatetheWebService
a. Inthesolutionexplorer,rightclickontheDateTimeWebServiceprojectnodeandselectAdd,
NewItem.
b. SelectWebServiceinthelistoftemplates.NamethefileCurrentDateTime.asmxandclick
Add.

c. VisualStudioaddsthefiletotheprojectandopensit.InthefileisadefaultHelloWorldweb
function.DeletetheHelloWorldfunctionandtheWebMethodattributefromthesource
codeinthefile.

d. CreateanewfunctionintheCurrentDateTimeclass.Thefunctionshouldlooklikethecode
snippetbelow.

publ i c Dat eTi me Get Cur r ent Dat eTi me( )


{
r et ur n Dat eTi me. Now;
}

e. Tomakethenewlycreatedfunctionexposedviathewebservice,addtheWebMethod
attributeabovethefunctiondefinition.Thecodeshouldlooklikethefollowingwhencompleted.

[ WebMet hod]
publ i c Dat eTi me Get Cur r ent Dat eTi me( )
{
r et ur n Dat eTi me. Now;
}

Page 5 of 11
Part3TestingtheWebService
a. Totestthewebservicefunction,gotoDebug,StartDebugging.VisualStudiowillcompilethe
webserviceandlaunchthewebserviceURLautomatically.

b. ClickonthelinknamedGetCurrentDateTimetoopenthetestharnessforthefunctionwejust
created.ClicktheInvokebuttontoexecutethefunctiononthewebserver.
c. Anewwindowwillopencontainingtheresponsefromthewebserver.Ifthewebserviceis
respondingcorrectly,itshouldreturnanXMLsoapmessagecontainingthecurrentdate/timeon
thewebserver.

d. Closethetwowebbrowserwindowsforthewebservice.Thiswillstopthedebuggingprocess
andshouldreturnyoutotheVisualStudioeditor.

Page 6 of 11
Part4ParameterizedWebMethods
a. Addanewwebfunctionthatacceptstwoparametersbypastinginthecodesnippetbelow.This
functionacceptsadateandanintegerrepresentingthenumberofdaystoaddtothegivendate
instance.Thewebfunctionwillreturntheresultofthecalculation.

[ WebMet hod]
publ i c Dat eTi me AddDays( Dat eTi me Dat e, i nt NumDays)
{
r et ur n Dat e. AddDays( NumDays) ;
}

b. Totestthenewwebservicefunction,gotoDebug,StartDebugging.VisualStudiowill
compilethewebserviceandlaunchthewebserviceautomatically.
c. ClickonthelinknamedAddDaystoopenthetestharnessforthefunctionwejustcreated.

d. Inthetestharness,entersomevaluesforthetwoavailableparameters.Afterclickingthe
Invokebutton,thewebserviceshouldreturnthevalueofthecalculation.
e. Closethetwowebbrowserwindowsforthewebservice.Thiswillstopthedebuggingprocess
andshouldreturnyoutotheVisualStudioeditor.

Page 7 of 11
Part5CreatetheClientApplication
a. Tocreatetheclientapplicationproject,gotoFile,Add,NewProject.
b. Intheprojecttypelist,clickonVisualC#.
c. Inthetemplatessection,selectConsoleApplication.
d. Intheprojectsettings,fillinthefollowingpropertiesandclickOK.
Name:DateTimeConsumer
Location:Default

e. VisualStudioaddsthenewprojecttotheexistingsolutionandopensuptheMainstaticclass.

Page 8 of 11
Part6AddtheWebReference
a. AddareferencetotheexistingwebservicebyrightclickingontheDateTimeConsumerproject
andselectingAddWebReference.

b. SelectWebServicesinthissolution.Inthelistofwebservices,selectCurrentDateTime

c. IntheWebReferenceNameproperty,enterDateTimeWSandhitAddReference.Visual
Studioautomaticallyaddstheclientclassesforthewebservicetotheproject.

Page 9 of 11
Part7ConsumingtheWebService
a. IntheMainfunctionoftheprogramclass,addthefollowingcodesnippettorepresenttheweb
serviceclient.Thissectionofcoderetrievesthecurrentdatetimefromthewebserviceand
displaysittotheconsolefortheuser.

st at i c voi d Mai n( st r i ng[ ] ar gs)


{
Dat eTi meWS. Cur r ent Dat eTi me Cl i ent = new
Dat eTi meConsumer . Dat eTi meWS. Cur r ent Dat eTi me( ) ;
Dat eTi me Ser ver Dat eTi me = Cl i ent . Get Cur r ent Dat eTi me( ) ;

Consol e. Wr i t eLi ne( " Cur r ent Dat e/ Ti me on Ser ver : " +
Ser ver Dat eTi me. ToSt r i ng( ) ) ;
}

Page 10 of 11
Part8TestingtheClientApplication
a. Totesttheclientprogram,gotoDebug,StartDebugging.Thiswillautomaticallystartthe
webserviceonthewebserverandbeginexecutingtheclientapplication.Theprogramshould
displaythecurrentdate/timefromtheserver.Thewindowwillautomaticallyclosewhen
debuggingends.Inordertoverifyoutput,utilizetheStartWithoutDebuggingoptionandthe
commandpromptwillremainopenupontermination.

Page 11 of 11
Part9PassingParameterstotheWebService
a. ToconsumetheAddDaysfunction,pastethefollowingcodesnippetintotheMainfunction
belowtheexistingcode.Thiscodesnippetsendsthecurrentdate/timeontheclientmachineto
theserverandthewebservicesaddsthenumberofdaysspecifiedtotheprovideddate.Inthis
instance,itaddstwodaystotheprovidedtime.

Dat eTi me AddedDays = Cl i ent . AddDays( Dat eTi me. Now, 2) ;



Consol e. Wr i t eLi ne( " Cur r ent Dat e/ Ti me pl us 2 Days: " + AddedDays. ToSt r i ng( ) ) ;

b. Runningtheapplicationshoulddisplaytheresultsofbothwebfunctionstotheuserinthe
console.

Das könnte Ihnen auch gefallen