【发布时间】:2016-12-06 11:33:49
【问题描述】:
我正在使用 Entity Framework Core 运行 ASP.NET Core 1.0 Web 应用程序。当应用程序运行了一段时间(24 - 48 小时)后,应用程序开始在对任何端点或静态资源的每次请求时崩溃并抛出错误 System.InvalidOperationException: ExecuteReader requires an open and available Connection. The connection's current state is closed. 我只能通过重新启动应用程序池来恢复。
我正在像这样配置实体框架:
Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
}
我正在使用类似这样的扩展方法在 owin 管道中加载数据:
Startup.cs
app.LoadTenantData();
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打开和关闭连接不正确有关。
我该如何调试呢?我是否错误地使用了 EF?
【问题讨论】:
-
你能在这里更新一下你是如何解决这个问题的!我遇到了同样的问题。我在有任何数据库查询的任何地方都使用异步和等待,当我进行负载测试时,这种情况持续出现在大约 30% 的情况下谢谢。
-
我从未设法解决问题 - 最终使用操作过滤器将我的所有租户数据加载到 MVC 管道内:github.com/severisv/MyTeam/blob/master/src/MyTeam/Filters/… 只有在正确添加过滤器时才有效订购
-
@severin 你能再把它添加到github吗?找不到页面。
标签: c# entity-framework asp.net-core entity-framework-core