Beruflich Dokumente
Kultur Dokumente
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.
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.
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.
b. Runningtheapplicationshoulddisplaytheresultsofbothwebfunctionstotheuserinthe
console.