我正在使用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.
我只能通过重新启动应用程序池来恢复。< / p>
我正在配置实体框架:
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 :(得分:2)
我遇到了同样的问题。
我认为多个线程可能同时使用相同的dbcontext实例。
你可能需要这个:
#include <bits/stdc++.h>
#define s 100001
using namespace std;
int main()
{
long long arr[s],i,j,t,m,n;
for(i=0;i<s;i++)
arr[i] = 1;
arr[0] = 0;
arr[1] = 0;
for(i=2;i<sqrt(s);i++)
{
if(arr[i] == 1)
{
for(j=2;i*j<s;j++)
arr[i*j] = 0;
}
}
cin >> t;
while(t--)
{
cin >> m >> n;
if(n<s)
{
for(i=m;i<=n;i++)
{
if(arr[i] == 1)
cout << i << endl;
}
}
}
return 0;
}
有一个问题。
https://github.com/aspnet/EntityFramework/issues/6491
答案 1 :(得分: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();
}
}
}