Open Closed

AbstractValidator & IRepository problem after 4.3.0 upgrade #1305


User avatar
0
yilmaz.atalar created
  • ABP Framework version: v4.3.0
  • UI type: Angular
  • DB provider: EF Core
  • Tiered (MVC) or Identity Server Separated (Angular): yes
  • Exception message and stack trace:
  • Steps to reproduce the issue:

Hi,

I've a validator class which inherits FluentValidaton's abstract class described as AbstractValidator<T>. I inject IRepository<MyEntity> within the constructor. Then I use this repository in a private method of my validator. Before 4.3.0 upgrade this code works fine. But after the upgrade it throws an exception that says "Cannot access a disposed context instance...". And also if I inject IUnitOfWorkManager and start a new unit of work by using scope, my code works fine again. What is the problem that lies behind, any ideas?

    public class MyDtoValidator : AbstractValidator<MyDto>
    {
        private readonly IRepository<MyEntity, long> _repository;
        private readonly IUnitOfWorkManager _unitOfWorkManager;

        public MyDtoValidator(IRepository<MyEntity, long> repository,            
            IUnitOfWorkManager unitOfWorkManager)
        {
            _repository = repository;
            _unitOfWorkManager = unitOfWorkManager;

            RuleFor(cf => cf.Property).NotEmpty().MustAsync((dto, key, cancellationToken) => MyAsyncMethod(dto)).WithMessage((dto, field) =>
            {
                return "message";
            }); ;

        }

        private async Task<bool> MyAsyncMethod(MyDto dto)
        {
            using (var uow = _unitOfWorkManager.Begin(true)) // without this line, it crashes.
            {
                var query = await _repository.GetQueryableAsync();

                query = query.Where(x => x.Property == dto.Property);

                var existingDto = await _repository.AsyncExecuter.FirstOrDefaultAsync(query);

                return !(existingDto != null && dto.Id != existingDto.Id);
            }
        }
    }

Thanks for your attention.


2 Answer(s)
  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi ]Can you share the error stack?

    using (var uow = _unitOfWorkManager.Begin(true))

    This is recommend way, because you can't assume there is uow.

    var query = await _repository.GetQueryableAsync();

    The method will start a new uow and then the dbcontext will be disposed.

  • User Avatar
    0
    ServiceBot created
    Support Team Automatic process manager

    This question has been automatically marked as stale because it has not had recent activity.

Made with ❤️ on ABP v8.2.0-preview Updated on March 25, 2024, 15:11