【发布时间】:2020-06-04 13:10:41
【问题描述】:
我在 API 控制器方法中获取了一个列表,并将其传递给 Handler,如下所示。 我打算做的是遍历列表并将列表中的所有项目保存到数据库中。
public class Create
{
public class Command : IRequest
{
public Guid A { get; set; }
public string B { get; set; }
public string C { get; set; }
public bool D { get; set; }
}
public class Handler : IRequestHandler<List<Command>>
{
private readonly DataContext _context;
private readonly IMapper _mapper;
public Handler(DataContext context, IMapper mapper)
{
_mapper = mapper;
_context = context;
}
public async Task<Unit> Handle(List<Command> request, CancellationToken cancellationToken)
{
// loop over the request list and save in the database
}
}
}
但是代码行中的“Handler”下面有一条红线:public class Handler : IRequestHandler<List<Command>>。
将鼠标悬停在“Handler”上,它会说:
类型 'System.Collections.Generic.List' 不能用作泛型类型中的类型参数“TRequest”或 方法'IRequestHandler'。没有隐式引用 从转换 'System.Collections.Generic.List' 到“MediatR.IRequest”。 [应用]csharp(CS0311)
我的 API 控制器方法是:
[HttpPost]
public async Task<ActionResult<Unit>> Create(List<Create.Command> commands) // not like this, it'll be a list
{
return await Mediator.Send(commands);
}
return await Mediator.Send(commands); 下的红线说:
无法将类型“object”隐式转换为 'Microsoft.AspNetCore.Mvc.ActionResult'。显式 存在转换(您是否缺少演员表?)[API]csharp(CS0266)
如果我在写问题时遗漏了一些信息,请对我放心,我会在询问时继续更新。
【问题讨论】:
标签: c# rest asp.net-core cqrs mediatr