當(dāng)前位置:工程項(xiàng)目OA系統(tǒng) > 泛普各地 > 河北O(jiān)A系統(tǒng) > 石家莊OA系統(tǒng) > 石家莊OA信息化
Ask the DotNetJunkies: Consuming Remote Web Services in ASP.
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.
- 1石家莊OA信息化的“三四五六七”(by AMT 石家莊OA信息化小組)
- 2Web服務(wù)內(nèi)幕,第10部分:深入主題:可靠性和事務(wù)
- 3石家莊OA信息化創(chuàng)造競爭優(yōu)勢
- 4Web服務(wù)的(革)創(chuàng)新,第3部分
- 5石家莊OA信息化的基本XML和RDF 技術(shù)(五):定義RDF和DAML+OIL圖示
- 6柴油機(jī)故障診斷專家系統(tǒng)知識庫設(shè)計(jì)
- 7拐點(diǎn)之年:中國管理軟件行業(yè)2008大盤點(diǎn)
- 8Using ASP.NET/WebServices For UPS Shipping Quotes
- 9石家莊OA信息化:挖掘企業(yè)的隱藏資源(姜鐵虎)
- 10Web服務(wù)內(nèi)幕,第4部分:介紹Web服務(wù)流語言
- 11我國商貿(mào)業(yè)將迎來新一輪的IT建設(shè)高潮
- 12架構(gòu)Web Service:為什么需要Web服務(wù)?
- 13軟件里面的思想黑馬:金和董事長欒潤峰
- 14泛普協(xié)同OA辦公軟件的信息資源共享
- 15泛普軟件石家莊OA信息化系統(tǒng)實(shí)施9大推進(jìn)步驟
- 16Applying .Net to Web Services
- 17知識地圖在項(xiàng)目型組織中的應(yīng)用
- 18Web Services Description Language (WSDL) 1.1
- 19在Web Service中使用ASP.net狀態(tài)保持
- 20石家莊OA信息化的基本XML和RDF技術(shù)(一):使用XSLT生成RDF
- 21OA支持工作流報表的格式自定義——通過工作流報表
- 22使用WSDL部署Web服務(wù),第1部分:Web服務(wù)和WSDL簡介
- 23不同視角看石家莊OA信息化技術(shù)(by AMT 夏敬華)
- 24Building a Stock-Quotes Web Service
- 25IBM推新工具包助用戶跨平臺開發(fā)Web服務(wù)
- 26對某集團(tuán)公司協(xié)同辦公系統(tǒng)未來3-5年的IT規(guī)劃建設(shè)藍(lán)圖
- 27微軟展示新版互聯(lián)網(wǎng)服務(wù)MSN 8.0
- 28大型集團(tuán)公司OA辦公系統(tǒng)平臺建設(shè)實(shí)施計(jì)劃
- 29透視Best Buy石家莊OA信息化實(shí)踐(by AMT 夏敬華 編譯)
- 30Licensing
成都公司:成都市成華區(qū)建設(shè)南路160號1層9號
重慶公司:重慶市江北區(qū)紅旗河溝華創(chuàng)商務(wù)大廈18樓
版權(quán)所有:泛普軟件 渝ICP備14008431號-2 渝公網(wǎng)安備50011202501700號 咨詢電話:400-8352-114