谁能解释IExceptionHandler与样品客户端应用程序的工作流程 [英] Can Anyone Explain the work flow of IExceptionHandler with Sample Client Application

查看:217
本文介绍了谁能解释IExceptionHandler与样品客户端应用程序的工作流程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我面临的问题,下面这个示例:

我无法找到 IsOutermostCatchBlock ExceptionContext

如果出现异常,这 HandleAsync 方法被执行了两次。

(<一个href=\"http://www.asp.net/web-api/overview/web-api-routing-and-actions/web-api-global-error-handling\">http://www.asp.net/web-api/overview/web-api-routing-and-actions/web-api-global-error-handling)

 公共类CustomExceptionHandler:IExceptionHandler
{
    公共虚拟任务HandleAsync(ExceptionHandlerContext的背景下,
                                        的CancellationToken的CancellationToken)
    {
        如果(!ShouldHandle(上下文))
        {
            返回Task.FromResult(0);
        }            返回HandleAsyncCore(背景下,的CancellationToken);
        }        公共虚拟任务HandleAsyncCore(ExceptionHandlerContext的背景下,
                                            的CancellationToken的CancellationToken)
        {
            HandleCore(上下文);
            返回Task.FromResult(0);
        }        公共虚拟无效HandleCore(ExceptionHandlerContext上下文)
        {
        }        公共虚拟BOOL ShouldHandle(ExceptionHandlerContext上下文)
        {
             返回context.ExceptionContext.IsOutermostCatchBlock;
        }    }    公共类OopsExceptionHandler:CustomExceptionHandler
    {
        公共覆盖无效HandleCore(ExceptionHandlerContext上下文)
        {
            context.Result =新TextPlainErrorResult
            {
                请求= context.ExceptionContext.Request,
                CONTENT =哎呀!对不起!出事了。 +
                          请联系support@contoso.com所以我们可以尝试修复它。
            };
        }        私有类TextPlainErrorResult:IHttpActionResult
        {
            公众的Htt prequestMessage请求{搞定;组; }
            公共字符串内容{搞定;组; }            公共任务&LT; Htt的presponseMessage&GT; ExecuteAsync(的CancellationToken的CancellationToken)
            {
                HTT presponseMessage响应=
                                 新的Htt presponseMessage(的HTTPStatus code.InternalServerError);
                response.Content =新的StringContent(内容);
                response.RequestMessage =请求;
                返回Task.FromResult(响应);
            }
        }
    }
}


解决方案

CatchBlock.IsTopLevel

IsOutermostCatchBlock 不存在。
使用 CatchBlock.IsTopLevel 而不是:

 公共虚拟BOOL ShouldHandle(ExceptionHandlerContext上下文)
{
  返回context.ExceptionContext.CatchBlock.IsTopLevel;
}

在NuDoq来源:<一个href=\"http://www.nudoq.org/#!/Packages/Microsoft.AspNet.WebApi.Core/System.Web.Http/ExceptionHandlerContext\">ExceptionHandlerContext和<一个href=\"http://www.nudoq.org/#!/Packages/Microsoft.AspNet.WebApi.Core/System.Web.Http/ExceptionContextCatchBlock\">ExceptionContextCatchBlock

I am facing below issues in this Sample:

I am not able to find IsOutermostCatchBlock in ExceptionContext

If Exception occurs, this HandleAsync method is executing twice.

(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);
            }
        }
    }
}

解决方案

CatchBlock.IsTopLevel

IsOutermostCatchBlock does not exists. Use CatchBlock.IsTopLevel instead:

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

Source on NuDoq: ExceptionHandlerContext and ExceptionContextCatchBlock

这篇关于谁能解释IExceptionHandler与样品客户端应用程序的工作流程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆