c# - C#在web api上实现一个接口
问题描述
我正在开发一个新的微服务,并希望为我的客户在 API 上放置一个接口。
例如。
我有一个 ApiController 的客户服务和一个方法:
[HttpGet("api/v1/[action]")]
[ProducesResponseType((int)HttpStatusCode.BadRequest)]
[ProducesResponseType((int)HttpStatusCode.NotFound)]
[ProducesResponseType(typeof(List<CustomerDto>), (int)HttpStatusCode.OK)]
public async Task<IActionResult> GetCustomers()
{
Log.Logger.Information($"Calling GetCustomers()");
var ret = await _db.Customer.GetListAsync<CustomerDto>();
Log.Logger.Information($"GetCustomers()=>{ret?.Count}");
if (ret == null)
{
return NotFound();
}
return Ok(ret);
}
在我的客户端代码中,我有一个服务接口,它将 HTTP 调用抽象为上述服务。因此,我使用此服务的客户可以简单地注入 aICustomerService
并调用GetCustomers()
.
这一切都很好,但是我真正想要的是ICustomerService
在 API 控制器上放置一个相同的接口,这样我就可以在编译时发现任何问题。问题是ICustomerService.GetCustomer
返回 List<CustomerDto>
和控制器返回IActionResult
。
Task<List<CustomerDto>> GetCustomers();
VS
public async Task<IActionResult> GetCustomers(){}
有没有办法ICustomerService
在构建时强制 API 控制器上的接口?