ASP.ET创建XML Web Service设计指南
使用输出缓存来提高您的 XML Web services 的性能。当打开输出缓存时,服务请求的结果在一段指定的时间内存储在输出缓存中。如果发出了类似的 XML Web services 请求,则可以从缓存中获得结果,而不是重新进行计算。这通过减少 XML Web services 服务器需要进行的处理缩短了 XML Web services 的反应时间。可以在客户端和服务器上执行缓存。Duration 属性允许您指定对 XML Web services 的输出进行缓存的时间量。
在客户端上启用输出缓存的指令是:
在客户端上启用输出缓存的指令是:
下面的代码示例演示如何在客户端应用程序上使用 Duration 属性指定 60 秒的输出缓存。<%@ OutputCache Duration="60" %>
您还可以使用 WebMethod 特性类的 CacheDuration 属性在服务器上启用缓存。下面的代码示例演示如何在 XML Web services 方法上使用 CacheDuration 属性指定 60 秒的输出缓存。<%...@ 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));
// Display the results in a Label control.
Total.Text = "Total: " + total.ToString();
}
</script>
<body>
<form action="MathClient.aspx" runat=server>
<font face="Verdana">
Enter the two numbers you want to add and press
the Total button.
<p>
Number 1:
<asp:textbox id="Num1"
runat=server/>
+
Number 2:
<asp:textbox id="Num2"
runat=server/>
=
<asp:button id="Total_Button"
text="Total"
OnClick="EnterBtn_Click"
runat=server/>
<p>
<asp:label id="Total" runat=server/>
</font>
</form>
</body>
</html>
<%@ WebService Language="C#" Class="MathService" %>
using System;
using System.Web.Services;
public class MathService : WebService ...{
[WebMethod(CacheDuration=60)]
public float Add(float a, float b)
...{
return a + b;
}
[WebMethod(CacheDuration=60)]
public float Subtract(float a, float b)
...{
return a - b;
}
[WebMethod(CacheDuration=60)]
public float Multiply(float a, float b)
...{
return a * b;
}
[WebMethod(CacheDuration=60)]
public float Divide(float a, float b)
...{
if (b==0) return -1;
return a / b;
}
}
0
相关文章
