当前位置:   article > 正文

【ABP】领域事件异常(Cannot access a disposed object)无法访问已释放对象_objectdisposedexception: cannot access a disposed

objectdisposedexception: cannot access a disposed object.

异常场景:

在应用层或者领域层的某个方法 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)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

解决方案:

如:注册领域事件的类: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  领域服务
        }
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

小结:

对ABP底层内容了解不多,有的东西也没办法讲详细;
有朋自远方来 虽远必诛,欢迎加微交流: 15249207822

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/113175?site
推荐阅读
相关标签
  

闽ICP备14008679号