我创建了一个OwinMiddleware来捕获我的api请求中所有未处理的异常。
public class UnhandledExceptionMiddleware : OwinMiddleware
{
private static readonly Logger logger = LogManager.GetCurrentClassLogger();
public UnhandledExceptionMiddleware(OwinMiddleware next) : base(next)
{ }
public override async Task Invoke(IOwinContext context)
{
try
{
await Next.Invoke(context);
}
catch (Exception ex)
{
logger.Fatal(ex, ex.Message);
}
}
}
但是如果我在我的控制器函数中抛出一些错误,则不使用catch块,在这个中间件中看起来一切都很好。为什么不使用我的catch块?
这是我的Owin Config。 Invoke方法仅适用于catch无法正常工作
public void Configuration(IAppBuilder app)
{
// Enables use of spatial data types
SqlServerTypes.Utilities.LoadNativeAssemblies(HostingEnvironment.MapPath("~/bin"));
HttpConfiguration config = new HttpConfiguration();
IContainer container = AutoFacConfig.Register(config, app);
app.UseApplicationInsights();
app.UseNLog();
app.Use<UnhandledExceptionMiddleware>();
答案 0 :(得分:0)
按照添加到管道的顺序调用中间件。确保在这种情况下,UnhandledExceptionMiddleware
在启动期间添加public class Startup {
public void Configuration(IAppBuilder app) {
app.Use<UnhandledExceptionMiddleware>();
//...Add other middleware
//...code removed for brevity
}
}
之前
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="300dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="16dp">
<TextView
android:id="@+id/title"/>
</LinearLayout>
<RelativeLayout
android:id="@+id/someid"
android:layout_width="match_parent"
android:layout_height="300dp"
android:visibility="gone"
tools:visibility="gone">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:visibility="visible">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="270dp"
android:orientation="vertical"
android:visibility="visible"
tools:visibility="visible">
<LinearLayout
android:id="@+id/editTextLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:visibility="gone"
tools:visibility="visible">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginBottom="10dp"
android:layout_marginTop="13dp"
android:gravity="bottom"
android:orientation="horizontal">
<EditText
android:id="@+id/EditText"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="40dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="16dp"
android:paddingRight="16dp">
<Button
android:id="@+id/Button" />
</LinearLayout>
<View
android:id="@+id/overlayButtons"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white_70"
android:visibility="gone"
tools:visibility="gone" />
</RelativeLayout>
</LinearLayout>
答案 1 :(得分:0)
解决了这篇文章。 Disable *all* exception handling in ASP.NET Web API 2 (to make room for my own)?
您必须对默认的IExceptionFilter进行ovverride以捕获中间件中的异常。