Asp.net core mvc 关闭异步响应

Asp.net core mvc 关闭异步响应,asp.net-core-mvc,asp.net-core-2.0,Asp.net Core Mvc,Asp.net Core 2.0,我正在尝试关闭当前响应,但在尝试HttpContext.response.Body.close()和response.End()时,什么也没有发生 我之所以尝试实现这一点,是因为传统的验证器函数编写错误并关闭响应,或者至少停止父WebAPI方法 例如: private async Task Register_v2() { //Read JSON to object UserRegisterRequest userRegisterRequest =

我正在尝试关闭当前响应,但在尝试
HttpContext.response.Body.close()
response.End()
时,什么也没有发生

我之所以尝试实现这一点,是因为传统的验证器函数编写错误并关闭响应,或者至少停止父WebAPI方法

例如:

    private async Task Register_v2()
    {
        //Read JSON to object
        UserRegisterRequest userRegisterRequest = Request.ReadBody().FromJson<UserRegisterRequest>();

        //Validate object (legacy static method with a lot of logic)
        //Validate() should end the response if object not validated
        userRegisterRequest.Validate(isJson: true, isThrowHttpError: true);

        //Code still reaches here and request does not close
        string test = "hey I'm alive";
   } 
专用异步任务寄存器_v2()
{
//将JSON读取到对象
UserRegisterRequest UserRegisterRequest=Request.ReadBody().FromJson();
//验证对象(具有大量逻辑的遗留静态方法)
//如果未验证对象,则Validate()应结束响应
验证(isJson:true,isThrowHttpError:true);
//代码仍然到达此处,请求未关闭
string test=“嘿,我还活着”;
} 
我可以用中间件解决这个问题吗


谢谢

有两种方法可以终止
请求管道

public class FirstMiddleware
{
    private readonly RequestDelegate _next;
    public FirstMiddleware(RequestDelegate next)
    {
        _next = next;
    }

    public async Task InvokeAsync(HttpContext context)
    {
        await context.Response.WriteAsync($"This is { GetType().Name }");
        //decide whether to invoke line below based on your business logic
        //await _next(context);
        bool isValid = userRegisterRequest.Validate(isJson: true, isThrowHttpError: true); 
      //change userRegisterRequest.Validate to reutrn whether the model is valid
       if(!isValid)
       {
             await context.Response.WriteAsync($"Model is not valid");
       }
       else
       {
             await _next(context);
        }
    }
}
  • 使用
    应用程序。在
    启动中运行
    。配置
  • 不要在
    中间件中调用
    \u下一步(上下文)
    。InvokeAsync
对于您的场景,您可以通过确定是否调用
\u next(context)
来尝试第二个选项

public class FirstMiddleware
{
    private readonly RequestDelegate _next;
    public FirstMiddleware(RequestDelegate next)
    {
        _next = next;
    }

    public async Task InvokeAsync(HttpContext context)
    {
        await context.Response.WriteAsync($"This is { GetType().Name }");
        //decide whether to invoke line below based on your business logic
        //await _next(context);
        bool isValid = userRegisterRequest.Validate(isJson: true, isThrowHttpError: true); 
      //change userRegisterRequest.Validate to reutrn whether the model is valid
       if(!isValid)
       {
             await context.Response.WriteAsync($"Model is not valid");
       }
       else
       {
             await _next(context);
        }
    }
}

这并不能解决我的问题,因为我有许多静态验证器,它们都接受不同方法中的不同参数。所以,我的问题是从静态方法内部关闭管道。您能与我们分享完整的代码来重现您的问题吗?我不确定什么是静态方法以及您将如何调用它。
Register\u v2
是web api方法吗?它将返回
Task
,而不是
Task
。您对客户端的预期结果是什么?您可以尝试在中间件中验证
请求主体
。检查我的更新。是的。您回答的问题是我有不同的对象,我不能为每个对象编写中间件,例如
UserRegisterRequest
。每个WebAPI函数处理不同的场景,此示例适用于
UserRegisterRequest
类型对象,另一个示例可能是
UserRemovalRequest
。我找不到一种方法来与中间件通信,让它知道它不应该深入到下一个中间件,或者只是以某种方式关闭当前响应。您是否考虑过先插入遗留代码,然后再捕获外部中间件?是的,我考虑过。问题是我有其他遗留方法打印到响应流,并且需要在完成后关闭流,而不引发异常。因此,您提出的解决方案确实适用于自然引发异常的错误,但不适用于非异常消息。我认为您不能,因为遗留代码希望以各种方式操纵管道。问题是经典的ASP.NET管道不再存在,正如您所遇到的那样。因此,您的旧代码无法与asp.net核心一起使用。您要么升级旧代码,要么降级到asp.net。您是否有可能对遗留代码进行一些小的更改?类似于将Validate方法重写为返回bool的函数。