我已经谷歌搜索了一段时间才发现我的问题。我已经在stackoverlow中读了一个关于dbcontext和autofac的话题,但是我会意识到我的问题是不一样的。
我有一个带有EF6 codeFirst的 Web API ,其中autofac为IOC。 我的问题是在记录器中我有一些这种类型的例外:
“由于DbContext已被处理,因此无法完成操作。”
大多数情况下使用unitofWork
来确定自定义ActionFilterAttribute我有单位工作的不同实现,因为我有不同的数据库。
.....
// register dependencies
builder.Register<DotNetCache>(u => new DotNetCache())
.As<ICache>()
.InstancePerRequest();
builder.Register<UnitOfWork>(u => new UnitOfWork())
.As<IUnitOfWork>()
.InstancePerRequest();
builder.Register<AdoUnitOfWork>(u => new AdoUnitOfWork())
.InstancePerRequest();
builder.Register<PrefAdoUnitOfWork>(u => new PrefAdoUnitOfWork())
.InstancePerRequest();
......
任何想法都可能发生吗?
由于
UPDATE:在ActionFilterAttribute内部进行OnActionExecuting和OnActionExecuted
堆栈跟踪:
System.Web.Http.Controllers.ExceptionFilterResult+<ExecuteAsync>d__0 in MoveNext
:Void MoveNext()
System.Runtime.CompilerServices.TaskAwaiter`1 in GetResult : TResult GetResult()
System.Runtime.CompilerServices.TaskAwaiter in HandleNonSuccessAndDebuggerNotification : Void HandleNonSuccessAndDebuggerNotification(System.Threading.Tasks.Task)
System.Runtime.CompilerServices.TaskAwaiter in ThrowForNonSuccess at : Void ThrowForNonSuccess(System.Threading.Tasks.Task)
System.Web.Http.Controllers.ActionFilterResult+<ExecuteAsync>d__2 in MoveNext at : Void MoveNext()
System.Runtime.CompilerServices.TaskAwaiter`1 in GetResult at line : TResult GetResult()
System.Runtime.CompilerServices.TaskAwaiter in HandleNonSuccessAndDebuggerNotification : Void HandleNonSuccessAndDebuggerNotification(System.Threading.Tasks.Task)
System.Runtime.CompilerServices.TaskAwaiter in ThrowForNonSuccess : Void ThrowForNonSuccess(System.Threading.Tasks.Task)
System.Web.Http.Filters.ActionFilterAttribute+<ExecuteActionFilterAsyncCore>d__0 in MoveNext : Void MoveNext()
System.Runtime.CompilerServices.TaskAwaiter in HandleNonSuccessAndDebuggerNotification :Void HandleNonSuccessAndDebuggerNotification(System.Threading.Tasks.Task)
System.Runtime.CompilerServices.TaskAwaiter in ThrowForNonSuccess : Void ThrowForNonSuccess(System.Threading.Tasks.Task)
System.Web.Http.Filters.ActionFilterAttribute in OnActionExecutingAsync : System.Threading.Tasks.Task OnActionExecutingAsync(System.Web.Http.Controllers.HttpActionContext, System.Threading.CancellationToken)
LanguageAttribute in OnActionExecuting : Void OnActionExecuting(System.Web.Http.Controllers.HttpActionContext)
System.Data.Entity.QueryableExtensions in Query : System.Linq.IQueryable`1[T] Query[T](System.Linq.IQueryable`1[T])
System.Data.Entity.Infrastructure.DbQuery`1 in Query :System.Data.Entity.Infrastructure.DbQuery`1[TResult] Query ()
System.Data.Entity.Internal.Linq.InternalSet`1 in Query System.Data.Entity.Internal.Linq.IInternalQuery`1[TEntity] Query ()
System.Data.Entity.Internal.Linq.InternalSet`1 in CreateObjectQuery :System.Data.Entity.Core.Objects.ObjectQuery`1[TEntity] CreateObjectQuery(Boolean, System.Nullable`1[System.Boolean], System.Data.Entity.Infrastructure.IDbExecutionStrategy)
System.Data.Entity.Internal.LazyInternalContext in get_ObjectContext :System.Data.Entity.Core.Objects.ObjectContext get_ObjectContext()
System.Data.Entity.Internal.LazyInternalContext in InitializeContext : Void InitializeContext()