c# - EF Core - System.InvalidOperationException: ExecuteReader requires an open and available Connection. The connection's current state is closed -


i'm running asp.net core 1.0 web app entity framework core. when app has been running while (24 - 48 hours), app starts crashing on every request endpoint or static resource throwing error system.invalidoperationexception: executereader requires open , available connection. connection's current state closed. can recover restarting app pool.

i configuring entity framework this:

startup.cs

public void configureservices(iservicecollection services) {         services.adddbcontext<applicationdbcontext>(options =>             options.usesqlserver(configuration.getconnectionstring("defaultconnection")));    } 

i loading data in owin pipeline extension method like this:

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();         });     } 

since error occurs when app has been running long time, hard reproduce, i'm assuming has ef opening , closing connections incorrectly.

how can go debug this? using ef incorrectly?

i got same issue.

i think same dbcontext instance being used concurrently multiple threads.

you may need this: services.adddbcontext<applicationdbcontext>(options => options.usesqlserver(configuration.getconnectionstring("defaultconnection")),servicelifetime.transient); there issue that. https://github.com/aspnet/entityframework/issues/6491


Comments