正文

一个实例:ASP.NET Web Service2008-10-02 21:10:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/yzrj/38655.html

分享到:

在这个例子中,我们会使用 ASP.NET 来创建一个简单的 Web Service。<%@ WebService Language="VB" Class="TempConvert" %> Imports System Imports System.Web.Services Public Class TempConvert :Inherits WebService <WebMethod()> Public Function FahrenheitToCelsius (ByVal Fahrenheit As Int16) As Int16 Dim celsius As Int16 celsius = ((((Fahrenheit) - 32) / 9) * 5) Return celsius End Function <WebMethod()> Public Function CelsiusToFahrenheit (ByVal Celsius As Int16) As Int16 Dim fahrenheit As Int16 fahrenheit = ((((Celsius) * 9) / 5) + 32) Return fahrenheit End Function End Class 此文档是一个 .asmx 文件。这是用于 XML Web Services 的 ASP.NET 文件扩展名。 要运行这个例子,我们需要一个 .NET 服务器 此文档中第一行表明这是一个 Web Service,由 VB 编写,其 class 名称是 "TempConvert"。<%@ WebService Language="VB" Class="TempConvert" %> 接下来的代码行从 .NET 框架导入了命名空间 "System.Web.Services"。Imports System Imports System.Web.Services 下面这一行定义 "TempConvert" 类是一个 WebSerivce 类:Public Class TempConvert :Inherits WebService 接下来的步骤是基础的 VB 编程。此应用程序有两个函数。一个把华氏度转换为摄氏度,而另一个把摄氏度转换为华氏度。 与普通的应用程序唯一的不同是,此函数被定义为 "WebMethod"。 请在您希望其成为 web services 的应用程序中使用 "WebMethod" 来标记函数。<WebMethod()> Public Function FahrenheitToCelsius (ByVal Fahrenheit As Int16) As Int16 Dim celsius As Int16 celsius = ((((Fahrenheit) - 32) / 9) * 5) Return celsius End Function <WebMethod()> Public Function CelsiusToFahrenheit (ByVal Celsius As Int16) As Int16 Dim fahrenheit As Int16 fahrenheit = ((((Celsius) * 9) / 5) + 32) Return fahrenheit End Function 最后要做的事情是终止函数和类:End Function End Class 假如您把此文件另存为 .asmx 文件,并发布于支持 .NET 的服务器上,那么您就拥有了第一个可工作的 Web Service。 ASP.NET 的自动化处理 通过 ASP.NET,你不必亲自编写 WSDL 和 SOAP 文档。 如果您仔细研究我们的这个例子,您会发现 ASP.NET 会自动创建 WSDL 和 SOAP 请求。

阅读(2350) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册