ASP.NET Code
  Home arrow ASP.NET Code arrow Consuming a Webservice Part 2 of 2
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
ASP.NET CODE

Consuming a Webservice Part 2 of 2
By: Dave - 123aspx.com
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2003-01-01

    Table of Contents:

    Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT



    Consuming 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 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

    Now that we've loaded the ASP.NET cache with our DataView, let's go ahead and bind the dataview to a Datagrid and display our results. With a little bit of formatting, our results can be seen below.
    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

    More ASP.NET Code Articles
    More By Dave - 123aspx.com

     

    IBM® developerWorks developerWorks - FREE Tools!


    Check out the new Jazz space on developerWorks

    <a href="http://zeus.developershed.com/shonuff.php?blackbird=3853&zoneid=442&source=&dest=http%3A%2F%2Fwww.ibm.com%2Fdeveloperworks%2Fspaces%2Fjazz%3FS_TACT%3D105AGY31%26S_CMP%3DDEVSHED&ismap="><img src="http://images.devshed.com/corp/img/news/jazz01.gif" alt="developerWorks Jazz space" align="left"></a>You've heard the buzz about Jazz... want to know more about it from a developer's perspective? Check out the Jazz space on developerWorks. This space is an up-to-date resource for developers, including technical information about Jazz and products built on Jazz, like Rational Team Concert Express. The Jazz space includes content from a wide variety of sources, including links, feeds, and comments from experts.
    FREE! Go There Now!


    NEW! Driving Business Success with Rational Process Library

    Join this webcast, to learn how the Rational Process Library can help with compliance issues, drive process improvement, and assist in service-oriented architecture (SOA) or Agile development. We will take a peek into the Rational Process Library with content around software and systems engineering (including RUP), operations and systems management, program and portfolio management, and asset and SOA governance.
    FREE! Go There Now!


    NEW! Calling all CC Power Users – and those that would like to be!

    Join this Rational Talks to You teleconference, featuring Paul Boustany and Mark Krasovich, to speak to the experts about becoming a Rational ClearCase power user. Get a chance to ask your questions and learn tips and tricks for using Rational ClearCase in Agile development
    FREE! Go There Now!


    NEW! Addressing software-as-a-service challenges using Tivoli security and WebSphere solutions

    Building a software-as-a-service solution requires addressing a few key technical challenges. In this webcast, we'll focus on the role of IBM Tivoli Directory Server and WebSphere Portlet Factory in creating a Software as a Service solution. We will demonstrate how to use Tivoli Directory Server to prevent the user population of one tenant from accessing the virtual portal and portlet components of another tenant. We will also use the dynamic profile capability of WebSphere Portlet Factory to create multiple highly customized applications from one code base.
    FREE! Go There Now!


    NEW! Application Development Tools for the Mainframe Developer

    You probably have thousands of lines of COBOL code loaded with business intelligence and being used to run your business, along with an army of developers maintaining these applications. Learn how to prepare your applications and developers so you can keep that competitive edge and move to a service-oriented architecture with the IBM Rational Enterprise Modernization solutions. Replay is available for 9 months.
    FREE! Go There Now!


    NEW! Develop Systems Software Assets with IBM Rational Asset Manager

    Join us for this on demand webcast to learn about developing complex systems more quickly and efficiently. We'll cover market drivers for developing, governing and reusing systems software assets and how you can develop system software assets with Rational Asset Manager.
    FREE! Go There Now!


    NEW! Improve your build process with IBM Rational Build Forge, Part 2: Automate builds for a real-world Tomcat project

    Learn how Rational Build Forge can extend a simple compile and package build process by adding customization and deployment capability. Go from a manual method to automating: checking for code changes; getting the latest source; compiling and packaging; customizing; copying to and restarting a deployment server; and sending e-mail notification that a new version is available.
    FREE! Go There Now!


    NEW! Rational Asset Manager eKit

    Learn how to do more with your reusable assets with the free Rational Asset Manager eKit. The eKit includes demos on how Rational Asset Manager tracks and audits your assets in order to utilize them for reuse. Plus you’ll find white papers and a Webcast that discuss the challenges of a Service Oriented Architecture and how Rational Asset Manager can provide quick and effective solutions.
    FREE! Go There Now!


    NEW! Software Change and Configuration Management Solution Guidelines

    This whitepaper provides areas to consider when evaluating any software configuration management solution. It addresses how the IBM solutions (Rational ClearCase and Rational ClearQuest) meet the needs and requirements of both project leaders and developers to provide successful Software Change and Configuration Management.
    FREE! Go There Now!


    NEW! Trial download: IBM Rational Functional Tester V7.0.1

    Get a free trial download of the latest version of IBM Rational Functional Tester V7.0.1. Rational Functional Tester is an automated functional and regression testing solution for QA teams concerned with the quality of their Java, Microsoft Visual Studio .NET, and Web-based applications.
    FREE! Go There Now!



    All FREE IBM® developerWorks Tools!

    ASP.NET CODE ARTICLES

    - How to Use the ListBox Control in ASP.NET 2.0
    - How to Load XML Documents in ASP.NET 2.0
    - DataGrid Code
    - ASP.NET Guestbook
    - User Controls and Client Side Scripting
    - ASP.NET Programming with Microsoft's AS...
    - ASP.NET Basics (part 3): Hard Choices
    - ASP.NET Basics (part 2): Not My Type
    - ASP.NET Basics (part 1): Nothing But .Net
    - Directory Tree Browser
    - How to get the confirmation of Yes/No from a...
    - Complete example using custom errors and wri...
    - Paging Certain # records per page .NET style
    - General Methods of formatting and Subtractin...
    - .NET LinkButton web control





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    Stay green...Green IT