Using StructureMap 3.1.5.0 - Scoping Issue with Linq to SQL data context objects

157 views
Skip to first unread message

Santosh Patro

unread,
May 26, 2015, 6:55:54 AM5/26/15
to structure...@googlegroups.com, jeremy...@yahoo.com, praveen kamineni

I am using StructureMap.WebApi2 nuget package for Web API 2 project for managing the depedency injection. In the DefaultRegistry.cs class I have mentioned all the dependencies required.  This uses Nested container approach only which is recommended.

For<IScoreRepository>().Use<ScoreRepository>();
 For<IExternalUsersAuditRepository>().Use<ExternalUsersAuditRepository>();
 For<ITestScanService>().Use<TestScanService>();


 // hook up specific questionnaire datacontext
 For<SportsDataContext>()
   .Use(c => new SportsDataContext(
     ConfigurationManager.ConnectionStrings["Database"].ConnectionString));

// hook up datacontext for data and services
For<QuestionDataContext>()
  .Use(c => new QuestionDataContext(
     ConfigurationManager.ConnectionStrings["Database"].ConnectionString));

Linq to Sql Datacontext objects are getting disposed and I get the below error:

Cannot access a disposed object.\r\nObject name: 'DataContext accessed after Dispose'

If I use Singleton or ThreadLocalStorageLifecycle scope for the Datacontext objects it's working fine. But I want to scope the DataContext objects to Per Http Request.

Can you please suggest? Our project got stuck due to this showstopper.

Thanks in advance.

Jeremy Miller

unread,
May 31, 2015, 10:34:13 AM5/31/15
to structure...@googlegroups.com, santosh.i...@gmail.com, pra...@gmail.com, jeremy...@yahoo.com
Santosh,

As I was trying to ask you in the private email, is some other service you're resolving trying to access a DbContext *after* the nested container is disposed? That would be my best guess as to the cause.

- Jeremy
Reply all
Reply to author
Forward
0 new messages