我检查过这个链接 http://www.asp.net/web-api/overview/web-api-routing-and-actions/web-api-global-error-handling . 在这个链接中,他们这样提到
class OopsExceptionHandler : ExceptionHandler
{
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);
}
}
}
我不知道如何在我的 Web API 操作中调用此类。那么谁能给我使用这个ExceptionHandler
的完整示例。
最佳答案
您不需要自己实现 IExceptionHandler 低级机制。
相反,您可以简单地继承自 ExceptionHandler并覆盖 Handle方法。
public class MyExceptionHandler: ExceptionHandler
{
public override void Handle(ExceptionHandlerContext context)
{
//TODO: Do what you need to do
base.Handle(context);
}
}
ExceptionHandler工具 IExceptionHandler并管理基本的核心机制(如异步和是否应处理该异常)。
像这样使用你的异常处理程序:
config.Services.Replace(typeof(IExceptionHandler), new MyExceptionHandler());
来源
This page解释了如何实现IExceptionHandler,但是有一些错别字,代码没有反射(reflect)最新版本的WebApi。
没有关于 System.Web.Http.ExceptionHandling
命名空间的文档(关于 NuDoq 的一点点)。
所以.. 使用.NET assembly decompiler 看看the source code on GitHub ,我看到了 ExceptionHandler
类,它实现了 IExceptionHandler
并且有一些虚拟方法。
ExceptionHandler 看起来像这样:
namespace System.Web.Http.ExceptionHandling
{
/// <summary>Represents an unhandled exception handler.</summary>
public abstract class ExceptionHandler: IExceptionHandler
{
/// <returns>Returns <see cref="T:System.Threading.Tasks.Task" />.</returns>
Task IExceptionHandler.HandleAsync(ExceptionHandlerContext context, CancellationToken cancellationToken)
{
if (context == null)
{
throw new ArgumentNullException("context");
}
ExceptionContext arg_14_0 = context.ExceptionContext;
if (!this.ShouldHandle(context))
{
return TaskHelpers.Completed();
}
return this.HandleAsync(context, cancellationToken);
}
/// <summary>When overridden in a derived class, handles the exception asynchronously.</summary>
/// <returns>A task representing the asynchronous exception handling operation.</returns>
/// <param name="context">The exception handler context.</param>
/// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
public virtual Task HandleAsync(ExceptionHandlerContext context, CancellationToken cancellationToken)
{
this.Handle(context);
return TaskHelpers.Completed();
}
/// <summary>When overridden in a derived class, handles the exception synchronously.</summary>
/// <param name="context">The exception handler context.</param>
public virtual void Handle(ExceptionHandlerContext context)
{
}
/// <summary>Determines whether the exception should be handled.</summary>
/// <returns>true if the exception should be handled; otherwise, false.</returns>
/// <param name="context">The exception handler context.</param>
public virtual bool ShouldHandle(ExceptionHandlerContext context)
{
if (context == null)
{
throw new ArgumentNullException("context");
}
ExceptionContext exceptionContext = context.ExceptionContext;
ExceptionContextCatchBlock catchBlock = exceptionContext.CatchBlock;
return catchBlock.IsTopLevel;
}
}
}
您可以清楚地看到 ShouldHandle
是使用 ExceptionContextCatchBlock.IsTopLevel
实现的,并且 HandleAsync
调用 Handle
:)
我希望这会有所帮助,直到出现完整的文档。
关于c# - 需要一个完整的示例来在 ASP.NET Web Api 中使用 "ExceptionHandler"处理未处理的异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21901808/