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

以下示例创建一个派生自 WebService 的 XML Web 服务,以便使用 Context 属性获取服务器上请求的时间。

<%@ 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(); <%@ 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 服务,而无需从 WebService 派生。 可以通过 访问 Context 其他 ASP.NET 对象。

属性为 OneWay SoapDocumentMethodAttribute 设置为 true SoapRpcMethodAttribute XML Web 服务方法无权访问其 HttpContext 。 因此,从该 XML Web 服务方法中访问 类的任何属性 WebService 时,将返回 null

如果使用 .NET Framework版本 1.0 XML Web 服务方法,这些方法应用了 SoapRpcMethodAttribute 或 属性,并将 OneWay 属性设置为 true ,则无法使用静态 Current 属性访问它们 HttpContext SoapDocumentMethodAttribute 。 若要访问 HttpContext ,请从 WebService 派生实现 XML Web 服务方法的类,并访问 Context 属性。