WebService 类

为 XML Web services 定义可选的基类,该基类提供对公共 ASP.NET 对象(如应用程序和会话状态)的直接访问。

**命名空间:**System.Web.Services
**程序集:**System.Web.Services(在 system.web.services.dll 中)

语法

声明
Public Class WebService
    Inherits MarshalByValueComponent
用法
Dim instance As WebService
public class WebService : MarshalByValueComponent
public ref class WebService : public MarshalByValueComponent
public class WebService extends MarshalByValueComponent
public class WebService extends MarshalByValueComponent

备注

如果不需要访问公共 ASP.NET 对象,仍可创建 XML Web services,而不用从 WebService 导出。通过 Context 可以访问其他 ASP.NET 对象。

如果 XML Web services 方法将 SoapRpcMethodAttributeSoapDocumentMethodAttributeOneWay 属性设置为 true,它们将无法访问自己的 HttpContext。因此,当从该 XML Web services 方法中访问访问 WebService 类的任何属性时,都将返回 空引用(在 Visual Basic 中为 Nothing)。

如果您使用的是 .NET Framework 1.0 版的 XML Web services 方法,而该方法应用了 OneWay 属性设置为 trueSoapRpcMethodAttributeSoapDocumentMethodAttribute 属性 (Attribute),那么请不要使用静态 Current 属性 (Property) 访问它们的 HttpContext。若要访问 HttpContext,请从 WebService 派生实现 XML Web services 方法的类,并访问 Context 属性。

主题 位置
演练:在 Visual Web Developer 中创建和使用 ASP.NET Web 服务 在 Visual Studio 中构建 ASP .NET Web 应用程序
演练:在 Visual Web Developer 中创建和使用 ASP.NET Web 服务 使用 Visual Web Developer 生成应用程序

示例

下面的示例通过从 WebService 导出来创建一个 XML Web services,以便使用 Context 属性在服务器上获取请求的时间。

<%@ WebService Language="VB" Class="Util" %>
 
Imports System
Imports System.Web.Services

Public Class Util
    Inherits WebService
    
    <WebMethod(Description := "Returns the time as stored on the Server", _
        EnableSession := False)> _
    Public Function Time() As String
        
        Return Context.Timestamp.TimeOfDay.ToString()
    End Function
End Class
 
<%@ WebService Language="C#" Class="Util" %>
 
 using System;
 using System.Web.Services;
 
 public class Util: WebService {
   [ WebMethod(Description="Returns the time as stored on the Server",
   EnableSession=false)]
   public string Time() {
      return Context.Timestamp.TimeOfDay.ToString();
   }
 }
 

继承层次结构

System.Object
   System.ComponentModel.MarshalByValueComponent
    System.Web.Services.WebService

线程安全

此类型的任何公共静态(Visual Basic 中的 Shared)成员都是线程安全的,但不保证所有实例成员都是线程安全的。

平台

Windows 98、Windows 2000 SP4、Windows Millennium Edition、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

请参见

参考

WebService 成员
System.Web.Services 命名空间