c# - HostingEnvironment.QueueBackgroundWorkItem 中的事务范围超时

标签 c# ef-core-2.2

我们正在使用 HostingEnvironment.QueueBackgroundWorkItem 对长时间运行的后台任务进行排队。这很好用,但是,当事务超时时,我们没有得到任何异常并且线程似乎被杀死或挂起。

有人知道这里发生了什么吗?我期待交易超时异常或相关的东西...... 我们注意到,在使用 DbContext 时线程会在超时后挂起,如果没有 DbContext,我们将遇到事务超时。

问题:为什么我们在使用 EF DbContext 时没有得到事务超时异常?

Hanging thread screenshot

    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Linq;
    using System.Threading;
    using System.Web;
    using System.Web.Hosting;
    using System.Web.Mvc;
    using Timer = System.Timers.Timer;

    namespace WebApplication5.Controllers
    {
        public class HomeController: Controller
        {
            public ActionResult Index()
            {
        ViewBag.Title = "Home Page";

        HostingEnvironment.QueueBackgroundWorkItem(cancellationToken =>
        {
            try
            {
                try
                {
                    var dbContext = new TestDbContext();
                    using (var backgroundTimer = CreateTimerWhichExtendsLocks((10)))
                    {
                        using (var Tran =
                            TransactionScopeBuilder.CreateWithDefaultIsolationLevel(timeout: new TimeSpan(0, 0, 30)))
                        {
                            try
                            {
                                int i = 0;
                                do
                                {
                                    var d = dbContext.MyTestEntities.SingleOrDefault(x => x.Id == 1);
                                    Thread.Sleep(500);
                                    i++;
                                    Debug.WriteLine(i);
                                } while (i < 100);
                            }
                            catch (Exception e)
                            {
                                Debug.WriteLine(e);
                                throw;
                            }

                            tran.Complete();
                        }
                    }
                }
                catch (Exception e)
                {
                    Debug.WriteLine(e);
                    throw;
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
                throw;
            }
        });

        return View();
    }

    private static Timer CreateTimerWhichExtendsLocks(int lockDurationInMs)
    {
        var backgroundTimer = new Timer(300);
        backgroundTimer.Elapsed += (sender, e) =>
        {
            Debug.WriteLine(DateTime.Now);
        };
        backgroundTimer.Start();
        return backgroundTimer;
    }
}

最佳答案

这似乎是 efcore 2.x 中的一个已知问题,已在 3.x 版本中修复。

https://github.com/aspnet/EntityFrameworkCore/issues/14218

当事务中的操作花费的时间超过环境事务的超时时间时,连接将被环境事务关闭,但内部方法不知道这一点并尝试重新打开连接,这使程序陷入困境陷入僵局。

关于c# - HostingEnvironment.QueueBackgroundWorkItem 中的事务范围超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57396251/

相关文章:

c# - WIQL : How to get the content of a field of a work item returned by a query

c# - 为什么要删除 String.Trim 制表符?

c# - 在 .NET 中编写 minidump on Unhandled Exception 有意义吗?

c# - EntityFrameworkCore 键列从特定值自动递增

c# - Entity Framework 仅在更新时抛出异常 - 无效的对象名称 'dbo.BirdBrain.service'

c# - xaml解析异常问题仅在win7上

c# - 在c#中将可为空的字符串转换为可为空的int

c# - 从数据库获取结果时防止循环引用

c# - 预加载包括使用 UseLazyLoadingProxies

c# - 带有 EF.Functions.Contains() 的单元测试方法