Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

160
Visualizações
How does a middleware automatically handle exceptions throw by the middleware below it?

Let's say we have the following code:

public class MiddleWareFirst
{
   private RequestDelegate next;

   public MiddleWareFirst(RequestDelegate nextDelegate) {
      next = nextDelegate;
   }
   public async Task Invoke(HttpContext context) {
      await context.Response.WriteAsync("Start \n");

      if (next != null) {
         await next(context);
      }
   }
}

public class MiddleWareSecond
{
   private RequestDelegate next;

   public MiddleWareSecond(RequestDelegate nextDelegate) {
      next = nextDelegate;
   }
   public async Task Invoke(HttpContext context) {
      if (next != null) {
         await next(context);    // <---- should throw the exception caused by MiddleWareThird
      }
   }
}

public class MiddleWareThird
{
   private RequestDelegate next;

   public MiddleWareThird(RequestDelegate nextDelegate) {
      next = nextDelegate;
   }
   public async Task Invoke(HttpContext context) {
      throw new System.Exception();
   }
}

and build the pipeline as:

// Configure method in startup.cs
app.UseMiddleware<MiddleWareFirst>();
app.UseMiddleware<MiddleWareSecond>();
app.UseMiddleware<MiddleWareThird>();

You can see MiddleWareThird throw an exception. When I run the application, I can see the output of the webpage is "Start", which confuses me, shouldn't it be a 500 error?

Because we know that await can throw the exception of a task. So for MiddleWareSecond, since it doesn't use try catch block to catch the exception, the task that return to MiddleWareFirst should contain the exception internally. Since MiddleWareFirst doesn't use try catch block neither, so the exception will be propagated to the web server which should generate a 500 error page. But the output is normal, so how does the exception thrown in MiddleWareThird magically get swallowed?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

exception will be propagated to the web server which should generate a 500 error page

As you have already started response with await context.Response.WriteAsync("Start \n"); status code 200 is written to the response stream as per HTTP protocol you send status in the first line.

If an exception will be thrown before WriteAsync you will get 500 from the server as you expect.

This is handled by aspnet core infrastructure, also in logs you may notice that the exception is logged.

Of course, you can have custom middleware to handle exceptions like DeveloperExceptionPageMiddleware

public async Task Invoke(HttpContext context)
{
    try
    {
        await _next(context);
    }
    catch (Exception ex)
    {
        //...
    }
}

https://github.com/dotnet/aspnetcore/blob/f74c6f12d868b8e5c91e85f1a2bcbd1134ed5fd5/src/Middleware/Diagnostics/src/DeveloperExceptionPage/DeveloperExceptionPageMiddleware.cs#L90-L127

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda