XML Web services 是一個強大的技術,用來提供可以從整個 Internet 以編程方式進行訪問的服務。下面的建議將幫助您創建專業的 XML Web services:
XML Web services 支持客戶端和承載該 XML Web services 的服務器之間的同步和異步通信。在同步通信情況下,客戶端向服務主機服務器發送對服務的請求並等待響應。這阻止客戶端在等待結果時執行其他操作。但是,異步通信讓客戶端在等待響應時繼續處理其他任務。客戶端在它可用時才響應服務請求的結果。
當您使用 Web 服務描述語言工具 (Wsdl.exe) 創建代理類時,它在該類中生成方法標準的同步版本和異步版本。異步版本由兩個方法組成,分別叫做 Begin 和 End。Begin 方法用於啓動 XML Web services,而 End 方法檢索結果。
使用異步通信提高了系統利用率,避免當客戶端等待 XML Web services 結果時在客戶端上造成延遲。
下面的代碼示例演示如何從客戶端應用程序對 XML Web services 進行異步調用。
[C#]
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Net" %>
<html>
<script language="C#" runat="server">
void EnterBtn_Click(Object Src, EventArgs E)
{
MyMath.Math math = new MyMath.Math();
// Call to Add XML Web service method asynchronously
// and then wait for it to complete.
IAsyncResult result =
math.BeginAdd(Convert.ToInt32(Num1.Text),
Convert.ToInt32(Num2.Text),
null,
null);
// Wait for asynchronous call to complete.
result.AsyncWaitHandle.WaitOne();
// Complete the asynchronous call to Add XML Web service method.
float total = math.EndAdd(result);
// Display 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 then 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>
通過 Internet 進行大量的服務請求可能影響客戶端應用程序的性能。當設計您的 XML Web services 時,通過創建將相關的信息聚集在一起的方法對服務請求進行有效的利用。例如,假設您有一個 XML Web services,該服務檢索關於一本圖書的信息。創建一個方法在一個服務請求中返回所有信息,而不是讓多個單獨的方法分別檢索書名、作者和出版商。一次傳輸一大塊信息比多次傳輸小塊信息更有效。
下面的代碼示例演示如何將相關的信息聚集到一個 XML Web services 方法中。
[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;
}
}
當設計您的 XML Web services 時,一定要遵循標準的面向對象的編程慣例。使用封裝來隱藏實現細節。對於更加複雜的 XML Web services,您可以使用繼承和多態性來重複利用代碼並簡化設計。
下面的代碼示例演示如何使用繼承來創建執行數學計算的 XML Web services。
[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;
}
}
使用輸出緩存來提高您的 XML Web services 的性能。當打開輸出緩存時,服務請求的結果在一段指定的時間內存儲在輸出緩存中。如果發出了類似的 XML Web services 請求,則可以從緩存中獲得結果,而不是重新進行計算。這通過減少 XML Web services 服務器需要進行的處理縮短了 XML Web services 的反應時間。可以在客戶端和服務器上執行緩存。Duration 屬性允許您指定對 XML Web services 的輸出進行緩存的時間量。
在客戶端上啓用輸出緩存的指令是:
<%@ OutputCache Duration="60" %>
下面的代碼示例演示如何在客戶端應用程序上使用 Duration 屬性指定 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>
您還可以使用 WebMethod 特性類的 CacheDuration 屬性在服務器上啓用緩存。下面的代碼示例演示如何在 XML Web services 方法上使用 CacheDuration 屬性指定 60 秒的輸出緩存。
<%@ 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;
}
}