web api 2 ExceptionHandlerFilter OnExceptionAsync not fired

本文关键字:not fired OnExceptionAsync ExceptionHandlerFilter api web | 更新日期: 2023-09-27 18:05:10

经过6年的发展,我的第一个问题,所以…你好每一个人。关于这个话题,我到处都找遍了,但是什么都找不到。

我已经把我所有的控制器的方法从同步到异步。在一切正常工作之前,当抛出异常时,触发OnException方法。

现在,当一个异常在异步控制器方法中被抛出时,没有办法触发OnException或OnExceptionAsync方法。

我的代码

  public class ExceptionHandlerFilter : ExceptionFilterAttribute
{
    public override void OnException(HttpActionExecutedContext context)
    {
        var resp = new HttpResponseMessage();
        resp.Content = new StringContent(context.Exception.Message + Environment.NewLine + Environment.NewLine + context.Exception.StackTrace);
        if (context.Exception is NotImplementedException)
            resp.StatusCode = HttpStatusCode.NotImplemented;
        else if (context.Exception is NotFoundException)
            resp.StatusCode = HttpStatusCode.NotFound;
        else if (context.Exception is ArgumentException)
            resp.StatusCode = HttpStatusCode.BadRequest;
        else
            resp.StatusCode = HttpStatusCode.InternalServerError;
        context.Response = resp;
    }
    public override Task OnExceptionAsync(HttpActionExecutedContext context, CancellationToken cancellationToken)
    {
        var task = base.OnExceptionAsync(context, cancellationToken);
        return task.ContinueWith((t) =>
        {
            var resp = new HttpResponseMessage();
            resp.Content = new StringContent(context.Exception.Message + Environment.NewLine + Environment.NewLine + context.Exception.StackTrace);
            if (context.Exception is NotImplementedException)
                resp.StatusCode = HttpStatusCode.NotImplemented;
            else if (context.Exception is NotFoundException)
                resp.StatusCode = HttpStatusCode.NotFound;
            else if (context.Exception is ArgumentException)
                resp.StatusCode = HttpStatusCode.BadRequest;
            else
                resp.StatusCode = HttpStatusCode.InternalServerError;
            context.Response = resp;
        });
    }        
}

这是我的控制器

[ExceptionHandlerFilter]
public class FascicolazioneController : ApiController
{      
    [ActionName("ClassificaFascicolaArchivisticamente")]
    [HttpPost]
    public async void ClassificaFascicolaArchivisticamente(string progressivoAnnuo, string archivio, int fascicoloId, string classificazione)
    {
        try
        {
            throw new NotImplementedException();
        }
        catch (Exception ex)
        {
            throw ex;
        }
        using (var client = new Cad.BDI.Archiflow.FacadeArchiflow("SUPER", this.GetTypeAndMethodName()))
        {

            await Task.Factory.StartNew(() => client.ClassificaFascicolaArchivisticamente(
                ProgressivoAnnuo.Parse(progressivoAnnuo), archivio, fascicoloId, Classificazione.Parse(classificazione)));
        }
    }

我已经尝试在WebApiConfig和/或global中设置适当的代码行。除了在控制器上设置属性之外,还添加了ExceptionHadlerFilter,但是没有任何帮助

web api 2 ExceptionHandlerFilter OnExceptionAsync not fired

多亏了这篇文章,我才明白了。

https://msdn.microsoft.com/en-us/magazine/jj991977.aspx?f=255& MSPPError = -2147217396

问题是控制器方法的签名。不要使用

    public async void Bla(){}

我应该用

   public async Task Bla(){}