What is the ASP.NET Core Minimal API?

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] okay so let's get started as i told you it's about the asp.net community stand up and i have shared the link with you in github let's get started and i am done talking i'm ready to learn some more minimal repo or minimal api stuff right well i guess i'll give a quick background of what we mean when we keep talking about these minimal apis in dot net six let me quickly pause here and give you a little bit of a background information john galloway and damian edwards are two very prominent uh figures in the net space especially damien edwards i think it's he's a senior program manager in the asp.net team i had the honor to meet him multiple times in person and to hear him speak multiple times in person he is one of the most influential figures in the dot net space he is a manager a program manager but a manager who is really good in coding steven on the on the bottom here he is a developer on the asp.net team so he really built the majority of codes behind minimal api okay there will be another person which we will hear or at least read in this in this community stand up and i will tell you more about him in a second so if you want to follow some important people on twitter for instance i would definitely recommend these three people because then you will read a lot of interesting information about uh programming in general and especially dot net and asp.net core in asp.net core because the name has gone through a couple of iterations on what it covers and what not and i think it's kind of settling down now based on um how people have interpreted what we call it and how we refer to it in blog posts and whatnot today is a release day we're in the middle of pushing a new release right now so the stuff that i'm talking about will be a combination of stuff we we've shown in preview six stuff that comes out today in preview seven and then stuff in daily builds because obviously we i work mostly in daily builds and as a stefan obviously because he's writing the code and i'm consuming it usually and then giving feedback and and we're iterating so this is not true as of today this video was recorded someone in august then the preview 7 came out and today we already have the release candidate one i think for a week or two or something like this so the things that damian is describing here that you need the nightly version does not apply anymore so you have the latest bits if you have installed as it was your homework visual studio 2020 with a release candidate so this is not perfectly true but still it is interesting because you will now hear how you can give really nightly or daily feedback and you can try the daily bits how they work internally um first of all if you want to play along with this thing you may have to uh consume bits that are a bit newer than the official previews sometimes um and you know the different people will decide whether they're ready to uh to consume those bits as new as that or not um but the minimal api effort uh started as i understand it last year between uh maria who's a pm on our on our team and fowler who's lurking in the comments today on youtube fowler is a very very prominent developer and product manager in asp.net core you should definitely google him f-o-w-l-e-r follower he is a extremely funny guy his presentations are chaotic funny and super interesting he's one of the smartest developers that i know i also had the honor to meet him multiple times and it's always a pleasure to listen to him he is responsible for many of the things that you learn so the things that we did last year were massively influenced by fowler and here you see listen to that here you see how even in huge enterprises like microsoft things start just two people meet and they think about the topic and they sit down write a little bit of code and over time this evolves into parts of asp.net and influences hundreds of thousands of developers so don't think that in microsoft there is a huge premium a huge panel of senior level executives and they have a strategy and then they think about what should we do in a five-year time frame on a coding level it doesn't work like that on a coding level people just get together they discuss stuff they write samples and then the ball starts rolling and this is what happened with minimal api just two people maria and fowler met and they thought about some things and the result is now what we are going to do in the next few weeks as to tens of thousands of developers currently around the world um and it was initially as i understand it and i want valor to correct me if i'm wrong um an effort to investigate what would it mean to try and uh simplify uh the code involved with you know starting from zero like a blank cs file to getting your first hello world in an asp.net core app um if we could somehow simplify that because uh doing that today for people who are new to net or new to c-sharp there's a lot of concepts right you know c sharp is about to release version 10 we've had all these amazing features uh added to the language over a whole bunch of years and very little if anything or nothing has been deprecated right like you can do it we might add new ways of doing things in c-sharp but often you can still do it the old way as well and so as a result the same is true for apis that we build and ship and so if we look at net core and asp.net core in particular folks who have been with us over this last five or six years probably recall we've gone through a few different iterations of the type of code required to boot your application and configure the application all the way from one o where it was it was a lot of code like you would do file new in the templates and our philosophy at that point was you can see everything that's happening there's like 20 lines required to get hello world with what we think are the right default sort of idioms and configuration like reading config from a settings file reading config from the environment variable reading config from the command line if you wanted all that to be on by default you had to call three methods to do that and the first iteration of the templates did that okay we have a short internet glitch doesn't matter let's quickly discuss what damian just described damian described that there are different approaches to building frameworks the old way that microsoft and asp.net asp.net team did it was let's not do anything magical if a user says file new project we will generate dozens of lines of code where you can see every little bit of what's going on inside your code no magic so the thought at that point in time was that as a developer i don't want conventions or magic i want to see in my code every little bit that's going on behind the scenes that was the design idea some years ago i was there in the asp.net world and i can definitely recall these times and believe it or not for me as a c-sharp and asp.net expert this is really interesting this view because then i really see and can influence all the things that are going on and i can fine tune everything to my specific needs but think back a year or two in your situation do you know frameworks and environments where you click find new project and then you get boom a hundred files with dozens or hundreds of lines of code and you want just hello world that's not great so this is what minimal api is all about microsoft stop stepped back and said what is the experience of a beginner of somebody like you two years ago what do they think about c-sharp when they fire up visual studio and say file new web api and the whole idea was oh my god that's a lot of complexity let's forget about c-sharp and move to node.js because in node.js that's five lines of code and it's done it's a single file no compiler five lines of code done got it interesting thought right we are optimizing asp.net core not for the power users but they are optimizing the experience for beginners very interesting from a marketing point of view um back in the day and so there was a lot of stuff to get from nothing to kind of hello um and as fowler says yeah the good old days uh sounding like a boomer um so we but in version two there was an effort to after we shipped the version one we were like ah a lot of this seems like boilerplate maybe we should be a little more opinionated as a product team about what we think represents a decent set of defaults so that you don't have to have 40 or 50 lines of code to get what we think those defaults could be um and perhaps you don't have to reference eight or nine or even ten different packages to get those defaults internally the system we thought was very well layered it's architected well a lot of effort was put into ensuring that the the actual dlls and the apis are factored in a way that they that we can improve the platform over time and you by by ensuring things live in the correct layers and we don't introduce problems with circular dependencies and that type of thing and also that your app doesn't have to take dependencies on lots of things um for to do whatever it is your app does like pay as you go was on one of our philosophies early on right so you wouldn't have to have stuff if you didn't use it so in version two we introduced um the concept of a static web host builder factory thing basically you would go web host dot create default now this is what we did last year the story that he told before it was prior to us meeting so this is two or three years ago now he's talking about the situation that you learned last year now we are in asp.net core two respectively three they work very very similar and he will going to he's going to show some code in a few minutes um and instead of saying web host builder dot new web host builder dot add a bunch of stuff dot you know build blah blah blah blah dot run uh you could just say hey give me a builder that has the defaults that you think are right and you know 40 lines went down to effectively if you know three or four um and so we did that in version two and then also in version two we we kind of did some experiments with how we referenced nuget packages and we introduced this idea of like a super meta package that represented a shared framework which was new in dot net core ii and that was a bit of an abomination and that went through iterations in two o two one and two two um before that finally got resolved the way it we ultimately wanted to in version three with um your shared frameworks with ref packs and runtime packs and all these new concepts that got introduced into nuget and ms build and the sdk et cetera so everything got a lot cleaner so so as you can see these platforms evolve that's really natural so now you as a developer can make your own decisions when did you when do will you jump on a new framework if you find something on the internet will you use version one well maybe if it is if it contains one feature that is super super interesting for you but if you are just measured by features and stability you might want to wait a little bit and then jump onto the train maybe a little bit later that's two strategies and later on when you are in business you often have to make this decision will i start immediately with version one or should i be more conservative depends now you will see this is not an easy decision in practice there's been this cycle of iterating and trying to identify places where there's a lot of boilerplate to um or even some boilerplate to achieve what we think is a reasonable kind of 80 case there'll always be advanced cases where people need lower level access to things but for the majority case can we make those things easier and so that includes apis and it includes sort of package structure and just the developer user experience and so then in dotnet uh five uh we we went a step for no done here dot net five not core three when did we do generic host that was three right so done f3 we also uh we took the learnings of the web host that we did in two and the foundations of that in one and we genericized it so instead of saying all this hosting goodness that gives you di and configuration and logging and everything in one place um in a single composition route for your app let's make that generic so that it's not just for web apps who knows about the generic host nobody i guess so so you probably have no idea what he is talking about so let's see you see there is a project template for a project which is called the worker service you see that one let's give that a try let's put that somewhere i will throw it away so i'll put in a temp folder and i will use the net core 3.1 version you see that one these are the versions and can you remember what i told you in today's introduction dot-net core 3.1 is still supported and it's up at in its at long-term support lts version 5 is not lts but current release and dotnet 6 is preview this is what you see here let's quickly go into dotnet core 3.1 because this is what uh what damian is referring to let's start a new project and if you take a look at the program.cs file i'll pump up the font a little bit see it here if you can remember what asp.net core is all about you will probably recognize this structure think of last year your program.cs looked very much similar to what you see here and this is what damien meant with dotnet 3 microsoft thought that it was a good idea to not only let web application be developed in the way that you learned it last year with dependency injection can you remember dependency injection services.add we had that last year wouldn't it be great to have something like this when writing a console app a complex console app that's maybe just watching a folder and creating thumbnails from a large picture of course it would and that's the generic host wouldn't it be nice to have a logging framework just like logging in asp.net even for console apps like a worker app of course it would and this is what what happened in asp.net they invented something great in asp.net and then they thought hey this is not just interesting for asp.net this should be generally available even if you build a worker process that is just monitoring a folder and this is what what damian is just referring to as generic host this was done in dotnet 3 so this is not a new thing but we will hear what has changed we could in theory use it for any type of app and we introduced the worker template in net core 3 which utilized this new generic host um and so we went from like web host dot create to host dot create see here host dot create an asp.net app that we learned last year had webhost.create and that's the difference the web host is specific for web apis and the host is for generic programs because now it's generic you don't have the word web in front of it and then we did some glue to kind of bridge the web host world to the host world um and that and that you know survived in dot net five and it was it was it was fine it's pretty good um and it gives you a lot of power and flexibility but if you look at it there's still a little bit of legacy because of this kind of split between the generic hosting layer and then the web host specific things like you know the support for a startup class um or you know extension methods that are that you want to appear on the builder that are web specific so if you look at a template for a 3o app and maybe i'll share my screen now sure yeah so let me uh i've got a few things going on here um but i've if i go in here and i say uh what version am i using in here all right so i'm using a bunch so let's look at all the sdks i've got i do have a three sdk here so what uh one important thing it's hard to read in the background so i will read it for you damien just typed in dot net space dash dash list dash dash no dash dash list dash sdks dot net list sdks and he sees that he has multiple sdks installed on his computer let me quickly show you that if i start a windows terminal here oh i want to have a cmd now for this demo dot net list sdks so it is possible as you can see it here to have multiple sdks installed on a single box you can try that on your own computer if you want but believe me you will get a similar result so you see i have the three one i have the five and i have the six installed at the same time this has not been possible with net framework in the old ancient past but in dot net core that this was introduced and it's still true for net five and six so this is what damian just did he looked up which sdks he has installed i'll make a 3.x folder then i'll drop a global json in there and there's nothing what is a global json file does anybody have an idea exactly if you take a folder on your machine and you put a global json file in this folder and then you navigate into this folder and you call the.netcli.net it will recognize the json file and it will know that inside of this project folder you want to use the 3.1 sdk and if you go to a different folder where you have a different global json it will know that it should behave differently it should use the another version of net and if you don't have any global json file it will take the latest one on your box question it's for the cli but it's also for visual studio for running the app for the debugger so if you for instance have an old project that you created in last year and you still want to run it and you have problems with dotnet 5 or 6 you might end up creating a global json file in order to give net a hint that this is an old project got it global json interesting approach different from for instance node.js node.js has a single install you have a single version on your machine you cannot specify this folder that version this folder that version okay i misspelled it and then i'll edit that global json file and i'll say yeah i want this to be 3.1. whatever that was so we scroll back up 3.1.411 okay now if i come back down here and i do version we're now using three one four four one so if i create a new app see now he called dot net minus minus version and he gets 3.1 before he got version six but now the system recognizes a global json file so it is 3.1 interesting feature that we just learned about we didn't discuss it last year so we learned something new in here.net new web uh we'll just do the empty app and i'll put that in my web one and then i do you know code web one we can see here loading loading loading open up there's my program cs right and i'll zoom in a bit for you so i know you like that john yeah we've got this kind of this setup in our program main okay we've got main is the obviously the normal entry method of a c-sharp console app again so that everybody can follow this is what we did last year this is old stuff old meaning it was the current stuff a few weeks or months ago okay basically and we call out to this sort of weird hanging static method which we'll get to a little bit in the future um create host builder uh which takes in the args as well and then this is where we've got that host dot i talked about so you've got host dot create default builder which is the generic host that we introduced in three and then i'm doing it a bit more because someone said zoom in and then we've got this configure web host defaults which you receive a web builder so you've got kind of a host builder within a host builder in 3x and 5.0 um and that was just kind of due to this evolution that we went through and so then you finally get down to like here's my web specific stuff and then that just effectively shells out to a different class which is your startup class right whichever can you remember the startup class from last year you probably can remember the configure services method we used it to set up dependency injection you can probably remember the configure method where we added things like authentication and authorization and things like that it's interesting that even the microsoft asp.net team themselves are no longer happy with this structure because they consider it as being too complex and this will bring us to the mini minimal api which we'll see in a second everyone kind of knows um and loves all right so that was three if we go back and i do the same i think in five x we didn't change anything stefan did we that was basically the same no in dot net five nothing really changed dot net five really worked very very similar to net core 3.1 a few things changed but not too many so many of the knowledge that you learned last year still applies to net 5 but not net 6. no it's basically the same 75 and like yeah for people who are curious like the generic host is so you can host like windows services and stuff like alongside your your web applications so right that's the reason delivery introduced right yep in fact if i look at um go back into my three folder and i do dot net new uh work dash dash help there's that worker template the details about that word i showed you that in visual studio a minute ago now that was the worker project another example for a worker project would be maybe in your diploma project you have a program that should run every hour and send an email to all customers who i don't know who who created an order with the thank you email that would be a worker process it doesn't have a ui it just runs on a schedule or continuously to do something that is a worker and believe me in practice workers are very very frequent so you build them on a daily basis template that i talked about in in that and if i go ahead and create one of those we can see how that works so if i do work a one i do code for worker one and we open that one up we'll see a very similar thing so here it is this is now not my web app this is a worker app which as stefan just said i typically run as a windows service or maybe a standalone container in a kubernetes cluster or it's deployed to a linux machine and run it using some type of daemon service like systemd but you can see it looks very similar the boot up code to my web application code in fact the only where it starts to differ is down here all this stuff is the same because it's the same hosting pattern and then we go oh but we don't have startup classes in generic host we we have a different pattern for kind of booting the app in this case you add a hosted service which and you tell it yeah there's my worker class and in this case this is kind of the app model for a worker you have a thing that implements an interface or so what you see here let me quickly emphasize that because we didn't cover that last year but i think it's very useful and this is why we are taking a look at this video to learn new things not just to recap if you build a worker process which is not asp.net again then all you had to do or have to do already also today you have to create a class which is called something it has to derive from background service you get the same dependency injection the same logging capabilities as in asp.net but instead of an http post handler or get handler or put handler or or post hand no delete handler this is what i was looking for you just write a single method which says execute async and there you can do whatever you want check whether you want to have when to send emails monitor a folder as i said large images come in you generate thumbnails every hour you do some magic machine learning stuff whatever you want to do it's just a worker without a ui arise from this background uh service uh abstract class and then your overrides and things and that's the app model like that's what you do right and web apps obviously are far more rich and they have a lot more features because it's a web app so that gets us you know through the history of effectively of the the startup stuff fast forward back to what i started talking about which was last year there was an experiment run by fowler and maria to look at what would it mean to create a simpler uh startup experience for folks who want to build web apps or apis who perhaps are familiar with other ecosystems whether it be javascript or go or python and but you wanted them to do it in c-sharp and so fowler wrote a an a library called feather feather http so if i google further http you can see that here's uh the repo and this was this was uh the foundation of of that experiment and that user study so they actually got people into a lab uh remotely because covered um and they got them to run through a tutorial and then they observed what how they went and they asked some questions at the end and they did the usual kind of user study thing and so you can see did you hear what damian was just describing in school you have definitely heard about user acceptance testing and about when you do user interface design that you bring people in a room and you monitor them and you ask them what do you think about the user interface did you hear what damian just said they are doing the same for apis many people forget that if you build a library an api that people should use then you also have users you also have a user interface but the user interface is not a green or blue button the user interface is the api how does it feel to create a new project how does it feel to perform a certain task with the api that you created so they are doing user interface tests for apis they bring in students like you ask them to get started with these with c sharp and then ask them did you like it and then they started to reiterate and thought about what is the user experience of our api and that's a very modern approach many people forget about that but apis have users developers so we have to do user interface design it's a kind of industrial design practice but for apis here here's this might look familiar now here's this hello world using feather http right which looks much simpler so there's a few things going on here already you see it's now just a bunch of usings and one two three four statements and that's hello world imagine back three years ago when you never saw c sharp and it would have seen such a hello world application consisting of five lines of code you can run it and you get your first positive experience because you see hello world on the screen that makes you smile right compared to program.cs startup class dependence injection configure services configure use dozens of lines of code that's the idea here what's also interesting here with this feather project is that for modern frameworks like net or node or go or rust things are not happening anymore behind closed doors things are happening out in the wild fowler was creating a github repo and it was not private microsoft did not put a copyright statement on top of it before they gave it to a world he just coded out in the wild and if you are adam fowler and you code out in the wild you can be sure that many people immediately recognize that you created a new repo take a look at it and give you feedback and that's exactly the power of open source and that is what modern companies like microsoft google facebook and so on what they are doing so when you go out into the industry especially here in austria you will see that your future employers sometimes hesitate from using open source they want to keep everything private everything is behind non-disclosure agreement you must not talk about what you are doing but they are forgetting that open source is a huge power understand what i mean it's risky because people might tell you that you are wrong and what you are doing is not good but it's better to get that feedback early than spending years in development and getting that feedback after years of investment got it so this is what what's happening behind the scenes uh that we can break down so before i even get into the apis this looks very different to a c-sharp program.cs file that we might be used to and that's because it's utilizing top-level statements which was a feature that was introduced in sharp nine can remember top level statements we had it already last year it is simple a c sharp file without the main method anymore that is called top level statements and we already did that last year but i'm not sure if you can remember i think somebody in this class did it if you tried to apply top level statements to an asp.net core app and you were using entity framework things broke so it was only half baked it was not perfect but now they invested a lot in making these top level statements perfect i want to say so the last version of c in dot net 5 and that basically allows you to have the primary entry file for your application typically program.cs historically doesn't need to declare the class and the entry method the main anymore if you have a top level statements file uh it's basically found by the compiler by default and that will be assumed to be the equivalent of your program main okay um and so that that simplifies a whole bunch of things you don't have to declare namespace or which is always optional but a stat a class and then have a method on it you just say no here's my code simple as that right so you can see here these are just statements these are just c sharp statements they're not in a class they're not anything else and up here we have the more typical usings uh that you would see uh at the top of any dot cs file so we we've made an evolution we've got to here and as a result of that experiment uh it turned out yeah this this this idea has some legs you know looking to remove certain boilerplate um does improve the experience that new users to the platform might have because it drastically reduces the amount of concepts that people need to be exposed to to get from i've got the sdk running to i've now seen output from an application for the first time right without having to like ignore a bunch of code i just want to mention as you're pointing out those top level statements and stuff i was hosting um mads christensen on the show and he was he was writing in a visual studio extension and we started playing he just recognized he just mentions a guy named matt christensen have you ever heard of math christensen you you're using his software every single day he is a very very important developer in the visual studio group so many of the visual studio visual helpers that you know and love refactorings and renaming and many of the plugins they are influenced or written or designed by matt christensen so you see on a meta level not on a c-sharp level how these different teams work together the asp.net team is designing an api and then they get back to the c-sharp team and say hey change something in the compiler because if you give the compiler additional features we can make our code more elegantly once the asp.net team has built that stuff and the c-sharp team has built their stars they maybe go to an internal customer visual studio and say hey won't you apply what we built in visual studio and they get feedback again why i'm describing that is because it's important for me that you understand that it is of big value that you have people using your stuff and this is why sometimes large companies like microsoft have an advantage because when they build something in one department they can go to the other department and ask them please use it and give me feedback if you as an htl as an hotel student go out and found your own company and you want to build a new product where do you get feedback from that's a big problem if you are in the middle of the development you cannot go out there and ask people about their opinion because you can't really show something understand what i mean so this is why it's so important and we can learn that from the microsoft teams from working inside that it's so important to whenever you build something new build something minimal and then go out as soon as possible to as many potential users as you can and ask them for early feedback you all know the term minimum viable product mvp this is what you should aim for a small product which is ready to be shown to an end customer where you can get feedback and believe me sometimes it's hard to go out and ask for feedback because people will tell you why it is not good what you have been what you have built but you have to you have to cope with that you will get criticism definitely live with it take it embrace it and change what you have built sometimes people will tell you things what they don't like where you are of a different opinion that's okay you can say okay this guy was not happy with this feature but i still think this feature is great then build it because maybe you're right and maybe you will get famous with it but if 10 people tell you that this feature is not great you might be wrong okay so from a meter level from what you heard here they are doing user interface tests externally and internally to get early feedback on csharp.net versions and this is what you should take away um as a last thing from this hour here we will now make a short break um until 12 45 and after this break the theory and the history lesson will be over we will look really into the code and see how the new code looks like okay so for now short break
Info
Channel: Rainer Stropek
Views: 1,156
Rating: undefined out of 5
Keywords: .NET, .NET 6, ASP.NET, ASP.NET Core 6, Web Development, Minimal API, HTL Leonding
Id: pFnJlr2J2Gk
Channel Id: undefined
Length: 39min 52sec (2392 seconds)
Published: Wed Sep 29 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.