Aspnetboilerplate: Single tenant – single database -- Multiple DbContext Issue

Created on 11 May 2016  Â·  3Comments  Â·  Source: aspnetboilerplate/aspnetboilerplate

First, Thank you for your awesome work.
I've upgrade my solution to ABP v0.9 and I started to face an issue with the DbContext injection

Background
My Solution is Single tenant – single database -- Multiple DbContext. I've multiple modules and every module has its own DbContext and all of the modules inheriting the base module DbContext
I've also a Base Repository and all the module Base Repositories inherits it and inject IDbContextProvider to it.

I'm using AutoRepositoryTypesAttribute to Register the Repositories.

Problem

After Upgrading to v 0.9 I started to get the below issue. If I enable MultiTenancy the problem disappear. If MultiTenancyis disabled the problem exist.

ERROR 2016-05-11 13:45:19,697 [33   ] lers.Filters.AbpExceptionFilterAttribute - Unable to cast object of type 'AdventureWorks.EntityFramework.AdventureWorksDbContext' to type 'AdventureWorks.EntityFramework.SalesDbContext'.
System.InvalidCastException: Unable to cast object of type 'AdventureWorks.EntityFramework.AdventureWorksDbContext' to type 'AdventureWorks.EntityFramework.SalesDbContext'.
   at Abp.EntityFramework.Uow.EfUnitOfWork.GetOrCreateDbContext[TDbContext](Nullable`1 multiTenancySide)
   at Castle.Proxies.SalesRepositoryBase`1Proxy.get_Context_callback()
   at Castle.Proxies.Invocations.EfRepositoryBase`3_get_Context.InvokeMethodOnTarget()
   at Castle.DynamicProxy.AbstractInvocation.Proceed()
   at Abp.Domain.Uow.UnitOfWorkInterceptor.PerformSyncUow(IInvocation invocation, UnitOfWorkOptions options)
   at Castle.DynamicProxy.AbstractInvocation.Proceed()
   at Castle.Proxies.SalesRepositoryBase`1Proxy.get_Context()
   at Abp.EntityFramework.Repositories.EfRepositoryBase`3.get_Table()
   at Castle.Proxies.Invocations.EfRepositoryBase`3_get_Table.InvokeMethodOnTarget()
   at Castle.DynamicProxy.AbstractInvocation.Proceed()
   at Abp.Domain.Uow.UnitOfWorkInterceptor.PerformSyncUow(IInvocation invocation, UnitOfWorkOptions options)
   at Castle.DynamicProxy.AbstractInvocation.Proceed()
   at Castle.Proxies.SalesRepositoryBase`1Proxy.get_Table()
   at Castle.Proxies.SalesRepositoryBase`1Proxy.GetAll_callback()
   at Castle.Proxies.Invocations.EfRepositoryBase`3_GetAll.InvokeMethodOnTarget()
   at Castle.DynamicProxy.AbstractInvocation.Proceed()
   at Abp.Domain.Uow.UnitOfWorkInterceptor.PerformSyncUow(IInvocation invocation, UnitOfWorkOptions options)
   at Castle.DynamicProxy.AbstractInvocation.Proceed()
   at Castle.Proxies.SalesRepositoryBase`1Proxy.GetAll()
   at AdventureWorks.Sales.SalesAppService.GetAll() in C:\Users\iyhammad\Downloads\AdventureWorks\AdventureWorks.Application\Sales\SalesAppService.cs:line 22
   at Castle.Proxies.Invocations.SalesAppService_GetAll.InvokeMethodOnTarget()
   at Castle.DynamicProxy.AbstractInvocation.Proceed()
   at Castle.DynamicProxy.AbstractInvocation.Proceed()
   at Abp.Domain.Uow.UnitOfWorkInterceptor.PerformSyncUow(IInvocation invocation, UnitOfWorkOptions options)
   at Castle.DynamicProxy.AbstractInvocation.Proceed()
   at Abp.Auditing.AuditingInterceptor.PerformSyncAuditing(IInvocation invocation, AuditInfo auditInfo)
   at Castle.DynamicProxy.AbstractInvocation.Proceed()
   at Castle.DynamicProxy.AbstractInvocation.Proceed()
   at Castle.Proxies.SalesAppServiceProxy.GetAll()
--- End of stack trace from previous location where exception was thrown ---
   at Abp.Extensions.ExceptionExtensions.ReThrow(Exception exception)
   at Abp.WebApi.Controllers.Dynamic.Interceptors.AbpDynamicApiControllerInterceptor`1.Intercept(IInvocation invocation)
   at Castle.DynamicProxy.AbstractInvocation.Proceed()
   at Castle.Proxies.DynamicApiController`1Proxy.GetAll()
   at lambda_method(Closure , Object , Object[] )
   at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass10.<GetExecutor>b__9(Object instance, Object[] methodParameters)
   at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary`2 arguments, CancellationToken cancellationToken)
--- 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.Web.Http.Controllers.ApiControllerActionInvoker.<InvokeActionAsyncCore>d__0.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.Web.Http.Controllers.ActionFilterResult.<ExecuteAsync>d__2.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.Web.Http.Controllers.AuthenticationFilterResult.<ExecuteAsync>d__0.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.Web.Http.Controllers.ExceptionFilterResult.<ExecuteAsync>d__0.MoveNext()

When I inject ISalesRepository (Entity one of the Sales Modules Entities) It inject the base Module DbContext while he should inject the SaleDbContext.

I've Created a new Project from the Template and I was able to regenerate the issue. I'm attaching here a Project that shows the issue.
If you Call the Sales Application Service It will fail unless you Enable the MultiTenancy.

AdventureWorks.zip

bug

All 3 comments

Hi,

Thank you for your problem report. I will try and fix it in a short time (I hope :)

Released Abp 0.9.0.1, please upgrade your packages and try again.

Thank You, I tried and it fixed the issue for me.

Was this page helpful?
0 / 5 - 0 ratings