Ask the DotNetJunkies: Consuming Remote Web Services in ASP. 来源: 时间:2002-09-12 10:47 作者:AMTeam.org Ask the DotNetJunkies: Consuming Remote Web Services in ASP.NET
Question: Is there any way one can call web services remotely? Suppose I have a web service which returns a DataSet. I want to call this web service from another URL and use the returned DataSet to display or enter it into my database. - Shoaib Pervaiz Answer: The .NET Framework has made creating and consuming web services quite easy for developers. Using built in technology you can create a function, declare it as a WebMethod and you have a web service. On the other side of that, consuming web services is just as easy. Using another stock .NET piece of technology, the WebServiceUtil.exe, you can create a DLL for consuming a web service. With that you can treat the data returned to you from the web service almost as if it were your own data. In this tutorial you will work with a web service that exposes a DataSet, and write an ASP.NET page to consume the web service from another domain. The Web Service The first thing you need is a web service to consume. I will be working with the ASPNextGen.com Tutorials Service, which can be found in .NETWebServ. If you would like, feel free to work with any web service, but your code may differ from mine. The ASPNextGen.com Tutorial Service returns a DataSet of tutorials, a summary of each tutorial, and a URL for the full tutorial. When a web service is exposed, a Service Description Language (SDL) file is generated (either pre-generated, or generated on demand by adding ?SDL to the URL for the .asmx file). The SDL is the contract between a provider and a consumer on how a web service is exposed and how it is to be consumed. The SDL for the ASPNextGen.com Tutorials Service is at: http://www.aspnextgen.com/services/tutorials.asmx?SDL
The SDL is critical for consuming a web service, as it details all the information you need to consume the service. The Consumer To build an ASP.NET page that consumes this service we must create a proxy client class. The proxy client class will provide the method(s) we need to call in order to consume the web service and use the DataSet returned. The .NET Framework provides a tool for building proxy client classes easily - WebServiceUtil.exe. You use this tool on the command line. You pass into it the path to the SDL file, and the namespace and location for the proxy client. To create a proxy client, go to the command line on a machine with the .NET Framework installed and enter the following with no line breaks (I'll explain it in a moment): C:> webserviceutil /c:proxy
After entering the command above you should have a Visual Basic file in your ASP.NET application's directory named aspnextgen_proxy.vb. The file created is a class file using the namespace codejunkies.aspnextgen.services. Here's how the WebServiceUtil.exe command worked: /c:proxy /pa:http://www.aspnextgen.com/services/tutorials.asmx?SDL /l:vb /n:codejunkies.aspnextgen.services /o:[enter the path to your ASP.NET
application]\aspnextgen_proxy.vb If you open the aspnextgen_tutorials.vb proxy class file you will find the code that WebServiceUtil.exe generated to consume the ASPNextGen.com Tutorials Service. '------------------------------------------------------------------------------ Imports System.Xml.Serialization Namespace codejunkies.aspnextgen.services Public Sub New() Public Function
<System.Web.Services.Protocols.SoapMethodAttribute("http://tempuri.org/GetTutorialsSummary")>
GetTutorialsSummary() As System.Data.DataSet Public Function BeginGetTutorialsSummary(ByVal callback As
System.AsyncCallback, ByVal asyncState As Object) As
System.IAsyncResult Public Function EndGetTutorialsSummary(ByVal asyncResult
As System.IAsyncResult) As System.Data.DataSet End Class As you can see, the codejunkies.aspnextgen.services namespace is declared, and a class named Tutorials has been created. In the Tutorials class there is a New() sub procedure and three functions, GetTutorialsSummary(), BeginGetTutorialsSummary() and EndGetTutorialsSummay(). The GetTutorialsSummary() function is the one we will call to consume the web service. From here you can consume the web service just as you would call any function in a DLL...of course, the DLL must be compiled and placed in your bin directory. You can compile this class in the same DLL as the rest of your project. The namespace codejunkies.aspnextgen.services will be in the DLL and the functions will be exposed. The ASP.NET Page Once you have compiled the DLL you can consume the service. Just as with other calls to methods in a DLL, you need to import the namespace, in this case codejunkies.aspnextgen.services, and instantiate the class, in this case the Tutorials class. Once you have a variable representing the class you can call the method to consume the web service. Here is a simple page to render a DataGrid with all the output from the ASPNextGen.com Tutorials Service. <%@ Page Language="vb" %> This is what the rendered page looks like: Summary The .NET Framework has made creating and consuming web services quite easy. Tools, such as WebServiceUtil.exe, are included in the framework to enable developers to quickly develop rich, data-driven applications. In the case of the example in this tutorial, you learned how to consume a web service that returns an ADO.NET DataSet. Once you have the DataSet in your application you can do virtually anything with it. You can change the properties of the DataGrid to alter the output, you can insert the data into a database, you can iterate through the data looking for specific parameters, etc. Virtually anything is possible. |
关键词:
|
相关文章 |