C# 实体框架核心在web应用程序中可能存在内存泄漏

C# 实体框架核心在web应用程序中可能存在内存泄漏,c#,dependency-injection,asp.net-core-mvc,entity-framework-core,C#,Dependency Injection,Asp.net Core Mvc,Entity Framework Core,我正在ASP.NET MVC Core(v1)应用程序中使用EF Core。我注意到,在将我的应用程序托管到生产环境中进行测试时,IIS服务器通常会由于达到内存限制而频繁地进行回收 我想验证我在应用程序中使用dbContext的方式是否有效,是否在后台造成任何内存泄漏。我读过一些类似的文章,人们建议在使用上下文对象后处理它 但我通过依赖注入使用它,如下所示 Startup.cs类代码段: // This method gets called by the runtime. Use this me

我正在ASP.NET MVC Core(v1)应用程序中使用EF Core。我注意到,在将我的应用程序托管到生产环境中进行测试时,IIS服务器通常会由于达到内存限制而频繁地进行回收

我想验证我在应用程序中使用
dbContext
的方式是否有效,是否在后台造成任何内存泄漏。我读过一些类似的文章,人们建议在使用上下文对象后处理它

但我通过依赖注入使用它,如下所示

Startup.cs类代码段:

// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
    services.AddDbContext<Context>();
}
    public class Context : IdentityDbContext<ApplicationUser> 
    {
        private IConfigurationRoot _config;
        private IHttpContextAccessor _HttpContextAccessor;

        public Context(IConfigurationRoot config, DbContextOptions options, IHttpContextAccessor HttpContextAccessor)
            : base(options)
        {
            _config = config;
            _HttpContextAccessor = HttpContextAccessor;
        }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) 
        {
            base.OnConfiguring(optionsBuilder);

            optionsBuilder.UseSqlServer(_config["ConnectionStrings:ContextConnection"]);
        }
}
//此方法由运行时调用。使用此方法向容器中添加服务。
public void配置服务(IServiceCollection服务)
{
services.AddDbContext();
}
Context.cs类代码段:

// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
    services.AddDbContext<Context>();
}
    public class Context : IdentityDbContext<ApplicationUser> 
    {
        private IConfigurationRoot _config;
        private IHttpContextAccessor _HttpContextAccessor;

        public Context(IConfigurationRoot config, DbContextOptions options, IHttpContextAccessor HttpContextAccessor)
            : base(options)
        {
            _config = config;
            _HttpContextAccessor = HttpContextAccessor;
        }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) 
        {
            base.OnConfiguring(optionsBuilder);

            optionsBuilder.UseSqlServer(_config["ConnectionStrings:ContextConnection"]);
        }
}
公共类上下文:IdentityDbContext
{
私有IConfigurationRoot\u config;
专用IHttpContextAccessor_HttpContextAccessor;
公共上下文(IConfigurationRoot配置、DbContextOptions选项、IHttpContextAccessor HttpContextAccessor)
:基本(选项)
{
_config=config;
_HttpContextAccessor=HttpContextAccessor;
}
配置时受保护的覆盖无效(DBContextOptions Builder Options Builder)
{
基本配置(选项生成器);
optionsBuilder.UseSqlServer(_config[“ConnectionString:ContextConnection”]);
}
}
services.AddDbContext
是否注入了上下文的共享实例,这会导致随着时间的推移查询的所有实体的累积,从而消耗内存

编辑:我还有以下几个单例实例:

services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();

services.AddSingleton<ILoggerService, LoggerService>();

services.AddSingleton<IEmailSender, EmailSender>();
services.AddSingleton();
services.AddSingleton();
services.AddSingleton();

谢谢

在我的例子中,在action filter和一些中间件中,我使用了使用services.BuildServiceProvider()创建的IServiceProvider,如下所示:

public class DependencyManager{
    static class IServiceProvider ServiceProvider{ get;set;}
}
public class SomeMiddleware{
    public void SomeMethod(){
         var someServiceInstance = DependencyManager.ServiceProvider.GetService<SomeService>();
    }
}
公共类依赖项管理器{
静态类IServiceProvider服务提供程序{get;set;}
}
公共类中间件{
公共方法(){
var someServiceInstance=DependencyManager.ServiceProvider.GetService();
}
}
因此,为注入此服务而创建的所有作用域对象都不会链接到任何请求,并且在请求结束时也不会被释放。我在HttpContext下使用RequestServices解决了这个问题:

public class SomeMiddleware{
    public void SomeMethod{
        var someServiceInstance = context.RequestServices.GetService<SomeService>()
    }
}
public类中间件{
公开无效法{
var someServiceInstance=context.RequestServices.GetService()
}
}

AddDbContext
添加了一个
作用域的
实例,因此它是按请求的。为什么要注入
IHttpContextAccessor
,而不是只在
AddDbContext
上传递配置?嗨,Camilo!我这样做是因为我也在使用一个名为entity framework plus()的库进行一些后台审计日志记录,因此需要一些进一步的信息来执行一些后台审计日志记录。因此,由于这是一个作用域实例,这意味着我的EF实现确实不会导致内存泄漏。是否有任何方法可以测量我的开发环境中内存的使用情况?谢谢这不是一件容易的事,不,您需要分析应用程序,看看它会出什么问题。不久前,我回答了一个关于注入
IConfiguration
时内存问题的问题,就像你刚才所做的那样(
\u config=config;
),所以检查一下,以及Camilo,我更新了我的帖子,在我的代码中还包含了一些额外的单例定义。是否建议将此类项目更改为范围/瞬态?