Reputation: 54103
I have a global datacontext that I renew each request:
protected void Page_Load(object sender, EventArgs e)
{
Response.ContentType = "application/json";
Response.Write(GetResponse());
Response.End();
}
public string GetResponse()
{
string fileName = this.Page.Request.PathInfo;
fileName = fileName.Remove(0, fileName.LastIndexOf("/") + 1);
DataContext.Renew();
MethodInfo method = this.GetType().GetMethod(fileName);
if (method == null)
throw new InvalidOperationException(
string.Format("Unknown method {0}.", fileName));
return (string)method.Invoke(this, new object[0]);
}
But for whatever reason, often when I get multiple requests (Like multiple ajax calls,) I get errors like these:
at System.ThrowHelper.ThrowArgumentException(ExceptionResource resource)
at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
at System.Data.Linq.DataContext.GetTable(MetaTable metaTable)
at System.Data.Linq.DataContext.GetTable[TEntity]()
at LibKezberProjectManager.KezberPMDBDataContext.get_Cases() in c:\Users\jlarouche\Documents\Visual Studio 2012\Projects\KezberProjectManager\LibKezberProjectManager\KezberPMDB.designer.cs:line 204
at LibKezberProjectManager.Data.Cases.GetAllScheduled() in c:\Users\jlarouche\Documents\Visual Studio 2012\Projects\KezberProjectManager\LibKezberProjectManager\Data.cs:line 113
at LibKezberProjectManager.Logic.ScheduleManager.GetScheduledCases() in c:\Users\jlarouche\Documents\Visual Studio 2012\Projects\KezberProjectManager\LibKezberProjectManager\Logic.cs:line 1035
at KezberProjectManager.CalendarServices.GetDates() in c:\Users\jlarouche\Documents\Visual Studio 2012\Projects\KezberProjectManager\KezberProjectManager\CalendarServices.aspx.cs:line 628
I renew the context each request though and it is only used for that request. Am I doing something wrong?
DataContext.Renew() calls db = new KezDBContext();
And all calls use this.
Could the fact that all my accessor methods are static be a problem? I wouldn't think so.
Thanks
Upvotes: 1
Views: 280
Reputation: 10874
You're not providing the code for your DataContext, but I assume it is declared as a static field/property. This is very much a problem, in that the DataContext is not thread-safe, but each concurrent request in ASP.NET is served on a separate thread. You need to create a DataContext per request. This can be achieved in many ways, the simplest (but not necessarily the most maintainable) is probably to associate the DataContext with the Page object by creating a base class derived from System.Web.UI.Page from which all your pages are in turn derived.
Upvotes: 3