首页 > 解决方案 > 无法在 .NET Core for Async 方法中访问 DbContext 的已处置对象

问题描述

我在我的一个微服务 Web api 中遇到了一个奇怪的问题。我的异步 GET 方法抛出无法访问我的 DbContext 的已处置对象异常,除非是第一次调用它们。我尝试在网上寻找答案,但没有任何效果。我确保我的方法不是异步无效的,我等待必要的调用。由于我的 POST 和 DELETE 方法工作正常,我相当肯定真正的罪魁祸首是 IMapper 实例。我认为它可能总是指向 DbContext 的第一个实例,这就是为什么第一次工作而不是之后的原因。任何帮助或指示将不胜感激

这是代码的一些快照。

启动.cs

...
// Add AutoMapper
        services.AddAutoMapper(new Assembly[] { typeof(AutoMapperProfile).GetTypeInfo().Assembly });

// Add DbContext using NoSQL Server Provider
services.AddDbContext<ProfileDbContext>(options =>
            options.UseMongoDb(Configuration.GetConnectionString("TeamJobProfilesDatabase")));
...

我的控制器.cs

    // GET api/profiles
    [HttpGet]
    [ProducesResponseType(StatusCodes.Status200OK)]
    public async Task<ActionResult<ProfilesListViewModel>> GetAll()
    {
        return Ok(await Mediator.Send(new GetAllProfilesQuery()));
    }

    // GET api/profiles/{id}
    [HttpGet("{id}")]
    [ProducesResponseType(StatusCodes.Status200OK)]
    [ProducesResponseType(StatusCodes.Status404NotFound)]
    public async Task<ActionResult<ProfileViewModel>> Get(int id)
    {
        return Ok(await Mediator.Send(new GetProfileQuery { Id = id }));
    }

GetAllProfilesQueryHandler.cs

public class GetAllProfilesQueryHandler : IRequestHandler<GetAllProfilesQuery, ProfilesListViewModel>
{
    private readonly ProfileDbContext _context;
    private readonly IMapper _mapper;

    public GetAllProfilesQueryHandler(ProfileDbContext context, IMapper mapper)
    {
        _context = context;
        _mapper = mapper;
    }

    public async Task<ProfilesListViewModel> Handle(GetAllProfilesQuery request, CancellationToken cancellationToken)
    {
        return new ProfilesListViewModel
        {
            Profiles = await _context.Profiles.ProjectTo<ProfileLookupModel>(_mapper.ConfigurationProvider).ToListAsync(cancellationToken)
        };
    }
}

ProfileDbContext.cs

[MongoDatabase("profileDb")]
public class ProfileDbContext : DbContext
{
    public ProfileDbContext(DbContextOptions<ProfileDbContext> options)
        : base(options)
    {
    }

    public DbSet<Domain.Entities.Profile> Profiles { get; set; }
}

异常消息

{ "error": [ "Cannot access a disposed object. 这个错误的一个常见原因是释放一个从依赖注入中解析的上下文,然后尝试在应用程序的其他地方使用相同的上下文实例。如果您是在上下文上调用 Dispose(),或将上下文包装在 using 语句中。如果使用依赖注入,则应让依赖注入容器负责处理上下文实例。\r\n对象名称:'ProfileDbContext'。" ], "stackTrace": " 在 Microsoft.EntityFrameworkCore.DbContext.CheckDisposed()\r\n 在 Microsoft.EntityFrameworkCore.DbContext.get_InternalServiceProvider()\r\n 在 Microsoft.EntityFrameworkCore.DbContext.get_ChangeTracker()\r\n 在Microsoft.EntityFrameworkCore.Query.Internal。1.<CompileAsyncQuery>b__0(QueryContext queryContext)\r\n at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.ExecuteAsync[TResult](Expression query)\r\n at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.ExecuteAsync[TResult](Expression expression)\r\n at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable1.System.Collections.Generic.IAsyncEnumerable.GetEnumerator()\r\n 在 System.Linq.AsyncEnumerable.Aggregate_[TSource,TAccumulate,TResult](IAsyncEnumerable 1 source, TAccumulate seed, Func3 累加器, Func 2 resultSelector, CancellationToken cancellationToken) in D:\\a\\1\\s\\Ix.NET\\Source\\System.Interactive.Async\\Aggregate.cs:line 118\r\n at Profile.Application.Profiles.Queries.GetAllProfiles.GetAllProfilesQueryHandler.Handle(GetAllProfilesQuery request, CancellationToken cancellationToken) in C:\\Users\\Adam\\Repositories\\TeamJob\\TeamJob\\src\\Services\\Profile\\Profile.Application\\Profiles\\Queries\\GetAllProfiles\\GetAllProfilesQueryHandler.cs:line 24\r\n at MediatR.Pipeline.RequestPostProcessorBehavior2.Handle(TRequest request, CancellationToken cancelToken, RequestHandlerDelegate 1 next)\r\n at MediatR.Pipeline.RequestPreProcessorBehavior2 .Handle(TRequest request, CancellationToken cancelToken, RequestHandlerDelegate 1 next)\r\n at MediatR.Pipeline.RequestPreProcessorBehavior2.Handle(TRequest request, CancellationToken cancelToken, RequestHandlerDelegate1 next)\r\n at Profile.API.Controllers.ProfilesController.GetAll() in C:\\Users\\Adam\\Repositories\\TeamJob\\TeamJob\\src\\Services\\Profile\\Profile.API\\Controllers\\ProfilesController.cs:line 19\r\n at lambda_method(Closure , Object )\r\n at Microsoft.Extensions.Internal.ObjectMethodExecutorAwaitable.Awaiter.GetResult()\r\n at Microsoft.AspNetCore.Mvc.Internal.ActionMethodExecutor.AwaitableObjectResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)\r\n at System.Threading.Tasks.ValueTask1.get_Result()\r\n 在 Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeActionMethodAsync()\r\n 在 Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeNextActionFilterAsync()\r\n 在 Microsoft.AspNetCore。 Mvc.Internal.ControllerActionInvoker.Rethrow(ActionExecutedContext 上下文)\r\n 在 Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)\r\n 在 Microsoft.AspNetCore.Mvc .Internal.ControllerActionInvoker.InvokeInnerFilterAsync()\r\n 在 Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextExceptionFilterAsync()" }

标签: c#.net-coreasync-awaitentity-framework-coreasp.net-core-webapi

解决方案


问题出在Mediator.Send方法中。Mediator类将请求处理程序存储在静态ConcurrentDictionary中

private static readonly ConcurrentDictionary<Type, object> _requestHandlers = new ConcurrentDictionary<Type, object>();

并且当Send调用该方法时,它使用GetOrAdd该字典上的方法。

var handler = (RequestHandlerWrapper<TResponse>)_requestHandlers.GetOrAdd(requestType, t => Activator.CreateInstance(typeof(RequestHandlerWrapperImpl<,>).MakeGenericType(requestType, typeof(TResponse))));

这意味着,如果字典中不存在请求处理程序实例,它会创建一个新实例(使用Activator)并将其添加到字典中,但如果字典中已经存在请求处理程序实例,则使用现有实例(这就是你的问题的原因)。

那么,究竟是什么导致了您的错误?

_requestHandlers字典是,这static意味着它存在于多个请求中,即在请求结束时不会被处理/收集垃圾。Your ProfileDbContext,当使用AddDbContext方法注册时,有一个作用域生命周期,这意味着它在每个请求中创建一次(并在请求结束时处理)。这意味着您最终可能会遇到这样一种情况,即_requestHandlers字典包含一个实例,GetAllProfilesQueryHandler该实例对ProfileDbContext.

这是发生的事情:

  1. 第一个请求到达。
  2. Mediator.Send(new GetProfileQuery { Id = id })被调用。
  3. Mediator.Send(new GetProfileQuery { Id = id })GetAllProfilesQueryHandler在字典中找不到_requestHandlers,所以它实例化它并解决它的ProfileDbContext依赖关系。
  4. 请求结束。您的indsance中的_context字段 ( ProfileDbContext)被释放(因为它具有生命周期),但字典(包含实例)没有被释放(因为它是静态的)。GetAllProfilesQueryHandlerscoped_requestHandlersGetAllProfilesQueryHandler
  5. 另一个请求到达。
  6. Mediator.Send(new GetProfileQuery { Id = id })再次被调用。
  7. 这次在字典中Mediator.Send(new GetProfileQuery { Id = id })查找GetAllProfilesQueryHandler实例并使用现有实例,其字段位于上一个请求的末尾_requestHandlers_context
  8. GetAllProfilesQueryHandler尝试访问已处置的_context字段,并收到“无法访问已处置的对象”错误。

可能的解决方案

不要让Mediator.SendresolveGetAllProfilesQueryHandler依赖。

也许传递IServiceProvider serviceProvider给您GetAllProfilesQueryHandler并让它根据需要解决其依赖关系:

public class GetAllProfilesQueryHandler : IRequestHandler<GetAllProfilesQuery, ProfilesListViewModel>
{
    private readonly IServiceProvider _serviceProvider;

    public GetAllProfilesQueryHandler(IServiceProvider serviceProvider)
    {
        _serviceProvider = serviceProvider;
    }

    public async Task<ProfilesListViewModel> Handle(GetAllProfilesQuery request, CancellationToken cancellationToken)
    {
        return new ProfilesListViewModel
        {
            ProfileDbContext context = (ProfileDbContext)this._serviceProvider.GetService(typeof(ProfileDbContext));
            IMapper mapper = (IMapper)this._serviceProvider.GetService(typeof(IMapper));

            Profiles = await context.Profiles.ProjectTo<ProfileLookupModel>(mapper.ConfigurationProvider).ToListAsync(cancellationToken)
        };
    }
}

编辑:

正如@Lucian Bargaoanu 在评论中指出的那样,您可以通过 DI 解析处理程序,如https://github.com/jbogard/MediatR.Extensions.Microsoft.DependencyInjection


推荐阅读