Asp.net core 如何正确处理ASP.NET核心中间件中的错误?

Asp.net core 如何正确处理ASP.NET核心中间件中的错误?,asp.net-core,async-await,.net-core,asp.net-core-mvc,asp.net-core-2.0,Asp.net Core,Async Await,.net Core,Asp.net Core Mvc,Asp.net Core 2.0,我创建了一个HandlerMiddleware类,它执行特定URL的处理程序 using System.Threading.Tasks; using Microsoft.AspNetCore.Http; namespace FailTest { public interface IHttpHandler { bool IsReusable { get; } void ProcessRequest(HttpContext context);

我创建了一个HandlerMiddleware类,它执行特定URL的处理程序

using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;


namespace FailTest
{


    public interface IHttpHandler
    {
        bool IsReusable { get; }
        void ProcessRequest(HttpContext context);
    }


    public abstract class HandlerMiddleware<T> 
        where T : IHttpHandler
    {
        private readonly RequestDelegate _next;

        public HandlerMiddleware() 
        { }

        public HandlerMiddleware(RequestDelegate next)
        {
            _next = next;
        }

        public async Task Invoke(HttpContext context)
        {
            try
            {
                await SyncInvoke(context);
            }
            catch (System.Exception ex)
            {
                System.Console.WriteLine(ex.Message);
                throw;
            }


        }

        public Task SyncInvoke(HttpContext context)
        {
            try
            {
                // IHttpHandler handler = (IHttpHandler)this;
                T handler = System.Activator.CreateInstance<T>();
                handler.ProcessRequest(context);
            }
            catch (System.Exception ex)
            {
                System.Console.WriteLine(ex.Message);
                throw;
            }


            return Task.CompletedTask;
        }
    } // End Abstract Class HandlerMiddleware


}
使用System.Threading.Tasks;
使用Microsoft.AspNetCore.Http;
命名空间失败测试
{
公共接口IHttpHandler
{
布尔可重用{get;}
void ProcessRequest(HttpContext上下文);
}
公共抽象类handler中间件
其中T:IHttpHandler
{
private readonly RequestDelegate\u next;
公共HandlerMiddleware()
{ }
公共HandlerMiddleware(RequestDelegate下一步)
{
_下一个=下一个;
}
公共异步任务调用(HttpContext上下文)
{
尝试
{
等待同步调用(上下文);
}
catch(System.Exception-ex)
{
系统控制台写入线(例如消息);
投掷;
}
}
公共任务SyncInvoke(HttpContext上下文)
{
尝试
{
//IHttpHandler handler=(IHttpHandler)this;
T handler=System.Activator.CreateInstance();
ProcessRequest(上下文);
}
catch(System.Exception-ex)
{
系统控制台写入线(例如消息);
投掷;
}
返回Task.CompletedTask;
}
}//结束抽象类handler中间件
}
现在我有了一个实现处理程序的类,例如

[HandlerPath("/treedata", "GET,POST")]
public class SomeFailingTask
    : HandlerMiddleware<SomeFailingTask>, IHttpHandler 
{
    public SomeFailingTask() : this(null)
    { }

    public SomeFailingTask(RequestDelegate next) : base(next)
    { }

    bool IHttpHandler.IsReusable
    {
        get { throw new System.NotImplementedException(); }
    }

    void IHttpHandler.ProcessRequest(HttpContext context)
    {
          // Do something in DB and return result as JSON
          // e.g. SQL with invalid syntax, or a missing parameter
    }
}
[HandlerPath(“/treedata”,“GET,POST”)]
公共类任务
:handler,IHttpHandler
{
public SomeFailingTask():此(null)
{ }
public SomeFailingTask(RequestDelegate next):基本(next)
{ }
布尔·伊赫特芬德勒是可维修的
{
获取{抛出新系统。NotImplementedException();}
}
void IHttpHandler.ProcessRequest(HttpContext上下文)
{
//在数据库中执行某些操作,并将结果作为JSON返回
//例如,SQL语法无效或缺少参数
}
}
只要ProcessRequest中没有错误,它就可以正常工作。 但是当ProcessRequest中抛出异常时,它会使用HTTP200OK完成请求

我做错了什么?

我在Invoke和SyncInvoke中都遇到了异常,但HTTP请求总是像一切正常一样完成…

如果出现异常,您希望有什么样的行为?引发什么样的异常以及具体在哪里?@cassandrad:HTTP 500;IHttpHandler.ProcessRequest中的DbException(SqlException)