監(jiān)理公司管理系統(tǒng) | 工程企業(yè)管理系統(tǒng) | OA系統(tǒng) | ERP系統(tǒng) | 造價咨詢管理系統(tǒng) | 工程設(shè)計(jì)管理系統(tǒng) | 甲方項(xiàng)目管理系統(tǒng) | 簽約案例 | 客戶案例 | 在線試用
X 關(guān)閉

Ask the DotNetJunkies: Consuming Remote Web Services in ASP.

申請免費(fèi)試用、咨詢電話:400-8352-114

AMTeam.org

Ask the DotNetJunkies: Consuming Remote Web Services in ASP.NET 


 
By: Doug Seven

Level: Intermediate

Posted Date: 12/13/2000

Tested with ASP.NET Beta 1 (v1.0.2204)

 Click for Printable Version

 Member Rating: 2.50 (Rated by 2 members)

Rate This Item 

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 file is an XML file that indicates the schema, the method(s) to call, and the data type(s) returned.

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

/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

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

/c: is the shorthand for command. This parameter indicates that you are using WebServiceUtil.exe to create a proxy client. Other possibilities are discovery, getSDL, makeSDL, or template.

/pa:http://www.aspnextgen.com/services/tutorials.asmx?SDL

/pa: is shorthand for path. This parameter specifies the path to an SDL file for the web service you are building a proxy client for. The path could be a local path to a SDL file, or a URI to a SDL for a remote web service.

/l:vb

/l: is shorthand for language. This could be Visual Basic, C#, JScript or any other language with a supplied CodeGenerator. I chose vb because I think more people can understand the Visual Basic code. Feel free to use any language.

/n:codejunkies.aspnextgen.services

/n: is shorthand for namespace. This parameter defines the namespace the proxy class will use. It is typically accepted to identify namespaces with the name of the company, followed by the purpose of the namespace, such as Microsoft.VisualBasic.

/o:[enter the path to your ASP.NET application]aspnextgen_proxy.vb

/o: is shorthand for output. This is the directory where the proxy class file should be created. For my system I
used /o:D:myProjectaspnextgen_tutorials.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.

'------------------------------------------------------------------------------

' <autogenerated>

' This class was generated by a tool.

' Runtime Version: 1.0.2204.21

'

' Changes to this file may cause incorrect behavior and will be lost if

' the code is regenerated.

' </autogenerated>

'------------------------------------------------------------------------------

Imports System.Xml.Serialization

Imports System.Web.Services.Protocols

Imports System.Web.Services

Namespace codejunkies.aspnextgen.services

 Public Class Tutorials

 Inherits System.Web.Services.Protocols.SoapClientProtocol

 Public Sub New()
 
MyBase.New
 
Me.Url = "
http://www.aspnextgen.com/services/tutorials.asmx"

 End Sub

 Public Function <System.Web.Services.Protocols.SoapMethodAttribute("http://tempuri.org/GetTutorialsSummary")> GetTutorialsSummary() As System.Data.DataSet
 
Dim results() As Object = Me.Invoke("GetTutorialsSummary", New Object(0) {})
 
Return CType(results(0),System.Data.DataSet)
 
End Function

Public Function BeginGetTutorialsSummary(ByVal callback As System.AsyncCallback, ByVal asyncState As Object) As System.IAsyncResult
 
Return Me.BeginInvoke("GetTutorialsSummary", New Object(0) {}, callback, asyncState)
 
End Function

 Public Function EndGetTutorialsSummary(ByVal asyncResult As System.IAsyncResult) As System.Data.DataSet
 
Dim results() As Object = Me.EndInvoke(asyncResult)
 
Return CType(results(0),System.Data.DataSet)
 
End Function

End Class

End Namespace 

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" %>

<%@ Import Namespace="codejunkies.aspnextgen.services" %>

<%@ Import Namespace="System.Data" %>

<html>

<head>

<script runat="server">

 Sub Page_Load(Source As Object, E As EventArgs)
 
Dim ds As New DataSet
 
Dim Service As Tutorials = New Tutorials
 
Ds = Service.GetTutorialsSummary()
 
dg.DataSource = ds.Tables("Tutorials").DefaultView
 
dg.DataBind()
 
End Sub

</script>

</head>

<body bgcolor="#FFFFFF">
 
<form method="post" runat="server">
 
<asp:DataGrid id="dg" Runat="server"
   
AlternatingItemStyle-BackColor="#eeeeee"
   
ShowHeaders="True" />
 
</form>

</body>

</html>

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.

發(fā)布:2007-03-25 13:23    編輯:泛普軟件 · xiaona    [打印此頁]    [關(guān)閉]
相關(guān)文章:
石家莊OA系統(tǒng)
聯(lián)系方式

成都公司:成都市成華區(qū)建設(shè)南路160號1層9號

重慶公司:重慶市江北區(qū)紅旗河溝華創(chuàng)商務(wù)大廈18樓

咨詢:400-8352-114

加微信,免費(fèi)獲取試用系統(tǒng)

QQ在線咨詢