Sunday, April 11, 2021

Early Support for .NET Hot Reload is Now Available with .NET 6 Preview 3

.NET 6 Preview 3 is released just a couple of days back with some nice features. One of the nicest features that got released with this preview is the early support for .NET Hot Reload. Currently, it's only available for ASP.NET Core and Blazor Projects. The command is dotnet watch.

So what's this? Let's have a look using a demo.

First things first. we need to install .NET 6 Preview 3. I have already installed and this is my dotnet version.
PS C:\Users\Jaliya\Desktop> dotnet --version
6.0.100-preview.3.21202.5
Now I am creating a new ASP.NET Core WebApi Project by running the following command.
dotnet new webapi -n webapi
Once the project is created, I am opening it up on VS Code, and let's go through some of the files. First the csproj file.
.csproj file
As expected it's targetting .NET 6. Now let's have a look at launchSettings.json file under Properties folder. It looks similar to what we have already seen and know, but you should notice something new.
launchSettings.json
There is a new property named hotReloadProfile and its value is set to aspnetcore. So this is what's going to enable hot loading. For Blazor WebAssembly projects, the profile value is blazorwasm.

Now I have got rid of the default WeatherForecastController and added a new TestController.
using Microsoft.AspNetCore.Mvc;

namespace webapi.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class TestController : ControllerBase
    {
        [HttpGet]
        public IActionResult Get()
        {
            return Ok("Hello World");
        }
    }
}
Nothing fancy here, it's just a single GET action returning some string. Not what I am going to do is run dotnet watch. And it's kind of hard to show hot reloading in writing, let me share a screencast.
Hot Reload
In the image above, I have snapped VS Code to the left and a browser to the right. I am doing code changes on VS Code and reloading the page on the right. Here I run the project with dotnet run and while it's running, I am changing the value of the string our endpoint is returning and saving. And sending another request, now the endpoint is returning the new value.

So is it restarting the application behind the scene? That's an interesting question. Let's look at the below sample.

Here I have this interface and its implementation.
public interface ISingletonService
{
    int GetCurrentNumber();
}

public class SingletonService : 
ISingletonService
{
    private int _someNumber;

    public int GetCurrentNumber()
    {
        return _someNumber++;
    }
}
It is registered as a Singleton meaning one instance per the whole lifetime of this application.
services.AddSingleton<ISingletonService, SingletonService>();
Now I am changing my test endpoint to something like below.
[ApiController]
[Route("[controller]")]
public class TestController : ControllerBase
{
    private readonly ISingletonService _singletonService;

    public TestController(ISingletonService singletonService)
    {
        _singletonService = singletonService;
    }

    [HttpGet]
    public IActionResult Get()
    {
        return Ok($"Number: {_singletonService.GetCurrentNumber()}");
    }
}
So whenever I call this endpoint, it will return the current number calculated inside GetCurrentNumber method. Initially, it will start from 0, then per each call, it will increment by 1 and return the new value. Whenever I restart the application, it will again start from 0.
Hot Reload Preserving App State
Here again, I am doing a dotnet watch and while it's running, I am doing a change. You can see the output is getting changed, but most importantly the number keeping getting incremented from the last number. Basically, while the runtime is doing the hot reload, the app state is being preserved. It's not just a simple restart behind the scene.

Note: As of the day I am writing this post, this feature is not available through Visual Studio.

Isn't it nice or what!

Do try it out! I am pretty sure this is going to be loved by all .NET devs around the world.

Happy Coding.

Regards,
Jaliya

No comments:

Post a Comment