DryIoc ASP.Net 范围容器中的核心 3.1 DbContext 存储

DryIoc ASP.Net Core 3.1 DbContext store in Scope Container

我正在使用 DryIoc(最新版本)进行依赖注入。 在我的应用程序 (Asp.net Core 3.1) 中,我使用 Entity Framework。 我的 AppDbContext hinerits DbContext 和实现 IDisposable 我也使用 UnitOfWork 模式并且 class 是一次性的。

这两个对象被声明为 Transient。 我遵循 DryIoc 的文档,该文档解释了 Transient Disposable 对象的上下文: https://github.com/dadhi/DryIoc/blob/master/docs/DryIoc.Docs/ReuseAndScopes.md

对于我的 AppDbContext,我手动解决了这个服务。我的 UnitOfWork 也一样。最后我调用 Dispose 方法。 但是这两个实例并没有被销毁,而是保存在DryIoc Container的Singleton Scope中。

我做了一些测试并使用了 JetBrain dotMemory。 我的测试是调用一个方法100次

最后,我的 AppDbContext 和 UnitOfWork 在容器范围内有 100 倍:

我尝试了很多创建容器的组合,但每次都是一样的:

var container = new Container(rules =>
                rules.With(propertiesAndFields: request => request.ServiceType.Name.EndsWith("Controller") ? PropertiesAndFields.Auto(request) : null)
            //    .WithoutThrowOnRegisteringDisposableTransient()
            //    .WithTrackingDisposableTransients()
                .WithoutThrowIfDependencyHasShorterReuseLifespan())
            .WithDependencyInjectionAdapter(services);

结果:由于作用域中存储了这两种对象,内存增长很快。

如果我评论 .WithoutThrowOnRegisteringDisposableTransient(),我的代码仍然有效(我以为会抛出异常)

我还尝试将这些服务声明为作用域(针对每个 http 请求),但它不起作用,因为我没有为每个查询创建作用域。 (抛出异常,Asp .Net Core 框架会根据每个 Web 请求自动打开一个范围。

也许我需要在每个请求结束时处理范围?

如何强制销毁对象?

感谢lib的作者,我找到了解决方案: https://github.com/dadhi/DryIoc/issues/261