2016-08-01 27 views
2

我正在使用實體框架核心運行ASP.NET Core 1.0 Web應用程序。當應用程序運行了一段時間(24-48小時)後,應用程序開始崩潰,對每個請求都拋出任何端點或靜態資源拋出錯誤System.InvalidOperationException: ExecuteReader requires an open and available Connection. The connection's current state is closed.我只能通過重新啓動應用程序池來從中恢復。EF Core - System.InvalidOperationException:ExecuteReader需要一個開放且可用的Connection。連接的當前狀態已關閉

我配置實體框架是這樣的:

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不正確?

+0

你能在這裏更新關於你是如何解決這個問題的!我遇到了同樣的問題。我使用異步並在任何地方都有任何db查詢等待,並且當我進行負載測試時,這種情況會持續30%左右。 –

+0

我從來沒有設法解決這個問題 - 最終使用動作過濾器將所有租戶數據加載到MVC管道中:https://github.com/severisv/MyTeam/blob/master/src/MyTeam/Filters /LoadTenantDataAttribute.cs 這隻適用於如果按正確順序添加過濾器 – severin

回答

1

我得到了同樣的問題。

我認爲多個線程可能同時使用相同的dbcontext實例。

您可能需要: services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")),ServiceLifetime.Transient); 這是一個問題。 https://github.com/aspnet/EntityFramework/issues/6491

相關問題