ASP.NET Core Exception Handling Tutorial | .NET 8

Поделиться
HTML-код
  • Опубликовано: 2 ноя 2024

Комментарии • 35

  • @VinodKumarGowda
    @VinodKumarGowda 11 месяцев назад +5

    As usual very neat and useful.

    • @juliocasal
      @juliocasal  11 месяцев назад

      Glad you think so!

  • @FB-eb6tx
    @FB-eb6tx 11 месяцев назад +3

    Very nice! Thanks Julio! This kind of videos are awesome!

    • @juliocasal
      @juliocasal  11 месяцев назад +1

      Glad you liked it!

  • @singhtech4938
    @singhtech4938 5 дней назад +2

    Hi @juli , could you please make a video to how we can save these logs into database or in third party logging system like loggly, or scaler ?

    • @juliocasal
      @juliocasal  5 дней назад

      I cover that here: dotnetmicroservices.com

  • @dorlugasigal
    @dorlugasigal 10 месяцев назад +1

    I wonder regarding the validationException you had there,
    I assume fluentValidation
    Isnt it having some performance hit throwing a validation exception rather then just getting the validation result object and look at the IsValid property?
    Its true that its more clean without the if statement, but is it more performant to enter a try catch statement for every place you have validation?

    • @juliocasal
      @juliocasal  10 месяцев назад

      Which validation exception? Not using FluentValidation here.

  • @bloopers2967
    @bloopers2967 11 месяцев назад +1

    Great and detailed information. Thanks

    • @juliocasal
      @juliocasal  11 месяцев назад +1

      Glad it was helpful!

  • @coding-in
    @coding-in 6 месяцев назад +1

    Hi Julio, thank you for this video especially for writing tuple i just found out in that way.
    Anyway, how to get the request body? It can help us to identify or reproduce the error.

    • @juliocasal
      @juliocasal  6 месяцев назад

      Glad to help. Have not tried getting the request body.

    • @coding-in
      @coding-in 6 месяцев назад

      @@juliocasal iv tried this:
      string requestBody = await new StreamReader(httpContext.Request.Body).ReadToEndAsync();
      httpContext.Request.Body.Position = 0;
      but always return null or empty.

    • @coding-in
      @coding-in 6 месяцев назад

      @@juliocasal
      finally i found the solotution to get request body using:
      using (var reader = new StreamReader(httpContext.Request.Body))
      {
      // Reset the stream position to the beginning after enabling buffering
      httpContext.Request.Body.Seek(0, SeekOrigin.Begin);
      // Read the request body as a string
      var requestBody = await reader.ReadToEndAsync();
      // Process the request body as needed
      Console.WriteLine(requestBody);
      }
      and set context.Request.EnableBuffering(); in program.cs or middleware.

  • @2u1u
    @2u1u 11 месяцев назад +1

    @julio prior to dotnet 8, would we just add a middleware try/catch and handle it that way?

    • @juliocasal
      @juliocasal  11 месяцев назад +1

      Prior to .NET 8, you can do this: ruclips.net/video/nycII-Cec9I/видео.html

  • @pt_trainer9244
    @pt_trainer9244 11 месяцев назад +1

    What's the best way to handle exceptions in microservices architecture?
    Can I create a shared project and reference the custom exception classes in each microservice?
    Is there a better approach ?
    Thanks

    • @juliocasal
      @juliocasal  11 месяцев назад +1

      Need to think a bit about this. Should be easy enough to have a global handler on each microservice, since each one might have their own custom exceptions.

    • @DK-vh5kt
      @DK-vh5kt 11 месяцев назад +1

      A Nuget package could be also ok for this purpose to handle all logs to a service like elasticsearch .

  • @brijesh1596
    @brijesh1596 11 месяцев назад

    Really useful stuff.Thanks!
    btw is that code completion coming from copilot or some VS extension?

    • @juliocasal
      @juliocasal  11 месяцев назад +1

      Thanks! That's Copilot.

  • @DisturbedNeo
    @DisturbedNeo 9 месяцев назад +2

    It's cool that the .NET team is formally supporting this commonly used pattern, but I don't like that it always catches _all_ exceptions, and the only way to handle specific types of exception is via reflection.
    If I could specify an exception type as a generic type argument for the "UseExceptionHandler" method, something like "UseExceptionHandler()", that would be ideal. Maybe in .NET 9.

    • @juliocasal
      @juliocasal  9 месяцев назад

      Great feedback! If you get a chance, you can always provide direct feedback here: github.com/dotnet/aspnetcore

  • @sudidav
    @sudidav 11 месяцев назад +1

    Thanks for sharing. What happens if I have like 20 services?Does that means I will have to chain the AddProblemDetails() and the GlobalExpection extension on every service injection? How can I handle that?

    • @juliocasal
      @juliocasal  11 месяцев назад

      Could you clarify what you mean by "services"? You mean application services in your single app? Or you mean multiple microservices?

    • @sudidav
      @sudidav 11 месяцев назад

      I mean if I have multiple repos services, will I e chaining the AddProblemDetails() and the GlobalExpection for each one of them or is there a better way to handle that? @@juliocasal

    • @2u1u
      @2u1u 11 месяцев назад

      ​@@juliocasalI believe because you chained the addproblemdetails to a addsignleton it looks like thr exception handler is attached to that Singleton.
      I believe it's attached to the service collection instead but the chaining looks like it's scoping the exception to the Singleton.

  • @GDWR2
    @GDWR2 11 месяцев назад +1

    I don't like the idea of mapping all errors of one type (unless you have one custom type specially for this) into an error message, especially when it's a stdlib exception. It feels like info could be leaked accidently and it should be done within the api endpoint handler explicitly. This also means the valid responses (I am also meaning the errors) are contained in one place and accessible to developers rather than hidden and magic.
    Thanks for the video, was good.

    • @juliocasal
      @juliocasal  11 месяцев назад +2

      That's fair. I used standard exceptions just to keep things simple, but you can use all sorts of custom exceptions there. You have full control of how each exception is mapped, so there should be no accidental leaks. Also, using a global exception handler is a very standard practice, so I don't think it's hidden and magic.
      Glad you liked the video!

  • @ivanaradenkovic4638
    @ivanaradenkovic4638 7 месяцев назад +1

    Thanks :)