Ho dovuto eseguire il debug attraverso System.Web.Http per trovare la risposta alla mia domanda. Quindi la risposta è:
E 'lecito ritenere che ExceptionFilters verranno eseguiti prima ExceptionHandlers
Se l'ExceptionFilter crea una risposta l'ExceptionHandler non sarebbe stato giustiziato.
perché questo è così:
Quando si dispone di un ExceptionFilter registrata per l'esecuzione a livello globale o per il vostro azione di controllo, la classe base ApiController da cui tutti i controller di api ereditano andrà a capo il risultato in un ExceptionFilterResult e chiama il suo metodo ExecuteAsync. Questo è il codice nel ApiController, che fa questo:
if (exceptionFilters.Length > 0)
{
IExceptionLogger exceptionLogger = ExceptionServices.GetLogger(controllerServices);
IExceptionHandler exceptionHandler = ExceptionServices.GetHandler(controllerServices);
result = new ExceptionFilterResult(ActionContext, exceptionFilters, exceptionLogger, exceptionHandler,
result);
}
return result.ExecuteAsync(cancellationToken);
Guardando il metodo ExceptionFilterResult.ExecuteAsync:
try
{
return await _innerResult.ExecuteAsync(cancellationToken);
}
catch (Exception e)
{
exceptionInfo = ExceptionDispatchInfo.Capture(e);
}
// This code path only runs if the task is faulted with an exception
Exception exception = exceptionInfo.SourceException;
Debug.Assert(exception != null);
bool isCancellationException = exception is OperationCanceledException;
ExceptionContext exceptionContext = new ExceptionContext(
exception,
ExceptionCatchBlocks.IExceptionFilter,
_context);
if (!isCancellationException)
{
// We don't log cancellation exceptions because it doesn't represent an error.
await _exceptionLogger.LogAsync(exceptionContext, cancellationToken);
}
HttpActionExecutedContext executedContext = new HttpActionExecutedContext(_context, exception);
// Note: exception filters need to be scheduled in the reverse order so that
// the more specific filter (e.g. Action) executes before the less specific ones (e.g. Global)
for (int i = _filters.Length - 1; i >= 0; i--)
{
IExceptionFilter exceptionFilter = _filters[i];
await exceptionFilter.ExecuteExceptionFilterAsync(executedContext, cancellationToken);
}
if (executedContext.Response == null && !isCancellationException)
{
// We don't log cancellation exceptions because it doesn't represent an error.
executedContext.Response = await _exceptionHandler.HandleAsync(exceptionContext, cancellationToken);
}
Si può vedere che l'ExceptionLogger viene eseguito per primo, poi tutti sono ExceptionFilters eseguito e, quindi, se eseguitoContext.Response == null, viene eseguito ExceptionHandler.
Spero sia utile!
fonte
2015-01-26 09:29:34