赞
踩
在应用层或者领域层的某个方法 MethodA() 对 A 表进行了增删改查操作,同时在 领域事件EventHanlderA 中绑定了A表的增删改查对应的领域事件,如果在MethodA方法和领域事件中用到了一个相同的领域服务DomainServiceA,这种场景下MethodA中的方法可以正常执行,但是领域事件中就会因为DomainServiceA已被释放而提示异常:具体原因和ABP的架构有关系,有兴趣的朋友可以去看看源代码!
Cannot access a disposed object
ObjectDisposedException: Cannot access a disposed object. A common cause of this error is disposing a context that was resolved from dependency injection and then later trying to use the same context instance elsewhere in your application. This may occur if you are calling Dispose() on the context, or wrapping the context in a using statement. If you are using dependency injection, you should let the dependency injection container take care of disposing context instances. Object name: 'NetEarnMultiTenancyDbContext'. STACK TRACE: at Microsoft.EntityFrameworkCore.DbContext.CheckDisposed() at Microsoft.EntityFrameworkCore.DbContext.get_DbContextDependencies() at Microsoft.EntityFrameworkCore.DbContext.get_Model() at Microsoft.EntityFrameworkCore.Internal.InternalDbSet`1.get_EntityType() at Microsoft.EntityFrameworkCore.Internal.InternalDbSet`1.CheckState() at Microsoft.EntityFrameworkCore.Internal.InternalDbSet`1.get_EntityQueryable() at Microsoft.EntityFrameworkCore.Internal.InternalDbSet`1.System.Linq.IQueryable.get_Provider() at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.Include[TEntity,TProperty](IQueryable`1 source, Expression`1 navigationPropertyPath) at NetEarnMultiTenancy.TenantDomain.Tenants.UTaskEventHandler.HandleEventAsync(EntityCreatedEventData`1 eventData) in E:\Projects\NetEarnMultiTenancy\aspnet-core\src\NetEarnMultiTenancy.Core\UTaskDomain\UTaskEventHandler.cs:line 43 at Abp.Events.Bus.EventBus.TriggerAsyncHandlingException(IEventHandlerFactory asyncHandlerFactory, Type eventType, IEventData eventData, List`1 exceptions) at Abp.Events.Bus.EventBus.Trigger(Type eventType, Object eventSource, IEventData eventData) at Abp.Events.Bus.EventBus.Trigger(Type eventType, IEventData eventData) at Abp.Events.Bus.Entities.EntityChangeEventHelper.<>c__DisplayClass17_0.<TriggerEventWithEntity>b__0(Object sender, EventArgs args) at Abp.Extensions.EventHandlerExtensions.InvokeSafely(EventHandler eventHandler, Object sender, EventArgs e) at Abp.Extensions.EventHandlerExtensions.InvokeSafely(EventHandler eventHandler, Object sender) at Abp.Domain.Uow.UnitOfWorkBase.OnCompleted() at Abp.Domain.Uow.UnitOfWorkBase.CompleteAsync() at Abp.AspNetCore.Mvc.Uow.AbpUowActionFilter.OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeNextActionFilterAsync>g__Awaited|10_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed context) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeInnerFilterAsync>g__Awaited|13_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextExceptionFilterAsync>g__Awaited|25_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
如:注册领域事件的类:xxxEventHanlder.cs
1、继承实现接口 IAsyncEventHandler<EntityCreatedEventData>
2、实现的接口方法添加工作单元属性 [UnitOfWork]
3、实现的接口方法添加 virtual 修饰符
public class UTaskEventHandler : NetEarnMultiTenancyDomainServiceBase, ITransientDependency, IAsyncEventHandler<EntityCreatedEventData<UTask>> { private readonly UTaskDomainService _uTaskDomainService; public UTaskEventHandler(UTaskDomainService uTaskDomainService) { _uTaskDomainService = uTaskDomainService; } [UnitOfWork] public virtual async Task HandleEventAsync(EntityCreatedEventData<UTask> eventData) { //内部使用_uTaskDomainService 领域服务 } }
对ABP底层内容了解不多,有的东西也没办法讲详细;
有朋自远方来 虽远必诛,欢迎加微交流: 15249207822
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。