WebService 类


为 XML Web services 定义可选的基类,该基类提供对公共 ASP.NET 对象(如应用程序和会话状态)的直接访问。Defines the optional base class for XML Web services, which provides direct access to common ASP.NET objects, such as application and session state.

public ref class WebService : System::ComponentModel::MarshalByValueComponent
public class WebService : System.ComponentModel.MarshalByValueComponent
type WebService = class
    inherit MarshalByValueComponent
Public Class WebService
Inherits MarshalByValueComponent


下面的示例创建了一个派生自的 XML Web service,以便 WebService 使用 Context 属性获取服务器上的请求时间。The example below creates an XML Web service, deriving from WebService, in order to use the Context property to obtain the time of the request on the server.

<%@ 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",
   public string Time() {
      return Context.Timestamp.TimeOfDay.ToString();
<%@ 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


如果不需要访问常见的 ASP.NET 对象,则仍可以创建 XML Web service,而不是从派生 WebServiceIf you don't need access to the common ASP.NET objects, you can still create an XML Web service without deriving from WebService. 可以通过访问其他 ASP.NET 对象 ContextAdditional ASP.NET objects can be accessed through Context.

如果 XML Web service 方法的 OneWay 属性为 SoapRpcMethodAttributeSoapDocumentMethodAttribute 设置为,则 true 无法访问其 HttpContextXML Web service methods that have the OneWay property of either SoapRpcMethodAttribute or SoapDocumentMethodAttribute set to true, do not have access to their HttpContext. 因此, WebService 从该 XML Web service 方法中访问类的任何属性,都将返回 nullAs such, accessing any of the properties of the WebService class, from within that XML Web service method, return null.

如果你使用的是 .NET Framework 版本 1.0 XML Web service 方法,这些方法 SoapRpcMethodAttribute 应用了或 SoapDocumentMethodAttribute 特性 OneWay ,并且属性设置为,则无权 true HttpContext 使用静态 Current 属性访问。If you are using the .NET Framework version 1.0 XML Web service methods that have either the SoapRpcMethodAttribute or SoapDocumentMethodAttribute attribute applied to them with the OneWay property of set to true, do not have access to their HttpContext using the static Current property. 若要访问 HttpContext ,请派生类实现 XML Web service 方法, WebService 并访问 Context 属性。To access the HttpContext, derive the class implementing the XML Web service method from WebService and access the Context property.



初始化 WebService 类的新实例。Initializes a new instance of the WebService class.



获取当前 HTTP 请求的应用程序对象。Gets the application object for the current HTTP request.


获取组件的容器。Gets the container for the component.

(继承自 MarshalByValueComponent)

获取当前请求的 ASP.NET HttpContext,它封装了由 HTTP 服务器用来处理 Web 请求的所有 HTTP 特定的上下文。Gets the ASP.NET HttpContext for the current request, which encapsulates all HTTP-specific context used by the HTTP server to process Web requests.


获取指示组件当前是否处于设计模式的值。Gets a value indicating whether the component is currently in design mode.

(继承自 MarshalByValueComponent)

获取附加到该组件的事件处理程序的列表。Gets the list of event handlers that are attached to this component.

(继承自 MarshalByValueComponent)

获取当前请求的 HttpServerUtilityGets the HttpServerUtility for the current request.


获取当前请求的 HttpSessionState 实例。Gets the HttpSessionState instance for the current request.


获取或设置组件的站点。Gets or sets the site of the component.

(继承自 MarshalByValueComponent)

获取用于向 XML Web services 发送 SOAP 请求的 SOAP 协议的版本。Gets the version of the SOAP protocol used to make the SOAP request to the XML Web service.


获取 ASP.NET 服务器 User 对象。Gets the ASP.NET server User object. 可用于验证用户是否有权执行请求。Can be used to authenticate whether a user is authorized to execute the request.



释放由 MarshalByValueComponent 使用的所有资源。Releases all resources used by the MarshalByValueComponent.

(继承自 MarshalByValueComponent)

释放由 MarshalByValueComponent 占用的非托管资源,还可以另外再释放托管资源。Releases the unmanaged resources used by the MarshalByValueComponent and optionally releases the managed resources.

(继承自 MarshalByValueComponent)

确定指定对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(继承自 Object)

作为默认哈希函数。Serves as the default hash function.

(继承自 Object)

获取 IServiceProvider 的实施者。Gets the implementer of the IServiceProvider.

(继承自 MarshalByValueComponent)

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)

返回包含 Component 的名称的 String(如果有)。Returns a String containing the name of the Component, if any. 不应重写此方法。This method should not be overridden.

(继承自 MarshalByValueComponent)



添加用于侦听组件的 Disposed 事件的事件处理程序。Adds an event handler to listen to the Disposed event on the component.

(继承自 MarshalByValueComponent)