托管代码中 Web 服务的代码模型

更新:2007 年 11 月

Web 服务由两部分组成:Web 服务入口点和用来实现 Web 服务功能的代码。在 ASP.NET 中,.asmx 文件是用作 Web 服务的可寻址入口点的文本文件。它引用预编译的程序集中的代码、代码隐藏文件或包含于 .asmx 文件本身的代码。

68zt3df9.alert_note(zh-cn,VS.90).gif说明:

如果代码直接包含在 .asmx 文件中,则 ASP.NET 将在需要时在服务器上对其进行编译。

位于 .asmx 文件顶部的 WebService 处理指令确定在何处查找 Web 服务的实现。默认情况下,当使用 ASP.NET Web 服务项目模板创建 Web 服务时,Visual Studio 使用代码隐藏文件,例如 Service1.asmx.vb 或 Service1.asmx.cs。

68zt3df9.alert_note(zh-cn,VS.90).gif说明:

Visual Studio 不支持跨语言编译。例如,不能将 Visual C# Web 服务包括在 Visual Basic ASP.NET Web 应用程序项目中,反之亦然。

当生成托管代码中的 Web 服务时,ASP.NET 自动提供基础结构并处理 Web 服务请求和响应,其中包括分析和创建 SOAP 消息。

WebService 处理指令

.asmx 页的顶部是 WebService 处理指令,它包括以属性形式表示的有关 Web 服务实现的信息。该处理指令向 ASP.NET 环境提供必要的信息,例如哪个类实现 Web 服务功能。以下是一个 WebService 处理指令的示例:

[Visual Basic]
<%@ WebService Language="vb" Codebehind="Service1.asmx.vb"
    Class="WebService1.Service1" %>


[C#]
<%@ WebService Language="c#" Codebehind="Service1.asmx.cs"
    Class="WebService1.Service1" %>

Language 属性指示用于开发该 Web 服务的编程语言。您可以使用任何符合 .NET 的语言(例如 Visual Basic .NET 或 Visual C#)创建 Web 服务。

当使用 Visual Studio .NET 以托管代码创建 Web 服务时,Web 服务的实现驻留在代码隐藏文件中。代码隐藏文件与使用 Codebehind 属性的 .asmx 页相关联。

68zt3df9.alert_note(zh-cn,VS.90).gif说明:

该属性帮助 Visual Studio 管理您的 Web 服务目,但运行时不需要该属性。

当使用 ASP.NET Web 服务项目模板时,Class 属性指示代码隐藏文件中哪个类实现 Web 服务的功能。

当您使用 ASP.NET Web 服务项目模板时,Visual Studio .NET 会自动将这一处理指令插入到 .asmx 文件。

68zt3df9.alert_note(zh-cn,VS.90).gif提示:

如果选择重命名类,请确保更改 WebService 指令的 Class 属性中的类名。

68zt3df9.alert_note(zh-cn,VS.90).gif说明:

若要查看 .asmx 文件的内容,请在“解决方案资源管理器”中右击 .asmx 文件,并在快捷菜单上单击“打开方式”。在“打开方式”对话框中选择“源代码(文本)编辑器”,然后单击“打开”

有关更多信息,请参见 演练:使用 ASP.NET 生成基本的 XML Web services

System.Web.Services.WebService 类

定义 Web 服务可选基类的 System.Web.Services.WebService 类提供对公共 ASP.NET 对象(例如应用程序和会话状态的对象)的直接访问。默认情况下,使用 Visual Studio 在托管代码中创建的 Web 服务从该类继承。Web 服务可以从该类继承以获得对 ASP.NET 的内部对象(例如请求和会话)的访问。有关更多信息,请参见 WebService 类

如果 Web 服务没有从该类继承,它可以从 System.Web.HttpContext.Current 访问 ASP.NET 内部对象。实现 Web 服务的类必须是公共的,并且必须具有公共的默认构造函数(不带参数的构造函数)。这使 ASP.NET 可以创建 Web 服务类的实例以处理传入的 Web 服务请求。有关更多信息,请参见 HttpContext.Current 属性

[Visual Basic]
Imports System.Web.Services
Public Class Service1
   Inherits System.Web.Services.WebService
   ' Implementation code.
End Class


[C#]
using System.Web.Services;
public class Service1 : System.Web.Services.WebService
{
   // Implementation code.
}

有关更多信息,请参见从 WebService 类继承

WebService 属性

每个 Web 服务都需要唯一的命名空间,以便客户端应用程序在可能使用相同方法名称的 Web 服务之间进行区分。在 Visual Studio .NET 中创建的 Web 服务的默认命名空间是“http://tempuri.org/WebService1/Service1”,其中 WebService1 是项目名,而 Service1 是类名。虽然命名空间与典型的 URL 相似,但不要认为可以在 Web 浏览器中查看它,它仅仅是一个唯一标识符。

68zt3df9.alert_note(zh-cn,VS.90).gif说明:

您可能要在包含有关所提供 Web 服务的信息的位置提供网页。

使用 WebService 属性可以指定命名空间,还可以提供有关 Web 服务的简短说明。当您在浏览器中调用 Web 服务而未指定查询字符串时,该简要说明将出现在服务帮助页上:

[Visual Basic]
<System.Web.Services.WebService( _ 
   Namespace:="http://tempuri.org/WebService1/Service1", _ 
   Description:="A short description of the Web service.")> _
Public Class Service1
   Inherits System.Web.Services.WebService
   ' Implementation code.
End Class


[C#]
[System.Web.Services.WebService( 
   Namespace="http://tempuri.org/WebService1/Service1",  
   Description="A short description of the Web service.")] 
public class Service1 : System.Web.Services.WebService
{
   // Implementation code.
}

有关更多信息,请参见 WebServiceAttribute 类使用 WebService 属性

应用 WebMethod 属性

若要将方法作为 Web 服务的一部分公开,您必须将 WebMethod 属性放置在要公开的每个公共方法的声明之前。有关更多信息,请参见 如何:创建 Web 服务方法

[Visual Basic]
<System.Web.Services.WebMethod()> _
Public Function MyString(ByVal x as string) As String
   ' Implementation code.
End Function


[C#]
[System.Web.Services.WebMethod()] 
public string MyString(string x) 
{
   //implementation code
}

WebMethod 属性 (Attribute) 包含几个用于配置 Web 服务行为的属性 (Property)。有关更多信息,请参见 WebMethodAttribute 类使用 WebMethod 属性。例如,您可以使用该属性提供将出现在关联服务帮助页上的简要说明:

[Visual Basic]
<System.Web.Services.WebMethod( _ 
   Description:="A short description of this method.")> _
Public Function MyString(ByVal x as string) As String
   ' Implementation code.
End Function


[C#]
[System.Web.Services.WebMethod( 
   Description="A short description of this method.")] 
public string MyString(string x) 
{
   // Implementation code.
}

用逗号分隔多个属性。例如,若要提供说明并对 Web 服务的结果进行 60 秒的缓存处理:

[Visual Basic]
<System.Web.Services.WebMethod( _ 
   Description:="A short description of this method.", _ 
           CacheDuration:=60)> _
Public Function MyString(ByVal x as string) As String
   ' Implementation code.
End Function


[C#]
[System.Web.Services.WebMethod( 
   Description="A short description of this method.", 
   CacheDuration=60)] 
public string MyString(string x) 
{
   // Implementation code.
}

汇总

每个 Web 服务都由一个 .asmx 文件和一个 Web 服务类组成。.asmx 文件包含一个引用该类的 WebService 处理指令。Web 服务类是公共的,具有公共的默认构造函数,并包含一个或多个标有 WebMethod 属性的公共方法。下面的关系图显示了项目、类、其方法和生成的 Web 服务之间的关系。

WebService 类和生成的项
Web 服务项目部署项

请参见

概念

在托管代码中访问 Web 服务的代码模型

其他资源

在托管代码中创建 Web 服务