使用块退出后,未处理LinqTODB数据连接

发布于 2025-02-12 06:58:11 字数 1829 浏览 1 评论 0原文

这个问题使我感到困惑很长一段时间。我正在尝试处置与数据库的连接,但是无论我如何处理它,它都会不断泄漏。更糟糕的是,它应该自动处置。

长话短说,我有一个以以下代码开头的托管服务:

    public async Task StartAsync(CancellationToken cancellationToken)
    {
        using (var scope = _serviceProvider.CreateScope())
        {
            _logger.LogDebug($"Инициализация операторов");
            using (var operatorRepo = scope.ServiceProvider.GetService<IOperatorRepository>()) {
                var availableOperators = operatorRepo.GetAllOperatorsWithSkills();
                _logger.LogDebug($"Инициализовано {availableOperators.Count()} операторов");

                foreach (OperatorRow opRow in availableOperators)
                {
                    OperatorMeta opMeta = new(opRow.Idr, opRow.Name, opRow.Number, opRow.OperatorSkills.Select(skill => (skill.SkillId, skill.Experience)));
                    if (!TryAddOperator(opMeta))
                        throw new ArgumentException($"Оператор с id {opRow.Idr} уже был добавлен!");
                }
                if (Convert.ToBoolean(_configuration.GetSection("isOperatorTesting")?.Value))
                {
                    _globalOperatorPool.TryAdd(
                        -1,
                        new CampaignOperator(
                            new OperatorMeta(-1, "Test operator", _configuration.GetSection("testOperatorNumber").Value, Enumerable.Empty<(int, int)>()),
                            OperatorState.Unassigned)
                        );
                }
            }
        }
    }

我试图将使用的内部限制在尝试中,以防万一,但老实说,我看不到这是一个问题,因为使用ISN一个内部。尽管如此,连接仍未关闭。

IperatorRepository的基础类别从linqtodb.dataconnection延伸的两个类别延伸。这是链中唯一可识别的。

老实说,我很难过。任何帮助将不胜感激。

编辑:我觉得我应该指定连接 class 的确确实返回了objectDisposedexception,但是该连接在数据库中保持开放,并且不断地制作更多的东西,好像它们没有被重复使用一样。

This issue has been perplexing me for quite a while. I'm trying to dispose of a connection to a database, but it keeps leaking no matter how I try to dispose of it. Worse yet, it's supposed to dispose automatically.

Long story short, I have a HostedService that starts with the following code:

    public async Task StartAsync(CancellationToken cancellationToken)
    {
        using (var scope = _serviceProvider.CreateScope())
        {
            _logger.LogDebug(
quot;Инициализация операторов");
            using (var operatorRepo = scope.ServiceProvider.GetService<IOperatorRepository>()) {
                var availableOperators = operatorRepo.GetAllOperatorsWithSkills();
                _logger.LogDebug(
quot;Инициализовано {availableOperators.Count()} операторов");

                foreach (OperatorRow opRow in availableOperators)
                {
                    OperatorMeta opMeta = new(opRow.Idr, opRow.Name, opRow.Number, opRow.OperatorSkills.Select(skill => (skill.SkillId, skill.Experience)));
                    if (!TryAddOperator(opMeta))
                        throw new ArgumentException(
quot;Оператор с id {opRow.Idr} уже был добавлен!");
                }
                if (Convert.ToBoolean(_configuration.GetSection("isOperatorTesting")?.Value))
                {
                    _globalOperatorPool.TryAdd(
                        -1,
                        new CampaignOperator(
                            new OperatorMeta(-1, "Test operator", _configuration.GetSection("testOperatorNumber").Value, Enumerable.Empty<(int, int)>()),
                            OperatorState.Unassigned)
                        );
                }
            }
        }
    }

I have tried to wrap the insides of the using in a try finally blocks just in case but I honestly don't see how that would be an issue because the using isn't inside of one. Still, connection doesn't close.

The underlying class of IOperatorRepository extends from a chain of two classes which extend from LinqToDB.DataConnection. It's the only IDisposable in the chain.

Honestly, I'm stumped. Any help would be appreciated.

Edit: I feel like I should specify that the connection class does indeed return ObjectDisposedException, but the connection stays open in the database, and more are made constantly as if they're not being reused.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文