Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/198.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Web API核心3.1 ExceptionHandler中间件抛出';无法写入关闭的流';在写入HttpContext.Response时_C#_Asp.net Core Webapi_.net Core 3.1 - Fatal编程技术网

C# Web API核心3.1 ExceptionHandler中间件抛出';无法写入关闭的流';在写入HttpContext.Response时

C# Web API核心3.1 ExceptionHandler中间件抛出';无法写入关闭的流';在写入HttpContext.Response时,c#,asp.net-core-webapi,.net-core-3.1,C#,Asp.net Core Webapi,.net Core 3.1,我有一个简单的WebAPI核心v3.1,我试图在其中全局处理异常。在遵循这个答案之后,下面是我的代码 app.UseExceptionHandler(appBuilder => appBuilder.Run(async context => { var exceptionHandlerPathFeature = context.Features.Get<IExceptionHandlerPathFeature>(); var exception =

我有一个简单的WebAPI核心v3.1,我试图在其中全局处理异常。在遵循这个答案之后,下面是我的代码

 app.UseExceptionHandler(appBuilder => appBuilder.Run(async context =>
 {
     var exceptionHandlerPathFeature = context.Features.Get<IExceptionHandlerPathFeature>();
     var exception = exceptionHandlerPathFeature.Error;

     var result = JsonConvert.SerializeObject(new { error = exception.Message });
     context.Response.ContentType = "application/json";
     await context.Response.WriteAsync(result);
 }));
app.UseExceptionHandler(appBuilder=>appBuilder.Run(异步上下文=>
{
var exceptionHandlerPathFeature=context.Features.Get();
var exception=exceptionHandlerPathFeature.Error;
var result=JsonConvert.SerializeObject(新的{error=exception.Message});
context.Response.ContentType=“应用程序/json”;
wait context.Response.WriteAsync(结果);
}));
我得到的错误位于
context.Response.WriteAsync(result)是:

System.ObjectDisposedException:无法访问封闭流

我很确定我错过了一些基本的东西,但我无法弄明白这一点


我基本上需要在异常发生时将响应包装到对象中。

看起来其他人(另一个middlewhare)已经关闭了流

在.net内核中,排序很重要。请记住,在您共享的链接中:

重要提示:记住在使用MVC(或.NETCore3中的UseRouting)之前添加它,因为顺序很重要

因此,在任何其他
Middlewhate
和/或配置声明之前声明
UseExceptionHandler

将多个请求委托与使用链接在一起。next参数表示管道中的下一个委托。不调用下一个参数可以使管道短路。您通常可以在下一个委托之前和之后执行操作,如下例所示:

UseExceptionHandler是添加到管道中的第一个中间件组件。因此,异常处理程序中间件将捕获以后调用中发生的任何异常

第一次运行终止管道。因此,您可以做一些工作,但最终当第一次运行时,响应将关闭


请为我们共享更多代码以提供更多帮助。

这意味着您在响应到达ExceptionHandler MiddleWare之前处理它。您是对的。我有另一个封装响应的中间件。知道如何将这两个分开吗?在调用任何其他中间件之前调用异常句柄。它应该解决这个问题issue@bit如链接答案所示,配置顺序很重要。您是否在
UseRouting
之前添加了
app.UseExceptionHandler
?配置代码是什么样子的?我已经在做了。我首先连接了异常处理中间件,然后连接了另一个中间件。在您的评论中,您提到了另一个封装响应的中间件。这不正确吗?我需要在两个中间件中更新响应。在发布了最后一个中间件应该实际更新响应后,我最终将这两种方法结合起来。谢谢
public class Startup
{
    public void Configure(IApplicationBuilder app)
    {
        app.Use(async (context, next) =>
        {
            // Do work that doesn't write to the Response.
            await next.Invoke();
            // Do logging or other work that doesn't write to the Response.
        });

        app.Run(async context =>
        {
            await context.Response.WriteAsync("Hello from 2nd delegate.");
        });