參與使用 ASP.NET 建立的 XML Web Service 中的交易
XML Web Service 的交易支援採用 Common Language Runtime中的支援功能,這些功能是根據與 Microsoft Transaction Server (MTS) 和 COM+ 服務相同的分散式交易模型而定。這個模型是以宣告方式決定物件是否參與交易,而不是撰寫特定程式碼來處理認可和復原交易。您可以在套用至 XML Web Service 方法的 WebMethod 屬性 (Attribute) 中,設定 TransactionOption 屬性 (Property),替使用 ASP.NET 建立的 XML Web Service 宣告 XML Web Service 的交易行為。如果 XML Web Service 方法執行時擲回例外狀況 (Exception),交易會自動中止;相反地,如果沒有例外發生,會自動認可交易。
WebMethod 屬性的 TransactionOption 屬性指定 XML Web Service 方法參與交易的方式。雖然這個宣告性層級代表交易的邏輯,它將會以一個步驟從實體交易移除。實體交易在交易物件存取資料資源時發生,例如資料庫或訊息佇列。與物件相關的交易會自動流向適當的資源管理員。.NET Framework 資料提供者 (例如 SQL Server 的 .NET Framework 資料提供者或 OLE DB 的 .NET Framework 資料提供者),會在物件的內容中查詢交易,然後透過分散式交易協調器 (DTC) 在交易中登記。整個實體交易會自動發生。
XML Web Service 方法僅可以新交易目錄的方式參與交易。因為它是新交易的根目錄,所以與資源管理員 (執行 Microsoft SQL Server、Microsoft Message Queuing (MSMQ) 和維護 ACID 屬性的 Microsoft Host Integration Server 等的伺服器) 之間的所有互動,都需要執行穩定的分散式應用程式。因為交易不會行經 XML Web Service 方法,呼叫其他 XML Web Service 方法的 XML Web Service 方法會參與不同的交易。
若要從 XML Web Service 方法參與交易
宣告 XML Web Service。
<%@ WebService Language="C#" Class="Orders" %> [Visual Basic] <%@ WebService Language="VB" Class="Orders" %>
將 Assembly 指示詞加入至 System.EnterpriseServices。
<%@ Assembly name="System.EnterpriseServices,Version=1.0.3300.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a" %>
加入 System.Web.Services 和 System.EnterpriseServices 命名空間的參考。
using System.Web.Services; using System.EnterpriseServices; [Visual Basic] Imports System.Web.Services Imports System.EnterpriseServices
宣告 XML Web Service 方法,將 WebMethod 屬性 (Attribute) 的 TransactionOption 屬性 (Property) 設定為 TransactionOption.RequiresNew。
[ WebMethod(TransactionOption=TransactionOption.RequiresNew)] public int DeleteAuthor(string lastName) [Visual Basic] < WebMethod(TransactionOption:=TransactionOption.RequiresNew)> _ Public Function DeleteAuthor(lastName As String) As Integer
下列程式碼範例示範 XML Web Service 公開名為 DeleteDatabase
的單一 XML Web Service 方法。這個 XML Web Service 方法執行在交易範圍內的資料庫作業。如果資料庫操作擲回例外狀況,交易會自動停止;否則會自動認可交易。
<%@ WebService Language="C#" Class="Orders" %>
<%@ Assembly name="System.EnterpriseServices,Version=1.0.3300.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a" %>
using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.Services;
using System.EnterpriseServices;
public class Orders : WebService
{
[ WebMethod(TransactionOption=TransactionOption.RequiresNew)]
public int DeleteAuthor(string lastName)
{
String deleteCmd = "DELETE FROM authors WHERE au_lname='" +
lastName + "'" ;
String exceptionCausingCmdSQL = "DELETE FROM NonExistingTable WHERE
au_lname='" + lastName + "'" ;
SqlConnection sqlConn = new SqlConnection(
"Persist Security Info=False;Integrated Security=SSPI;database=pubs;server=myserver");
SqlCommand deleteCmd = new SqlCommand(deleteCmdSQL,sqlConn);
SqlCommand exceptionCausingCmd = new
SqlCommand(exceptionCausingCmdSQL,sqlConn);
// This command should execute properly.
deleteCmd.Connection.Open();
deleteCmd.ExecuteNonQuery();
// This command results in an exception, so the first command is
// automatically rolled back. Since the XML Web service method is
// participating in a transaction, and an exception occurs, ASP.NET
// automatically aborts the transaction. The deleteCmd that
// executed properly is rolled back.
int cmdResult = exceptionCausingCmd.ExecuteNonQuery();
sqlConn.Close();
return cmdResult;
}
}
[Visual Basic]
<%@ WebService Language="VB" Class="Orders" %>
<%@ assembly name="System.EnterpriseServices" %>
Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Web.Services
Imports System.Web.Util
Imports System.EnterpriseServices
Public Class Orders
<WebMethod(TransactionOption:=TransactionOption.RequiresNew)> _
Public Function DeleteAuthor (lastName as String) as Integer
Dim deleteCmdSQL As String = "DELETE FROM authors WHERE au_lname='" + _
lastName + "'"
Dim exceptionCausingCmdSQL As String = "DELETE FROM " + _
"NonExistingTable WHERE au_lname='" + lastName + "'"
Dim sqlConn As SqlConnection = New SqlConnection( _
"Persist Security Info=False;Integrated Security=SSPI;database=pubs;server=myserver")
Dim deleteCmd As SqlCommand = New SqlCommand(deleteCmdSQL,sqlConn)
Dim exceptionCausingCmd As SqlCommand = New _
SqlCommand(exceptionCausingCmdSQL,sqlConn)
' This command should execute properly.
deleteCmd.Connection.Open()
deleteCmd.ExecuteNonQuery()
' This command results in an exception, so the first command is
' automatically rolled back. Since the XML Web service method is
' participating in a transaction, and an exception occurs, ASP.NET
' automatically aborts the transaction. The deleteCmd that
' executed properly is rolled back.
Dim cmdResult As Integer = exceptionCausingCmd.ExecuteNonQuery()
sqlConn.Close()
Return cmdResult
End Function
End Class
注意 當實作 XML Web Service 方法的方法,由於具 .asmx 副檔名之 Internet 要求 (方法常駐或與其關聯) 而未呼叫,則 TransactionOption 屬性的值便不會影響。當方法所在的類別屬於 Visual Studio .NET 內專案的成員,而且沒有使用 Proxy 類別叫用 XML Web Service 時,就會發生這種情形。在 Visual Studio .NET 中,加入 Web 參考時會產生 Proxy 類別。
請參閱
處理交易 | TransactionOption 列舉型別 | WebMethodAttribute.TransactionOption 屬性 | 使用 ASP.NET 建置 XML Web Service