Consuming a Webservice Part 2 of 2

Source: Internet
Author: User
Tags command line insert readable switches wsdl
Web we discovered how to create a webservice. In this article we "re going to learn, 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 u  s with a very readable interface. In this example, we can be have only 1 method available to us:getnewresources. Upon further examination, this WebMethod returns a dataset. This are a screen shot the 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 in our dataset:
1. URL
2. dateupdated
3. Domain
4. Name
This 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 so 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 this interfaces with the webservice. By creating a proxy, all the work are done for us, and we are able to call the webservice, just like we would no other obj ECT that resides locally on our system.
To create the proxy we use a utility 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 "the" the "I", "I like" 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, run it through the compiler, vbc.exe, to create the. dll. 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 would create two files as specified by the Outcodefilename
The 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 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 newly created source file to produce our. dll. Once we have our DLLs, we copy it to Our/bin directory found under our application Web.


Calling our Webservice
To call my webservice from our. aspx page, we are need to reference the namespace we created file, using a n Import directive.
<%@ Import namespace= "AspX123"%>

Now so we have we namespace, we can dimension a variable to hold a instance of my we webmethod, and call our WebMethod.
Dim mywebsvc as Aspx123.aspx123websvc = New aspx123websvc ()
Dim DS as DataSet
ds = Mywebsvc.getnewresources ()

Aspx123websvc are our classes we defined in our WebService, and getnewresources () are our WebMethod the 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 of our website.   Instead, we want to cache the default DataView of the dataset in our webserver, and set it to expire every. The following code would 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 a 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


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.