c# - 无法在 .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.EntityQueryable
1.System.Collections.Generic.IAsyncEnumerable.GetEnumerator()\r\n 在 System.Linq.AsyncEnumerable.Aggregate_[TSource,TAccumulate,TResult](IAsyncEnumerable1 source, TAccumulate seed, Func
3 累加器, Func2 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.RequestPostProcessorBehavior
2.Handle(TRequest request, CancellationToken cancelToken, RequestHandlerDelegate1 next)\r\n at MediatR.Pipeline.RequestPreProcessorBehavior
2 .Handle(TRequest request, CancellationToken cancelToken, RequestHandlerDelegate1 next)\r\n at MediatR.Pipeline.RequestPreProcessorBehavior
2.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.ValueTask
1.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()" }
解决方案
问题出在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
.
这是发生的事情:
- 第一个请求到达。
Mediator.Send(new GetProfileQuery { Id = id })
被调用。Mediator.Send(new GetProfileQuery { Id = id })
GetAllProfilesQueryHandler
在字典中找不到_requestHandlers
,所以它实例化它并解决它的ProfileDbContext
依赖关系。- 请求结束。您的indsance中的
_context
字段 (ProfileDbContext
)被释放(因为它具有生命周期),但字典(包含实例)没有被释放(因为它是静态的)。GetAllProfilesQueryHandler
scoped
_requestHandlers
GetAllProfilesQueryHandler
- 另一个请求到达。
Mediator.Send(new GetProfileQuery { Id = id })
再次被调用。- 这次在字典中
Mediator.Send(new GetProfileQuery { Id = id })
查找GetAllProfilesQueryHandler
实例并使用现有实例,其字段位于上一个请求的末尾。_requestHandlers
_context
GetAllProfilesQueryHandler
尝试访问已处置的_context
字段,并收到“无法访问已处置的对象”错误。
可能的解决方案
不要让Mediator.Send
resolveGetAllProfilesQueryHandler
依赖。
也许传递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
推荐阅读
- monorepo - Lerna 本地依赖项未保存在 package-lock.json 中
- github - 如何将我的项目从 android studio 连接到 github
- mysql - MySQL 条件内连接 X # 列
- python - pytest pyproject.toml 配置忽略特定路径
- python - 如何使用python返回if in的所有输出
- google-cloud-functions - Google Cloud Functions 应用不写入自定义日志
- java - 如何从应用程序内部安装 apk?
- linux - 获取目录中所有文件的名称、大小和时间戳
- javascript - 自定义登录
- java - 如何在 postgres 中使用 Java 插入 json 文档