【发布时间】:2020-04-30 16:21:18
【问题描述】:
更新到 3.1 后,我们收到的错误消息表明:
System.InvalidOperationException:当前的 TransactionScope 已经完成。
我们对此过程所做的唯一真正改变是从 2.x 使用带括号的语句的方式转移到 3.x 的使用不带括号的语句的方式(推荐)。我们是不是做错了什么?
目前的方式:
// As multiple copies of this service can be running at once, we should lock the table while we flag the messages to be worked on
using var transactionScope = new TransactionScope(TransactionScopeOption.Required,
new TransactionOptions()
{ IsolationLevel = IsolationLevel.ReadCommitted });
messagesToProcess = context.InboundMessageQueueRecords.Where(x => x.State == QueueMessageState.New)
.OrderBy(x => x.Added)
.Take(NUMBER_OF_MESSAGES_TO_PROCESS).ToList();
// Flag them as in progress
messagesToProcess.ForEach(x => x.State = QueueMessageState.InProgress);
context.SaveChanges();
transactionScope.Complete();
老路:
// As multiple copies of this service can be running at once, we should lock the table while we flag the messages to be worked on
using (var transactionScope = new TransactionScope(TransactionScopeOption.Required,
new TransactionOptions()
{ IsolationLevel = IsolationLevel.ReadCommitted }))
{
messagesToProcess = context.InboundMessageQueueRecords.Where(x => x.State == QueueMessageState.New)
.OrderBy(x => x.Added)
.Take(NUMBER_OF_MESSAGES_TO_PROCESS).ToList();
// Flag them as in progress
messagesToProcess.ForEach(x => x.State = QueueMessageState.InProgress);
context.SaveChanges();
transactionScope.Complete();
}
【问题讨论】:
标签: transactions asp.net-core-3.1 ef-core-3.1