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 fileNext 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 byaspXNewResources = 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 developerWorks - FREE Tools! | <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!
| | | | Hear how IBM Rational Project and Portfolio Management integrated solutions help teams put the right tools and processes in place to maximize the effectiveness and efficiency of project teams and ensure that the business vision is being executed correctly. Learn how to automate and integrate requirements prioritization, top-down project planning, communications and controls, and methodology deployment to keep your scope, costs, and schedules under control. Tackle with an end-to-end approach the management of scope and scope changes, usage of methodology to control and empower project teams, and optimization of resources to align activity costs with the overall project plan. FREE! Go There Now!
| | | | Poor Requirements Management capabilities in an Enterprise have been linked to excessive project failures, escalating IT costs, and failure to deliver competitive advantage into the marketplace. Join Brianna M Smith from IBM Rational and learn about how successful organizations align IT and Business stakeholders through collaborative processes and tools for effective requirements management, and how an integrated approach across the IT lifecycle can provide unparalleled visibility and traceability to ensure that project teams are delivering on the business vision by "doing the right things" and "doing things right." FREE! Go There Now!
| | | | Download a free trial version of IBM Rational Software Analyzer Developer Edition V7.0 to identify bug defects earlier in the software development cycle. Rational Software Analyzer is an extensible software development solution that reduces the expense of bug-fixes by enabling static analysis code reviews and bug identification very early in the development cycle. FREE! Go There Now!
| | | | Listen to this webcast to get an overview of Info 2.0 and a technical demo of how to quickly build an enterprise mashup. IBM's Info 2.0 technology leverages emerging Web 2.0 technologies such as mashups, feeds, AJAX, and JSON in order to simplify assembly of information using feeds and services. Come learn about the technical elements of Info 2.0 including the Feed Generation framework, Mashup Engine, and mashup assembly components. Learn how to pull information from databases, departmental information, and the Web to create mashups critical to your company’s success. We will also discuss best practices to help you get started. FREE! Go There Now!
| | | | Join this Rational Talks to You teleconference on December 4 at 1:00 pm ET to discuss how Rational Method Composer can help meet your compliance objectives. Get your questions answered! FREE! Go There Now!
| | | | Learn the basics of the IBM Customer Information Control System (CICS). With a hands-on exercise, learn how to get your first CICS application up and running on your desktop using TXSeries V6.1 for Windows. The tutorial shows you how to download and install a free trial version of TXSeries V6.1. FREE! Go There Now!
| | | | Whether you are creating new applications or modifying existing ones, managing integration of new components with traditional z/OS elements is a critical part of building and deploying modern applications. Listen to this webcast to see how IBM can help you optimize your development process using an IDE like Rational Developer for System z that integrates with management tools, such as ClearCase to manage your application development on mainframes. FREE! Go There Now!
| | | | Join this Rational Talks to You teleconference, to hear how Enterprise Generation Language (EGL) eliminates the need for tedious and error-prone low level coding, so developers can focus on business requirements. EGL extends the Rational software development platform with a simplified programming language that enables developers who have little or no experience with Java, Web technologies or Service Oriented Architecture, to create enterprise-class applications and services quickly and easily. It also allows developers who may have little or no mainframe programming experience to quickly create traditional mainframe components. FREE! Go There Now!
| | | | In this webcast, IBM Rational will discuss the importance of Web application security and will share techniques and best practices to introduce application security testing into current QA processes including: understanding common security vulnerabilities and techniques to integrate security testing with defect tracking and remediation systems in an effort to safeguard sensitive online information. FREE! Go There Now!
| | | | All FREE IBM® developerWorks Tools! | |