我有一个奇怪的问题,DataExceptions
来自 Dapper 没有正确地正确处理。
这是我的设置:
public class CustomerController : Controller
{
private readonly IMediator _mediator;
public CustomerController(IMediator mediator)
{
_mediator = mediator;
}
[HttpGet]
public async Task<IActionResult> Get(Get.Query query)
{
var result = await _mediator.Send(query);
return Ok(result);
}
}
public class Get
{
public class Query : IRequest<IEnumerable<Result>>
{
}
public class Result
{
public Guid Id { get; set; }
public string Name { get; set; }
}
public class QueryHandler : IAsyncRequestHandler<Query, IEnumerable<Result>>
{
private readonly IDbConnection _dapper;
public QueryHandler(IDbConnection dapper)
{
_dapper = dapper;
}
public async Task<IEnumerable<Result>> Handle(Query message)
{
// the below throws because of incorrect type mapping
// (yes, the connection is open)
var customers =
await _dapper.Connection.QueryAsync<Result>("SELECT Id, Name FROM [Customer].[Customers]");
return customers;
}
}
}
结果
curl
curl -X GET ' http://localhost:5000/api/Customer '
请求 URL
http://localhost/api/Customer
响应正文
无内容
响应码
500
预期的
我期待 500 有错误描述,而不是没有内容。
这是抛出的异常:
如果我将Handle
方法更改为:
public async Task<IEnumerable<Result>> Handle(Query message)
{
throw new DataException("What is going on?");
}
我得到了预期的结果。500 错误提示“发生了什么事?”
因为我app.UseDeveloperExceptionPage();
启用了它看起来像这样。
An unhandled exception occurred while processing the request. DataException: What is going on? ...Customer.Get+QueryHandler+<Handle>d__2.MoveNext() in Get.cs, line 42 Stack Query Cookies Headers DataException: What is going on? ...
但这是意料之中的。
那么发生了什么?为什么DataException
Dapper 没有按预期工作?