gpt4 book ai didi

c# - 使用 MediatR 执行 API 方法时出现错误

转载 作者:行者123 更新时间:2023-12-01 19:32:53 25 4
gpt4 key购买 nike

这是我第一次实现 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 core 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>));

关于c# - 使用 MediatR 执行 API 方法时出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59948576/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com