【问题标题】:Can Anyone Explain the work flow of IExceptionHandler with Sample Client Application任何人都可以用示例客户端应用程序解释 IExceptionHandler 的工作流程吗
【发布时间】:2014-03-29 03:04:48
【问题描述】:

我在此示例中面临以下问题:

我在ExceptionContext 中找不到IsOutermostCatchBlock

如果发生异常,这个HandleAsync方法会执行两次。

(http://www.asp.net/web-api/overview/web-api-routing-and-actions/web-api-global-error-handling)

public class CustomExceptionHandler : IExceptionHandler
{
    public virtual Task HandleAsync(ExceptionHandlerContext context,
                                        CancellationToken cancellationToken)
    {
        if (!ShouldHandle(context))
        {
            return Task.FromResult(0);
        }

            return HandleAsyncCore(context, cancellationToken);
        }

        public virtual Task HandleAsyncCore(ExceptionHandlerContext context,
                                            CancellationToken cancellationToken)
        {
            HandleCore(context);
            return Task.FromResult(0);
        }

        public virtual void HandleCore(ExceptionHandlerContext context)
        {
        }

        public virtual bool ShouldHandle(ExceptionHandlerContext context)
        {    
             return context.ExceptionContext.IsOutermostCatchBlock;
        }

    }

    public class OopsExceptionHandler : CustomExceptionHandler
    {
        public override void HandleCore(ExceptionHandlerContext context)
        {
            context.Result = new TextPlainErrorResult
            {
                Request = context.ExceptionContext.Request,
                Content = "Oops! Sorry! Something went wrong." +
                          "Please contact support@contoso.com so we can try to fix it."
            };
        }

        private class TextPlainErrorResult : IHttpActionResult
        {
            public HttpRequestMessage Request { get; set; }
            public string Content { get; set; }

            public Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken)
            {
                HttpResponseMessage response =
                                 new HttpResponseMessage(HttpStatusCode.InternalServerError);
                response.Content = new StringContent(Content);
                response.RequestMessage = Request;
                return Task.FromResult(response);
            }
        }
    }
}

【问题讨论】:

  • 我不确定我是否理解您的问题。 HandleAsync 确实被多次调用。这就是为什么有 ShouldHandle 守卫的原因。我不知道你找不到“IsOutermostCatchBlock”是什么意思。您显示的代码是否编译?
  • 如果我使用 context.ExceptionContext.CatchBlock.IsTopLevel 表示此代码正在编译。

标签: c# asp.net-web-api


【解决方案1】:

CatchBlock.IsTopLevel

IsOutermostCatchBlock 不存在。 请改用 CatchBlock.IsTopLevel

public virtual bool ShouldHandle(ExceptionHandlerContext context)
{
  return context.ExceptionContext.CatchBlock.IsTopLevel;
}

NuDoq 上的来源:ExceptionHandlerContextExceptionContextCatchBlock

【讨论】:

    猜你喜欢
    • 2014-03-22
    • 2016-04-09
    • 2011-03-31
    • 1970-01-01
    • 2021-07-11
    • 1970-01-01
    • 2011-02-28
    • 1970-01-01
    • 2010-12-15
    相关资源
    最近更新 更多