.NET 6 Hot Reload in Visual Studio 2022, VS Code, and NOTEPAD?!?

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey friends let's take a look at a feature that i really like on.net 6 called hot reload i'm going to go ahead and do all of this demo entirely as much as i can at the command line and that way we'll get to understand what's actually happening so what i'm going to do is i'm going to make a folder here called hot reload i'll go into it and you'll see that it's empty and then we'll make one called.net5 and then we'll make one called.n6 and i'll remember that i can hit ctrl l to clear my console so i got two folders they're empty let's go into the dot net five one empty and i'll say dot net new and i'm gonna say global.json because what i'm going to do is i'm going to pin this folder to net5 you see here it says.net 6 in my prompt if you like my prompt i've done a whole video on how to set yours up i also have a blog post so check those out so i'm going to say net dash dash info and see the dotnets that i have i have.net 5 sdk i've got a release candidate of 6 and i've got a release of 6 60 which is coming out next week so i'm going to copy that into the clipboard.net 5 and then we'll just go and open up our global.json inside of visual studio code and i'm going to change this from.net 6 to 5. this is the sdk number for 5. i'm going to hit enter and you're going to notice that now this folder is pinned to net5 okay now we will say dot net new console make a console app and let's open this up i'll open up vs code we'll do it in visual studio senior in a minute but uh just using a regular text editor i'm just going to use this as a text editor okay and uh what we're going to do after we say yes to our various prompts i'm just going to say net run that will compile it and run it take a second it'll say hello world cool now let's go and maybe um let's say like wow true and then we'll uh put that in a oops put that in a thing here and then we'll say thread.sleep oops red that's not helpful intellisense sometimes vs code is a little goofy thread dot sleep i'll have it wait for a second or so okay it's going to get salty me about using system.threading okay and uh then that will just kind of go up up up all right so we're gonna hit done and run and that's gonna go hello world hello world hello world cool all right now i'm going to say net watch run i'm going to say watch the source code and run it it says i'm going to start watching and it says hello world hello world hello world and i'm going to change hello world to hello friends i'm going to hit control s just hit save and you'll see that it exited recompiled and came back okay how can we prove that it exited recompiled and came back so what i'm going to do is i'm going to have it say system.diagnostics.process.getcurrentprocess and then we'll take the id of that process and what i'll do here in our hello friends bit of text is we'll make that a interpolated string we'll go like that we're going to say hello friends and then we're going to put out the current pid the current number all right and then we'll see if that changes or not so i'll put that back over here and then we'll dotnet watch run again we see hello friends hello friends and we see it's got a pid process id there of 34 756 hello y'all control s exited recompiles now it's got a new process id and that's what netwatch has done it basically watches the source code and then when you make a change it kicks it stops the process recompiles and uh and comes back so that's kind of cool i'm gonna go ahead and put that into the clipboard we'll hang on to that for a little bit later now i remind you that that is here in our.net 5 demo folder let's go back up and we'll go down to our.net 6 folder and we'll say net new console in here there's our console app dotnet run compiles takes a second and boom says hello world pause for effect there we go and again.net run is a little slower because it is actually doing the um the compilation if you think.net is slow because of that demo let's go down into the beast and i'll prove to you that it's in fact really fast it's just that the dot-net run does a full recompile okay so now let's go ahead and open that in vs code and then we'll snap and snap snap you'll notice that our console files a little simpler it doesn't have the using and the public static void main it's still there it's just implicit if you don't like it you can put it back it doesn't hurt anybody and then what we'll do here now is we will take the console.writeline that i saved from before with the process id and then what i'm going to do is i'll do my wow true and if i put that in here it'll work as the before now there is an implicit entry point here there is a public static void main entry point meaning with the entry into our application that is happening here it's just that you can't see it okay and that static entry point is not something that can be rewritten in in real time but i'm going to show you what hot reload does and what i'm going to do is i'm going to actually make a separate little class so we're going to have an instance variable we'll have public class i don't know utils i guess and then we'll have it do a you know public void pid some function here some helpful function we'll move our stuff down here and then i'll say var x equals new utils and then we'll say x dot output pid so now we're going to spin and then we'll probably add that threading as well so we'll say thread dot sleep now you see how thread.sleep was available there as opposed to before when i had to go and say using system.threading there's a bunch of implicit usings that are added by default to make this easy out of the box and then we'll say sleep1000 cool all right so now let's go over here we'll do.net run should work and it'll say hello y'all hello y'all yellow okay now certainly if i make a change nothing's going to happen because it's not watching so now let's say net watch run hot reload enabled hot reload enabled control r will restart me so if i say hello folks and then hit control s just going to save it notice it says hello folks and the pid the process id has not changed there it notices the change does the hot reload says hello scott that's pretty interesting let's close visual studio code let us open up a another powershell and let's go over into that folder there we'll go into net 6 demo and i'll say notepad 2 because it's one better than notepad do i don't have notepad2 on my machine this is a brand new computer i didn't put that into my path yet so here i have a notepad hello notepad 2 control s and that works in that cool so i'm just doing this with the command line and with notepad which is hotness now if i want we'll go ahead and stop this okay and we will close that up and then i'm going to load up visual studio 2022 here and i'm going to go ahead and open up that.net 6 demo visual studio is free you can get the community version of visual studio it's very nice works for open source projects works for small businesses and stuff okay here's our project here now i'm not going to do a debug i could do a debug but i'm going to say start without debugging okay and my thing is started up there i'll put visual studio off to the side there we go and we have a little hot reload daily here check it out you select hot reload on save and then you can choose to force a hot reload restart the application do it on save you're in control it's up to you then we say hello vs 2022 and hit control s and look instantaneously and the pid the process id does not change we're actually injecting that into the process with hot reload which is pretty fantastic and i'm doing this inside of vs so as i just make my changes and i hit control s it automatically updates again i put that into this public class utils i put into a separate util because you can't change the entry point you can't change the static main entry point but you can change everything else and if it looks like you need to force a hot reload you can just click the the little fire deal and that'll go and say hot reload now so if i turned off hot reload on save i said hello scott i hit save you notice that it hasn't changed yet and then i push the hot reload i force it out there and now i get that right there which is pretty sweet even cooler even cooler my friends let's do this let's open up another console and let's go to a different folder now let's go up and do a web app on our web app 99 why not net new razer i like razer pages these are nice simple websites they they can use mvc the model view controller stuff but um i like them and then um let's go and say dot net watch run okay and then i'm going to open up another tab over here and let's get back over into that folder i should have been more organized with my my things that i'm doing so you wouldn't have to wait and watch me i need to put notepad into the into the path here i'm looking at a razor page and then where's our application i think it's over on this other monitor let me pull that over here so i'm going to do a little something tricky here let's go and do a little of this action go and put that there put this down here probably don't need that i'll do it like this oh that's kind of cool i've never seen that before what's happening oh wow never seen that all right cool so here's our application i'm going to just make a change here we'll say instead of learnabout.net i'm just going to change the html and say scott is awesome control s look i did not press refresh i did not press refresh it automatically pushed the change to the html out directly i'm just a notepad again works in notepad works and vim works and whatever makes you happy hit control s hot reload pushes that code back out the other side if you don't believe me that's the cshtml we can do the same thing with let's say the code behind to the c sharp part of it let's try that again i'm doing notepad just to make the point and let's watch right here where it says hot relative changes well maybe we'll do something with the with the logger we'll say logger.log error zunu okay hit control uh control s and you'll notice that that code behind got logged and immediately updated so i can make changes to the code behind i can make changes to the cs html works amazingly and if you right click here and look at view source the magic is happening here where that asp.net core browser refresh is poking it in at development time so that is hot reload available in in.net six dot net six coming out next week or this week depending on where you exist in time and space and built into visual studio uh 2022 although you can use it in notepad nano whatever makes you happy makes me happy that was only 15 minutes long and hopefully you learned something and make sure that you check out dotnetconf.net conf if you have not seen it is happening right now let me actually open up a browser and i'll show you that it's going to be this week dot net conf well again depending on space and timing november 9th to the 11th six days from now and if you get to the.net comp and it's already happened all of those things will be available uh on youtube you can check them out it's gonna be a great virtual event you can check out all of our great sponsors and go and watch all these things there's a big agenda we're going to have three fun days of sessions community sessions what's new and all the different things that make you happy so be sure to check that out and download.net six that's a good time that was a very short demo but there's so much more to show you hopefully i'll do more please comment in the chats what you want to see bye
Info
Channel: Scott Hanselman
Views: 85,308
Rating: undefined out of 5
Keywords:
Id: 4S3vPzawnoQ
Channel Id: undefined
Length: 16min 14sec (974 seconds)
Published: Wed Nov 03 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.