Επεξεργασία

Κοινή χρήση μέσω


WebMethodAttribute.TransactionOption Property

Definition

Indicates the transaction support of an XML Web service method.

public:
 property System::EnterpriseServices::TransactionOption TransactionOption { System::EnterpriseServices::TransactionOption get(); void set(System::EnterpriseServices::TransactionOption value); };
public System.EnterpriseServices.TransactionOption TransactionOption { get; set; }
member this.TransactionOption : System.EnterpriseServices.TransactionOption with get, set
Public Property TransactionOption As TransactionOption

Property Value

The transaction support of an XML Web service method. The default is Disabled.

Examples

The example below begins a new transaction when the Transfer method is called.

// <Snippet1>
<%@ WebService Language="C#" Class="Bank"%>
<%@ assembly name="System.EnterpriseServices,Version=1.0.3300.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a" %>
 
 using System;
 using System.Web.Services;
 using System.EnterpriseServices;
 
 public class Bank : WebService {
 
      [ WebMethod(TransactionOption=TransactionOption.RequiresNew) ]
      public void Transfer(long Amount, long AcctNumberTo, long AcctNumberFrom)  {
            MyCOMObject objBank = new MyCOMObject();
               
            if (objBank.GetBalance(AcctNumberFrom) < Amount )
               // Explicitly abort the transaction.
               ContextUtil.SetAbort();
            else {
               // Credit and Debit methods explicitly vote within
               // the code for their methods whether to commit or
               // abort the transaction.
               objBank.Credit(Amount, AcctNumberTo);
               objBank.Debit(Amount, AcctNumberFrom);
            }
      }
 }
      
// </Snippet1>


 public class MyCOMObject {
    public long GetBalance(long AcctNumber){return 0;}
    public void Credit( long Amount, long AcctNumber) {}
    public void Debit( long Amount, long AcctNumber) {}

 }
' <Snippet1>
<%@ WebService Language="VB" Class="Bank"%>
<%@ assembly name="System.EnterpriseServices,Version=1.0.3300.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a" %>
 
Imports System
Imports System.Web.Services
Imports System.EnterpriseServices

Public Class Bank
    Inherits WebService    
    
    <WebMethod(TransactionOption := TransactionOption.RequiresNew)> _	
    Public Sub Transfer(Amount As Long, AcctNumberTo As Long, AcctNumberFrom As Long)
        
        Dim objBank As New MyCOMObject()
        
        If objBank.GetBalance(AcctNumberFrom) < Amount Then
            ' Explicitly abort the transaction.
            ContextUtil.SetAbort()
        Else
            ' Credit and Debit method explicitly vote within
            ' the code for their methods whether to commit or
            ' abort the transaction.
            objBank.Credit(Amount, AcctNumberTo)
            objBank.Debit(Amount, AcctNumberFrom)
        End If
    End Sub
End Class
      
' </Snippet1>
Public Class MyCOMObject
    Public Function GetBalance(AcctNumber As Long)
    End Function

    Public Sub Credit(Amount as Long, AcctNumber As Long)
    End Sub

    Public Sub Debit(Amount as Long, AcctNumber As Long)
    End Sub
    

End Class

Remarks

XML Web service methods can only participate as the root object in a transaction, due to the stateless nature of the HTTP protocol. XML Web service methods can invoke COM objects that participate in the same transaction as the XML Web service method, if the COM object is marked to run within a transaction in the Component Services administrative tool. If an XML Web service method with a TransactionOption property of Required or RequiresNew invokes another XML Web service method with a TransactionOption property of Required or RequiresNew, each XML Web service method participates in its own transaction, because an XML Web service method can only act as the root object in a transaction.

Item Description
Disabled Indicates that the XML Web service method does not run within the scope of a transaction. When a request is processed, the XML Web service method is executed without a transaction.

[WebMethod(TransactionOption= TransactionOption.Disabled)]
NotSupported Indicates that the XML Web service method does not run within the scope of a transaction. When a request is processed, the XML Web service method is executed without a transaction.

[WebMethod(TransactionOption= TransactionOption.NotSupported)]
Supported Indicates that the XML Web service method does not run within the scope of transactions. When a request is processed, the XML Web service is created without a transaction.

[WebMethod(TransactionOption= TransactionOption.Supported)]
Required Indicates that the XML Web service method requires a transaction. Since XML Web service methods can only participate as the root object in a transaction, a new transaction will be created for the XML Web service method.

[WebMethod(TransactionOption= TransactionOption.Required)]
RequiresNew Indicates that the XML Web service method requires a new transaction. When a request is processed, the XML Web service is created within a new transaction.

[WebMethod(TransactionOption= TransactionOption.RequiresNew)]

If an exception is thrown from or not caught by an XML Web service method, the transaction is automatically aborted. If no exceptions occur the transaction is automatically committed unless the method explicitly calls SetAbort.

Applies to

See also