views:

1300

answers:

4

I have been using the new MVC framework with StructureMap recently and have had good results overall, however, I keep running into a very strange error that I cannot understand or work out how to resolve.

This is my architecture:

  • DBContext - linqToSql data context.
  • IRepository - contract defining data methods.
  • IService - contract defining service methods.
  • Controllers - two in this example.

I therefore have:

public class Repo : IRepository
{
    public Repo(DBContext db)
    {
       .....
    }
}

public class Service : IService
{
    public Service(IRepository repo)
    {
       .....
    }
}

public class ControllerOne : Controller
{
    public ControllerOne(IService service)
    {
       .....
    }
}

public class ControllerTwo : Controller
{
    public ControllerTwo(IService service)
    {
       .....
    }
}

StructureMap is being used to define concrete types for IRepository and IService and the DBContext is constructed by the lamba expression - () => new DBContext() configured by DSL registry.

There is no caching of the DBContext at present

Onto the problem:

My index page loads and makes two simultaneous Ajax requests to ControllerOne and ControllerTwo, which are constructed via the StructureMap controller factory from MvcContrib.

StructureMap is injecting the concrete types of IService, which in turn are created with the configured IRepository instance and a new DBContext object.

ControllerOne is requesting a model from the IService instance, which is then returned as a JsonActionResult, which is rendered by Newtonsoft.Json.

ControllerTwo is requesting a different model from the IService instance, which is also serialised to a Json object when the MVC framework executes the ActionResult.

I am running the website via Cassini in VS2008.

The problem I am seeing every now and then is an error generated from within LinqToSql

  • data cannot be read, there is already a reader open or
  • cannot load data into a data table as the data already exists (I do not have the exact exceptions to hand at present but both are internal to LinqToSql).

If the error happens in ControllerOne then ControllerTwo will also fail with a similar error, as if the two requests are running with shared objects.

It doesn't error all the time but it's enough to make me concerned about my architecture and that it's configured wrong in some way.

Is there any way StructureMap could be returning the same instance of ControllerOne and ControllerTwo on subsequent requests, or if it's caching the DBContext in any way? Even though I am not asking it to?

Has anyone seen anything similar when working within Visual Studio / Cassini? Does going through IIS help?

Should I removed LinqToSql?

Closing down Visual Studio and opening up again can often resolve the problem for a while.

Many thanks if anyone can shed any light on the problem.

EDIT: Including logging snippet from NLog log file (thread id is number before semi colon):

03/20/2009 01:40:32 12: controller=Timesheet,date=2001-05-06,Action=WeekEnding /beta/Timesheet/2001-05-06?_dc=1237513232397 
03/20/2009 01:40:32 10: controller=Timesheet,date=2001-05-06,Action=WeekEnding /beta/Timesheet/2001-05-06?_dc=1237513232449 
03/20/2009 01:40:32 10: There is already an open DataReader associated with this Command which must be closed first. System.InvalidOperationException[br]   at System.Data.SqlClient.SqlInternalConnectionTds.ValidateConnectionForExecute(SqlCommand command)
   at System.Data.SqlClient.SqlConnection.ValidateConnectionForExecute(String method, SqlCommand command)
   at System.Data.SqlClient.SqlCommand.ValidateCommand(String method, Boolean async)
   at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result)
   at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)
   at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method)
   at System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior)
   at System.Data.Common.DbCommand.ExecuteReader()
   at System.Data.Linq.SqlClient.SqlProvider.Execute(Expression query, QueryInfo queryInfo, IObjectReaderFactory factory, Object[] parentArgs, Object[] userArgs, ICompiledSubQuery[] subQueries, Object lastResult)
   at System.Data.Linq.SqlClient.SqlProvider.ExecuteAll(Expression query, QueryInfo[] queryInfos, IObjectReaderFactory factory, Object[] userArguments, ICompiledSubQuery[] subQueries)
   at System.Data.Linq.SqlClient.SqlProvider.System.Data.Linq.Provider.IProvider.Execute(Expression query)
   at System.Data.Linq.DataQuery`1.System.Linq.IQueryProvider.Execute[S](Expression expression)
   at System.Linq.Queryable.Sum[TSource](IQueryable`1 source, Expression`1 selector)
   at HCD.Intranet.Core.Data.Linq.LinqEmployeeRepository.CalculateHolidaysRemaining(Employee employee, DateTime weekEnding, Int32 nonProjectId)
   at HCD.Intranet.Core.Services.Impl.EmployeeService.CalculateHolidaysRemaining(Employee employee, DateTime weekEnding)
   at HCD.Intranet.Core.Models.Timesheet.CalculateHolidaysRemaining()
   at HCD.Intranet.Core.Models.Json.TimesheetJsonConverter.WriteTimesheet(JsonWriter writer, Timesheet timesheet)
   at HCD.Intranet.Core.Models.Json.TimesheetJsonConverter.WriteJson(JsonWriter writer, Object value)
   at Newtonsoft.Json.JsonSerializer.SerializeValue(JsonWriter writer, Object value, JsonConverter memberConverter)
   at Newtonsoft.Json.JsonSerializer.WriteMemberInfoProperty(JsonWriter writer, Object value, JsonMemberMapping memberMapping)
   at Newtonsoft.Json.JsonSerializer.SerializeObject(JsonWriter writer, Object value)
   at Newtonsoft.Json.JsonSerializer.SerializeValue(JsonWriter writer, Object value, JsonConverter memberConverter)
   at Newtonsoft.Json.JsonSerializer.Serialize(JsonWriter jsonWriter, Object value)
   at HCD.Intranet.Core.Web.Mvc.NewtonsoftJsonResult.ExecuteResult(ControllerContext context)
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult)
   at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass11.<InvokeActionResultWithFilters>b__e()
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation)
   at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass11.<>c__DisplayClass13.<InvokeActionResultWithFilters>b__10()
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation)
   at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass11.<>c__DisplayClass13.<InvokeActionResultWithFilters>b__10()
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult)
   at System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName)
03/20/2009 01:40:32 12: Invalid attempt to call Read when reader is closed. System.InvalidOperationException[br]   at System.Data.Linq.SqlClient.SqlProvider.Execute(Expression query, QueryInfo queryInfo, IObjectReaderFactory factory, Object[] parentArgs, Object[] userArgs, ICompiledSubQuery[] subQueries, Object lastResult)
   at System.Data.Linq.SqlClient.SqlProvider.ExecuteAll(Expression query, QueryInfo[] queryInfos, IObjectReaderFactory factory, Object[] userArguments, ICompiledSubQuery[] subQueries)
   at System.Data.Linq.SqlClient.SqlProvider.System.Data.Linq.Provider.IProvider.Execute(Expression query)
   at System.Data.Linq.DataQuery`1.System.Linq.IQueryProvider.Execute[S](Expression expression)
   at System.Linq.Queryable.Sum[TSource](IQueryable`1 source, Expression`1 selector)
   at HCD.Intranet.Core.Data.Linq.LinqEmployeeRepository.CalculateHolidaysRemaining(Employee employee, DateTime weekEnding, Int32 nonProjectId)
   at HCD.Intranet.Core.Services.Impl.EmployeeService.CalculateHolidaysRemaining(Employee employee, DateTime weekEnding)
   at HCD.Intranet.Core.Models.Timesheet.CalculateHolidaysRemaining()
   at HCD.Intranet.Core.Models.Json.TimesheetJsonConverter.WriteTimesheet(JsonWriter writer, Timesheet timesheet)
   at HCD.Intranet.Core.Models.Json.TimesheetJsonConverter.WriteJson(JsonWriter writer, Object value)
   at Newtonsoft.Json.JsonSerializer.SerializeValue(JsonWriter writer, Object value, JsonConverter memberConverter)
   at Newtonsoft.Json.JsonSerializer.WriteMemberInfoProperty(JsonWriter writer, Object value, JsonMemberMapping memberMapping)
   at Newtonsoft.Json.JsonSerializer.SerializeObject(JsonWriter writer, Object value)
   at Newtonsoft.Json.JsonSerializer.SerializeValue(JsonWriter writer, Object value, JsonConverter memberConverter)
   at Newtonsoft.Json.JsonSerializer.Serialize(JsonWriter jsonWriter, Object value)
   at HCD.Intranet.Core.Web.Mvc.NewtonsoftJsonResult.ExecuteResult(ControllerContext context)
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult)
   at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass11.<InvokeActionResultWithFilters>b__e()
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation)
   at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass11.<>c__DisplayClass13.<InvokeActionResultWithFilters>b__10()
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation)
   at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass11.<>c__DisplayClass13.<InvokeActionResultWithFilters>b__10()
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult)
   at System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName)
03/20/2009 01:41:58 12: controller=Timesheet,month=6,year=2001,Action=Calendar /beta/Timesheet/Calendar/6/2001?_dc=1237513318470 
03/20/2009 01:41:59 10: controller=Timesheet,date=2001-06-03,Action=WeekEnding /beta/Timesheet/2001-06-03?_dc=1237513318509 
03/20/2009 01:41:59 12: The null value cannot be assigned to a member with type System.Int32 which is a non-nullable value type. System.InvalidOperationException[br]   at Read_TimesheetEntry(ObjectMaterializer`1 )
   at System.Data.Linq.SqlClient.ObjectReaderCompiler.ObjectReader`2.MoveNext()
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at HCD.Intranet.Core.Data.Linq.LinqTimesheetRepository.GetEntries(Int32 timesheetHeaderId)
   at HCD.Intranet.Core.Services.Impl.TimesheetService.GetEntries(Int32 timesheetHeaderId)
   at HCD.Intranet.Core.Models.Timesheet.get_InnerEntries()
   at HCD.Intranet.Core.Models.TimeMap..ctor(Timesheet timesheet)
   at HCD.Intranet.Core.Models.Json.TimesheetCalendarJsonConverter.WriteTimesheet(JsonWriter writer, Timesheet[] timesheets)
   at HCD.Intranet.Core.Models.Json.TimesheetCalendarJsonConverter.WriteJson(JsonWriter writer, Object value)
   at Newtonsoft.Json.JsonSerializer.SerializeValue(JsonWriter writer, Object value, JsonConverter memberConverter)
   at Newtonsoft.Json.JsonSerializer.Serialize(JsonWriter jsonWriter, Object value)
   at HCD.Intranet.Core.Web.Mvc.NewtonsoftJsonResult.ExecuteResult(ControllerContext context)
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult)
   at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass11.<InvokeActionResultWithFilters>b__e()
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation)
   at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass11.<>c__DisplayClass13.<InvokeActionResultWithFilters>b__10()
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation)
   at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass11.<>c__DisplayClass13.<InvokeActionResultWithFilters>b__10()
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult)
   at System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName)
03/20/2009 01:41:59 12: controller=Timesheet,date=2001-06-03,Action=WeekEnding /beta/Timesheet/2001-06-03?_dc=1237513318545 
03/20/2009 01:41:59 12: There is already an open DataReader associated with this Command which must be closed first. System.InvalidOperationException[br]   at System.Data.SqlClient.SqlInternalConnectionTds.ValidateConnectionForExecute(SqlCommand command)
   at System.Data.SqlClient.SqlConnection.ValidateConnectionForExecute(String method, SqlCommand command)
   at System.Data.SqlClient.SqlCommand.ValidateCommand(String method, Boolean async)
   at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result)
   at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)
   at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method)
   at System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior)
   at System.Data.Common.DbCommand.ExecuteReader()
   at System.Data.Linq.SqlClient.SqlProvider.Execute(Expression query, QueryInfo queryInfo, IObjectReaderFactory factory, Object[] parentArgs, Object[] userArgs, ICompiledSubQuery[] subQueries, Object lastResult)
   at System.Data.Linq.SqlClient.SqlProvider.ExecuteAll(Expression query, QueryInfo[] queryInfos, IObjectReaderFactory factory, Object[] userArguments, ICompiledSubQuery[] subQueries)
   at System.Data.Linq.SqlClient.SqlProvider.System.Data.Linq.Provider.IProvider.Execute(Expression query)
   at System.Data.Linq.DataQuery`1.System.Linq.IQueryProvider.Execute[S](Expression expression)
   at System.Linq.Queryable.Sum[TSource](IQueryable`1 source, Expression`1 selector)
   at HCD.Intranet.Core.Data.Linq.LinqEmployeeRepository.CalculateHolidaysRemaining(Employee employee, DateTime weekEnding, Int32 nonProjectId)
   at HCD.Intranet.Core.Services.Impl.EmployeeService.CalculateHolidaysRemaining(Employee employee, DateTime weekEnding)
   at HCD.Intranet.Core.Models.Timesheet.CalculateHolidaysRemaining()
   at HCD.Intranet.Core.Models.Json.TimesheetJsonConverter.WriteTimesheet(JsonWriter writer, Timesheet timesheet)
   at HCD.Intranet.Core.Models.Json.TimesheetJsonConverter.WriteJson(JsonWriter writer, Object value)
   at Newtonsoft.Json.JsonSerializer.SerializeValue(JsonWriter writer, Object value, JsonConverter memberConverter)
   at Newtonsoft.Json.JsonSerializer.WriteMemberInfoProperty(JsonWriter writer, Object value, JsonMemberMapping memberMapping)
   at Newtonsoft.Json.JsonSerializer.SerializeObject(JsonWriter writer, Object value)
   at Newtonsoft.Json.JsonSerializer.SerializeValue(JsonWriter writer, Object value, JsonConverter memberConverter)
   at Newtonsoft.Json.JsonSerializer.Serialize(JsonWriter jsonWriter, Object value)
   at HCD.Intranet.Core.Web.Mvc.NewtonsoftJsonResult.ExecuteResult(ControllerContext context)
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult)
   at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass11.<InvokeActionResultWithFilters>b__e()
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation)
   at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass11.<>c__DisplayClass13.<InvokeActionResultWithFilters>b__10()
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation)
   at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass11.<>c__DisplayClass13.<InvokeActionResultWithFilters>b__10()
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult)
   at System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName)
03/20/2009 01:41:59 10: Invalid attempt to call Read when reader is closed. System.InvalidOperationException[br]   at System.Data.Linq.SqlClient.SqlProvider.Execute(Expression query, QueryInfo queryInfo, IObjectReaderFactory factory, Object[] parentArgs, Object[] userArgs, ICompiledSubQuery[] subQueries, Object lastResult)
   at System.Data.Linq.SqlClient.SqlProvider.ExecuteAll(Expression query, QueryInfo[] queryInfos, IObjectReaderFactory factory, Object[] userArguments, ICompiledSubQuery[] subQueries)
   at System.Data.Linq.SqlClient.SqlProvider.System.Data.Linq.Provider.IProvider.Execute(Expression query)
   at System.Data.Linq.DataQuery`1.System.Linq.IQueryProvider.Execute[S](Expression expression)
   at System.Linq.Queryable.Sum[TSource](IQueryable`1 source, Expression`1 selector)
   at HCD.Intranet.Core.Data.Linq.LinqEmployeeRepository.CalculateHolidaysRemaining(Employee employee, DateTime weekEnding, Int32 nonProjectId)
   at HCD.Intranet.Core.Services.Impl.EmployeeService.CalculateHolidaysRemaining(Employee employee, DateTime weekEnding)
   at HCD.Intranet.Core.Models.Timesheet.CalculateHolidaysRemaining()
   at HCD.Intranet.Core.Models.Json.TimesheetJsonConverter.WriteTimesheet(JsonWriter writer, Timesheet timesheet)
   at HCD.Intranet.Core.Models.Json.TimesheetJsonConverter.WriteJson(JsonWriter writer, Object value)
   at Newtonsoft.Json.JsonSerializer.SerializeValue(JsonWriter writer, Object value, JsonConverter memberConverter)
   at Newtonsoft.Json.JsonSerializer.WriteMemberInfoProperty(JsonWriter writer, Object value, JsonMemberMapping memberMapping)
   at Newtonsoft.Json.JsonSerializer.SerializeObject(JsonWriter writer, Object value)
   at Newtonsoft.Json.JsonSerializer.SerializeValue(JsonWriter writer, Object value, JsonConverter memberConverter)
   at Newtonsoft.Json.JsonSerializer.Serialize(JsonWriter jsonWriter, Object value)
   at HCD.Intranet.Core.Web.Mvc.NewtonsoftJsonResult.ExecuteResult(ControllerContext context)
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult)
   at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass11.<InvokeActionResultWithFilters>b__e()
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation)
   at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass11.<>c__DisplayClass13.<InvokeActionResultWithFilters>b__10()
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation)
   at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass11.<>c__DisplayClass13.<InvokeActionResultWithFilters>b__10()
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult)
   at System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName)
A: 

Add:

MultipleActiveResultSets=True

To the end of your connection string (assuming MSSQL 2005+)

To do this for your linq context: Open the properties tab -> Expand Connection -> Click the "..." on 'Connection String' -> 'Advanced' -> 'MultipleActiveResultSets' -> true.

I solved this one myself today and my architecture is almost identical (save for unity instead of structure map). Including the two controllers being loaded via JS!

DaRKoN_
@DaRKoN - thanks. Do you have any more explanation for why this is the solution? ie what LinqToSql is doing? I would expect a linq query to open the reader and close it. There should be no readers open on a connection. There are no synchronous data calls either, it's all within the single thread.
Tim Peel
I'm likely wrong, but due to dependency injection, the two controller actions are sharing the same datacontext. The JS requests them both virtually at the same time, so there is a chance a request is still open when the other request comes in. Id like to hear your thoughts on this too
DaRKoN_
I'd be surprised if this were the case as both controllers are being created by the object container, which will use a different DBContext for each one. I'd expect the dependencies to cascade as new instances. BUT some caching like this must be happening somewhere to result in the error I am getting
Tim Peel
Seems MultipleActiveResultsSets=True doesn't quite fix things. This problem returned today. Then I noticed I had configured my IoC container to manage my context.
DaRKoN_
A: 

One common mistake when using DI containers with ASP.NET MVC is that many DI containers default to a Singleton pattern. I don't know if that's the case here, but you should double check. ASP.NET MVC requires that the controller is created on every request because it has per-request state and context.

Haacked
Thanks Phil but I'd say the controllers are getting created per request - I am just employing the MvcContrib controller factory wired up as per their examples. The ObjectFactory.GetInstance methods from StructureMap return new instances of the requested type. I'll keep digging!
Tim Peel
Phil, thanks for making this point. I was under the impression (I'm sure I read it somewhere) that my DI container was setup to be per request by default and it turns out it's not.
DaRKoN_
@Haacked Structure Map defaults to a new one each time. Do you know if mvccontrib (or mvc) reuse instances?
eglasius
+4  A: 

I was talking to Jeremy Miller about this and you don't want to manage the lifetime of the db context with SM - let the repo instantiate as needed. This presents problems with how you will do object updating/persistence (if you're relying on a context staying alive for more than one request) but it's worth it to not rely on this for a web app.

I had to remove the db context management stuff from the storefront for a reason like this - I was getting memory leaks. I won't say it's SM's fault - but overall just let the repo open a new context.

Rob Conery
Thanks Rob, that's very interesting. The DbContext management was based on the store front code. Is there any more info to explain this problem and how you identified it? I will check this is the solution tonight and feed back.
Tim Peel
In my case, my repo's are newing up their own DBContext each time the repo is being created (eg manually in the constructor, not using 'using(){}' as per storefront code), My previous thoughts and Phils post point me to repo's that were being shared and not newly created.
DaRKoN_
@Rob - I made your fix and it looked to have sorted the problem but I saw it again last night. My repo now constructs its own DbContext at constructor level. I'm thinking of moving it to method level within a using statement.@DaRKoN - Have you progressed your problem?
Tim Peel
A: 

Are you completely sure nothing along in the object hierarchy is being Cached / or has a lifetime config? I have a prod app with a config just like this:

    ForRequestedType<SomeDataContext>().TheDefault.Is.ConstructedBy(
        () => new SomeDataContext(someConnString);

It isn't using asp.net MVC. Regarding the scoping issue, if you haven't set up anything, structure map will default to PerRequest (not asp.net request but structure map request, like each .GetInstance call) - http://structuremap.sourceforge.net/Scoping.htm. If you are positive on no configuration to affect it, then look at whether the mvc contrib or something else might be reusing instances.


Regarding the exception info posted. The error involves a combination of action results, action result filters, json serialization, custom methods, and also one linq2sql call failing because of an unexpected null. That's a combination of many different pieces, some of which I don't know. I would play safer and move the calculate stuff (that calls into linq2sql) being called when doing serialization to somewhere else where the json serialization/action results pieces aren't involved. This is just a wild guess, as I don't know how/when those pieces are called and what type of actions they have internally.

eglasius
@Freddy - thanks. the problem has returned so I am back to finding a scoping issue. The object build that mvc is using calls GetInstance each time a controller is requested - how will this work with scoping? Will I ever get the same controller instance twice? I suspect this is happening.
Tim Peel
@Tim as it has passed some time, please post the exceptions you are getting. Also consider running it using the mvc contrib source. I don't know much about the mvc contrib, I browse it a bit and I did wonder if subcontrollers might cause something like this (you didn't mentioned those thought).
eglasius
@Tim when you debug it with the source, set a breakpoint on the context constructor, and run with it to see where/when it is being instantiated.
eglasius
@Freddy - thanks again. Exceptions included in edited post. When I step through it, it never triggers the problem :( From the logging snippet, it shows different threads are involved yet there are still clashes on the dbcontext.
Tim Peel
@Tim there are many pieces involved where the error occurs, I suggest moving the bits that call into linq2sql to a place in the code where there are less things involved. That's just a wild guess, as I don't know how many of those do internally (or how they are used in their respective context).
eglasius
@Tim also, how do you tell the clashes are actually between different requests? Do you have something you can add to the log that identifies the request? so you know there is no internal weird automatic parallelization going on. Note: my suggestions are now more on isolating the problem.
eglasius