通过因特网产生许多服务请求可能影响客户应用程序的性能。当设计你的XML Web服务时,通过创建把有关信息集中在一起的方法可以有效利用服务请求。例如,假定你有一个XML Web服务,用来检索一本书的信息。我们可以创建一个在一条服务请求中返回所有的信息的方法,来代替单独的检索书名、作者和出版社的方法。一次传送大块的信息比多次传送小块的信息更有效率。
下面的代码示例解释如何把有关信息组织到单个XML Web服务方法中。
[C#]
<%@ WebService Language="C#" Class="DataService" %>
using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.Services;
public class DataService {
[WebMethod]
public DataSet GetTitleAuthors() {
SqlConnection myConnection = new SqlConnection("Persist Security Info=False;Integrated Security=SSPI;server=localhost;database=pubs");
SqlDataAdapter myCommand1 = new SqlDataAdapter ("select * from Authors", myConnection);
SqlDataAdapter myCommand2 = new SqlDataAdapter("select * from Titles", myConnection);
DataSet ds = new DataSet();
myCommand1.Fill(ds, "Authors");
myCommand2.Fill(ds, "Titles");
return ds;
}
}
[Visual Basic]
<%@ WebService Language="VB" Class="DataService" %>
Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Web.Services
Public Class DataService
<WebMethod> _
Public Function GetTitleAuthors() As DataSet
Dim myConnection As New SqlConnection("Persist Security Info=False;Integrated Security=SSPI;server=localhost;database=pubs")
Dim myCommand1 As New SqlDataAdapter("select * from Authors", myConnection)
Dim myCommand2 As New SqlDataAdapter("select * from Titles", myConnection)
Dim ds As New DataSet()
myCommand1.Fill(ds, "Authors")
myCommand2.Fill(ds, "Titles")
Return ds
End Function
End Class
当设计你的XML Web服务时,请确保使用标准的面向对象编程操作。使用封装来隐藏实现细节。对于更复杂的XML Web服务,你可以使用继承和多态性来再次使用代码并简化你的设计。
下面的代码示例显示如何使用继承来创建一个执行数学计算的XML Web服务。
[C#]
<%@ WebService Language="C#" Class="Add" %>
using System;
using System.Web.Services;
abstract public class MathService : WebService
{
[WebMethod]
abstract public float CalculateTotal(float a, float b);
}
public class Add : MathService
{
[WebMethod]
override public float CalculateTotal(float a, float b)
{
return a b;
}
}
public class Subtract : MathService
{
[WebMethod]
override public float CalculateTotal(float a, float b)
{
return a - b;
}
}
public class Multiply : MathService
{
[WebMethod]
override public float CalculateTotal(float a, float b)
{
return a * b;
}
}
public class Divide : MathService
{
[WebMethod]
override public float CalculateTotal(float a, float b)
{
if (b==0)
return -1;
else
return a / b;
}
}
[Visual Basic]
<%@ WebService Language="VB" Class="Add" %>
Imports System
Imports System.Web.Services
MustInherit Public Class MathService : Inherits WebService
<WebMethod> _
Public MustOverride Function CalculateTotal(a As Single, _
b As Single) As Single
End Class
Public Class Add : Inherits MathService
<WebMethod> Public Overrides Function CalculateTotal(a As Single, b As Single) As Single
Return a b
End Function
End Class
Public Class Subtract : Inherits MathService
<WebMethod> Public Overrides Function CalculateTotal(a As Single, b As Single) As Single
Return a - b
End Function
End Class
Public Class Multiply : Inherits MathService
<WebMethod> Public Overrides Function CalculateTotal(a As Single, b As Single) As Single
Return a * b
End Function
End Class
Public Class Divide : Inherits MathService
<WebMethod> Public Overrides Function CalculateTotal(a As Single, b As Single) As Single
If b = 0 Then
Return - 1
Else
Return a / b
End If
End Function
End Class
使用输出缓冲来改善你的XML Web服务的性能。当输出缓冲开启时,服务请求的结果被保存在输出缓冲中一段指定的时间。如果一个类似的XML Web服务请求被产生,结果可以从缓冲中取得,而不用重新计算。这样就通过减少XML Web服务服务器所需的处理来改善了XML Web服务的反馈时间。高速缓存可在客户端和服务器两者上执行。Duration属性允许你指定高速缓冲保存XML Web服务输出的时间。
在客户端上使用输出高速缓冲的指令是:
<%@ OutputCache Duration="60" %>
下面的代码示例显示如何在客户应用程序上使用Duration属性来指定输出高速缓冲为60秒。
[C#]
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Net" %>
<%@ OutputCache Duration="60" VaryByParam="none" %>
<html>
<script language="C#" runat="server">
void EnterBtn_Click(Object Src, EventArgs e)
{
MyMath.Math math = new MyMath.Math();
// Call the XML Web service.
float total = math.Add(Convert.ToInt32(Num1.Text),
Convert.ToInt32(Num2.Text));
文章整理:西部数码--专业提供域名注册、虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!



