共用方式為


IEnlistmentNotification 介面

定義

描述資源管理員應該實作的介面,可提供兩階段認可告知回呼,讓交易管理員用來登記參與。

public interface class IEnlistmentNotification
public interface IEnlistmentNotification
type IEnlistmentNotification = interface
Public Interface IEnlistmentNotification
衍生

範例

下列範例示範這個介面的實作,以及使用 EnlistVolatile 方法將 物件登記為交易中的參與者。

static void Main(string[] args)
{
    try
    {
        using (TransactionScope scope = new TransactionScope())
        {
        
            //Create an enlistment object
            myEnlistmentClass myElistment = new myEnlistmentClass();

            //Enlist on the current transaction with the enlistment object
            Transaction.Current.EnlistVolatile(myElistment, EnlistmentOptions.None);

            //Perform transactional work here.

            //Call complete on the TransactionScope based on console input
                            ConsoleKeyInfo c;
            while(true)
                            {
                Console.Write("Complete the transaction scope? [Y|N] ");
                c = Console.ReadKey();
                Console.WriteLine();
        
                                    if ((c.KeyChar == 'Y') || (c.KeyChar == 'y'))
                {
                    scope.Complete();
                    break;
                }
                else if ((c.KeyChar == 'N') || (c.KeyChar == 'n'))
                {
                    break;
                }
            }
        }
    }
    catch (System.Transactions.TransactionException ex)
    {
        Console.WriteLine(ex);
    }
    catch
    {
        Console.WriteLine("Cannot complete transaction");
        throw;
    }
}

class myEnlistmentClass : IEnlistmentNotification
{
    public void Prepare(PreparingEnlistment preparingEnlistment)
    {
        Console.WriteLine("Prepare notification received");

        //Perform transactional work

        //If work finished correctly, reply prepared
        preparingEnlistment.Prepared();

        // otherwise, do a ForceRollback
        preparingEnlistment.ForceRollback();
    }

    public void Commit(Enlistment enlistment)
    {
        Console.WriteLine("Commit notification received");

        //Do any work necessary when commit notification is received

        //Declare done on the enlistment
        enlistment.Done();
    }

    public void Rollback(Enlistment enlistment)
    {
        Console.WriteLine("Rollback notification received");

        //Do any work necessary when rollback notification is received

        //Declare done on the enlistment
        enlistment.Done();
    }

    public void InDoubt(Enlistment enlistment)
    {
        Console.WriteLine("In doubt notification received");

        //Do any work necessary when indout notification is received
        
        //Declare done on the enlistment
        enlistment.Done();
    }
}
    Public Shared Sub Main()
        Try
            Using scope As TransactionScope = New TransactionScope()

                'Create an enlistment object
                Dim myEnlistmentClass As New EnlistmentClass

                'Enlist on the current transaction with the enlistment object
                Transaction.Current.EnlistVolatile(myEnlistmentClass, EnlistmentOptions.None)

                'Perform transactional work here.

                'Call complete on the TransactionScope based on console input
                Dim c As ConsoleKeyInfo
                While (True)
                    Console.Write("Complete the transaction scope? [Y|N] ")
                    c = Console.ReadKey()
                    Console.WriteLine()
                    If (c.KeyChar = "Y") Or (c.KeyChar = "y") Then
                        scope.Complete()
                        Exit While
                    ElseIf ((c.KeyChar = "N") Or (c.KeyChar = "n")) Then
                        Exit While
                    End If
                End While
            End Using
        Catch ex As TransactionException
            Console.WriteLine(ex)
        Catch
            Console.WriteLine("Cannot complete transaction")
            Throw
        End Try
    End Sub
End Class

Public Class EnlistmentClass
    Implements IEnlistmentNotification

    Public Sub Prepare(ByVal myPreparingEnlistment As PreparingEnlistment) Implements System.Transactions.IEnlistmentNotification.Prepare
        Console.WriteLine("Prepare notification received")

        'Perform transactional work

        'If work finished correctly, reply with prepared
        myPreparingEnlistment.Prepared()
    End Sub

    Public Sub Commit(ByVal myEnlistment As Enlistment) Implements System.Transactions.IEnlistmentNotification.Commit
        Console.WriteLine("Commit notification received")

        'Do any work necessary when commit notification is received

        'Declare done on the enlistment
        myEnlistment.Done()
    End Sub

    Public Sub Rollback(ByVal myEnlistment As Enlistment) Implements System.Transactions.IEnlistmentNotification.Rollback
        Console.WriteLine("Rollback notification received")

        'Do any work necessary when rollback notification is received

        'Declare done on the enlistment
        myEnlistment.Done()
    End Sub

    Public Sub InDoubt(ByVal myEnlistment As Enlistment) Implements System.Transactions.IEnlistmentNotification.InDoubt
        Console.WriteLine("In doubt notification received")

        'Do any work necessary when indout notification is received

        'Declare done on the enlistment
        myEnlistment.Done()
    End Sub
End Class

備註

為了讓資源管理員參與交易,它必須透過交易管理員登記交易。 Transaction 類別會定義一組提供這個功能的方法,其名稱開頭為 Enlist。 不同的 Enlist 方法會對應至資源管理員可能具有之不同型別的登記。

這個類別描述資源管理員應該實作的介面,以在登記參與時為交易管理員提供兩個階段認可通知回呼。 針對每個資源管理員的 介面實 IEnlistmentNotification 作,您應該使用 EnlistVolatile 類別的 方法或 方法 Transaction 來登記,視您的資源是變動性還是 EnlistDurable 永久性而定。 如需登記和 2PC 的詳細資訊,請參閱分別將 資源編列為交易中的參與者 ,以及分別 在Single-Phase和多階段認可交易

交易管理員會透過下列方法,在兩階段認可通訊協定的不同階段通知登記的物件。

方法 描述
Prepare 當交易管理員詢問參與者是否能夠認可交易時,交易管理員會在交易的第一個階段中,使用登錄物件的這個方法做為回呼。
Commit 如果認可交易,交易管理員會在交易的第二個階段中,使用登錄物件的這個方法做為回呼。
Rollback 如果交易已中止 (,則會在交易的第二個階段中,使用登錄物件的這個方法做為交易管理員的回呼,也就是回復) 。
InDoubt 如果交易不確定,則交易管理員會在交易的第二個階段中,使用登錄物件的這個方法做為回呼。

注意

您應該注意,通知可能不會循序傳送,或以特定順序傳送。

方法

Commit(Enlistment)

告知登記的物件正在認可交易。

InDoubt(Enlistment)

告知登記的物件交易的狀態不確定。

Prepare(PreparingEnlistment)

告知登記的物件正在準備認可交易。

Rollback(Enlistment)

告知登記的物件正在復原 (中止) 交易。

適用於

另請參閱