title: DelegatingHandler not getting invoked if exception caught by ExceptionHandler
body: <p>I am trying to understand how Web API Http pipeline works!</p>
  
  <p>In my Web API project, I am using the following technique to log/handle exceptions:</p>
  
  <ul>
  <li>ExceptionHandler -> Handle exceptions at the global level</li>
  <li>ExceptionFilterAttribute -> Handle custom exception thrown by user</li>
  <li>DelegatingHandler -> log request and response data</li>
  </ul>
  
  <p>Sample code for each implementation:</p>
  
  <p>ExceptionFilter:</p>
  
  <pre><code>public class CustomExceptionFilter : ExceptionFilterAttribute
  {
      public override void OnException(HttpActionExecutedContext context)
      {
          var request = context.ActionContext.Request;
          if (context.Exception is ItemNotFoundException)
          {
              context.Response = request.CreateResponse(HttpStatusCode.NotFound, context.Exception.Message);
          }
          else if (context.Exception is InvalidRequestException)
          {
              context.Response = request.CreateResponse(HttpStatusCode.BadRequest, context.Exception.Message);
          }
      }
  }
  </code></pre>
  
  <p>Exception Handler:</p>
  
  <pre><code>public class GlobalExceptionHandler : ExceptionHandler
  {
      public override void Handle(ExceptionHandlerContext context)
      {
          var result = new HttpResponseMessage(HttpStatusCode.InternalServerError)
          {
              Content = new StringContent(Constant.ErrorMessage.InternalServerError)
          };
          context.Result = new ErrorMessageResult(context.Request, result);
      }
  }
  
  public class ErrorMessageResult : IHttpActionResult
  {
      private readonly HttpRequestMessage _request;
      private readonly HttpResponseMessage _httpResponseMessage;
  
      public ErrorMessageResult(HttpRequestMessage request, HttpResponseMessage httpResponseMessage)
      {
          _request = request;
          _httpResponseMessage = httpResponseMessage;
      }
  
      public Task&lt;HttpResponseMessage&gt; ExecuteAsync(CancellationToken cancellationToken)
      {
          return Task.FromResult(_httpResponseMessage);
      }
  }
  </code></pre>
  
  <p>DelegatingHandler:</p>
  
  <pre><code>public class LogRequestAndResponseHandler : DelegatingHandler
  {
      private readonly ILoggingService _loggingService;
  
      protected override async Task&lt;HttpResponseMessage&gt; SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
      {
          string requestBody = await request.Content.ReadAsStringAsync();
          _loggingService.FirstLevelServiceLog(requestBody);
          var result = await base.SendAsync(request, cancellationToken);
          if (result.Content != null)
          {
              var responseBody = await result.Content.ReadAsStringAsync();
              _loggingService.FirstLevelServiceLog(responseBody);
          }
          return result;
      }
  }
  </code></pre>
  
  <p><strong>Observation:</strong></p>
  
  <ul>
  <li>When there is an custom exception <code>CustomExceptionFilter</code> is getting invoked and later the response is logged in <code>LogRequestAndResponseHandler</code>.</li>
  <li>However, if the exception is not handled, it goes in <code>GlobalExceptionHandler</code> then the response DOES NOT come to <code>LogRequestAndResponseHandler</code> for logging.</li>
  </ul>
  
  <p><strike>Could anyone let me know, what code change have to be done in <code>CustomExceptionFilter/GlobalExceptionHandler</code> in order to receive the response in <code>DelegatingHandler</code>?</strike></p>
  
  <blockquote>
    <p><strong>Solution: (Updated 10/09/2018)</strong></p>
  </blockquote>
  
  <p>Okay, so i found the solution <a href="https://stackoverflow.com/a/43041718/1169180">here</a></p>
  
  <p>By modifying <code>ExceptionHandler</code> code, i am able to catch the response in <code>DelegatingHandler</code></p>
  
  <p>Key was to inherit from <code>IExceptionHandler</code> rather than <code>ExceptionHandler</code></p>
  
  <p>Code:</p>
  
  <pre><code>public class GlobalExceptionHandler : IExceptionHandler
  {
      public Task HandleAsync(ExceptionHandlerContext context, CancellationToken cancellationToken)
      {
          var httpResponse = context.Request.CreateResponse(HttpStatusCode.InternalServerError, Constant.ErrorMessage.InternalServerError);
          context.Result = new ResponseMessageResult(httpResponse);
  
          return Task.FromResult(0);
      }
  }
  </code></pre>
  
  <p><strong>Question:</strong></p>
  
  <ul>
  <li>I am still not able to understand how it's working? What is the difference between <code>IExceptionHandler</code> &amp; <code>ExceptionHandler</code>?</li>
  </ul>
  
  <p>Could anyone shed some light on this?</p>