Transaction.EnlistVolatile 方法

定义

登记可变资源管理器以参与事务。

重载

EnlistVolatile(IEnlistmentNotification, EnlistmentOptions)

登记在事务中支持两阶段提交参与的易失性资源管理器。

EnlistVolatile(ISinglePhaseNotification, EnlistmentOptions)

登记支持单阶段提交优化的可变资源管理器以参与事务。

注解

易失性资源管理器无法从失败中恢复,无法完成参与的事务。 有关易失性和持久资源以及如何登记资源的详细信息,请参阅实现 A 资源管理器。 有关资源管理器如何响应提交通知并准备提交的详细信息,请参阅 在Single-Phase和多阶段提交事务

EnlistVolatile(IEnlistmentNotification, EnlistmentOptions)

登记在事务中支持两阶段提交参与的易失性资源管理器。

C#
public System.Transactions.Enlistment EnlistVolatile (System.Transactions.IEnlistmentNotification enlistmentNotification, System.Transactions.EnlistmentOptions enlistmentOptions);
C#
public System.Transactions.Enlistment EnlistVolatile (System.Transactions.IEnlistmentNotification notification, System.Transactions.EnlistmentOptions options);

参数

enlistmentNotificationnotification
IEnlistmentNotification

实现 IEnlistmentNotification 接口,以接收两阶段提交通知的对象。

enlistmentOptionsoptions
EnlistmentOptions

如果资源管理器在准备阶段想要执行额外工作,则为 EnlistDuringPrepareRequired

返回

Enlistment

描述登记的 Enlistment 对象。

示例

以下示例演示接口的 IEnlistmentNotification 实现,以及使用 EnlistVolatile 该方法将对象登记为事务中的参与者。

C#
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();
    }
}

注解

易失性资源管理器无法从失败中恢复,无法完成参与的事务。 若要在事务中获取持久登记,请使用 EnlistDurable 该方法。

通过此方法登记参与事务的资源管理器会收到两个对应于接口上 IEnlistmentNotification 定义的方法的阶段提交通知。

适用于

.NET 7 和其他版本
产品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8
.NET Standard 2.0, 2.1

EnlistVolatile(ISinglePhaseNotification, EnlistmentOptions)

登记支持单阶段提交优化的可变资源管理器以参与事务。

C#
public System.Transactions.Enlistment EnlistVolatile (System.Transactions.ISinglePhaseNotification singlePhaseNotification, System.Transactions.EnlistmentOptions enlistmentOptions);
C#
public System.Transactions.Enlistment EnlistVolatile (System.Transactions.ISinglePhaseNotification notification, System.Transactions.EnlistmentOptions options);

参数

singlePhaseNotificationnotification
ISinglePhaseNotification

实现 ISinglePhaseNotification 接口的对象,该对象必须能够接收单阶段提交和两阶段提交通知。

enlistmentOptionsoptions
EnlistmentOptions

如果资源管理器在准备阶段想要执行额外工作,则为 EnlistDuringPrepareRequired

返回

Enlistment

描述登记的 Enlistment 对象。

注解

易失性资源管理器无法从未能完成参与的事务中恢复。 若要在事务中获取持久登记,请使用 EnlistDurable 该方法。 有关易失性和持久资源的详细信息以及如何登记资源,请参阅实现 A 资源管理器。 有关资源管理器如何响应提交通知并准备提交的详细信息,请参阅 提交Single-Phase和多阶段中的事务

应注意,即使资源管理器实现加入此方法,也不保证它收到单个阶段提交。 事务管理器仍然可以改为发送两个阶段提交通知。 有关单阶段提交优化的详细信息,请参阅 使用单阶段提交和可促销单阶段通知进行优化

另请参阅

适用于

.NET 7 和其他版本
产品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8
.NET Standard 2.0, 2.1