Coding Shorts: ASP.NET 8 - New Exception handling

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome back I'm Sean Werth I've been taking a look at net 8 as the release is really impending now working with some of my clients to upgrade their own projects finding some new features that are really adding some Niche opportunities to make our applications better before we get started I just wanted to ask you please like And subscribe if you like the content I'm doing or even want to suggest content go down below the like button and put it in the comments I really try I have a whole list of topics I'm covering and I'd love to put your ideas in the mix as well so today we're going to talk about exception handling in asp.net now exception handling asp.net has been possible but you had to write your own middleware really to handle it correctly and what Microsoft has introduced inet 8 is this ability to have an exception Handler class that actually is made for this that you can add into the pipeline there is one little caveat about it that we'll talk about as we get into it but let's take a look at some [Music] code so I'm just going to create a new project it's just going to be a simple asp.net core web API project and I'm just going to call this fun with exception handling because I like things easy we're going to use dnet 8 because this only exists in D net 8 and I'm just going to turn off htps because I don't care about it much and I'm not going to use controllers or enable open API I want a really clean project in fact when I look at this I have a really simple project there's actually only one C file in here and I'm going to even clean that up to's get rid of that and that cuz I don't want any of the weather forecast stuff to happen I'm just going to say just so the code is as clean as possible map get I'm just going to throw a new invalid operation exception good bad things happen to good developers right and because we're running in a development build I'm not using any dependencies I can just simply run this let's actually turn off launching the browser because we're going to do everything inside of visual studio so if I run this I can open up this HTTP file we can see it's automatically configured a prefix for us and I'm just going to say API Fu right save that send the request and it's going to stop because we're on the debugger on the exception but if we get past it or we weren't running in the debugger what do we get we get a five 500 error and it's giving us some information and some of this is useful bad thing bad things happen to good developers we're seeing all this information but this may not be what you want this may not be exactly what you want it to experience and so we might want to handle that on our own instead of relying on the default here and so if we come over here I'm going to create a new class don't create this in your program.cs but for this example I'm going to do it rather quick quickly here and I'm going to call it our exception Handler and I'm going to implement I exception Handler I'm going to bring in those Diagnostics and I'm also going to implement the interface and all the interface does is pass you the ability to handle the exception and then based on this return value you can tell it whether you want to continue or just exit let you make the decision about whe whether you want to be the one who handles it and then it goes back to the user or whether you want to let it go through other middleware and for ours I'm actually going to just say return true which means this is the end of the road let's make this a sync because we're going to want to actually work with this async and what we're given is the HTTP context and I can go to the request and write response and I'm just going to write async some text so I'm going to say exception Throne sorry right not all that useful but because it's Ayn I need to say wait here but I also could take that HP context. response say status code equals let's say 501 not a valid status code but we want to see what actually happens here and we also want to say content type and let's just say it's going to be text plain right so what this is trying to show you is that you can do what you want with this it's giving you the exception so in fact let's make this even a little better and let's just say exception. message right we're going to throw the message in here but we're going to return it in a different way and then not an uncommon way to do this is actually to say write Json async and include a problem return object and that would format in certain ways that might be useful to get back out but I want you to see how this works I have the exception Handler and so the first thing we need to do is use our Builder Services add exception Handler which is a new API and I'm just going to pass it our exception Handler now you can override whether this is transient or whatever but by default it's going to be uh only transi so only going to be created when it's needed and there's no State here so that's perfectly fine so now that we've added the exception Handler let's come down here and add the middleware so we're going to say app. use exception Handler this is an API we've had before this is allows you to register some middleware or to point the exception Handler at a known Earl to have that page handled but this exception Handler is supposed to look for our exception Handler that we created up here right well not quite let's restart this and we're going to see almost immediately this error eror configuring the exception Handler middleware because it wants the exception handling path or the exception Handler property to be set and this empty Constructor because it was used before doesn't do either of those things it doesn't assume oh if this exists therefore I must be able to do this and and this is a bug it's actually been confirmed by Microsoft and probably won't make it into the release of net 8 just because it's so soon it's week and a half away but it will probably be in a service release soon after because having this throw an exception isn't all that clear that message we got about need to be sending the exception Handler or whatever isn't obvious and the way to get around that is actually to pass in a configuration Lambda now notice I'm using the underscore here because I'm not actually going to do anything with this I could I might want to configure certain things about the exception Handler but in order to just use our exception Handler here we're actually going to just pass in a Lambda use that overload instead and hopefully either the empty exception Handler or the docks will be better or maybe even the exception be better based on us also using both of these in in tandem so if we restart this now we'll see it doesn't throw that exception and when we execute this we're going to get that exception because we're running a debug but if we run it again we'll see we're getting that 501 exception that we created we're saying the message here and we could even be including the error message like we're doing here and there's different ways to handle exceptions you could be returning a problem result in the case of everything being an API but remember this exception handling isn't limited to apis this could also be used for what you want to do for any of the different apis whether you're using razor Pages whether you're using Blazer whether you're using MVC all of that would be supported here now there is something to know remember I said earlier that return true meant do this and stop it now why would you want return false return false here would be I want you to continue doing what you're doing I just wanted to be notified about the exception and so it wouldn't be surprising to do something like logger.log error X something like that right that would not surprise me and because we're using exception Handler now we can actually use a Constructor and so if we ask for our logger our exception Handler logger and let's go ahead and just prop that in then obviously I could be just logging this and returning false which means do everything I wanted to do I just wanted to know about it and then we can continue so the facility of using an exception Handler here has some really key benefits to how it works us being allowed to create an exception Handler that also uses its own Services instead of having to Cobble something together means the exception handling that you're writing in this does a few things one it has an interface so we know what we're expecting it's enforcing that we're going to be dealing with tasks and asynchronicity as well as supplying these common elements that have been kind of hard in middleware to get before had to pass in the HTP contact accessor to get at the context and also weeding through what is the real exception here was it the last one run or whatever the case may be so I find this handling of the Exception by creating a Handler much easier than it's been before again if you've gotten this far like And subscribe it's about all I'm going to say about that but thank you for watching these if you have any ideas about net 8 content you want me to cover that you don't see other people covering please let me know down beyond the like button in the comments I'll see you next time for coding [Music] shorts [Music]
Info
Channel: Shawn Wildermuth
Views: 14,952
Rating: undefined out of 5
Keywords: Web Development, Azure, Azure Static Web Apps, Static Web Apps
Id: 0hh9v6Y_cOg
Channel Id: undefined
Length: 11min 9sec (669 seconds)
Published: Tue Nov 14 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.