使用 MediatR 执行 API 方法时,出现错误
When executing API method using MediatR, getting an error
这是我第一次实现 Mediator,但现在调用 API 方法时出现以下错误:
错误
{"error":"Enumerator failed to MoveNextAsync."}
DI
services.AddMediatR(Assembly.GetExecutingAssembly());
services.AddTransient(typeof(IPipelineBehavior<,>), typeof(RequestValidationBehavior<,>));
基础API
[ApiController]
[Route("api/[controller]")]
public abstract class ApiController : ControllerBase
{
private IMediator _mediator;
protected IMediator Mediator => _mediator ??= HttpContext.RequestServices.GetService<IMediator>();
}
API
[HttpGet]
[Route("client/{clientId}/template/list")]
public async Task<ActionResult<TemplateListDTO>> GetClientTemplateList(long clientId)
{
return await Mediator.Send(new GetClientTemplateListQuery { ClientId = clientId });
}
我正在使用 DotNet 核心 3.1
问题描述如下:
https://github.com/jbogard/MediatR/issues/317
Ah. You can't do generic type constraints with vanilla
MS.Extensions.DI. See my PR to fix this:
https://github.com/aspnet/DependencyInjection/pull/635
执行如下操作:
services.AddScoped(typeof(IPipelineBehavior<CustomerAddRequest, CommandResponse>), typeof(CustomerAddBehavior<CustomerAddRequest, CommandResponse>));
services.AddScoped(typeof(IPipelineBehavior<BrandDeleteRequest, CommandResponse>), typeof(BrandDeleteBehavior<BrandDeleteRequest, CommandResponse>));
services.AddScoped(typeof(IPipelineBehavior<CustomerDeleteRequest, CommandResponse>), typeof(CustomerDeleteBehavior<CustomerDeleteRequest, CommandResponse>));
我发现了问题,不是 Mediator 的问题。管道出现异常,处理不当,无法进入next()管道。
这是我第一次实现 Mediator,但现在调用 API 方法时出现以下错误:
错误
{"error":"Enumerator failed to MoveNextAsync."}
DI
services.AddMediatR(Assembly.GetExecutingAssembly());
services.AddTransient(typeof(IPipelineBehavior<,>), typeof(RequestValidationBehavior<,>));
基础API
[ApiController]
[Route("api/[controller]")]
public abstract class ApiController : ControllerBase
{
private IMediator _mediator;
protected IMediator Mediator => _mediator ??= HttpContext.RequestServices.GetService<IMediator>();
}
API
[HttpGet]
[Route("client/{clientId}/template/list")]
public async Task<ActionResult<TemplateListDTO>> GetClientTemplateList(long clientId)
{
return await Mediator.Send(new GetClientTemplateListQuery { ClientId = clientId });
}
我正在使用 DotNet 核心 3.1
问题描述如下:
https://github.com/jbogard/MediatR/issues/317
Ah. You can't do generic type constraints with vanilla MS.Extensions.DI. See my PR to fix this: https://github.com/aspnet/DependencyInjection/pull/635
执行如下操作:
services.AddScoped(typeof(IPipelineBehavior<CustomerAddRequest, CommandResponse>), typeof(CustomerAddBehavior<CustomerAddRequest, CommandResponse>));
services.AddScoped(typeof(IPipelineBehavior<BrandDeleteRequest, CommandResponse>), typeof(BrandDeleteBehavior<BrandDeleteRequest, CommandResponse>));
services.AddScoped(typeof(IPipelineBehavior<CustomerDeleteRequest, CommandResponse>), typeof(CustomerDeleteBehavior<CustomerDeleteRequest, CommandResponse>));
我发现了问题,不是 Mediator 的问题。管道出现异常,处理不当,无法进入next()管道。