【问题标题】:The current TransactionScope is already complete error after converting to .net core 3.1转换为 .net core 3.1 后,当前的 TransactionScope 已完成错误
【发布时间】: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


    【解决方案1】:

    当你到达 using 块的末尾时,它可能完成了作用域。所以我会尝试从旧方式中删除 transactionScope.Complete(); 并检查它是否已完成。

    【讨论】:

      猜你喜欢
      • 2010-11-29
      • 2018-07-14
      • 2020-08-17
      • 1970-01-01
      • 1970-01-01
      • 2020-08-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多