快精灵印艺坊 您身边的文印专家
广州名片 深圳名片 会员卡 贵宾卡 印刷 设计教程
产品展示 在线订购 会员中心 产品模板 设计指南 在线编辑
 首页 名片设计   CorelDRAW   Illustrator   AuotoCAD   Painter   其他软件   Photoshop   Fireworks   Flash  

 » 彩色名片
 » PVC卡
 » 彩色磁性卡
 » 彩页/画册
 » 个性印务
 » 彩色不干胶
 » 明信片
   » 明信片
   » 彩色书签
   » 门挂
 » 其他产品与服务
   » 创业锦囊
   » 办公用品
     » 信封、信纸
     » 便签纸、斜面纸砖
     » 无碳复印纸
   » 海报
   » 大篇幅印刷
     » KT板
     » 海报
     » 横幅

ASP.NET创建XML Web服务全接触(8)

设计方针(3)

    

 使用输出缓冲来改善你的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));
 // 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>
[Visual Basic]
<%@ Page Language=\"VB\" %>
<%@ Import Namespace=\"System.Net\" %>
<%@ OutputCache Duration=\"60\" VaryByParam=\"none\" %>
<html>
<script language=\"VB\" runat=\"server\">
Sub EnterBtn_Click(Src As Object, e As EventArgs)
 Dim math As New MyMath.Math()
 \\\' Call the XML Web service.
 Dim addtotal As Single = math.Add(Convert.ToInt32(Num1.Text), Convert.ToInt32(Num2.Text))
 \\\' Display the results in a Label control.
 Total.Text = \"Total: \" & addtotal.ToString()
End Sub
</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>

  你还可以使用WebMethod属性类的CacheDuration属性来在服务器上答应高速缓冲。下面的代码示例显示如何在XML Web服务方式上使用CacheDuration属性来指定输出高速缓冲为60秒。

[C#]
<%@ 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;
}
}
[Visual Basic]
<%@ WebService Language=\"VB\" Class=\"MathService\" %>
Imports System
Imports System.Web.Services
Public Class MathService
Inherits WebService
<WebMethod(CacheDuration := 60)> _
Public Function Add(a As Single, b As Single) As Single
Return a + b
End Function

<WebMethod(CacheDuration := 60)> _
Public Function Subtract(a As Single, b As Single) As Single
Return a - b
End Function

<WebMethod(CacheDuration := 60)> _
Public Function Multiply(a As Single, b As Single) As Single
Return a * b
End Function

<WebMethod(CacheDuration := 60)> _
Public Function Divide(a As Single, b As Single) As Single
If b = 0 Then
Return - 1
End If
Return a / b
End Function
End Class

  当设计你的XML Web服务时,努力遵循如何格式化模式的结构。

  XML Web服务使用SOAP作为主要的传送和序列化协议。一个SOAP消息由一个可选择的头体和消息体组成。头部分包含可以被Web服务器体系结构处理的信息。SOAP没有定义任何头。消息体部分包含由应用程序处理的信息,例如用于XML Web服务的参数或返回值。

  提供用于你的XML Web服务的文档,如一个静态HTML文件,描述你的服务的操作和数据结构。还包括如何使用这个XML Web服务的示例。不要依赖服务描述或服务帮助页面作为你唯一的文档。

返回类别: 教程
上一教程: 一个asp+ 版本的 Active Server Explorer
下一教程: ASP.NET创建XML Web服务全接触(6)

您可以阅读与"ASP.NET创建XML Web服务全接触(8)"相关的教程:
· ASP.NET创建XML Web服务全接触(7)
· ASP.NET创建XML Web服务全接触(9)
· ASP.NET创建XML Web服务全接触(13)
· ASP.NET创建XML Web服务全接触(12)
· ASP.NET创建XML Web服务全接触(3)
    微笑服务 优质保证 索取样品