首页 > 开发 > 综合 > 正文

Consuming a Webservice Part 2 of 2

2024-07-21 02:21:35
字体:
来源:转载
供稿:网友

we discovered how to create a webservice.  in this article we're going to learn how to consume the 123aspx.com webservice and provide additional value to our users.

overview
there are 3 parts to consuming a webservice:
1. discovering the methods are available.
2. creating a proxy to the webservice.
3. calling the webservice

discovering the methods are available
before we can begin using a webserivce, we need to understand it.  asp.net makes this extremely easy by providing a helper page. if you call a webservice from your browser, such as http://64.85.12.73/websvc/whatsnew123aspx_ds.asmx , asp.net provides us with a very readable interface.  in this example, we can see we have only 1 method available to us: getnewresources. upon further examination, this webmethod returns a dataset. here is a screen shot of this page.

we can manually call this webservice by clicking invoke.  invoking our webservices produces a human readable xml document.  by examining this xml document, we can determine the 4 different fields of our dataset:
1. url
2. dateupdated
3. domain
4. name
here is a snippet of the xml produced from invoking our webservice.
<xsd:element name="url" type="xsd:string" minoccurs="0" />
<xsd:element name="dateupdated" type="xsd:string" minoccurs="0" />
<xsd:element name="domain" type="xsd:string" minoccurs="0" />
<xsd:element name="name" type="xsd:string" minoccurs="0" />  

creating a proxy
now that we know what our webservice returns, we can start to access it.   however, before we can access the webservice, we need to create a proxy dll that interfaces with the webservice.  by creating a proxy, all the work is done for us, and we are able to call the webservice, just like we would any other object that resides locally on our system.
to create the proxy we use a utility in the .net sdk called "wsdl.exe".  wsdl.exe is a command line tool that requires different switches. because i can never type the switches correctly the first time, i like to use batch files.  once we provide wsdl.exe with the correct switches, it outputs a .vb file (because i chose to use vb.net as the language, using the /l switch).   once you have the .vb file, you run it through the compiler, vbc.exe, to create the .dll. here is the batch file i used to create the proxy dll.

echo copy the following lines to a text file and save with the extension ".bat"
echo this batch file will create two files as specified by the outcodefilename
echo and the outdllfilename

set uselanguage=vb
set wsdlpath=http://64.85.12.73/websvc/whatsnew123aspx_ds.asmx?wsdl
set outcodefilename=whatsnew123aspx_ds.vb
set outdllfilename=whatsnew123aspx_ds.dll
set mynamespace=aspx123
set assemblies=system.web.dll,system.dll,system.web.services.dll,system.xml.dll,system.data.dll

c:/wsdl.exe /n:%mynamespace% /l:%uselanguage% /out:%outcodefilename% %wsdlpath%
c:/vbc.exe /out:%outdllfilename% /t:library /r:%assemblies% %outcodefilename%


in our batch file, we are using the following switches:
    /c: -- creates a proxy source code file from a sdl
    /n: -- namespace for our generated proxy,i.e.aspx123
    /l -- language we want to use (i.e. vb, csharp, etc.. )
    /out: -- the location to output our source code file
next we execute the vbc compiler,  feeding it the newly created source file to produce our .dll.  once we have our dll, we copy it to our /bin directory found under our application web.


calling our webservice
to call our webservice from our .aspx page, we first need to reference the namespace we created in our batch file, using an import directive.
        <%@ import namespace="aspx123" %>

now that we have our namespace, we can dimension a variable to hold an instance of our webmethod, and call our webmethod.
dim mywebsvc as aspx123.aspx123websvc = new aspx123websvc()
dim ds as dataset
ds = mywebsvc.getnewresources()

aspx123websvc is our class we defined in our webservice, and getnewresources() is our webmethod that returns our dataset.

additional notes
because www.123aspx.com is really only updated once a day, we don't want to have to call the webservice every time someone views our website.  instead, we want to cache the default dataview of the dataset on our webserver, and set it to expire every 12 hours.   the following code will achieve this.
dim cache_duration as integer = 12
dim aspxnewresources as dataview
aspxnewresources = ctype( cache("aspxnewresources"),dataview )

if ( aspxnewresources is nothing ) then
    rem -- use the webservice, and populate the cache
    dim mywebsvc as aspx123.aspx123websvc = new aspx123websvc()
    dim ds as dataset
    dim fwebservice as boolean = false

    try
        ds = mywebsvc.getnewresources()
        fwebservice = true
    catch eweb as system.exception
        fwebservice = false
    end try

    if fwebservice then 'insert the dataview into the cache
        aspxnewresources = new dataview( ds.tables("resourcelist") )
        cache.insert("aspxnewresources", aspxnewresources, nothing, datetime.now.addhours(cache_duration), timespan.zero)
    end if

end if
mydatagrid.datasource=aspxnewresources
mydatagrid.databind()


aspxnewresources is declared as a dataview and we attempt to retrieve it from the webserver cache by
    aspxnewresources = ctype( cache("aspxnewresources"),dataview )
if aspxnewresources  is nothing, then we need to call our webservice and populate the cache, by calling cache.insert. cache.insert is an overloaded function. in our example, cache.insert takes the following 5 parameters:
cache.insert(param1, param2, param3, param4, param5)    
    param1 -- name of the cache object
    param2 -- object to cache
    param3 --    cache dependency
    param4 -- timespan for cache to last
    param5 -- absolute time for cache to expire

our results
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表