【问题标题】:Get the transaction id when the transaction is committed事务提交时获取事务id
【发布时间】:2018-01-15 13:03:03
【问题描述】:

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

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

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

有没有其他方法可以在作用域完成时获取事务id?

【问题讨论】:

    标签: c# transactions transactionscope


    【解决方案1】:
            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
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-25
      • 2011-04-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多