如何:创建使用自动事务的 Web 服务方法

以下过程描述如何创建使用自动事务的 Web 服务方法。如果在 Web 服务方法参与某一事务时发生异常,ASP.NET 将自动中止该事务。同样,如果未发生任何异常,则自动提交该事务。

创建使用自动事务的 Web 服务

  1. 导入 System.WebServices 和 System.EnterpriseServices 命名空间。也可以根据需要导入其他命名空间,例如 System.Data 和 System.Data.SqlClient。

    <%@ WebService Language="VB" Class="Orders" %>
    <%@ assembly name="System.EnterpriseServices" %>
    Imports System.Web.Services
    Imports System.EnterpriseServices
    
    <%@ WebService Language="C#" Class="Orders" %>
    <%@ assembly name="System.EnterpriseServices" %>
    using System.Web.Services;
    using System.EnterpriseServices;
    
  2. 定义一个从 WebService 类派生的类。例如,以下代码定义一个名为 Orders 的类,它是从 WebService 类派生的。

    Public Class Orders
       Inherits WebService
    End Class
    
    public class Orders : WebService 
    {
    }
    
  3. 对于每个必须自动参与事务的 Web 方法,应用 WebMethodAttribute 属性,并设置事务选项。例如,在以下代码中,WebMethod 特性应用于 DeleteAuthor 方法,而 TransactionOption 属性设置为 TransactionOption.RequiresNew。

    <WebMethod(TransactionOption := TransactionOption.RequiresNew)> _
    Public Function DeleteAuthor(lastName As String) As Integer
         ' Perform the required database task.
    End Function
    
    [ WebMethod(TransactionOption=TransactionOption.RequiresNew)]
    public int DeleteAuthor(string lastName)  
    {
       // Perform the required database task.
    }
    

示例

<%@ 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
   Inherits WebService
   
   <WebMethod(TransactionOption := TransactionOption.RequiresNew)> _
Public Function DeleteAuthor(lastName As String) As Integer

      Dim deleteCmd As [String] = "DELETE FROM authors2 where au_lname='" 
         & lastName & "'"
      Dim sqlConn As New SqlConnection("Integrated Security=SSPI;database=pubs;server=myserver")
      Dim myCommand As New SqlCommand(deleteCmd, sqlConn)

      ' If a Web service method is participating in a transaction and 
      ' an exception occurs, ASP.NET automatically aborts the transaction.
      ' Likewise, if no exception occurs, then the transaction is
      ' automatically committed.
      myCommand.Connection.Open()
      Return myCommand.ExecuteNonQuery()
   End Function
End Class
<%@ WebService Language="C#" Class="Orders" %>
<%@ assembly name="System.EnterpriseServices" %>

using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.Services;
using System.Web.Util;
using System.EnterpriseServices;

public class Orders : WebService 
  {
     [ WebMethod(TransactionOption=TransactionOption.RequiresNew)]
     public int DeleteAuthor(string lastName)  
     {
       String deleteCmd = "DELETE FROM authors2 
          where au_lname='" + lastName + "'" ;
    
         SqlConnection sqlConn = new SqlConnection("Integrated Security=SSPI;database=pubs;server=myserver");
         SqlCommand myCommand = new SqlCommand(deleteCmd,sqlConn);

   // If a Web service method is participating in a transaction and an 
   // exception occurs, ASP.NET automatically aborts the transaction.
   // Likewise, if no exception occurs, then the transaction is
   // automatically  committed.

         myCommand.Connection.Open();
      return myCommand.ExecuteNonQuery();
     }
}

请参见

概念

自动事务和 XML Web 服务

Footer image

版权所有 (C) 2007 Microsoft Corporation。保留所有权利。