程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> .NET實例教程 >> ASP.NET創建Web服務之設計方針

ASP.NET創建Web服務之設計方針

編輯:.NET實例教程

使用ASP.NET構造一個簡單的XML Web服務是相對容易的,然而,XML Web服務的真正的強大的功能只有等你研究了基礎結構以後才能領悟。XML Web服務是建立在.NET框架和公共語言運行時間基礎上的。一個XML Web服務可以利用這些技術。例如,ASP.Net支持的性能、狀態管理和驗證全都可被用來構造XML Web服務。

  XML Web服務的基礎結構是構建來符合象SOAP、XML和WSDL這樣的行業標准,並且它允許其他的平台的客戶端與XML Web服務互操作。只要一個客戶端可以發送符合標准的SOAP消息、依據格式化的服務描述,那麼這個客戶端可以調用一個使用ASP.Net創建的XML Web服務(不管客戶端存在於何種平台)。

  當你使用ASP.NET構造一個XML Web服務時,它自動支持客戶端使用SOAP、HTTP-GET和HTTP-POST協議通訊。即使HTTP-GET和HTTP-POST支持使用URL編碼的變量名/變量值對來傳送消息,支持這兩個協議的數據類型也沒有支持SOAP協議的數據類型豐富。在SOAP中,使用XML把數據傳送到XML Web服務或從XML Web服務取回消息,你可以使用支持豐富的數據類型集的XSD模式定義復雜的數據類型。使用ASP.NET構造一個XML Web服務的開發者不必明確地定義復雜的數據類型。他們可以只構造一個管理類。ASP.Net處理定義到一個XSD模式的映射類和到XML數據的映射對象實例,以便通過網絡傳輸。

  重要的是要注意XML Web服務並不能取代DCOM,我們應該說XML Web服務是跨越使用行業標准的平台通信的一種消息傳遞基礎結構。

  因為ASP.NET提供了為XML Web服務內部工作的基礎結構,開發者可以集中精力來實現他們的特定的XML Web服務的功能。使用ASP.Net開發一個XML Web服務從下面三步開始:

  1. 創建一個帶有.asmx擴展名的文件。

  2. 在這個文件裡面,使用一條指令聲明XML Web服務。

  3. 定義組成XML Web服務功能的XML Web服務方法。

  XML Web服務是一個強大的技術,用於提供可通過因特網變成訪問的服務。下面的建議將幫助你創建高效的XML Web服務:

  XML Web服務既支持同步的又支持異步的客戶端和存放XML Web服務的服務器之間的通信。在同步通信情況下,客戶端發送一個對服務的請求到服務主機服務器上等待響應。這防止客戶端在等待結果的時候,執行其它的操作。然而異步通信導致客戶端在等待相應的時候繼續處理其它的任務。客戶端在可用的時候響應服務請求的結果。

  當你使用Web服務描述語言工具(Wsdl.exe)來創建你的代理類的時候,它產生類中的方法的標准的、同步版本和異步版本。異步版本由兩個方法組成,稱為Begin和End。Begin方法用於初始化XML Web服務,而End方法取得結果。

  使用異步通信能夠改善系統使用率和避免客戶端在它等待XML Web服務結果的時候的延遲。

  下面的代碼示例顯示如何從一個客戶應用程序產生一個到XML Web服務的異步調用。

[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>
[Visual Basic]
<%@ Page Language="VB" %>
<%@ Import Namespace="System.Net" %>
<Html>
<script language="VB" runat="server">
Sub EnterBtn_Click(Src As Object, E As EventArgs) 
Dim math As New MyM
ath.Math()
' Call to Add XML Web service method asynchronously 
' and then wait for it to complete.
Dim result As IAsyncResult = _
math.BeginAdd(Convert.ToInt32(Num1.Text), _
Convert.ToInt32(Num2.Text), _
Nothing, _
Nothing)

' Wait for asynchronous call to complete.
result.AsyncWaitHandle.WaitOne()
' Complete the asynchronous call to Add XML Web service method.
Dim addtotal As Single = math.EndAdd(result)
' Display 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 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>

  想獲得更多關於異步通信的信息,請參閱後面的"和XML Web服務異步地通訊"。
  通過因特網產生許多服務請求可能影響客戶應用程序的性能。當設計你的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 Lan
guage="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));
 // 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服務的示例。不要依靠服務描述或服務幫助頁面作為你唯一的文檔。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved