c# - 使用MediatR时,Dapper的DataException消失了

标签 c# error-handling asp.net-core dapper mediatr

我有一个奇怪的问题,来自Dapper的DataExceptions无法正确设置。

这是我的设置:

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'
Request URL
http://localhost/api/Customer
Response Body
no content
Response Code
500



预期

我期望500带有错误描述,而不是没有内容。

这是抛出的异常:

enter image description here

如果我将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? 
...


但这是预料之中的。

那么发生了什么?为什么Dapper的DataException无法正常工作?

最佳答案

MediatR的预处理器会在AggregateException中聚集管道中的所有异常。

您必须公开它-例如ExceptionFilterAttribute:

public class MyExceptionFilterAttribute : ExceptionFilterAttribute
{
    public override void OnException(HttpActionExecutedContext context)
    {
        Exception exception = context.Exception;

        if (exception is AggregateException
            && exception.InnerException != null)
        {
            exception = exception.InnerException;
        }
        // check type and do you stuff ........
        context.Response = new HttpResponseMessage
        {
            Content = this.CreateContent(response),
            StatusCode = HttpStatusCode.BadRequest
        };
        //....

关于c# - 使用MediatR时,Dapper的DataException消失了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45432325/

相关文章:

c# - 如何在数据绑定(bind)转发器中创建条件内容

c# - 在 lambda 表达式中的另一个对象列表中获取一个对象列表

php - PHP禁止第三方库警告

sql-server - 从服务器接收结果时发生传输级错误(连接关闭且 AUTO CLOSE false)

c# - ASP.NET Core 5.0 MVC 中的自定义字段验证错误

c# - 将命令文本转换为已回答问题中的字符串问题

c# - 在C#中的rabbit Mq中获取xDeath中队列消息的最大重试次数

vba - excel vba - 有没有办法防止宏按钮垃圾邮件?

python - Python ValueError : Bin edges must be unique

c# - 如何使用 .NET Core Web 应用程序的单个实例中的动态租户对 Azure Active Directory 中的用户进行身份验证?