Transaction.EnlistVolatile Méthode

Définition

Inscrit un gestionnaire de ressources volatil pour participer à une transaction.

Surcharges

EnlistVolatile(IEnlistmentNotification, EnlistmentOptions)

Inscrit un gestionnaire de ressources volatiles qui prend en charge la validation à deux phases pour participer à une transaction.

EnlistVolatile(ISinglePhaseNotification, EnlistmentOptions)

Inscrit un gestionnaire de ressources volatil qui prend en charge l'optimisation de la validation à phase unique pour participer à une transaction.

Remarques

Les gestionnaires de ressources volatiles ne peuvent pas récupérer en cas d’échec de la réalisation d’une transaction dans laquelle ils participaient. Pour plus d’informations sur les ressources volatiles et durables, ainsi que sur l’inscription d’une ressource, consultez Implémentation d’un Resource Manager. Pour plus d’informations sur la façon dont un gestionnaire de ressources répond à la notification de validation et prépare la validation, consultez Commiting A Transaction In Single-Phase and Multi-Phase.

EnlistVolatile(IEnlistmentNotification, EnlistmentOptions)

Inscrit un gestionnaire de ressources volatiles qui prend en charge la validation à deux phases pour participer à une transaction.

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);

Paramètres

enlistmentNotificationnotification
IEnlistmentNotification

Un objet qui implémente l’interface IEnlistmentNotification pour recevoir des notifications de validation en deux phases.

enlistmentOptionsoptions
EnlistmentOptions

EnlistDuringPrepareRequired si le gestionnaire de ressources souhaite exécuter un travail supplémentaire pendant la phase de préparation.

Retours

Enlistment

Un objet Enlistment qui décrit l'inscription.

Exemples

L’exemple suivant montre une implémentation de l’interface, ainsi que l’inscription de IEnlistmentNotification l’objet en tant que participant à une transaction à l’aide de la EnlistVolatile méthode.

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

Remarques

Les gestionnaires de ressources volatiles ne peuvent pas récupérer en cas d’échec de la réalisation d’une transaction dans laquelle ils participaient. Pour obtenir une inscription durable dans une transaction, utilisez la EnlistDurable méthode.

Les gestionnaires de ressources inscrits pour participer à une transaction par le biais de cette méthode reçoivent des notifications de validation en deux phases qui correspondent aux méthodes définies sur l’interface IEnlistmentNotification .

S’applique à

.NET 7 et autres versions
Produit Versions
.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)

Inscrit un gestionnaire de ressources volatil qui prend en charge l'optimisation de la validation à phase unique pour participer à une transaction.

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);

Paramètres

singlePhaseNotificationnotification
ISinglePhaseNotification

Objet qui implémente l'interface ISinglePhaseNotification qui doit être capable de recevoir une validation à phase unique et des notifications de validation à deux phases.

enlistmentOptionsoptions
EnlistmentOptions

EnlistDuringPrepareRequired si le gestionnaire de ressources souhaite exécuter un travail supplémentaire pendant la phase de préparation.

Retours

Enlistment

Un objet Enlistment qui décrit l'inscription.

Remarques

Les gestionnaires de ressources volatiles ne peuvent pas se récupérer de l’échec de la réalisation d’une transaction dans laquelle ils participaient. Pour obtenir une inscription durable dans une transaction, utilisez la EnlistDurable méthode. Pour plus d’informations sur les ressources volatiles et durables, ainsi que sur la façon d’inscrire une ressource, consultez Implémentation d’un Resource Manager. Pour plus d’informations sur la façon dont un gestionnaire de ressources répond à la notification de validation et prépare la validation, consultez Commiting A Transaction In Single-Phase and Multi-Phase.

Vous devez noter que même lorsque votre implémentation resource manager s’inscrit avec cette méthode, elle n’est pas garantie qu’elle reçoit une validation de phase unique. Le gestionnaire de transactions peut toujours envoyer deux notifications de validation de phase à la place. Pour plus d’informations sur l’optimisation de la validation de phase unique, consultez Optimisation à l’aide de la validation de phase unique et de la notification de phase unique promotable.

Voir aussi

S’applique à

.NET 7 et autres versions
Produit Versions
.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