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服务内幕,第10部分:深入主题:可靠性和事务
- 3石家庄OA信息化创造竞争优势
- 4Web服务的(革)创新,第3部分
- 5石家庄OA信息化的基本XML和RDF 技术(五):定义RDF和DAML+OIL图示
- 6柴油机故障诊断专家系统知识库设计
- 7拐点之年:中国管理软件行业2008大盘点
- 8Using ASP.NET/WebServices For UPS Shipping Quotes
- 9石家庄OA信息化:挖掘企业的隐藏资源(姜铁虎)
- 10Web服务内幕,第4部分:介绍Web服务流语言
- 11我国商贸业将迎来新一轮的IT建设高潮
- 12架构Web Service:为什么需要Web服务?
- 13软件里面的思想黑马:金和董事长栾润峰
- 14泛普协同OA办公软件的信息资源共享
- 15泛普软件石家庄OA信息化系统实施9大推进步骤
- 16Applying .Net to Web Services
- 17知识地图在项目型组织中的应用
- 18Web Services Description Language (WSDL) 1.1
- 19在Web Service中使用ASP.net状态保持
- 20石家庄OA信息化的基本XML和RDF技术(一):使用XSLT生成RDF
- 21OA支持工作流报表的格式自定义——通过工作流报表
- 22使用WSDL部署Web服务,第1部分:Web服务和WSDL简介
- 23不同视角看石家庄OA信息化技术(by AMT 夏敬华)
- 24Building a Stock-Quotes Web Service
- 25IBM推新工具包助用户跨平台开发Web服务
- 26对某集团公司协同办公系统未来3-5年的IT规划建设蓝图
- 27微软展示新版互联网服务MSN 8.0
- 28大型集团公司OA办公系统平台建设实施计划
- 29透视Best Buy石家庄OA信息化实践(by AMT 夏敬华 编译)
- 30Licensing
成都公司:成都市成华区建设南路160号1层9号
重庆公司:重庆市江北区红旗河沟华创商务大厦18楼