Sunteți pe pagina 1din 15

Calling a Web Service from

an ASP.NET Web Page



Tasks for this Module

How wed ideally call a Web service from an


ASP.!T Web "a#e

$reatin# a Pro%y class with wsdl.exe


&
'sin# the Pro%y class
&
!%aminin# the Pro%y class
&
!%aminin# wsdl.exe
Time allotment for this module and questions: 75 minutes

$allin# a Web Service from an
ASP.!T Web Pa#e

(deally the develo"er callin# a Web service from an


ASP.!T Web "a#e should be able to work with the
Web service usin# the same syntax as if she was
workin# with a com"onent local to the Web server.

Since callin# a Web service involves marshallin# the


in#oin# "arameters "ro"erly and bein# able to marshal
the return "arameters "ro"erly) obviously "erformin#
such actions in an ASP.!T Web "a#e would make
the synta% differ wildly from usin# a local com"onent)
where such e%"licit marshallin# was not needed.

!nter the Pro%y $lass

(n order to achieve the end #oal of treatin# a Web


service call *ust like a call to a local com"onent)
the .!T framework contains a command+line
"ro#ram called wsdl.exe ,Web Service
Descri"tion Lan#ua#e-) which creates a "ro%y
class for a s"ecific Web service.

This "ro%y class serves as an intermediate between


the ASP.!T Web "a#e and the Web service.
,The diagram on the next slide should help clarify
this-

WEB
SERER !"
Web service
WEB SERER !#
SomePa#e.as"%
an ASP.NT !e" page
P./01 $2ASS
The .ole of the Pro%y $lass
STEP #$
The ASP.!T Web "a#e Some Pa#e.as"%
instantiates an instance of the Pro%y class3
Dim objClass = New ProxyClassName
And calls one of the Web service methods3
objClass.MethodName(paramList)

$reatin# a Pro%y $lass

$reatin# a Pro%y class involves three ste"s3


&
$reate the source code for the class) which de"ends u"on the
WS42 of the Web service.
&
$om"ile the class into a 422
&
$o"y the 422 to the \bin directory

/nce these three ste"s are com"lete we can use our Pro%y
class to access Web service methods as if they were
methods of a local com"onent5
#isual Studio.NT can perform all of these tas$s %ith the clic$ of a
"utton. !e %ill examine ho% to do it &ia the command'line for those
%ho don(t ha&e #S.NT installed.

$reatin# a Pro%y $lass
)*ontinued+

When creatin# a "ro%y class) you must s"ecify the


WS42 for the Web service. This 0M2+formatted file
contains information on the incomin# and out#oin#
"arameters. See
http://localhost/UCSDTeaching/Session6/SimpleWebService.asmx?WSDL

$reate the "ro%y class by sim"ly callin# wsdl.exe


with the full '.2 of the WS42 as the "arameter3
Wsdl http://localhost/UCSDTeachin/Session!/SimpleWebSe"#ice.asmx$WSD%

$reatin# a Pro%y $lass
)*ontinued+

/nce we have the source code we need to com"ile


this code into a 422.

6inally) once we have the Pro%y class com"iled


into a 422) we need to move it to the \bin
directory) since that is the location where our
422s need to reside to be "icked u" automatically
by ASP.!T.
Run cl.demo1.txt demo

'sin# the Pro%y $lass

/nce we have the Pro%y class com"iled and the 422


in the \bin directory) we can use it throu#h an
ASP.!T Web "a#e.
Show CallSimpleWS.aspx Demo

ote how) when e%aminin# the synta%) it is im"ossible


to tell if the instantiation of the &csd.'ath class is
referrin# to a local or remote com"onent. ,ission
accomplished-

!%aminin# the Pro%y $lass

2ets take a moment and e%amine 'ath.cs) the source


code for the Pro%y class created by wsdl.exe
Examine ath.cs

ote that the Pro%y class is in $7 + wsdl.exe "rovides


a command+line switch to s"ecify what lan#ua#e to use.

ote that a class is created with the same name as the


Web service class ,'ath) in this e%am"le-. Also) for
each "ublic Web method there is a "ublic method in the
Pro%y class with the correct in"ut and out"ut
"arameters.

!%aminin# the Pro%y $lass
,*ontinued-

ote that in this Pro%y class the Web


service is bein# called usin# the S/AP
re8uest9res"onse "ayloads.

All of the com"le% behavior in hidden in


macros or throu#h methods defined in base
classes from which our custom 'ath class
inherits from.

!%aminin# wsdl.exe
(
Wsdl.exe contains only one required "arameter)
a '.2 or "ath to the WS42 for the Web service.
The followin# o"tional "arameters can be s"ecified3
)
/lan&ae:language & s"ecifies what lan#ua#e the
Pro%y class should be created in ,vb9cs9*s-.
)
/p"otocol:protocol & s"ecifies the "ayload "rotocol to
be used ,S/AP) Htt":et) Htt"Post-
)
/namespace:namespace & the names"ace of the
#enerated Pro%y class.
)
/o&t:filename & the filename of the Pro%y class.

Module ; in $onclusion

(deally) wed like to be able to call Web services from an


ASP.!T Web "a#e in an identical fashion to which we call
local com"onents.

$reatin# a Pro%y class allows us to call a Web services


methods as if the Web service resided locally. The Pro%y
class handles the actual HTTP re8uest as well as the
marshallin# of the in"ut and out"ut "arameters.

The command+line "ro#ram wsdl.exe can create the


source code of the Pro%y class for us based u"on a Web
services WS42. /nce this class is com"iled and "laced in
the \bin directory) it can be used throu#h an ASP.!T Web
"a#e.

.uestions///
No% %ould "e a great time
to as$ questions- 0on(t you
thin$/ So as$ a%ay-

S-ar putea să vă placă și