EF Core - System.InvalidOperationException: ExecuteReader 需要一个打开且可用的连接。连接当前的状态是关闭的。

5
我正在运行一个使用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. 我只能通过重新启动应用程序池来恢复它。
我是这样配置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?


你能说一下你是怎么解决这个问题的吗?我也遇到了同样的问题。我在所有需要进行数据库查询的地方都使用了async和await,但当我进行负载测试时,大约有30%的情况出现了这个问题。谢谢。 - ravi punjwani
我从未成功解决这个问题 - 最终使用操作过滤器在MVC管道内加载了所有租户数据:https://github.com/severisv/MyTeam/blob/master/src/MyTeam/Filters/LoadTenantDataAttribute.cs 只有在正确的顺序添加过滤器时才能正常工作。 - severin
@severin能否再次将它添加到GitHub上?页面未找到。 - Erdogan
1
@Erdogan https://gist.github.com/severisv/1749a13cf38aeb7e6891a9f305441448 - severin
2个回答

6

我有同样的问题。

我认为可能是多个线程同时使用相同的数据库上下文实例。

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


1
虽然一开始我不相信,但是在引入lock到一个执行多表插入的代码块后,错误消失了。谢谢,qin。 - Matas Vaitkevicius
1
这帮助我意识到我的Page方法没有声明为async,而我的服务方法(包含EF查询)是异步的。我正在更新到新编码(异步)的服务方法,而之前的版本不是异步的。谢谢! - burton

0

我的应用程序非常基础(快速编写,运行一次,然后忘记),因此解决上述问题的方法是在执行多表插入的部分周围简单地引入lock

    public void CallGooglePlacesAPIAndSetCallback(string websiteName)
    {
        using (var db = new WebAnalyzerEntities())
        {
            IList<IRecord> addressesToBeSearched = db.Rent.Where<IRecord>(o => o.Url.Contains(websiteName) && o.SpatialAnalysis.Count == 0).ToList().Union(db.Sale.Where<IRecord>(oo => oo.Url.Contains(websiteName) && oo.SpatialAnalysis.Count == 0)).ToList();
            foreach (var locationTobeSearched in addressesToBeSearched)
            {
                try
                {
           //this is where I introduced the lock
                    lock (_lock)
                    {
                        dynamic res = null;
                        using (var client = new HttpClient())
                        {
                            while (res == null || HasProperty(res, "next_page_token"))
                            {
                                var url = $"https://maps.googleapis.com/maps/api/geocode/json?address={locationTobeSearched.Address}&key={googlePlacesApiKey}&bounds=51.222,-11.0133788|55.636,-5.6582363";
                                if (res != null && HasProperty(res, "next_page_token"))
                                    url += "&pagetoken=" + res["next_page_token"];
                                var response = client.GetStringAsync(url).Result;
                                JavaScriptSerializer json = new JavaScriptSerializer();
                                res = json.Deserialize<dynamic>(response);
                                if (res["status"] == "OK")
                                {
                                    Tuple<decimal?, decimal?, string> coordinatesAndPostCode = ReadResponse(res["results"][0]);
                                    if (coordinatesAndPostCode != null && coordinatesAndPostCode.Item1.HasValue && coordinatesAndPostCode.Item2.HasValue)
                                    {
           //this is the line where exception was thrown
                                        locationTobeSearched.SpatialAnalysis.Add(new SpatialAnalysis() { Point = CreatePoint(coordinatesAndPostCode.Item1.Value, coordinatesAndPostCode.Item2.Value) });
                                        locationTobeSearched.PostCode = coordinatesAndPostCode.Item3;
                                    }
                                }
                                else if (res["status"] == "OVER_QUERY_LIMIT")
                                {
                                    return;
                                }
                            }
                        }
                    }
                }
                catch (Exception e)
                {

                }

                db.SaveChanges();
            }
        }
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接