EF Core - System.InvalidOperationException:ExecuteReader 需要打开且可用的连接.连接的当前状态是关闭的 [英] EF Core - System.InvalidOperationException: ExecuteReader requires an open and available Connection. The connection's current state is closed

查看:24
本文介绍了EF Core - System.InvalidOperationException:ExecuteReader 需要打开且可用的连接.连接的当前状态是关闭的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行带有 Entity Framework Core 的 ASP.NET Core 1.0 Web 应用程序.当应用程序运行一段时间(24 - 48 小时)时,应用程序开始在对任何端点或静态资源的每个请求时崩溃,并抛出错误 System.InvalidOperationException: ExecuteReader requires an open and available Connection.连接的当前状态是关闭的.我只能通过重新启动应用程序池来恢复.

I'm running an ASP.NET Core 1.0 web app with Entity Framework Core. When the app has been running for a while (24 - 48 hours), the app starts crashing on every request to any endpoint or static resource throwing the error System.InvalidOperationException: ExecuteReader requires an open and available Connection. The connection's current state is closed. I can only recover from this by restarting the App Pool.

我正在像这样配置实体框架:

I am configuring Entity Framework like this:

Startup.cs

public void ConfigureServices(IServiceCollection services)
{
        services.AddDbContext<ApplicationDbContext>(options =>
            options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));   
}

我正在使用类似这样的扩展方法在 owin 管道中加载数据:

I am loading data in the owin pipeline with an extension method like this like this:

Startup.cs

app.LoadTenantData();

AppBuilderExtensions.cs:

AppBuilderExtensions.cs:

public static void LoadTenantData(this IApplicationBuilder app)
    {
        app.Use(async (context, next) =>
        {
            var dbContext = app.ApplicationServices.GetService<ApplicationDbContext>();        
            var club = dbContext.Clubs.Single(c => c.Id == GetClubIdFromUrl(context));
            context.Items[PipelineConstants.ClubKey] = club;
            await next();
        });
    }

由于该错误仅在应用程序长时间运行时发生,因此很难重现,但我假设它与 EF 打开和关闭连接不正确有关.

Since the error only occurs when the app has been running for a long time, it is hard to reproduce, but I'm assuming it has something to do with EF opening and closing connections incorrectly.

我该如何调试?我是否错误地使用了 EF?

How can I go about to debug this? Am I using EF incorrectly?

推荐答案

我遇到了同样的问题.

我认为同一个 dbcontext 实例很可能被多个线程同时使用.

I think it is likely that the same dbcontext instance is being used concurrently by multiple threads.

你可能需要这个:services.AddDbContext(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")),ServiceLifetime.Transient);有一个问题.https://github.com/aspnet/EntityFramework/issues/6491

You may need this: services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")),ServiceLifetime.Transient); There is a issue about that. https://github.com/aspnet/EntityFramework/issues/6491

这篇关于EF Core - System.InvalidOperationException:ExecuteReader 需要打开且可用的连接.连接的当前状态是关闭的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆