Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Martino 14 posts 135 karma points c-trib
    Dec 06, 2021 @ 15:09
    Martino
    0

    dotnet watch run doesn't reload browser

    Hey all!

    I'm having some troubles setting up live reload for my website. I have tried setting up a new v9 instance with dotnet new umbraco and surely I miss some configuration. I've tried setting up a new website with dotnet new mvc which should be almost the same, and all works as expected.

    I'm trying to live reload .cshtml views and also when I edit a .cs file.

    Both projects when launched via dotnet watch run give the same message in console: dotnet-watch reload socket connected. and when a file changes it correctly prompts File changes detected. Waiting for application to rebuild. but in v9's project nothing happens.

    Maybe there's some configuration to do which I am not aware of?

  • Sebastiaan Janssen 5060 posts 15522 karma points MVP admin hq
    Dec 06, 2021 @ 16:10
    Sebastiaan Janssen
    0

    Sounds like the feature that you want is in .NET 6.0+ which is called Hot Reload. You can try it now by using .NET 6 in your v9 project (v10 will be .NET 6 by default). Just note that we don't officially support .NET 6 on v9 yet as we're not entirely sure that everything works. So far, we've had no bug reports about .NET 6-specific problems though.

    Warren has just released a video on how to use Hot Reload: https://www.youtube.com/watch?v=5775tFUz79I

    To be honest, personally, I am not super impressed. I mean, Warren is absolutely impressive, always, but I mean with Hot Reload. From my short time playing with it I just get frustrated when it doesn't work and I just have to do a manual restart of the webserver. I might as well use my old-fashioned development method by CTRL+SHIFT+F5'ing after code changes.

    Do note that dotnet watch run is absolutely fine when it works, same as with Hot Reload, it has similar limitations and you will need to restart the webserver sometimes. The only thing that it doesn't do on .NET 5 is refresh your browser, after you see File changes detected. Waiting for application to rebuild. you should be able to refresh the page by hand and see the changes.

    Here's a list of things that are supported: https://github.com/dotnet/roslyn/blob/main/docs/wiki/EnC-Supported-Edits.md

    As you can see, a lot of things don't work, you can't change a method signature, you can't add async, you can't add a using statement or modify interfaces.. all of those require a full build. So.. yeah, it's fine for simple things I guess but I am not likely to use Hot Reload for much in the future unless they can significantly improve what's supported.

    Just my opinion though, play with it and see how it feels for you!

  • Martino 14 posts 135 karma points c-trib
    Dec 06, 2021 @ 16:40
    Martino
    0

    Yeah I heard about .NET 6's Hot Reload, though as you said too I'm not feeling enough confortable to risk the full compatibility of v9 & .NET 5.

    Refreshing the page manually works like a charm for me in v9 project, the thing that frustrates me the most is that doing the same thing on a console project (no Umbraco, just dotnet new mvc) in some way it triggers me a browser refresh with every change, even if I modify something that needs a recompile (obviously after it had recompiled it).

    Thank you for the links though! I will definitely watch Warren's video!

  • Sebastiaan Janssen 5060 posts 15522 karma points MVP admin hq
    Dec 06, 2021 @ 16:43
    Sebastiaan Janssen
    0

    I bet it's because dotnet new mvc uses net6.0 by default for you.

    enter image description here

  • Martino 14 posts 135 karma points c-trib
    Dec 07, 2021 @ 07:59
    Martino
    0

    Well that's weird as I don't even have .net6.0 SDK installed!

    enter image description here

  • Martino 14 posts 135 karma points c-trib
    Dec 07, 2021 @ 16:08
    Martino
    0

    Okay I dug a little bit more on this, and it seems that for some reason if you remove all the logging providers in Program.cs with Host.CreateDefaultBuilder(args).ConfigureLogging(x => x.ClearProviders()) it also breaks most of the dotnet watch run functionalities.

    If instead I remove completely the ConfigureLogging or re-configure the console logger, it works as expected:

    public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureLogging(logging => 
                {
                    logging.ClearProviders();
                    logging.AddConsole(); // I assume is this that somehow makes the dotnet watch run works
                })
                .ConfigureWebHostDefaults(webBuilder => webBuilder.UseStartup<Startup>());
    

    I was a bit confused at first, but then I realized that the output printed to the console is not coming from the CLI but instead from Serilog.

    With the default one though we have a messier logging, but I guess it will be worth it if I want to have dotnet watch run working?

Please Sign in or register to post replies

Write your reply to:

Draft