-1

i have table for set user Access Level in Tabel .

this is my Access :

    public Guid RoleId { get; set ; }
    public string Access { get ; set ; }

i want when the AccessLevel is changed it must changed the SecurityStamp in Role table .

    public async Task<OperationResult<string>> Handle(SetAccessLevelCommand request, CancellationToken cancellationToken)
    {
        var result = await unitOfWork.RoleRepository.AccessLevelRepository.SetAccess(new AccessLevelDto { RoleId = request.RoleId, Access = request.AccessList });
        if (result.Success)
        {
            try
            {
                try
                {
                    var findRole = await unitOfWork.RoleRepository.GetRoleByIdAsync(request.RoleId, cancellationToken);
                    findRole.Result.UpdateSecurityStamp();
                    if (findRole.Result != null)
                    {
                        unitOfWork.RoleRepository.Update(findRole.Result, cancellationToken);
                        unitOfWork.CommitSaveChange();
                        return OperationResult<string>.BuildSuccessResult("Add Success");
                    }
                }
                catch (Exception ex)
                {
                    return OperationResult<string>.BuildFailure(ex.Message);
                }
            }
            catch (Exception ex)
            {
                return OperationResult<string>.BuildFailure(ex.Message);
            }
        }
        return OperationResult<string>.BuildFailure(result.ErrorMessage);
    }

i write this code for doing this work .

this is the SetAccess :

  public async Task<OperationResult<string>> SetAccess(AccessLevelDto accessLevels)
    {
        try
        {
            var currentRoleAccessValue = GetAccessLevels(accessLevels.RoleId);
            var currentAccess = currentRoleAccessValue.Select(x => x.Access).ToList();
            var newAccess = accessLevels.Access.Except(currentAccess).ToList();

            if (newAccess != null)
            {
                foreach (var item in newAccess)
                {
                    context.Add(new AccessLevel
                    {
                        Access = item,
                        RoleId = accessLevels.RoleId
                    });
                }
            }

            var removeItems = currentAccess.Except(accessLevels.Access).ToList();
            if (removeItems != null)
            {
                foreach (var item in removeItems)
                {
                    var accClaim = currentRoleAccessValue.SingleOrDefault(x => x.Access == item);
                    if (accClaim != null)
                    {
                        context.Remove(accClaim);
                    }
                }
            }

            return OperationResult<string>.BuildSuccessResult("SuccessAdd");
        }
        catch (Exception ex)
        {
            return OperationResult<string>.BuildFailure(ex);
        }
    }

this is code for change State of entity and it's worked fine and when i call the CommitSaveChange() it worked fine . but when i add the RoleUpdate commands :

        var findRole = await unitOfWork.RoleRepository.GetRoleByIdAsync(request.RoleId, cancellationToken);
                    findRole.Result.UpdateSecurityStamp();
                    if (findRole.Result != null)
                    {
                        unitOfWork.RoleRepository.Update(findRole.Result, cancellationToken);
                    }

and then call the unitOfWork.CommitSaveChange() it show me this error :

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: 'FilmstanContext'.

whats the problem ? how can i solve this problem ????

mr coder
  • 199
  • 2
  • 14
  • Why do you have multiple try/catch blocks immediately nested in each other like that? Your code could benefit from some [rubber duck debugging](https://en.wikipedia.org/wiki/Rubber_duck_debugging). – mason Apr 18 '20 at 15:32

1 Answers1

0

When I have seen this type of issue in the past, it has usually been due to either a) missing await commands or b) async methods with a void return type. In my case, it was related to Entity Framework. Just in case it helps. Cannot access a disposed object

A related observation, I would think your call to CommitSaveChange should be asynchronous. This probably writes data to some type of data store. If reading 'GetRoleById' is async, I would think writing should also be async. Just my 2 cents.