Liberation of ObjectContext when using StructureMap

I used StructureMap to insert ObjectContext objects into my repositories along with lazily loaded POCO classes. This is my Structuremap registration.

For<WebEntities>().LifecycleIs(new HybridLifecycle()).Use(()=>new WebEntities()); 

I defined Partial classes against my POCO classes to get information that is not defined in my EDMX schema. for example, Community.FloorPlanImages will be a recipient that filters only floor plans from all available images. It worked very well until I started optimizing my queries. Using EFProf, I found out that none of my connections are closing. I could not use the 'using' statement since I am inserting an ObjectContext into my repository. So, I added the following to my Application_EndRequest ().

 ObjectFactory.ReleaseAndDisposeAllHttpScopedObjects(); 

This works too long until I get access to any of my Partial POCOs. I think StructureMap closes the connection before we move on to Partial. I got the following error.

The ObjectContext instance has been deleted and can no longer be used for operations that require a connection.

Any ideas to get around this?

UPDATE - here's the stack trace

ObjectDisposedException: An instance of ObjectContext and can no longer be used for operations that require a connection.] System.Data.Objects.ObjectContext.EnsureConnection () +8550458 System.Data.Objects.ObjectQuery 1.GetResults(Nullable 1 forMergeOption) +46
System.Data.Objects.ObjectQuery 1.Execute(MergeOption mergeOption) +31
System.Data.Objects.DataClasses.EntityCollection
1.Execute(MergeOption mergeOption) +31
System.Data.Objects.DataClasses.EntityCollection
1.Execute(MergeOption mergeOption) +31
System.Data.Objects.DataClasses.EntityCollection
1.Load (List 1 collection, MergeOption mergeOption) +243 System.Data.Objects.DataClasses.EntityCollection 1.Load (MergeOption mergeOption) +25
System.Data.Objects.DataClasses.RelatedEnd.Load () +37. System.Data.Objects.DataClasses.RelatedEnd.DeferredLoad () +8032198 System.Data.Objects.Internal.LazyLoadBehavior.LoadProperty (TItem propertyValue, String RelationName, String targetRoleName, Boolean mustBeNull, Object wra85Object)
System.Data.Objects.Internal <. > C_DisplayClass7 2.<GetInterceptorDelegate>b__1(TProxy proxy, TItem item) +101
System.Data.Entity.DynamicProxies.Community_39641A615E1AD4E19D637735C7A1EBEE61BF70BF579CDD4EBB0267E6636BEC62.get_Videos() +55 Rdx.Web.UI.AppCode.Controllers.CDController.GetCDModel(SearchParams searchParams, Int32 page, Boolean isSorting) in D:\Solutions\RDX\Rdx.Mvc\src\app\Rdx.Web.UI\AppCode\Controllers\CDController.cs:365 Rdx.Web.UI.AppCode.Controllers.CDController.Show(Int32 cid, Int32 bid) in D:\Solutions\RDX\Rdx.Mvc\src\app\Rdx.Web.UI\AppCode\Controllers\CDController.cs:70 lambda_method(Closure , ControllerBase , Object[] ) +145
System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +17
System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary
2.<GetInterceptorDelegate>b__1(TProxy proxy, TItem item) +101
System.Data.Entity.DynamicProxies.Community_39641A615E1AD4E19D637735C7A1EBEE61BF70BF579CDD4EBB0267E6636BEC62.get_Videos() +55 Rdx.Web.UI.AppCode.Controllers.CDController.GetCDModel(SearchParams searchParams, Int32 page, Boolean isSorting) in D:\Solutions\RDX\Rdx.Mvc\src\app\Rdx.Web.UI\AppCode\Controllers\CDController.cs:365 Rdx.Web.UI.AppCode.Controllers.CDController.Show(Int32 cid, Int32 bid) in D:\Solutions\RDX\Rdx.Mvc\src\app\Rdx.Web.UI\AppCode\Controllers\CDController.cs:70 lambda_method(Closure , ControllerBase , Object[] ) +145
System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +17
System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary
2.<GetInterceptorDelegate>b__1(TProxy proxy, TItem item) +101
System.Data.Entity.DynamicProxies.Community_39641A615E1AD4E19D637735C7A1EBEE61BF70BF579CDD4EBB0267E6636BEC62.get_Videos() +55 Rdx.Web.UI.AppCode.Controllers.CDController.GetCDModel(SearchParams searchParams, Int32 page, Boolean isSorting) in D:\Solutions\RDX\Rdx.Mvc\src\app\Rdx.Web.UI\AppCode\Controllers\CDController.cs:365 Rdx.Web.UI.AppCode.Controllers.CDController.Show(Int32 cid, Int32 bid) in D:\Solutions\RDX\Rdx.Mvc\src\app\Rdx.Web.UI\AppCode\Controllers\CDController.cs:70 lambda_method(Closure , ControllerBase , Object[] ) +145
System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +17
System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary
2 parameters) +208
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod (ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary 2 parameters) +27
System.Web.Mvc.<>c__DisplayClass15.<InvokeActionMethodWithFilters>b__12() +55 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func
2 parameters) +27
System.Web.Mvc.<>c__DisplayClass15.<InvokeActionMethodWithFilters>b__12() +55 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func
2 parameters) +27
System.Web.Mvc.<>c__DisplayClass15.<InvokeActionMethodWithFilters>b__12() +55 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func
. C_DisplayClass17.b_14 () +19 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter (IActionFilter filter, ActionExecutingContext preContext, Func 1 continuation) +263 System.Web.Mvc.<>c__DisplayClass17.<InvokeActionMethodWithFilters>b__14() +19 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func 1 continued) +263 System.Web.Mvc <. C_DisplayClass17.b_14 () +19 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter (IActionFilter filter, ActionExecutingContext preContext, Func 1 continuation) +263 System.Web.Mvc.<>c__DisplayClass17.<InvokeActionMethodWithFilters>b__14() +19 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func 1 continued) +263 System.Web.Mvc <. > C_DisplayClass17.b_14 () +19 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters (ControllerContext controllerContext, IList 1 filters, ActionDescriptor actionDescriptor, IDictionary 2 parameters) +191
System.Web.Mvc.ControllerActionInvoker.InvokeAction (ControllerContext controllerContext, String actionName) +343 System.Web.Mvc.Controller.ExecuteCore () +116 System.Web.Mvc.ControllerBase.Execute (RequestContext requestContext) +97
System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute (RequestContext requestContext) +10
System.Web.Mvc <. > C_DisplayClassb.b_5 () +37 System.Web.Mvc.Async. <> c_DisplayClass1.b_0 () +21 System.Web.Mvc.Async. <> c_DisplayClass8 1.<BeginSynchronous>b__7(IAsyncResult _) +12 System.Web.Mvc.Async.WrappedAsyncResult 1.End () +62 System.Web.Mvc. <> c_DisplayClasse.b_d () +50 System.Web.Mvc.SecurityUtil.b__0 (Action f) +7 System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust (Action action) +22
System.Web.Mvc.MvcHandler.EndProcessRequest (IAsyncResult asyncResult) +60
System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest (IAsyncResult result) +9
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute () +8841105 System.Web.HttpApplication.ExecuteStep (IExecutionStep step, Boolean & completedSynchronously) +184

** UPDATE 2: My Application_EndRequest gets hit twice. What can happen? **

STACK on first hit.

Nhs.Web.UI.DLL! Nhs.Web.UI.Global.Application_EndRequest (object sender = {ASP.global_asax}, System.EventArgs e = {System.EventArgs}) Line 58 C # System.Web.dll! System.Web.HttpApplication.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute () + 0x95 bytes
System.Web.dll! System.Web.HttpApplication.ExecuteStep (System.Web.HttpApplication.IExecutionStep step = {System.Web.HttpApplication.SyncEventExecutionStep}, ref bool completedSynchronously = true) + 0x4c bytes
System.Web.dll! System.Web.HttpApplication.ApplicationStepManager.ResumeSteps (System.Exception error) + 0x13e bytes
System.Web.dll! System.Web.HttpApplication.System.Web.IHttpAsyncHandler.BeginProcessRequest (System.Web.HttpContext context, System.AsyncCallback cb, object extraData) + 0xad bytes
System.Web.dll! System.Web.HttpRuntime.ProcessRequestInternal (System.Web.HttpWorkerRequest wr = {System.Web.Hosting.ISAPIWorkerRequestInProcForIIS6}) + 0x1a2 bytes
System.Web.dll! System.Web.HttpRuntime.ProcessRequestNoDemand (System.Web.HttpWorkerRequest wr) + 0x7d bytes
System.Web.dll! System.Web.Hosting.ISAPIRuntime.ProcessRequest (System.IntPtr ecb, int iWRType) + 0xfd bytes
[Going to Appdomain]
[Sent to a managed transition]

STACK on the second hit

Nhs.Web.UI.DLL! Nhs.Web.UI.Global.Application_EndRequest (object sender = {ASP.global_asax}, System.EventArgs e = {System.EventArgs}) Line 58 C # System.Web.dll! System.Web.HttpApplication.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute () + 0x95 bytes
System.Web.dll! System.Web.HttpApplication.ExecuteStep (System.Web.HttpApplication.IExecutionStep step = {System.Web.HttpApplication.SyncEventExecutionStep}, ref bool completedSynchronously = true) + 0x4c bytes
System.Web.dll! System.Web.HttpApplication.ApplicationStepManager.ResumeSteps (System.Exception error) + 0x13e bytes
System.Web.dll! System.Web.HttpApplication.ResumeStepsWaitCallback (object error) + 0x1e bytes
mscorlib.dll! System.Threading.QueueUserWorkItemCallback.WaitCallback_Context (object state) + 0x2d bytes mscorlib.dll! System.Threading.ExecutionContext.Run (System.Threading.ExecutionContext executeContext, System.Threading.ContextCallback callback, object state, bool ignoreSyncCtx) + 0xb0 bytes
mscorlib.dll! System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem () + 0x5a bytes mscorlib.dll! System.Threading.ThreadPoolWorkQueue.Dispatch () + 0x147 bytes
mscorlib.dll! System.Threading._ThreadPoolWaitCallback.PerformWaitCallback () + 0x2d bytes
Transition to a managed transition

[Going to Appdomain]
[Sent to a managed transition]

+2
dependency-injection structuremap entity-framework-4 objectcontext
Feb 16 '11 at 15:51
source share
1 answer

Try

 For(typeof(WebEntities)).LifecycleIs(InstanceScope.Hybrid); 

Global.asax

 protected void Application_EndRequest(object sender, EventArgs e) { ObjectFactory.ReleaseAndDisposeAllHttpScopedObjects(); } 

StructureMap will keep the context every time you access

 var db = ObjectFactory.GetInstance<WebEntities>(); 
+2
Feb 17 2018-11-11T00:
source share



All Articles