ASP.NET Core Kestrel Explained

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
you may or may not know that asp.net core sits on top of kestrel if you've never heard of kestrel or you were slightly curious but never exported you're in luck in this video we will explore where Kestrel sits inside the asp.net core framework we will touch it a little bit closer and we're also going to answer the question of whether you should even care about trying to build your own asp.net core or your own web framework on top of kestrel my name is Anton welcome to the Rock coding YouTube Channel please don't forget if you're enjoying the video don't forget to leave a like And subscribe go ahead visit the links in the description let's go ahead and get started here we have three projects all of them are really small first of all is the controllers project where we map controllers and then we have a home controller with just hello world pretty much the same thing is happening on the endpoint site we have one endpoint with hello world now these two projects are essentially our Baseline the reason these two projects are here is the Baseline is when we worked with MVC and controllers we've seen how going from that two endpoints we've seen a massive well maybe not massive but we've seen a performance boost now if you are aware of asp.net core the framework which is sitting on top of kestrel the server you may think well there is this bulky framework in the way what if I take this framework and I just throw out the window and I create my own very very minimal endpoints on top of the Kestrel server right the story sounds a little bit like minimal apis so with that said we have the Kestrel extension project where we are going to be working on actually exploring where Kestrel is and how it's built so the first thing that you really want to understand is you're going to run your application it's going to try to execute a server or set up some connections under the hood so the first thing we want to do is we want to go into this run function we are gonna hook for URL and then we're gonna keep going down to the run on the I host interface we're gonna again trigger the Run async function we're gonna go down to this function and here the important thing to note off to further functions so the first one is start async we're starting the server and then we're waiting for shutdown async and this is going to be relevant a little bit further on if we go further into start async we're going to end up on the function on the I host but we'll then want to go to the implementations of star async and look for host because web application is an adapter or a wrapper class around I host the host class is the actual thing the implementation that will then get passed into web application now the host itself will have the start async function and under the hood we are going to find that it's trying to surface instances of I hosted service interface so if you've ever implemented a background service this is the same interface on the individual I hosted Services we're then gonna call start async so here we're working with the I hosted service interface but we'll then go even further into start async and here we're gonna find a couple of implementations now background service is what you implement if you need a background service data protection hosted service I'm not quite sure what this is I know that we need to go into the generic web host service and that is specifically if we come back to program CS when you're creating the Builder when you're Builder under the hood it's going to be using a generic web host Builder to build this service now if you've been following along and you're already lost you don't know where you are don't worry all you need to understand is that a bunch of stuff gets set up over here and by the end of it we're going down the chain to the Kestrel server to run it so in the generic web host service we have the I server interface if we look look for the implementations of I server we're going to find the Kestrel server and Kestrel server implementations now Kestrel server implementation is the actual Kestrel server and Kestrel server is a wrapper around the Kestrel server implementation so implementation is internal to the asp.net core framework Kestrel server is something that we can use let's go ahead take the Kestrel server we're going to come back to program Cs and we're going to close all of the tabs except the Kestrel server here we're going to go ahead and create our server sochestral server and then we're going to start looking through the parameters and slowly filling them up we're going to import missing references in the Kestrel server implementation we want I options of Castrol server options so as the first parameter let's go to the options class and we're going to create default of new gastro server options we'll then put a comma and then inside Castrol server options we want an iconnection listener Factory if we look for for the implementations of this class there is only going to be a single one here let's go ahead and grab it we will say new one of these ones import the references and the last parameter to the Kestrel server is I logger Factory if we again take a look at the implementations there is either a logger Factory or a no logger Factory so no logger Factory is basically a no operation implementation and that's what we're going to be opting in for so no logger Factory over here the socket transport Factory is looking for options and the logger Factory as well we'll just give the same things to it in the Constructor and there we have an instance of the Kestrel server again if we go down the run and to run over here run a sync and then into start async and then the implementation host down to start async of the hosted service and generic web hosted service in the start async function over here and again I'm just going to close everything so we have the generic web host service available so we can keep coming back to seeing how it's actually starting the server so if we go down down and a little bit further down on the server we're gonna see start async and what it's passing into the start async of the server is some kind of application a hosting application let's come back into program Cs and first of all we will go into server we're going to start async semicolon on the end and then we want two things the whatever and the cancellation token for the cancellation token I will just pass none and then for the second thing if we come back into the generic web host service hosting application if we go look on the interface we have this this is made up of three parts we have creating a context and the T context in this situation are the things that you want to capture around your application and the HTTP request so it's like an amalgamation of an HTTP context in the context of your application it's like the overall context of what is currently happening setup method we create the context we process the context and then we dispose of the context and these functions are going to be called by the Kestrel server so when a Kestrel server is going to receive a request it is going to pass around these context features into this interface so then the hosting application the thing that implements this interface what it's really doing is it has a bunch of services injected into the hosting applications and then serve surfaces and HTTP context puts it into the overall host context so right over here and this is all during the setup so it makes sure to grab the HTTP context and put it onto this overall context and then processing the context if we take a look at the underscore application what it actually is is a request delegate if you understand asp.net core middleware you know that this is Middle word this is your use endpoint use authentication use authorization this is the middleware chain whatever you're setting up the HTTP context gets pushed into there right here at this process request async and then finally you have the dispose context now the reason you have setup and dispose in the asp.net core framework is you can imagine that it's going to be making many and many requests one of the most crucial things that are being set up in these three method is object pulling so if you imagine if there is a new object for each individual HTTP context to your web application you're going to be creating and destroying many HTTP context objects so the hosting application is leveraged here for performance optimizations otherwise it's not doing anything too complicate it let's go ahead grab the interface it is implementing some kind of class we are just going to create a new public class HTTP app and we're going to implement the ihttp application we don't have a context we can create one so public class context whatever we are capturing let's go ahead and pass it here let's make sure we import missing references and then we're implementing the interface all of the three functions again this stuff does not need to be complicated we can get rid of the disposed context because we might not be doing any optimizations none of that process request this is where we can start adding some simple processing of the incoming HTTP context otherwise for the create context this is where we want to surface the HTTP context and actually put it on the context over here so we can process it then so first of all we want to have a property of the HTTP context and by the way HTTP context comes from Kestrel HTTP context does not belong to the asp.net core framework HTTP context belongs to Kestrel let's have our HTTP context right over here by the end we are going to return a new context notice how this is not asynchronous and when we're returning the new context we want to be setting the HTTP context now I'm not going to bore you with how you find out about the default HTTP context and how you can pass context features into it and it's going to go through some default setup by the end of it you can have your request context HTTP context request you can then have your response new context HTTP context response if you ever used a node.js Express where essentially in the same situation where we have to do something along the lines of this request path equals and we say that it equals to slash we will go to the response body and first we're gonna get a writer so new stream writer put a request body inside of there we're gonna need a using on the rider we're gonna write async hello world this will turn our method async and we're gonna need an await on our using statement over here response status code can be set to 200 otherwise if we do not hit any routes we can say that the response status code is 404 so page no file and there you have it this place is essentially if you would be building your own web framework or you're trying to strip away asp.net core this is where the bulk of your work would be happening we can now take the HTTP app place it into the start async and we're ready to go well almost if we open up the terminal over here and we run our application is actually going to run because I've not removed any of this stuff so we're actually stopping over here and we're never really executing the Kestrel server I'm going to comment all of this stuff out because I'm gonna need to return to something in just a second you will see that our application just finishes if you remember that on the app run when we are running the application right over here run async we have start async and wait for shutdown async after we start the application we actually just want to wait for it for a little bit let's remove all of this stuff and create a shutdown promise new task completion Source all we have to do now is just to wait on shutdown task semicolon on the end and now if we run the application it will just be running and then we can also will cancel it there might come a question of where is the Kestrel server actually running like what address what I peep you can set that through the Kestrel options as with many things you pass something into the Constructor to configure it nothing different here so these are just going to be options and on options we're going to say listen to any AP and here we can just provide a port like 5001 take the options pass them through provide a little bit of space and let's go ahead and rerun our application it may seem like it's hanging but it's not let's go ahead to localhost 5001 and we're gonna get hello world now this is as far as we're going to go we don't need to go too far to figure out whether building on top of kestrel is a lot more beneficial than building on top of endpoints so the main reason why you'd want to build on top of kestrel is to strip away the asp.net core framework that you will think is bulky or something like that the main reason to do it is basically performance so how performant is it is it faster than minimal API endpoint let's find out here we have the three applications running the Kestrel server the endpoints and the controllers here I'm going to be executing these k6 scripts so one for controllers one for endpoints and one for kestrels the only real difference between them is that they're pointing to different endpoints I will leave a link for k6 in the description I'm pretty sure you could maybe use scenarios or something like that over here if you are more familiar with k6 go ahead and leave me a comment let's go ahead and give this a try I'm gonna run all of these from one terminal I'm gonna cut the videos and display the results on screen so first of all let's go ahead and run the controllers with controllers we managed to achieve around 1.22 million let's go ahead and try endpoints here we managed to achieve a 1.36 million requests not a crazy performance booster but is still pronounced we can then execute our custom framework on top of Castrol so kestrel.js and the result lands at around 1.36 million so not that big of an improvement from the 1.36 million of the minimal apis so in the grand scheme of things asp.net core may seem like this massive bulky framework however if you try to go to the core if you try to throw it away and just use Kestrel directly it is not going to be that much faster so in the end is it worth building your own web framework on top of kestrel definitely not minimal apis are actually sitting to the Kestrel server a lot closer than you think think so by the removing asp.net core you're not getting much in return other than losing a bunch of tools that are already there available for you to use this will be it for this video thank you very much for watching if you enjoyed it don't forget to leave a like subscribe if you have any questions leave them in the comment section below I really enjoy my work and want to say thank you please come support me on patreon get the source code a big thank you to all of my patrons that are already supporting me your help is very much appreciated hope you're having a good day and see you in the next video
Info
Channel: Raw Coding
Views: 9,777
Rating: undefined out of 5
Keywords: asp.net core, .net, kestrel, c#, csharp, web framework, what is kestrel, kestrel explained
Id: M6N6Ia5z6Ro
Channel Id: undefined
Length: 15min 58sec (958 seconds)
Published: Tue Jan 03 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.