ASP.NET Core Exception Handling Tutorial | .NET 8

  Рет қаралды 14,607

Julio Casal

Julio Casal

Күн бұрын

Пікірлер: 35
@VinodKumarGowda
@VinodKumarGowda 11 ай бұрын
As usual very neat and useful.
@juliocasal
@juliocasal 11 ай бұрын
Glad you think so!
@FB-eb6tx
@FB-eb6tx Жыл бұрын
Very nice! Thanks Julio! This kind of videos are awesome!
@juliocasal
@juliocasal Жыл бұрын
Glad you liked it!
@singhtech4938
@singhtech4938 13 күн бұрын
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 13 күн бұрын
I cover that here: dotnetmicroservices.com
@bloopers2967
@bloopers2967 11 ай бұрын
Great and detailed information. Thanks
@juliocasal
@juliocasal 11 ай бұрын
Glad it was helpful!
@dorlugasigal
@dorlugasigal 10 ай бұрын
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.
@2u1u
@2u1u 11 ай бұрын
@julio prior to dotnet 8, would we just add a middleware try/catch and handle it that way?
@juliocasal
@juliocasal 11 ай бұрын
Prior to .NET 8, you can do this: kzbin.info/www/bejne/pKrGenxjeMqWb6s
@coding-in
@coding-in 6 ай бұрын
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.
@sudidav
@sudidav 11 ай бұрын
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.
@pt_trainer9244
@pt_trainer9244 11 ай бұрын
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 ай бұрын
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 ай бұрын
A Nuget package could be also ok for this purpose to handle all logs to a service like elasticsearch .
@GDWR2
@GDWR2 11 ай бұрын
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 ай бұрын
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!
@brijesh1596
@brijesh1596 11 ай бұрын
Really useful stuff.Thanks! btw is that code completion coming from copilot or some VS extension?
@juliocasal
@juliocasal 11 ай бұрын
Thanks! That's Copilot.
@ivanaradenkovic4638
@ivanaradenkovic4638 7 ай бұрын
Thanks :)
@juliocasal
@juliocasal 7 ай бұрын
Welcome!
@DisturbedNeo
@DisturbedNeo 9 ай бұрын
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
The New Way To Build Cloud Native Applications With .NET 8
46:51
ТЫ В ДЕТСТВЕ КОГДА ВЫПАЛ ЗУБ😂#shorts
00:59
BATEK_OFFICIAL
Рет қаралды 3,3 МЛН
The IMPOSSIBLE Puzzle..
00:55
Stokes Twins
Рет қаралды 97 МЛН
小丑揭穿坏人的阴谋 #小丑 #天使 #shorts
00:35
好人小丑
Рет қаралды 48 МЛН
Who's spending her birthday with Harley Quinn on halloween?#Harley Quinn #joker
01:00
Harley Quinn with the Joker
Рет қаралды 22 МЛН
Golang: The Last Interface Explanation You'll Ever Need
17:58
Flo Woelki
Рет қаралды 19 М.
The Right Way To Return API Errors in .NET
10:40
Nick Chapsas
Рет қаралды 44 М.
ASP.NET Core tutorial for Beginners | NEW .NET 8
39:15
Raw Coding
Рет қаралды 17 М.
5 Rules For DTOs
17:56
Ardalis
Рет қаралды 44 М.
VS Code Tutorial For C# Developers
39:22
Julio Casal
Рет қаралды 30 М.
The Logging Everyone Should Be Using in .NET
15:34
Nick Chapsas
Рет қаралды 81 М.
How To Build Resilient Cloud Applications With .NET 8
32:10
Julio Casal
Рет қаралды 4,3 М.
"Stop Using Async Await in .NET to Save Threads" | Code Cop #018
14:05
Asp.net core middleware explained
40:05
Tural Suleymani
Рет қаралды 1,4 М.
ТЫ В ДЕТСТВЕ КОГДА ВЫПАЛ ЗУБ😂#shorts
00:59
BATEK_OFFICIAL
Рет қаралды 3,3 МЛН