[英]Get the transaction id when the transaction is committed
我正在从事务作用域中使用IEnlistmentNotification接口,并且要在作用域完成后像下面这样在commit方法中访问当前事务ID:
public void Commit(Enlistment enlistment)
{
string transactionId = Transaction.Current.TransactionInformation.LocalIdentifier;
enlistment.Done();
}
但是我收到一个错误,因为现在transaction.current为null。 从我检查的结果来看,征募实例具有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
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.