我收到以下错误:
Cannot access a disposed object.
Object name: 'IServiceProvider'.
尝试调用 _mediator.Send
方法时。
这仅在我尝试测试控制器的方法时发生。每当从 api 调用控制器时,它似乎都能正常工作。
控制器:
public class Controller : ControllerBase
{
private readonly IMediator mediator;
public Controller(IMediator mediator)
{
this.mediator = mediator;
}
[HttpPost]
public async Task<IActionResult> Post(Command command)
{
try
{
await mediator.Send(command); // exception occurs here
return Ok();
}
catch (Exception ex)
{
return BadRequest(ex.InnerException?.Message ?? ex.Message);
}
}
}
请求处理器:
public class CommandHandler
: IRequestHandler<Command, bool>
{
private readonly AppDbContext dbContext;
public CommandHandler(AppDbContext dbContext)
{
this.dbContext = dbContext;
}
public async Task<bool> Handle(Command request, CancellationToken cancellationToken)
{
try
{
await dbContext.Companies.AddAsync(new Company(request.CompanyId, request.CompanyName), cancellationToken);
await dbContext.SaveChangesAsync(cancellationToken);
return true;
}
catch(Exception)
{
throw;
}
}
}
MediatR 是这样注册的:
services.AddMediatR(Assembly.GetExecutingAssembly());
我正在尝试使用真实的数据库连接来测试这个控制器。
protected Controller GetController() // method where controller is created and mediatR injected
{
using var scope = _scopeFactory.CreateScope();
var mediator = scope.ServiceProvider.GetRequiredService<IMediator>();
return new Controller(mediator);
}
protected async Task<IActionResult> PostControllerMethod(Command command) // this method is call in test method
=> await GetController().Post(command);
解决问题的唯一方法是将 MediatR 注册为单例:
services.AddMediatR(mediatRServiceConfiguration => mediatRServiceConfiguration.AsSingleton(), Assembly.GetExecutingAssembly());
我不确定后果。