Elegant Global Error Handling Using Middleware In ASP.NET Core

  Рет қаралды 89,646

Milan Jovanović

Milan Jovanović

Күн бұрын

Пікірлер: 221
@MilanJovanovicTech
@MilanJovanovicTech 2 жыл бұрын
Get the source code for this video for FREE → the-dotnet-weekly.ck.page/error-handling Want to master Clean Architecture? Go here: bit.ly/3PupkOJ Want to unlock Modular Monoliths? Go here: bit.ly/3SXlzSt
@pawegruba4719
@pawegruba4719 2 жыл бұрын
I think it might be worth to mention something about Exception Filters, I know they are a bit different but in general are also used to handle exceptions
@MilanJovanovicTech
@MilanJovanovicTech 2 жыл бұрын
I haven't used them in practice to be honest, Middleware is what I default to
@bloopers2967
@bloopers2967 Жыл бұрын
​@@MilanJovanovicTechI use exception filter all the time, you just have more content in the exception filter than exception middleware because they are called later after the pipelines
@Uncle-Dee
@Uncle-Dee 8 ай бұрын
Thank you Milan... This was a very clear and beautiful explanation.
@MilanJovanovicTech
@MilanJovanovicTech 8 ай бұрын
Glad it was helpful!
@88spaces
@88spaces 9 ай бұрын
Thank you, Milan. I'm using your example in a new project. It's already helpful.
@MilanJovanovicTech
@MilanJovanovicTech 9 ай бұрын
You are welcome!
@sagarchowdhury2493
@sagarchowdhury2493 2 жыл бұрын
Hi Milan, this is a great tutorial for handling error globally. Of course, I would love to know some more complex functionality which we can make place inside some other subsequent middleware to make our code better.
@MilanJovanovicTech
@MilanJovanovicTech 2 жыл бұрын
Good point Sagar, I'll see what I can do
@AmjadKhateeb
@AmjadKhateeb 2 жыл бұрын
Thanks for the great video. I think using the status code again in the body of the response is unnecessary duplicate. I also prefer to add a RefID in the response body which is linked to the error log. So, in case that the client contacted the support team, they can easily find the original error log.
@MilanJovanovicTech
@MilanJovanovicTech 2 жыл бұрын
It's not a unnecessary/duplicate. Why would the consumer of your API have to see the HTTP response code? We're giving them the ability to take a look at the response body, and have all of the relevant information.
@AmjadKhateeb
@AmjadKhateeb 2 жыл бұрын
​@@MilanJovanovicTech There is an http status in the response header
@MilanJovanovicTech
@MilanJovanovicTech 2 жыл бұрын
@@AmjadKhateeb www.rfc-editor.org/rfc/rfc7807
@AmjadKhateeb
@AmjadKhateeb 2 жыл бұрын
​@@MilanJovanovicTech Thanks. The explanation is very clear in the document. I didn't know that there is such a standard.
@James66662
@James66662 8 ай бұрын
You make concepts very easy
@MilanJovanovicTech
@MilanJovanovicTech 8 ай бұрын
Thank you!
@nikuniku1235
@nikuniku1235 7 ай бұрын
how simple and efficient, really good job.
@MilanJovanovicTech
@MilanJovanovicTech 7 ай бұрын
Thank you!
@zikkrype
@zikkrype 2 жыл бұрын
Like. Unless you need to have additional data from failed request middleware is a good option. In other cases consider exception filters.btw: may be a nice topic middleware vs filter
@MilanJovanovicTech
@MilanJovanovicTech 2 жыл бұрын
That's certainly a nice topic to explore!
@asadnaeem123
@asadnaeem123 4 күн бұрын
Awesome video, highly recomended to others
@MilanJovanovicTech
@MilanJovanovicTech 4 күн бұрын
Much appreciated!
@ssushovan
@ssushovan Жыл бұрын
Excellent video! But, could you tell what's the benefit of using 3rd approach here?
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
Strong typing
@francoislepron2301
@francoislepron2301 2 жыл бұрын
Yes, I have learned something new, but I can not tell that I master it. Your audience is not only composed by experts, but also by intermediate developers. I would be really interested in more details explanations or links on the documentations where we can find what you don't explain, because you assume that developers already know these fundamental concepts. It is an hard work, but this is how Brackeys has reached more than one million of followers for Unity3D tutorials.
@MilanJovanovicTech
@MilanJovanovicTech 2 жыл бұрын
Hey Francois, I really appreciate the feedback. To be honest, intermediate-to-senior level engineers are my intended target audience at the moment. Until I can master the skill of being able to explain concepts in even simpler terms. For documentation links, that's a great suggestion. I'll try to improve on that front.
@francoislepron2301
@francoislepron2301 2 жыл бұрын
@@MilanJovanovicTech Many thanks.
@yaKaiZen
@yaKaiZen 2 жыл бұрын
The 3rd option was new learning for me and enjoyed the custom JSON response throwing most! Thanks, ​ @Milan Jovanović for your community effort!
@MilanJovanovicTech
@MilanJovanovicTech 2 жыл бұрын
You're most welcome! 😁
@richardhaughton9633
@richardhaughton9633 2 жыл бұрын
Hi Milan, Great Tutorial. Could you make a tutorial on how to handle Errors on the client side. For example for a http client who can have a T Resposne or a problemDetails. I struggle to find an elegant way to handle both type of response. And maybe also how to handle it in an MVC or even better a blazor app. Thanks!
@richardhaughton9633
@richardhaughton9633 2 жыл бұрын
Also, why not return an application/json+problem as response content type?
@MilanJovanovicTech
@MilanJovanovicTech 2 жыл бұрын
- why not return an application/json+problem as response content type? I missed that, to be honest - And maybe also how to handle it in an MVC or even better a blazor app You need some way to create a hook when an error happens, and you need to make your error responses generic for the entire API to be able to do that. If you always return a ProblemDetails, on the API consumer side that becomes doable.
@krishanthadharmasiri5266
@krishanthadharmasiri5266 Жыл бұрын
Great video, thanks , is there any particular reason to add the middleware as Transient why not scoped?
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
Scoped only makes sense if you need a scoped service inside
@xDeltaDoggox
@xDeltaDoggox 3 ай бұрын
​@@MilanJovanovicTech Could you please elaborate on "if you need a scoped service inside"?🤔😅
@nayanchoudhary4353
@nayanchoudhary4353 2 жыл бұрын
@milan I wish you had explained the sequence of middlewares invocation; how order matters
@MilanJovanovicTech
@MilanJovanovicTech 2 жыл бұрын
Doesn't the visual explanation at the start illustrate the concept? 🤔
@nayanchoudhary4353
@nayanchoudhary4353 2 жыл бұрын
@@MilanJovanovicTech Kind of. But what happens when I mix IMiddleware and non IMiddleware implementation?
@MilanJovanovicTech
@MilanJovanovicTech 2 жыл бұрын
@@nayanchoudhary4353 Both approaches work, the order they are registered in is the order in which they execute
@musacj
@musacj 2 жыл бұрын
Great tutorial Milan. I wonder if it would make sense to have an error tracking id, like a GUID or any unique id. That is, to pass it to LogError and also on the to the Title or Detail property of ProblemDetails object.
@MilanJovanovicTech
@MilanJovanovicTech 2 жыл бұрын
We can add a middleware that sets a Request Id header for each request
@musacj
@musacj 2 жыл бұрын
@@MilanJovanovicTech thanks for the reply. I'll try that out
@oneillci
@oneillci 2 жыл бұрын
Instead of manually serializing and setting the content type, I like to just use _await context.Response.WriteAsJsonAsync(problem);_ Really enjoying your videos BTW
@MilanJovanovicTech
@MilanJovanovicTech 2 жыл бұрын
You're writing to a Response which is in memory. Nothing too asynchronous there.
@velebyte
@velebyte 2 жыл бұрын
@@MilanJovanovicTech What he is saying, why do serialization and setting the content type with those two lines of code, when the WriteAsJsonAsync method does both of those things? Great videos btw, and I love the WoW collection in the back
@stevehiggin
@stevehiggin 2 жыл бұрын
My comment seemed to be deleted before, but the StatusCodes class in the Microsoft.AspNetCore.Http namespace already has fields that are ints (no casting) for all the Http response codes you might want to return, and it also has the code with the description, which is good when dealing with the less common response codes.
@MilanJovanovicTech
@MilanJovanovicTech 2 жыл бұрын
Absolutely Steve, that's what I usually use with Swagger's [ProducesResponseType]
@abdelrahmananwar885
@abdelrahmananwar885 Жыл бұрын
i liked your way of explanation, thank you.
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
You are most welcome
@lazziemunetsi1498
@lazziemunetsi1498 Жыл бұрын
Great stuff, thanks very much Milan.👏
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
Glad you liked it!
@Retro80smusic
@Retro80smusic Жыл бұрын
Great video Milan! Thank you for presenting these important topics. One observation: Instead of using two statements to write the json body in the response you could merge them in one statement. await context.Response.WriteAsJsonAsync(errorResponse); Also, why do you need to make the middleware a transient service? Can you please explain, I was sure this worked without needing to do this
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
With the IMiddleware approach you do. Otherwise it's fine.
@vicky2118
@vicky2118 Жыл бұрын
Great video One Question Why you registered GlobalExceptionHandlingMiddleware as Transient not Scoped?
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
Since I don't inject any Scoped services, it can be Transient
@Marcos-XurryPET
@Marcos-XurryPET 9 ай бұрын
Nice work, Milan Jovanovic!!! How to use middelware to make filters, orderby and pagination???, Thank u. Regards.
@MilanJovanovicTech
@MilanJovanovicTech 9 ай бұрын
No idea
@adilbangush5014
@adilbangush5014 Жыл бұрын
Can you make one video about Extending ProblemDetails, using with the above global error handling in which a developer can include the custom payload of an error information.
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
ProblemDetails already has an Errors property which is a dictionary, and you can extend it freely
@AnkurPriyaDarshi-j3c
@AnkurPriyaDarshi-j3c 7 ай бұрын
In the middleware everything performs according to their presence in the sequence. Because we know that every component will execute after their prior component. In this video, you are putting exception handling(obviously as a middleware component) after executing other components of middleware. Have you think, it should be introduced at initial levels in the Program.cs
@MilanJovanovicTech
@MilanJovanovicTech 7 ай бұрын
You're welcome to apply the ordering you want. Most middleware doesn't throw exceptions until we get to our actual request handler
@upendrakumarpandey2838
@upendrakumarpandey2838 7 ай бұрын
This is very good example of
@MilanJovanovicTech
@MilanJovanovicTech 7 ай бұрын
Thanks!
@kodindoyannick5328
@kodindoyannick5328 9 ай бұрын
Great Great content! Thank you so much Milan.
@MilanJovanovicTech
@MilanJovanovicTech 9 ай бұрын
Glad you liked it!
@samehkeshta89
@samehkeshta89 2 жыл бұрын
Great tutorial, thanks Milan.
@MilanJovanovicTech
@MilanJovanovicTech 2 жыл бұрын
Glad you liked it!
@davidemmanuel3001
@davidemmanuel3001 2 жыл бұрын
This is cool. Please can you drop the github public repo for your videos? It will really be helpful
@MilanJovanovicTech
@MilanJovanovicTech 2 жыл бұрын
I share the code with my Patreon supporters, but I'm sure you can dig up something similar on my github: github.com/m-jovanovic
@AtenaMohammadvandAzad
@AtenaMohammadvandAzad 4 ай бұрын
Great Tutorial ! Thanks😍
@MilanJovanovicTech
@MilanJovanovicTech 4 ай бұрын
Happy to help!
@mesutdemirci7933
@mesutdemirci7933 Жыл бұрын
Great tutorial Milan.thanks
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
You're welcome, glad you liked it!
@Nicetrycutiepie
@Nicetrycutiepie 2 жыл бұрын
Thank you, Very useful, i learned lot from this.
@MilanJovanovicTech
@MilanJovanovicTech 2 жыл бұрын
Glad it was helpful!
@coder3123
@coder3123 9 ай бұрын
Hey, can you shed some light on use of AddTransient vs AddScoped in this case?
@MilanJovanovicTech
@MilanJovanovicTech 9 ай бұрын
learn.microsoft.com/en-us/dotnet/core/extensions/dependency-injection#service-lifetimes
@Valoo24
@Valoo24 Жыл бұрын
Great Tutorial ! Thanks :)
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
Glad it was helpful!
@MilosBencek
@MilosBencek Жыл бұрын
excellent video Milan (y) Thanks
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
Glad you think so 😁
@filthkyory
@filthkyory 8 ай бұрын
Milan... How to configure an inline watch in your visual studio, could you tell us?
@MilanJovanovicTech
@MilanJovanovicTech 8 ай бұрын
ReSharper
@filthkyory
@filthkyory 8 ай бұрын
@@MilanJovanovicTech thanks 🙏🏻👍🏻
@manojkumar-gt6ng
@manojkumar-gt6ng 10 ай бұрын
Can you provide a video on c# creating custom attributes for class, method and variable
@MilanJovanovicTech
@MilanJovanovicTech 10 ай бұрын
Great suggestion!
@FalcoSparverius
@FalcoSparverius Жыл бұрын
Hi! thanks for your videos, are amazing. I have a question, (im sorry if its a bad one, I am a begginer ) I could add some others Catchs with particular exceptions and handle them each one in a particular way, with differentes codes an differentes ProblemDetails, or thats not the use for this kind of middleware? thanks!
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
No bad question on this channel :) Yes, you can definitely do that. A global exception handler based on exception type. 👌
@tayyabshaikh3286
@tayyabshaikh3286 9 ай бұрын
Awesome video, great explanation!!
@MilanJovanovicTech
@MilanJovanovicTech 9 ай бұрын
Glad you liked it!
@GarethDoherty1985
@GarethDoherty1985 2 жыл бұрын
Weird that your video appeared when it did. I had already applied option 2 to my API. But randomly, when I explicitly throw, or an unhandled error occurs, the middleware isn't catching it =/ I can see I enter the middleware fine, but it never enters the catch. Can you think of any reason why?
@MilanJovanovicTech
@MilanJovanovicTech 2 жыл бұрын
Which exception are you catching?
@GarethDoherty1985
@GarethDoherty1985 2 жыл бұрын
@@MilanJovanovicTech Just the base "Exception" exact same code as you and everyone else on the internet has done when writing about this :D
@MilanJovanovicTech
@MilanJovanovicTech 2 жыл бұрын
@@GarethDoherty1985 Could be something around invoking the RequestDelegate
@GarethDoherty1985
@GarethDoherty1985 2 жыл бұрын
@@MilanJovanovicTech private readonly RequestDelegate _next; public ExceptionHandlingMiddleware(RequestDelegate next) { _next = next; } public async Task InvokeAsync(HttpContext context) { try { await _next.Invoke(context); } catch (Exception ex) { await HandleExceptionAsync(ex, context); } } Anything weird here you see? I see it goes into the try, go elsewhere in code get or throw error, but never goes inside the catch!
@MilanJovanovicTech
@MilanJovanovicTech 2 жыл бұрын
@@GarethDoherty1985 That is entirely wild 😱 I have no idea why it's behaving like that
@hamlet.h.hakobyan
@hamlet.h.hakobyan Жыл бұрын
I think, if we will put this middleware at the start of the pipeline we can log unhandled exceptions from other middleware
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
That is indeed the case
@AmirHashemZadeh
@AmirHashemZadeh Жыл бұрын
it was simple and awesome.
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
Glad you liked it!
@talwinter
@talwinter Жыл бұрын
I am trying to catch the Model Validation error where I get an 400 bad request when one of the fields have incorrect data like required validation or regex validation but I can't catch it a log it. I can use a middleware and get the request but the response returns as 200 status code and not 400 bad request so I can't differ between requests ... any ideas?
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
Place your custom middleware at the top of the middleware chain, so any exception thrown is propagated to it. Secondly, are you sure Model Validation throws an exception at all?
@talwinter
@talwinter Жыл бұрын
@@MilanJovanovicTech well, in the middleware I just use next.invoke and then if I check the Context.Reaponse.Body, I get the 400 status code on the validation error which sets as bad request. The problem is that I need to open a new MemoryStream for every request and then check for the status code and that's a bad practice that can cause a huge memory leak. I didn't find anything more elegant.
@mohamedroshdy4916
@mohamedroshdy4916 2 жыл бұрын
very clear and good, but by this way all errors will have the same response can you indicate this point to me , what I should do to describe each type of error ..thanks
@MilanJovanovicTech
@MilanJovanovicTech 2 жыл бұрын
You have two options: - Use the middleware only for 500 internal server + return meaningful errors in controller - Throw custom exceptions (NotFound, BadRequest) from your code, and handle them in middleware and return appropriate response code
@mohamedroshdy4916
@mohamedroshdy4916 2 жыл бұрын
@@MilanJovanovicTech, oh right, thanks
@mohamedroshdy4916
@mohamedroshdy4916 2 жыл бұрын
@@MilanJovanovicTech ,please can you have an example or demo to view this point,I understand it but need to show in practice
@DarshanaHashendra
@DarshanaHashendra 2 жыл бұрын
Great stuff. keep up the good work.
@MilanJovanovicTech
@MilanJovanovicTech 2 жыл бұрын
Thanks a lot! 😁
@sunzhang-d9v
@sunzhang-d9v Жыл бұрын
ExceptionHandlingMiddleware switch need to add OrderNotFoundException, Write it every time?
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
You can create a base NotFoundException and catch that one
@sajithsageer182
@sajithsageer182 Жыл бұрын
Nice one as usual😀
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
Thanks again!
@nullentrophy
@nullentrophy 6 ай бұрын
What do you think about Exception filter instead of middleware for error handling?
@MilanJovanovicTech
@MilanJovanovicTech 6 ай бұрын
Middleware is usually simpler
@monsieurnet
@monsieurnet Жыл бұрын
I followed the 2nd way in your tutorial step by step but got this error : "Unable to resolve service for type 'Microsoft.Extensions.Logging.ILogger' while attempting to activate 'WebApi.Middleware.GlobalExceptionHandlingMiddleware'". So i tried to add this : "builder.Services.AddTransient(typeof(ILogger), typeof(Logger)); " in the Program.cs but still not working. Do you have a solution ?
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
services.AddLogger? 🤔
@monsieurnet
@monsieurnet Жыл бұрын
@@MilanJovanovicTech oh gosh i didn't know there was such a service. Thank you bro ;-D
@Zisane21
@Zisane21 8 ай бұрын
Hi Milan, if you place the middleware AFTER UseAuthorization\UseAuthentication then it won't cacth exceptions thrown by an AuthHanlder class for example. Shouldn't it go before UseAuthorization\UseAuthentication ?
@MilanJovanovicTech
@MilanJovanovicTech 8 ай бұрын
Yes
@Zisane21
@Zisane21 8 ай бұрын
@@MilanJovanovicTech Why with the 3rd option did you register the ExceptionMiddleware with Transient scope? ChatGPT says it should be singleton 🙂
@birukayalew3862
@birukayalew3862 2 жыл бұрын
Hi Milan as Always this is helpful ... How can we ensure the exact status code and description for problem detail ?
@MilanJovanovicTech
@MilanJovanovicTech 2 жыл бұрын
What do you mean by "exact"? Do you want the status code to be dynamic based on the exception?
@birukayalew3862
@birukayalew3862 2 жыл бұрын
Yes i mean the status code
@MilanJovanovicTech
@MilanJovanovicTech 2 жыл бұрын
@@birukayalew3862 We have to catch the specific exception in the middleware, and set the appropriate status code
@namratamatkar7638
@namratamatkar7638 Жыл бұрын
System.InvalidOperationException: 'An error occurred when configuring the exception handler middleware. Either the 'ExceptionHandlingPath' or the 'ExceptionHandler' property must be set in 'UseExceptionHandler()'. Alternatively, set one of the aforementioned properties in 'Startup.ConfigureServices' as follows..I am facing this issue
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
I have no idea how you got that
@IroshanaRavishan
@IroshanaRavishan 6 күн бұрын
Really, it is a great content...
@MilanJovanovicTech
@MilanJovanovicTech 5 күн бұрын
Thanks!
@СашаТкаченко-и4б
@СашаТкаченко-и4б Жыл бұрын
Thanks a lot 🎉
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
Always welcome
@surenrdashrestha2831
@surenrdashrestha2831 2 жыл бұрын
Hey thanks for sharing this. I have one question. I am using global exception handler in my project but some scenario i need to keep going even if i have exception. For example i have Mongo Database and SQL Database, If mongo fails for some reason (wrong conn string) and It comes to exception however i dont want to send back 500 status code in response because i have SQL database to query and send back data to the client. Please advise.
@MilanJovanovicTech
@MilanJovanovicTech 2 жыл бұрын
Well you need to handle this with some sort of decorator on your data access code
@BiaoTV
@BiaoTV 2 жыл бұрын
Hi As a note, on the blog its missing the third option code.
@MilanJovanovicTech
@MilanJovanovicTech 2 жыл бұрын
Isn't it this: www.milanjovanovic.tech/blog/3-ways-to-create-middleware-in-asp-net-core#adding-factory-based-middleware
@ramonventoso
@ramonventoso Жыл бұрын
What about aspnetcore web apps. There's a if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.UseExceptionHandler("/Home/Error"); app.UseStatusCodePagesWithRedirects("/Home/Error/{0}"); } code to add. Do you have a video touching on this ? Thanks
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
I don't, no
@shakib4845
@shakib4845 Жыл бұрын
nice video but just for very beginners! you could dig more into various ways of global error handling and come up with a nice fancy way dude :)
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
Do we need anything more complicated than the basics?
@MolotovClipes
@MolotovClipes Жыл бұрын
When i do return BadRequest this code dont catch... Is correctly ?
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
Yes
@voltairend
@voltairend Жыл бұрын
Thank you
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
You're welcome!
@maxiklife
@maxiklife 2 жыл бұрын
Awesome content
@MilanJovanovicTech
@MilanJovanovicTech 2 жыл бұрын
I'm glad you liked it :)
@Busk1
@Busk1 2 жыл бұрын
What about "UseExceptionHandler" ? It's cleaner for me to use "UseExceptionHandler", so you don't have to add the middleware to the serviceCollection and you dont have to put that try..catch block but good approaches 😁😁
@MilanJovanovicTech
@MilanJovanovicTech 2 жыл бұрын
That's also a nice option, definitely
@sabajangidze
@sabajangidze Жыл бұрын
Hello,1:18 three middleware is just an example or it is 3 middleware between request and respone?
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
It's one middleware - implement in three different ways
@sabajangidze
@sabajangidze Жыл бұрын
@@MilanJovanovicTech thank for you answer
@catalyysst
@catalyysst Жыл бұрын
Mine seems to not have triggered the exception even when it threw an exception
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
How so?
@moon5964
@moon5964 5 ай бұрын
Interesting, y set ContentType after sending response? that might works for you:(
@MilanJovanovicTech
@MilanJovanovicTech 5 ай бұрын
Mistake, fixed that in post
@osherhajaj8565
@osherhajaj8565 2 жыл бұрын
On minute 0:19 you say the concept of middleware in asp.net core but concepts (e.g middleware) are framework agnostic.
@MilanJovanovicTech
@MilanJovanovicTech 2 жыл бұрын
"the concept of middleware in asp.net core" is obviously not framework agnostic That's why I said it like that and not just "the concept of middleware"
@osherhajaj8565
@osherhajaj8565 2 жыл бұрын
There is no such thing as concept x in framework y. Middleware in .net are not different at the concept level from nestjs middleware and so on. If you want to talk about a concept it is important to note that the framework is insignificant.
@regestea
@regestea 2 жыл бұрын
don't forget to use sentry in your project
@MilanJovanovicTech
@MilanJovanovicTech 2 жыл бұрын
I've never used it to be honest. Should I take a look?
@regestea
@regestea 2 жыл бұрын
@@MilanJovanovicTech absolutely, use it in all of your projects, every time that your product users get an error sentry tells you when and where, and which line of your code has an error and you can see every error that user sees with full detail
@ryan-heath
@ryan-heath 2 жыл бұрын
@@regestea like … an … exception? 😊
@regestea
@regestea 2 жыл бұрын
@@ryan-heath maybe 😁
@sekarcse
@sekarcse 29 күн бұрын
How can you return errors using Error Codes along with messages that are translated based on the language?
@MilanJovanovicTech
@MilanJovanovicTech 28 күн бұрын
IStringLocalizer: learn.microsoft.com/en-us/aspnet/core/fundamentals/localization?view=aspnetcore-8.0
@ivanaradenkovic4638
@ivanaradenkovic4638 26 күн бұрын
Thanks :)
@MilanJovanovicTech
@MilanJovanovicTech 25 күн бұрын
Sure thing
@washingtonacero8375
@washingtonacero8375 Жыл бұрын
awesome
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
Thanks!
@ramytawfik9168
@ramytawfik9168 11 ай бұрын
why did you register it as a transient service not scoped ?
@MilanJovanovicTech
@MilanJovanovicTech 11 ай бұрын
Because it's stateless
@ramytawfik9168
@ramytawfik9168 11 ай бұрын
@@MilanJovanovicTech could you please explain what do you mean by stateless ?
@CarrigansGuitarClub
@CarrigansGuitarClub 4 ай бұрын
Excellent
@MilanJovanovicTech
@MilanJovanovicTech 4 ай бұрын
Thanks
@coding-gemini
@coding-gemini 2 жыл бұрын
Very Interesting, thanks for the video Milan, looking forward for more content from you. Can you post a video of how to use the Ilogger to log the error to database or a file ?
@MilanJovanovicTech
@MilanJovanovicTech 2 жыл бұрын
I'll add it to my list, sure 😁
@maxpuissant2
@maxpuissant2 2 жыл бұрын
What if exceptions are thrown for 404 or other status code?
@MilanJovanovicTech
@MilanJovanovicTech 2 жыл бұрын
How will you know it's thrown for 404 status code?
@MilanJovanovicTech
@MilanJovanovicTech 2 жыл бұрын
@@MarcusKaseder Right, that's the idea!
@unskeptable
@unskeptable 2 жыл бұрын
Exception.ToString() is bulletproof
@sudhirkumar-wx8hn
@sudhirkumar-wx8hn 2 жыл бұрын
Awesome
@MilanJovanovicTech
@MilanJovanovicTech 2 жыл бұрын
Thank you!
@kiquenet
@kiquenet 2 жыл бұрын
Applies to Blazor ?
@MilanJovanovicTech
@MilanJovanovicTech 2 жыл бұрын
I don't think so
@mumk
@mumk 6 ай бұрын
thx!
@MilanJovanovicTech
@MilanJovanovicTech 6 ай бұрын
Any time!
@gabriel.andreato
@gabriel.andreato 8 ай бұрын
Amazing
@MilanJovanovicTech
@MilanJovanovicTech 7 ай бұрын
Thanks
@diegobonura3485
@diegobonura3485 2 жыл бұрын
Thanks for your videos Milan. Main question is about JsonSerializerOptions, how to easily get same json options configured by AddControllers() .AddJsonOptions() call? is it possible to share same settings also on middleware?
@MilanJovanovicTech
@MilanJovanovicTech 2 жыл бұрын
I think a better option would be create a static field with JsonSerializerOptions and use the inside of the middleware
@diegobonura3485
@diegobonura3485 2 жыл бұрын
@@MilanJovanovicTech I will try to inject IOptions and let you know!
@jayakumar2927
@jayakumar2927 Жыл бұрын
can you share source code
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
It's shared on Patreon
@oguzhan2393
@oguzhan2393 11 ай бұрын
for every error you are returning 500 code. I think it is not good for use case.
@MilanJovanovicTech
@MilanJovanovicTech 11 ай бұрын
This is a fail-safe for unhandled exceptions. What else would you return?
@oguzhan2393
@oguzhan2393 11 ай бұрын
@@MilanJovanovicTech can be 404 sometimes can be 400 error. dependens on the case.
@gauravsingh-qt2zo
@gauravsingh-qt2zo 2 жыл бұрын
Can anyone tell when to use "is null" vs == while comparison
@neociber24
@neociber24 2 жыл бұрын
Always use "is null" because the operator == can be override. But to be real, rarely someone override the equality operator, maybe library authors
@MilanJovanovicTech
@MilanJovanovicTech 2 жыл бұрын
"is null" reads better. What Fredd mentioned is correct. But in 99% of use cases, this won't happen.
@gauravsingh-qt2zo
@gauravsingh-qt2zo 2 жыл бұрын
@@neociber24 One more question-> When to use object keyword vs dynamic keyword?. Both can be used to store some complex data at run time. Any advantage of using one over other
@gauravsingh-qt2zo
@gauravsingh-qt2zo 2 жыл бұрын
@@MarcusKaseder thanks a lot for your answer. Can u answer one more question-> the one reason of using var keyword is that it is short to write as compared to big data type. What is the other major reason to use var keyword????
@gauravsingh-qt2zo
@gauravsingh-qt2zo 2 жыл бұрын
@@MarcusKaseder thankyou so much sir. One last question how many years of .net development experience u have???
@lunamoonfang3926
@lunamoonfang3926 12 күн бұрын
How to unit test this?
@MilanJovanovicTech
@MilanJovanovicTech 12 күн бұрын
Don't... Just write a functional test and check the response
@AndreasToth
@AndreasToth 9 ай бұрын
That's how to do a tutorial!
@MilanJovanovicTech
@MilanJovanovicTech 9 ай бұрын
Thank you!
@punitchachan2517
@punitchachan2517 10 ай бұрын
your videos are good, but person who has only good knowledge of Dot net tech can understand your stuff. You explain it very fast it is tough to understand.
@MilanJovanovicTech
@MilanJovanovicTech 10 ай бұрын
Well, I only care about speaking to a .NET audience 🤷‍♂️
@eobardthawne6903
@eobardthawne6903 9 ай бұрын
This is Wendy's sir 😂
@onlyforkbj
@onlyforkbj Жыл бұрын
Hey Mian, I love your video and they are so educational to me and I'm sure to others as well. I would like to ask your opinion on another video from Nick Chapsas which is quite intriguing. kzbin.info/www/bejne/l2LclmybfLl1b5o What do you reckon on this? There seems to be a clear trade off when throwing exception and catching them as part of the global exception middleware.
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
With proper Client side validation - bad requests are highly unlikely to reach your API. So the only reason a bad request would happen is someone calling your API directly - so I'm fine with the tradeoff i'm making here, and throwing exceptions
@olekristiansjbakken4204
@olekristiansjbakken4204 2 жыл бұрын
I like your videos, but please stop with the zoom inn and out. Looks better without it.
@MilanJovanovicTech
@MilanJovanovicTech 2 жыл бұрын
It's only in the first 30s, come on 🙄🙄🙄
@dylanv.dmerwe5960
@dylanv.dmerwe5960 Жыл бұрын
context.Response.WriteAsJsonAsync
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
If that's your only concern, I'll gladly take it
The New Global Error Handling in ASP.NET Core 8
13:41
Milan Jovanović
Рет қаралды 45 М.
А я думаю что за звук такой знакомый? 😂😂😂
00:15
Денис Кукояка
Рет қаралды 3,6 МЛН
The Logging Everyone Should Be Using in .NET
15:34
Nick Chapsas
Рет қаралды 84 М.
Ollama on Kubernetes: ChatGPT for free!
18:29
Mathis Van Eetvelde
Рет қаралды 6 М.
Brutally honest advice for new .NET Web Developers
7:19
Ed Andersen
Рет қаралды 258 М.
Adding Filtering, Sorting And Pagination To a REST API | .NET 7
24:02
Milan Jovanović
Рет қаралды 59 М.
Don't throw exceptions in C#. Do this instead
18:13
Nick Chapsas
Рет қаралды 262 М.
Deep Dive into REST API Design and Implementation Best Practices
12:02
Software Developer Diaries
Рет қаралды 61 М.