C# Try Catch-与当前连接关联的事务已完成,但尚未释放
我收到错误:与当前连接关联的事务已完成,但尚未处理。在Log.WriteEntry之后($”尝试为事务{transaction.TransactionLogId}:{ex.Message}创建发票时出错);当我运行代码时。。有人能帮忙吗C# Try Catch-与当前连接关联的事务已完成,但尚未释放,c#,error-handling,runtime,transactionscope,C#,Error Handling,Runtime,Transactionscope,我收到错误:与当前连接关联的事务已完成,但尚未处理。在Log.WriteEntry之后($”尝试为事务{transaction.TransactionLogId}:{ex.Message}创建发票时出错);当我运行代码时。。有人能帮忙吗 public virtual GroupCreationResult CreateGroups(IEnumerable<TransactionDetail> transactions) { var transactionDet
public virtual GroupCreationResult CreateGroups(IEnumerable<TransactionDetail> transactions)
{
var transactionDetails = transactions as TransactionDetail[] ?? transactions.ToArray();
var successes = new List<int>(transactionDetails.Length);
var errors = new List<TransactionError>();
foreach (var transaction in transactionDetails)
{
using (var scope = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions { IsolationLevel = IsolationLevel.ReadCommitted, Timeout = new TimeSpan(0, 2, 0) }))
{
try
{
foreach (var service in _invoiceCreationServices)
{
try
{
service.CreateInvoice(transaction);
}
catch (Exception ex)
{
Log.WriteEntry($"Error occurred while trying to create invoice for transaction {transaction.TransactionLogId} : {ex.Message}");
if (!(ex.ToString().Contains("Violation of PRIMARY KEY constraint 'PK_APInvGrp'.") || ex.ToString().Contains("Violation of PRIMARY KEY constraint .")))
{
Log.WriteEntry($"error occured while adding the transaction {transaction.TransactionLogId} - {ex.ToString()}");
errors.Add(new TransactionError(transaction.TransactionLogId, ex.ToString()));
scope.Complete();
break;
}
}
}
Log.WriteEntry($"successfully added the transaction {transaction.TransactionLogId}");
successes.Add(transaction.TransactionLogId);
scope.Complete();
}
catch (Exception exception)
{
Log.WriteEntry($"error1 occured while adding the transaction {transaction.TransactionLogId} - {exception.ToString()}");
//errors.Add(new TransactionError(transaction.TransactionLogId, exception.ToString()));
}
}
}
return BuildGroupCreationResult(successes, errors);
}
公共虚拟组CreationResult CreateGroups(IEnumerable事务)
{
var transactionDetails=作为TransactionDetail[]?transactions.ToArray()的交易;
var成功=新列表(transactionDetails.Length);
var errors=新列表();
foreach(transactionDetails中的var交易)
{
使用(var scope=new TransactionScope(TransactionScopeOption.Required,new TransactionOptions{IsolationLevel=IsolationLevel.ReadCommitted,Timeout=new TimeSpan(0,2,0)}))
{
尝试
{
foreach(发票创建服务中的var服务)
{
尝试
{
服务。创建发票(交易);
}
捕获(例外情况除外)
{
Log.WriteEntry($”尝试为事务{transaction.TransactionLogId}:{ex.Message}创建发票时出错);
如果(!(例如ToString()包含(“违反主键约束‘PK_APInvGrp’”))| |例如ToString()包含(“违反主键约束”))
{
Log.WriteEntry($”添加事务{transaction.TransactionLogId}-{ex.ToString()}时出错);
添加(新TransactionError(transaction.TransactionLogId,例如ToString());
scope.Complete();
打破
}
}
}
Log.WriteEntry($“已成功添加事务{transaction.TransactionLogId}”);
添加(transaction.TransactionLogId);
scope.Complete();
}
捕获(异常)
{
Log.WriteEntry($”添加事务{transaction.TransactionLogId}-{exception.ToString()})时发生错误1);
//添加(新TransactionError(transaction.TransactionLogId,exception.ToString());
}
}
}
返回BuildGroupCreationResult(成功、错误);
}
您只能调用Complete()
一次。然后,您必须处理该事务并启动另一个事务。你试图在循环中重复使用它。要么为循环的每个迭代创建一个新事务,要么直到最后才调用Complete()
。谢谢你的建议对我有效。。。我