简体   繁体   English

无法访问已处置的 object。 此错误的常见原因是处理从依赖注入中解析的上下文

[英]Cannot access a disposed object. A common cause of this error is disposing a context that was resolved from dependency injection

i have table for set user Access Level in Tabel.我有在表中设置用户访问级别的表。

this is my Access :这是我的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.我想在更改AccessLevel时必须更改Role表中的SecurityStamp

    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 :这是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.这是更改实体的State的代码,它工作正常,当我调用CommitSaveChange()时它工作正常。 but when i add the RoleUpdate commands:但是当我添加RoleUpdate命令时:

        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:然后调用unitOfWork.CommitSaveChange()它告诉我这个错误:

Cannot access a disposed object.无法访问已处置的 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.如果您在上下文上调用 Dispose() 或将上下文包装在 using 语句中,则可能会发生这种情况。 If you are using dependency injection, you should let the dependency injection container take care of disposing context instances.如果你使用依赖注入,你应该让依赖注入容器负责处理上下文实例。 Object name: 'FilmstanContext'. Object 名称:'FilmstanContext'。

whats the problem?有什么问题? how can i solve this problem????我怎么解决这个问题????

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.当我过去看到此类问题时,通常是由于 a) 缺少等待命令或 b) 具有 void 返回类型的异步方法。 In my case, it was related to Entity Framework.就我而言,它与实体框架有关。 Just in case it helps.以防万一它有帮助。 Cannot access a disposed object 无法访问已处置的 object

A related observation, I would think your call to CommitSaveChange should be asynchronous.一个相关的观察,我认为您对 CommitSaveChange 的调用应该是异步的。 This probably writes data to some type of data store.这可能会将数据写入某种类型的数据存储。 If reading 'GetRoleById' is async, I would think writing should also be async.如果阅读“GetRoleById”是异步的,我认为写作也应该是异步的。 Just my 2 cents.只是我的2美分。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误:无法访问已处理的对象。 此错误的一个常见原因是处理从依赖注入解决的上下文 - Error: Cannot access a disposed object. A common cause of this error is disposing a context that was resolved from dependency injection &#39;无法访问已处理的对象。 此错误的一个常见原因是处理从依赖注入解决的上下文 - 'Cannot access a disposed object. A common cause of this error is disposing a context that was resolved from dependency injection Asp.net 内核:无法访问已处置的 object。 此错误的常见原因是处理从依赖注入中解析的上下文 - Asp.net core: Cannot access a disposed object. A common cause of this error is disposing a context that was resolved from dependency injection .Net core/EF:无法访问已处置的 object。 此错误的常见原因是处理上下文 - .Net core/EF : cannot access a disposed object. A common cause of this error is disposing a context 无法访问已处理的对象。此错误的原因 - 与依赖注入相关的 Asp.net Core 异常 - Cannot access a disposed object.cause of this error - Asp.net Core Exception related to Dependency Injection c#XNA无法访问已处置的对象。 对象名称:“ Texture2D”。 但是我没有处理它 - c# XNA Cannot access a disposed object. Object name: 'Texture2D'. But im not disposing it 无法访问已处置的对象。 与XPO - Cannot access a disposed object. with XPO 无法访问已处置的对象。 交易 - Cannot access a disposed object. Transaction 检查IsDisposed和Dispose时“无法访问已处置的对象” - “Cannot access a disposed object” when checking for IsDisposed and Disposing 无法访问已处置的 object。 Object 名称:AspNet Core/EF Core 项目中出现“IServiceProvider”错误 - Cannot access a disposed object. Object name: 'IServiceProvider' error in AspNet Core/EF Core project
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM