.NET 使用 WebService 行为实现 Web 服务






3.90/5 (10投票s)
2004 年 7 月 19 日
8分钟阅读

175932

1344
.NET 极大地简化了 Web 服务的创建和使用。 .NET 中的 Web 服务利用 HTTP、SOAP、XML 和 WSDL 等开放标准,能够快速创建、扩展和部署。请继续阅读。
引言
在此 测试 Web 服务。
我写这篇文章的初衷是因为很少有资源详细介绍客户端如何调用 Web 服务。当需要直接从客户端向远程 Web 服务器发出 Web 服务调用时,这种调用方式非常方便。可以设想出许多需要从客户端调用 Web 服务的场景。例如,请看这个 网站,它使用客户端双击来调用 Web 服务。双击任何单词即可获取该单词的含义。另一方面,从客户端调用 Web 服务还有一个额外的优点,就是只需刷新访问 Web 服务的页面部分。Web 服务调用不会重新渲染页面上的整个内容。因此,可以实现 Web 页面的局部刷新,从而显著提高最终用户的浏览效率。
Web 服务是一个函数、方法或子例程,它不仅可以被一个计算机(所有者或宿主)访问,还可以被 Internet 上的任何计算机访问。从某种意义上说,Web 服务是一组普遍可用的函数。
Web 服务使用率的提高归因于它们能够轻松地在 Internet 上共享应用程序。您可能会发现一个网站提供多种功能,如股票报价、天气信息和机票预订。网站可能直接提供所有这些服务,但在大多数情况下,每项服务都由专门的第三方提供。例如,这里是获取天气信息的良好来源。同样,这里是搜索网络出色的资源。网站所有者或解决方案提供商可以从 Weather.com 或 Google.com 获取相应服务,而不是创建用于天气和/或网络搜索的工具。这种方法不仅节省时间,还能为最终用户提供可靠、准确和一致的信息。像这样通过 Internet 向世界任何地方的其他计算机提供的服务称为 Web 服务。
入门 - Web 方法和客户端代码
.NET 极大地简化了 Web 服务的创建和使用。 .NET 中的 Web 服务利用 HTTP、SOAP、XML 和 WSDL 等开放标准,能够快速创建、扩展和部署。使用 .NET 创建 Web 服务非常简单。我们将在以下段落中介绍如何创建和调用 Web 服务。
为简单起见,我们创建一个返回 Web 服务器当前日期和时间的 Web 服务。由于这是一个 Web 服务,它将返回其服务器所在地理位置的日期和时间。如果 Web 服务器在芝加哥,美国,那么我们将获得芝加哥的当地时间。同样,如果 Web 服务器在新德里,印度,那么我们将看到其当地时间。
在此 测试此 Web 服务。
使用 .NET 创建 Web 服务(请注意,创建和调用是 Web 服务的两个不同概念)需要两个步骤。
- 创建 .asmx 文件(命名为 dateTimeWebSvc.asmx)。
- 编写必要函数以完成业务流程。
在进行步骤 1 之前,我们将首先介绍步骤 2。以下是用于返回当前日期和时间的函数的代码,返回类型为 string
。
<WebMethod()> Public Function currDateTime() As String
Dim dateTimeStr As String
Dim tZn As TimeZone 'get the time zone
Dim tZnNameStr As String 'time zone name (CST, EST, etc)
tZn = System.TimeZone.CurrentTimeZone 'get name of time zone
If tZn.IsDaylightSavingTime(Now) Then
tZnNameStr = tZn.DaylightName
Else
tZnNameStr = tZn.StandardName
End If
dateTimeStr = "Current date and time at this" & _
" web server's geographical location is- " _
& Now.ToString & " " & tZnNameStr
Return dateTimeStr
End Function
如您所见,我声明了一个名为 currDateTime
的 public
函数。此函数返回当前日期和时间作为 string
。更重要的是,请注意函数定义之前的 WebMethod
关键字。这表示该函数可以通过 Internet 作为 Web 方法供任何计算机访问。现在,创建(使用 Visual Studio 编辑器或文本编辑器)一个 .asmx 文件(命名为 dateTimeWebSvc.asmx)(这是我们的步骤 1),并将上述函数复制到该文件中。创建的 dateTimeWebSvc.asmx 文件将是您的 Web 服务类。
废话不多说,我将开始介绍如何使用 JavaScript 和 Web 服务组件 (HTC) 从客户端访问(或调用)我们创建的 Web 服务。
调用 Web 服务有 3 个步骤:
- 创建 .aspx 页面。
- 使用 JavaScript 向 .aspx 页面添加
WebService
行为 (HTC 文件)。 - 为我们创建的 Web 服务创建客户端代理。
步骤 1
创建 .aspx 页面。这将是访问 Web 服务的 Web 窗体。以下代码显示了 consumeWebSvcDateTime.aspx 页面。它有两个标签。第一个标签用于测试 Web 服务,第二个标签显示执行 Web 服务的结果。
(在此 查看 Web 窗体。)
<%@ Page Language="vb" AutoEventWireup="false"
Codebehind="consumeWebSvcDateTime.aspx.vb" %>
<HTML>
<HEAD>
<title>Web Services - Create and Consume Demo</title>
</HEAD>
<body>
<form id="frmWebSvcDateTime" method="post" runat="server">
<table cellpadding="0" cellspacing="0">
<tr>
<td>
<asp:Label id="lblAllText" runat="server" Width="581px"
Height="36px">We are trying to understand web services here.
At this time, we know how to create a web service.
Do we know how to consume yet? It will be no more than
2 minutes till we get there..Now double click on any word
to get server's date time using our web service.</asp:Label>
</td>
</tr>
<tr>
<td>
<asp:Label backcolor="#99eedd" id="lblDateTime"
runat="server" Width="581px" Height="36px">
Date and time will be displayed here.</asp:Label>
</td>
</tr>
</table>
</form>
</body>
</HTML>
第二步
使用 JavaScript 向 consumeWebSvcDateTime.aspx 页面添加 WebService
行为 (HTC 文件)。此步骤可进一步细分为 3 个小步骤。
- 将 HTC 文件复制到 Web 窗体项目文件夹。
- 使用 JavaScript 编写脚本以通过
WebService
行为访问 Web 服务。 - 修改 consumeWebSvcDateTime.aspx 页面上的 HTML 以处理单击事件。
步骤 a:WebService
行为使我们能够使用脚本语言调用 Web 服务。它是一个可重用的 DHTML 组件,通过 SOAP 通过 HTTP 进行通信来使用 Web 服务(来源)。请阅读此处提供的链接中有关 WebService
行为的文档。简而言之,请记住,此行为用于从客户端使用脚本语言调用 Web 服务。此外,此行为封装在 HTC (HTML Component) 文件中。因此,要使用 WebService
行为,我们需要在项目文件夹中拥有 HTC 文件。要在我们的 consumeWebSvcDateTime.aspx 页面中使用此行为,请下载 WebService
HTC 文件并将其复制到您的 Web 窗体项目文件夹。您将在此 处下载 HTC 文件。
步骤 b:要调用“日期和时间”方法,我们需要在 consumeWebSvcDateTime.aspx 中的 DIV
元素(或任何其他有效元素)上附加 WebService
HTC 文件,如下所示:
<DIV id="dNtWbSvc" style="BEHAVIOR: url(webservice.htc)"></DIV>
一旦附加了此行为,请将以下代码添加到 consumeWebSvcDateTime.aspx 以启用 WebService
行为并调用 Web 服务:
<SCRIPT language="JavaScript">
function initWebHTC()
{
//init and create short-cut name for web service
dNtWbSvc.useService("consumeDnTWebSvc.asmx?WSDL","dNtSvc");
var iCallID;
if (dNtWbSvc.dNtSvc)
{
document.getElementById("lblDateTime").innerHTML="Loadin...";
iCallID= dNtWbSvc.dNtSvc.callService(gtDnTRslt,"currDateTime");
}
}
function gtDnTRslt(result)
{
//if an error then get error details
if(result.error)
{
//Pull error info from event.result.errorDetail properties
var xfaultcode = result.errorDetail.code;
var xfaultstring = result.errorDetail.string;
var xfaultsoap = result.errorDetail.raw;
// Add code to handle specific error codes here
document.getElementById("lblDateTime").innerHTML="Error: "
+ xfaultcode + " ; " + xfaultstring + " ; " + xfaultsoap;
}
else
{
//alert(result.value);
document.getElementById("lblDateTime").innerHTML=result.value;
//clear any selection
var sRng;
sRng = document.selection.createRange();
sRng.execCommand("unselect");
}
}</SCRIPT>
步骤 c:修改 consumeWebSvcDateTime.aspx 页面上的 HTML 以处理双击事件。
<body ondblclick="initWebHTC();">
代理服务器代码
步骤 3
为 Web 服务创建代理服务器。
要调用 Web 方法,我们需要一个代理服务器。什么是代理服务器?客户端不能直接调用位于远程 Web 服务器上的 Web 方法。相反,它必须通过本地 Web 服务器路由 Web 方法调用。请参阅此 链接中的图表。
因此,为了访问远程 Web 方法,我们创建了一个代理服务器,如下所示:
我们需要复制通过编译包含本文前面段落中定义的 Web 方法的项目而创建的 DLL。要编译项目,您可以使用 Visual Studio 编辑器或命令提示符命令。要通过命令提示符编译项目,请使用以下语句:
c:> WSDL https:///yourWebServices/yourWebMethodClass.asmx?WSDL
编译项目后,您将在其 bin 文件夹中找到项目的 DLL 文件。复制 DLL 文件并将其粘贴到包含客户端页面的项目的 bin 文件夹中。您还可以(并且可能需要)“右键单击”项目名称,然后选择“添加引用”来添加您创建的 DLL 文件。
在您的客户端页面所在的项目中创建一个 .asmx 文件(命名为 consumeDnTWebSvc.asmx)。在此 .asmx 文件中添加以下代码:
<WebMethod()> Public Function currDateTime() As String
Dim dNtMainWebSvc As New yourWebServices.dateTimeWebSvc
'instantiate date and time web svc
Dim dateTimeStr As String
dateTimeStr = dNtMainWebSvc.currDateTime()
'call date and time web svc
Return dateTimeStr
End Function
请注意,我们创建了一个(代理)Web 方法,它与真实 Web 方法具有相同的名称(您可以为它命名,这里只是为了简单起见,我使用了相同的名称)。如上所述,客户端无法直接与远程 Web 服务器通信。相反,它会将请求发送到其本地 Web 服务器,然后由本地 Web 服务器向远程 Web 服务器发出请求。
另外请注意 WebMethod
中的另一件事:我们声明并定义了一个名为 dNtMainWebSvc
的对象(Dim dNtMainWebSvc As New yourWebServices.dateTimeWebSvc
)。这是可能的,因为我们将此对象的引用(DLL)(参见上面的段落)添加到了客户端的项目中。
完成后,您的客户端 consumeWebSvcDateTime.aspx 页面应如下所示:
<%@ Page Language="vb" AutoEventWireup="false"
Codebehind="consumeWebSvcDateTime.aspx.vb" %>
<HTML>
<HEAD>
<title>Web Services - Create and Consume Demo</title>
<SCRIPT language="JavaScript">
function initWebHTC()
{
//init and create short-cut name for web service
dNtWbSvc.useService("consumeDnTWebSvc.asmx?WSDL","dNtSvc");
var iCallID;
if (dNtWbSvc.dNtSvc)
{
document.getElementById("lblDateTime").innerHTML="Loadin...";
iCallID= dNtWbSvc.dNtSvc.callService(gtDnTRslt,"currDateTime");
}
}
function gtDnTRslt(result)
{
//if an error then get error details
if(result.error)
{
//Pull error info from event.result.errorDetail properties
var xfaultcode = result.errorDetail.code;
var xfaultstring = result.errorDetail.string;
var xfaultsoap = result.errorDetail.raw;
// Add code to handle specific error codes here
document.getElementById("lblDateTime").innerHTML="Error: "
+ xfaultcode + " ; " + xfaultstring + " ; " + xfaultsoap;
}
else
{
//alert(result.value);
document.getElementById("lblDateTime").innerHTML=result.value;
//clear any selection
var sRng;
sRng = document.selection.createRange();
sRng.execCommand("unselect");
}
}
</SCRIPT>
</HEAD>
<body ondblclick="initWebHTC();">
<DIV id="dNtWbSvc" style="BEHAVIOR: url(webservice.htc)"></DIV>
<form id="frmWebSvcDateTime" method="post" runat="server">
<table cellSpacing="0" cellPadding="0">
<tr>
<td>
<asp:label id="lblAllText" runat="server"
Width="581px" Height="36px">We are trying to understand
web services here. At this time, we know how to create
a web service. Do we know how to consume yet?
It will be no more than 2 minutes till we get there..
Now double click on any word to get server's date time
using our web service.</asp:label>
</td>
</tr>
<tr>
<td>
<asp:Label backcolor="#99eedd" id="lblDateTime"
runat="server" Width="581px" Height="36px">
Date and time will be displayed here.</asp:Label>
</td>
</tr>
</table>
</form>
</body>
</HTML>
测试 Web 服务
就是这样。您已经具备了所有条件。您有一个“真实”的 Web 服务类。有一个可以调用 Web 服务的客户端。您还为客户端创建了一个“代理”Web 服务。在浏览器中启动您的项目,然后双击页面上的任何单词,即可查看来自 Web 服务的返回结果——您应该看到来自远程 Web 服务器的当前日期和时间。
在此 测试 Web 服务。
结论
客户端可能会出现“服务不可用”的错误。当客户端无法连接到远程 Web 服务时,就会发生这种情况。如果您缺少 .HTC 文件,或者代理服务器 Web 方法的名称拼写错误,以及其他相关原因,就会出现此错误。如果您遇到这种情况,请给我写信,我们将一起解决。
Prasad Kopanati 是 XemanteX Inc. 的副总裁,这是一家提供面向 Web 用户语言相关服务的互联网公司。
来源
- 介绍 XML Web 服务
- WebService 行为
- 下载 HTC 文件
- 立即使用 XemanteX
许可证
本文未附加明确的许可证,但可能在文章文本或下载文件本身中包含使用条款。如有疑问,请通过下面的讨论区联系作者。
作者可能使用的许可证列表可以在此处找到。