在 ASP.NET MVC 应用程序中使用 OWIN,为什么我会随机获得已处置的 DbContext (InvalidOperationException)?

问题描述 投票:0回答:1

我知道这个问题对于SO来说太笼统了,如果它被关闭我不会感到惊讶。不管怎样,我尝试在这里发布我的问题,如果它不属于这里,我很抱歉。

我正在开发一个使用 OWIN 的 ASP.NET MVC 应用程序。基本上,我有:

public partial class Startup
{
    public void ConfigureAuth(IAppBuilder app)
    {
        app.CreatePerOwinContext(ApplicationDbContext.Create);
        app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
        app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);
        app.CreatePerOwinContext<ApplicationRoleManager>(ApplicationRoleManager.Create);
        app.CreatePerOwinContext<ApplicationActionManager>(ApplicationActionManager.Create);
        app.CreatePerOwinContext<ApplicationApplicationManager>(ApplicationApplicationManager.Create);
        app.CreatePerOwinContext<ApplicationMenuManager>(ApplicationMenuManager.Create);
        app.CreatePerOwinContext<ReminderManager>(ReminderManager.Create);
        ...

问题是,当我运行查询时,我遇到了随机的 DbContext Dissolved (

InvalidOperationException
) 问题。没有手动调用
Dispose
方法。我无法找到问题的根源。我努力替换
ApplicationDbContext
的每个手动实例(在我开始解决这个问题之前有多个),只使用
HttpContext.Current.GetOwinContext().Get<ApplicationDbContext>()
代替,但没有成功。

不幸的是,我无法提供其他细节,因为情况是这样的:一个似乎随机出现的错误。

一些堆栈跟踪:

System.ObjectDisposedException: The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.
   at System.Data.Entity.Core.Objects.ObjectContext.ReleaseConnection()
   at System.Data.Entity.Core.Objects.ObjectContext.<ExecuteInTransactionAsync>d__156`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.<ExecuteAsyncImplementation>d__6`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Data.Entity.Utilities.TaskExtensions.CultureAwaiter`1.GetResult()
   at System.Data.Entity.Core.Objects.ObjectQuery`1.<GetResultsAsync>d__43.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Data.Entity.Utilities.TaskExtensions.CultureAwaiter`1.GetResult()
   at System.Data.Entity.Internal.LazyAsy

System.InvalidOperationException: The operation cannot be completed because the DbContext has been disposed.
   at System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
   at System.Data.Entity.Internal.InternalContext.Initialize()
   at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)
   at System.Data.Entity.Internal.Linq.InternalSet`1.Initialize()
   at System.Data.Entity.Internal.Linq.InternalSet`1.get_InternalContext()
   at System.Data.Entity.Infrastructure.DbQuery`1.System.Linq.IQueryable.get_Provider()
   at System.Linq.Queryable.Where[TSource](IQueryable`1 source, Expression`1 predicate)
   at Test.ApplicationUserManager.GetLineMannagerByLogin(String login) in [...]
   at Test.Controllers.BaseController.OnAuthorization(AuthorizationContext authContext) in [...]
   at System.Web.Mvc.Controller.System.Web.Mvc.IAuthorizationFilter.OnAuthorization(AuthorizationContext filterContext)
   at System.Web.Mvc.ControllerActionInvoker.InvokeAuthorizationFilters(ControllerContext controllerContext, IList`1 filters, ActionDescriptor actionDescriptor)
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass21.<BeginInvokeAction>b__19(AsyncCallback asyncCallback, Object asyncState)
   at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`1.CallBeginDelegate(AsyncCallback 
c# asp.net asp.net-mvc owin dbcontext
1个回答
0
投票

我正在开发一个使用 OWIN 的 ASP.NET MVC 应用程序。

在 System.Data.Entity.Internal.LazyAsy

问题是,当我运行查询时,我遇到了随机 DbContext 已处置 (InvalidOperationException) 问题。

听起来好像相关实体的查询是延迟加载的,即它没有在初始数据库调用中加载,然后当您尝试使用导航属性(相关实体)时,DbContext 已被释放。您尚未提供函数调用的详细信息,因此不确定如何设置对后续查询的函数调用,但请尝试从原始实体加载传递 DBContext。 HTH.

© www.soinside.com 2019 - 2024. All rights reserved.