日期:2012-09-22  浏览次数:20410 次

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