从WebService类衍生
使用ASP.NET创建的实现一个XML Web服务的类可以选择性地衍生于WebService类来获得访问公共的ASP.NET对象,例如Application、Session、User和Context的权限。Application和Session属性提供保存和接收Web应用程序的生命周期或一个特定的会话的状态的权限。想获得关于状态的更多的信息,请看在使用ASP.NET创建的XML Web服务中管理状态一节。User属性包含了XML Web服务调用者的身份。XML Web服务可以使用调用者身份来判定请求是否被授权。有关验证的更多信息,请看加强XML Web服务安全一节。Context属性提供了取得XML Web服务客户端请求的所有特定HTTP信息的权限。
下面的代码示例使用Context属性来获得服务器上的请求时间。
[C#] <%@ 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(); } } [Visual Basic] <%@ 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 |
[C#] <%@ WebService Language="C#" Class="Util" %> using System; using System.Web.Services; public class Util: WebService { public int Add(int a, int b) { return a + b; } [ WebMethod] public long Multiply(int a, int b) { return a * b; } } [Visual Basic] <%@ WebService Language="VB" Class="Util" %> Imports System Imports System.Web.Services Public Class Util Inherits WebService Public Function Add(a As Integer, b As Integer) As Integer Return a + b End Function < WebMethod()> _ Public Function Multiply(a As Integer, b As Integer) As Long Return a * b End Function End Class |
……