熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

ASP.NET創建Web服務之使用事務

2022-06-13   來源: .NET編程 

  支持XML Web服務的事務利用公共語言運行期中的支持其是基於Microsoft Transaction Server ( MTS)和COM+ Services中相同的分布式事務模型該模型基於明確的判斷一個對象是否參與一個事務而不是編寫特定的代碼用來處理委托和回調一個事務對於一個使用ASPNET創建的XML Web服務你可以通過設置其應用到一個XML Web服務方法上的WebMethod屬性的TransactionOption屬性來聲明一個XML Web服務的事務行為如果該XML Web服務方法執行的時候拋出一個異常那麼該事務自動地結束相反如果沒有發生異常該事務自動委托

  WebMethod屬性的TransactionOption屬性規定一個XML Web服務方法如何參與一個事務雖然這個聲明級別表示一個事務邏輯但是它是消除實際事務的一個步驟當事物對象訪問數據源(如數據庫或消息隊列)時實際事務產生關聯該對象的事務自動流向適當的資源管理程序NET Framework Data Provider(用於SQL Server或OLE DB)這樣的NET Framework數據提供者在對象的上下文中查找事務並通過Distributed Transaction Coordinator (DTC分布式事務協調程序)編目事務全部的事務自動產生

  XML Web服務方法只能參與一個作為新事務的根的事務作為一個新事務的根所有的與資源管理器(像運行Microsoft SQL ServerMicrosoft Message Queuing和Microsoft Host Integration Server的服務器)的相互作用維護需要運行健壯的分布式應用程序的ACID性質調用其他的XML Web服務方法的XML Web服務方法參與不同的事務因為事務不流經XML Web服務方法

  使用來自XML Web服務方法的事務
 聲明一個XML Web服務
  [C#]
  <%@ WebService Language=C# Class=Orders %>
  [Visual Basic]
  <%@ WebService Language=VB Class=Orders %>

  把一個匯編指令加到SystemEnterpriseServices上
  <%@ Assembly name=SystemEnterpriseServicesVersion=Culture=neutralPublicKeyToken=bfffdaa %>

  添加引用到SystemWebServices和SystemEnterpriseServices域名空間
  [C#]
  using SystemWebServices;
  using SystemEnterpriseServices;
  [Visual Basic]
  Imports SystemWebServices
  Imports SystemEnterpriseServices

  聲明一個XML Web服務方法設置WebMethod屬性的TransactionOption屬性為TransactionOptionRequiresNew
  [C#]
  [ WebMethod(TransactionOption=TransactionOptionRequiresNew)]
  public int DeleteAuthor(string lastName)
  [Visual Basic]
  < WebMethod(TransactionOption:=TransactionOptionRequiresNew)> _
  Public Function DeleteAuthor(lastName As String) As Integer

  下面的代碼示例顯示一個使用單個XML Web服務方法的XML Web服務調用DeleteDatabase這個XML Web服務方法執行一個事務范圍內的數據庫操作如果該數據庫操作拋出一個異常該事務自動地停止否則該事務自動地委托

  [C#]
  <%@ WebService Language=C# Class=Orders %>
  <%@ Assembly name=SystemEnterpriseServicesVersion=Culture=neutralPublicKeyToken=bfffdaa %>
  using System;
  using SystemData;
  using SystemDataSqlClient;
  using SystemWebServices;
  using SystemEnterpriseServices;

  public class Orders : WebService
  {
  [ WebMethod(TransactionOption=TransactionOptionRequiresNew)]
  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(deleteCmdSQLsqlConn);
  SqlCommand exceptionCausingCmd = new
  SqlCommand(exceptionCausingCmdSQLsqlConn);

  // This command should execute properly
  deleteCmdConnectionOpen();
  deleteCmdExecuteNonQuery();

  // 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 ASPNET
  // automatically aborts the transaction The deleteCmd that
  // executed properly is rolled back

  int cmdResult = exceptionCausingCmdExecuteNonQuery();

  sqlConnClose();

  return cmdResult;
  }
  }
  [Visual Basic]
  <%@ WebService Language=VB Class=Orders %>
  <%@ assembly name=SystemEnterpriseServices %>

  Imports System
  Imports SystemData
  Imports SystemDataSqlClient
  Imports SystemWebServices
  Imports SystemWebUtil
  Imports SystemEnterpriseServices

  Public Class Orders

  <WebMethod(TransactionOption:=TransactionOptionRequiresNew)> _
  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(deleteCmdSQLsqlConn)
  Dim exceptionCausingCmd As SqlCommand = New _
  SqlCommand(exceptionCausingCmdSQLsqlConn)

   This command should execute properly
  deleteCmdConnectionOpen()
  deleteCmdExecuteNonQuery()

   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 ASPNET
   automatically aborts the transaction The deleteCmd that
   executed properly is rolled back

  Dim cmdResult As Integer = exceptionCausingCmdExecuteNonQuery()
  sqlConnClose()

  Return cmdResult
  End Function
  End Class


From:http://tw.wingwit.com/Article/program/net/201311/11378.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.