事务提交时获取事务id

Get the transaction id when the transaction is committed

我正在使用事务范围的 IEnlistmentNotification 接口,我想在范围完成后访问提交方法中的当前事务 ID,如下所示:

public void Commit(Enlistment enlistment)
{     
    string transactionId = Transaction.Current.TransactionInformation.LocalIdentifier;
    enlistment.Done();
} 

但是我收到一个错误,因为现在 transaction.current 为空。 根据我的检查,征募实例具有 transactionId 的私有成员,但我无法访问它的保护级别。

当范围完成时,还有其他方法可以获取交易 ID 吗?

        using (TransactionScope scope = new TransactionScope())
        {
            using (YourContext context = new YourContext())
            {
                //DB operations
                context.SaveChanges();
            }
            Transaction.Current.TransactionCompleted += Current_TransactionCompleted;
            TransactionInformation info = Transaction.Current.TransactionInformation;
            string transactionId = Guid.Empty.Equals(info.DistributedIdentifier) ? info.LocalIdentifier : info.DistributedIdentifier.ToString();
            scope.Complete();
        }

        //For the transaction completed event:
        private void Current_TransactionCompleted(object sender, TransactionEventArgs e)
        {
             //e.Transaction.TransactionInformation contains the Id
        }