方法 :自動トランザクションを使用する 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 クラスから派生するクラスを定義します。たとえば、次のコードは、WebService クラスから派生する Orders という名前のクラスを定義します。

    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

Copyright © 2007 by Microsoft Corporation.All rights reserved.