2016-06-30 4 views
0

I를 글로벌 예외 핸들러 클래스 내 예외 처리하기 위해 :글로벌 예외 처리기가 오류를 처리하지 (웹 API)

namespace MyProject.Extensions 
{ 
    public class MyExceptionHandler : ExceptionHandler 
    { 
     public override void Handle(ExceptionHandlerContext context) 
     { 
      context.Result = new MyErrorResult 
      { 
       Request = context.ExceptionContext.Request, 
       ContextException = context.Exception 
      }; 
     } 

    } 

    private class MyErrorResult : IHttpActionResult 
    { 
     public HttpRequestMessage Request { get; set; } 
     public Exception ContextException { get; set; } 

     public Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken) 
     { 
      HttpResponseMessage response = MyErrorResponse(Request, ContextException); 
      response.RequestMessage = Request; 

      return Task.FromResult(response); 
     } 
    } 
} 

을 그리고의 Global.asax에 등록 :

protected void Application_Start() 
    { 
     ConfigureLog4Net(); 
     AreaRegistration.RegisterAllAreas(); 

     WebApiConfig.Register(GlobalConfiguration.Configuration); 
     FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); 
     RouteConfig.RegisterRoutes(RouteTable.Routes); 
     DependencyConfig.RegisterDependencies(GlobalConfiguration.Configuration); 
     GlobalConfiguration.Configuration.EnsureInitialized(); 
    } 

public static class WebApiConfig 
{ 
    public static void Register(HttpConfiguration config) 
    { 
     config.Services.Replace(typeof(IExceptionHandler), new MyExceptionHandler()); 

     config.Routes.MapHttpRoute(
      name: "DefaultApi", 
      routeTemplate: "api/{controller}/{action}", 
      defaults: new { action = RouteParameter.Optional } 
     ); 
    } 
} 

및 내 컨트롤러 중 하나에서 테스트 예외를 throw하면 Visual Studio가 중지되고 '예외 처리되지 않음'오류가 발생합니다. 내가 도대체 ​​뭘 잘못하고있는 겁니까? 어떤 도움이라도 대단히 감사 할 것입니다.

+1

처리기에 중단 점을 설정 한 다음 첫 번째 예외에서 VS가 중지되면 "계속"을 클릭하면 어떻게됩니까? 오류 처리기가 있습니까? –

+0

정확히 무엇을 얻습니까? –

답변

0

Visual Studio (VS)가 중지되고 디버그 모드에서 처리되지 않은 예외가있을 때 이것이 기본 VS 동작 ("throw되는 경우 중단")이므로 '예외 처리되지 않았습니다'오류가 발생합니다.

변경하려면 "디버그"-> "Windows"-> "예외 설정"으로 이동하십시오.

"던져진 상태에서 휴식"조건을 변경할 수있는 창을 엽니 다. 귀하의 경우에는 "공용 언어 런타임 예외"섹션으로 게임을해야합니다 : 전체 섹션 또는 특정 유형의 예외를 선택 취소하십시오.

enter image description here

또 다른 옵션 확인은 VS의 외부 웹 API 응용 프로그램을 실행 (자체 호스팅 또는 사용하여 IIS 등)이다.

관련 문제