我正在运行一个使用Entity Framework Core的ASP.NET Core 1.0 Web应用程序。当应用程序运行了一段时间(24-48小时)后,每个端点或静态资源的请求都会导致应用程序崩溃,并抛出错误
我是这样配置Entity Framework的:
Startup.cs
System.InvalidOperationException: ExecuteReader requires an open and available Connection. The connection's current state is closed.
我只能通过重新启动应用程序池来恢复它。我是这样配置Entity Framework的:
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?