2

我一直在使用 StructureMap 将 ObjectContext(实体)与延迟加载的 POCO 类一起注入到我的存储库中。这是我的 Structuremap 注册。

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

我已经针对我的 POCO 类定义了 Partial 类,以检索未在我的 EDMX 模式中定义的信息。例如 Community.FloorPlanImages 将是一个吸气剂,它仅从所有可用图像中过滤平面图图像。在我开始优化我的查询之前工作得很好。使用 EFProf,我发现我的所有连接都没有被关闭。我无法使用“使用”语句,因为我将 ObjectContext 本身注入到我的存储库中。因此,我在 Application_EndRequest() 中添加了以下内容。

ObjectFactory.ReleaseAndDisposeAllHttpScopedObjects();

只要我不访问我的部分 POCO 上的任何内容,这也有效。我认为 StructureMap 会在我们到达 Partial 之前关闭连接。我收到以下错误。

ObjectContext 实例已被释放,不能再用于需要连接的操作。

有什么想法可以解决这个问题吗?

更新 - 这是堆栈跟踪

ObjectDisposedException:ObjectContext 实例已被释放,不能再用于需要连接的操作。] System.Data.Objects.ObjectContext.EnsureConnection() +8550458 System.Data.Objects.ObjectQuery 1.GetResults(Nullable1 forMergeOption) +46
System.Data .Objects.ObjectQuery 1.Load(List 1.Load(MergeOption mergeOption) +25 System.Data.Objects.DataClasses.RelatedEnd.Load() +37 System.Data.Objects.DataClasses.RelatedEnd.DeferredLoad() +8032198 系统。 Data.Objects.Internal.LazyLoadBehavior.LoadProperty(TItem propertyValue, String relationshipName, String targetRoleName, Boolean mustBeNull, Object wrapperObject) +85 System.Data.Objects.Internal.<> c_DisplayClass7 2 个参数) +2081.Execute(MergeOption mergeOption) +31
System.Data.Objects.DataClasses.EntityCollection
1 collection, MergeOption mergeOption) +243 System.Data.Objects.DataClasses.EntityCollection

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

System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary 1 继续) +263 System.Web.Mvc.<>c2 parameters) +27
System.Web.Mvc.<>c__DisplayClass15.<InvokeActionMethodWithFilters>b__12() +55 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func
_DisplayClass17.b_ 14() +19 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter过滤器,ActionExecutingContext preContext,Func 1 continuation) +263 System.Web.Mvc.<>c__DisplayClass17.<InvokeActionMethodWithFilters>b__14() +19 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func1 延续)+263 System.Web.Mvc.<>c _DisplayClass17.b_14 ()+19 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter 过滤器,ActionExecutingContext preContext,Func 1 continuation) +263 System.Web.Mvc.<>c__DisplayClass17.<InvokeActionMethodWithFilters>b__14() +19 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func1 延续)+263 System.Web.Mvc.<>c _DisplayClass17.b_14() +19 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext controllerContext, IList 1 filters, ActionDescriptor actionDescriptor, IDictionary2 参数) +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.WrappedAsyncResult1.End() +62 System. Web.Mvc.<> c_DisplayClasse.b_d() +50 System.Web.Mvc.SecurityUtil.b__0(动作 f) +7
System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(动作动作) +22
System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +60
System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult 结果) +9
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +8841105 System.Web.HttpApplication.ExecuteStep(IExecutionStep 步骤, 布尔值和同步完成)+184

**更新 2:我的 Application_EndRequest 被击中两次。什么可能导致这种情况发生?**

第一次命中时堆叠。

Nhs.Web.UI.DLL!Nhs.Web.UI.Global.Application_EndRequest(object sender = {ASP.global_asax}, System.EventArgs e = {System.EventArgs}) 第 58 行 C# System.Web.dll!System.Web .HttpApplication.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() + 0x95 字节
System.Web.dll!System.Web.HttpApplication.ExecuteStep(System.Web.HttpApplication.IExecutionStep step = {System.Web.HttpApplication.SyncEventExecutionStep }, ref bool completedSynchronously = true) + 0x4c 字节
System.Web.dll!System.Web.HttpApplication.ApplicationStepManager.ResumeSteps(System.Exception 错误) + 0x13e 字节
System.Web.dll!System.Web.HttpApplication.System.Web .IHttpAsyncHandler.BeginProcessRequest(System.Web.HttpContext context, System.AsyncCallback cb, object extraData) + 0xad 字节
System.Web.dll!System.Web.HttpRuntime.ProcessRequestInternal(System.Web.HttpWorkerRequest WR = {System.Web.Hosting.ISAPIWorkerRequestInProcForIIS6}) + 0x1a2 字节
System.Web.dll!System.Web.HttpRuntime.ProcessRequestNoDemand(System. Web.HttpWorkerRequest wr) + 0x7d 字节
System.Web.dll!System.Web.Hosting.ISAPIRuntime.ProcessRequest(System.IntPtr ecb, int iWRType) + 0xfd 字节
[Appdomain 转换]
[本机到托管转换]

第二次命中时堆叠

Nhs.Web.UI.DLL!Nhs.Web.UI.Global.Application_EndRequest(object sender = {ASP.global_asax}, System.EventArgs e = {System.EventArgs}) 第 58 行 C# System.Web.dll!System.Web .HttpApplication.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() + 0x95 字节
System.Web.dll!System.Web.HttpApplication.ExecuteStep(System.Web.HttpApplication.IExecutionStep step = {System.Web.HttpApplication.SyncEventExecutionStep }, ref bool completedSynchronously = true) + 0x4c 字节
System.Web.dll!System.Web.HttpApplication.ApplicationStepManager.ResumeSteps(System.Exception 错误) + 0x13e 字节
System.Web.dll!System.Web.HttpApplication.ResumeStepsWaitCallback(object错误)+ 0x1e 字节
mscorlib.dll!System.Threading.QueueUserWorkItemCallback.WaitCallback_Context(object state) + 0x2d bytes mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool ignoreSyncCtx) + 0xb0 字节
mscorlib.dll!System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem() + 0x5a 字节 mscorlib.dll!System.Threading.ThreadPoolWorkQueue.Dispatch() + 0x147 字节
mscorlib.dll!System.Threading._ThreadPoolWaitCallback .PerformWaitCallback() + 0x2d 字节
[本地到托管转换]
[Appdomain 转换]
[本地到托管转换]

4

1 回答 1

2

试试看

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

全球.asax

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

每次访问时,StructureMap 都会存储上下文

var db = ObjectFactory.GetInstance<WebEntities>();
于 2011-02-17T00:05:08.843 回答