Hot Reload not working in Blazor Server .NET 6
Asked Answered
L

2

12

My Issue

I am using Visual Studio Professional 2022 version 17.2.5 and I have a Blazor Server project, targeting .NET 6, where the Hot Reload feature is giving me a headache. This feature works fine in other projects of mine, built entirely the same way.

Problem Description

The Hot Reload feature seems to work only when it comes to c# code-behind. However, in my .razor files, the hot reload does not work. When I make a simple change of text, and hit save, I notice at the bottom of Visual Studio the message:

No code changes were found.

And thus, no Hot Reload. This is not an issue in the other projects of mine, when I make the same changes on them I get the message:

Item(s) Saved
Code changes were applied successfully.

And Hot Reload works as intended.

I want to point out, I expect it to function like it does in my other projects by just clicking save or hitting the Hot Reload button. I don't want to have to use dotnet watch --project run and then have to wait for it to recompile every single time.

Settings and what I have tried

  1. I have gone into Tools > Options > Debugging > .NET / C++ Hot Reload and made sure all of those settings were checked.
  2. I have compared the launchSettings.json files of both projects, they look the same.
  3. I have compared the Program.cs files of both projects. Nothing stood out to me that would seem like the cause.
  4. Like I mentioned above, I have tried the dotnet watch run approach and this works. It say's Hot Reload enabled, but for any minor changes to take effect the project gets recompiled - I do not want this.
  5. I have been researching my issue over the passed couple weeks but keep coming up empty handed. Most of the answers on SO seem to be either targeted towards Blazor WASM or they are dealing with issues prior to Hot Reload working out of the box in .NET 6. Though maybe I have missed something.

Edit

  1. I have tried running the project with and without the debugger, no difference.

  2. With the suggestion from Scott Perry, I have tried closing VS and deleting the .vs, bin, and obj folders and then reloading my project. Still nothing.

So, does anyone have any idea what could have gone wrong with this project? Any insight on what I should look into?

Thanks

Lishalishe answered 1/7, 2022 at 20:46 Comment(0)
L
5

I finally got to the bottom of it.

In my .csproj file I had the code below:

<PropertyGroup>
        <TargetFramework>net6.0</TargetFramework>
        <Nullable>enable</Nullable>
        <ImplicitUsings>enable</ImplicitUsings>
        <UseRazorSourceGenerator>false</UseRazorSourceGenerator> 
</PropertyGroup>

I'm not sure why I had the <UseRazorSourceGenerator> line in there to begin with... but setting the value to true like in the updated code below solved my Hot Reload issue.

<PropertyGroup>
        <TargetFramework>net6.0</TargetFramework>
        <Nullable>enable</Nullable>
        <ImplicitUsings>enable</ImplicitUsings>
        <UseRazorSourceGenerator>true</UseRazorSourceGenerator> 
</PropertyGroup>
Lishalishe answered 19/7, 2022 at 16:34 Comment(0)
C
5

Since you said your problem seems to be project-specific, have you tried closing Visual Studio, deleting the .vs, bin, and obj folders, then reloading the project? That may help clean the build environment.

Calcicole answered 12/7, 2022 at 18:50 Comment(2)
Just tried it, didn't work unfortunately. Still got the "No code changes were found" message, thus no Hot Reload. Thank you for your suggestion thoughLishalishe
Generally, you shouldn’t leave comments as answers if you don’t have enough reputation. That said, this is a potentially valid answer, and not e.g. a request for clarification, and so I’ve removed the disclaimer, while also improving your formatting and explanation.Ferren
L
5

I finally got to the bottom of it.

In my .csproj file I had the code below:

<PropertyGroup>
        <TargetFramework>net6.0</TargetFramework>
        <Nullable>enable</Nullable>
        <ImplicitUsings>enable</ImplicitUsings>
        <UseRazorSourceGenerator>false</UseRazorSourceGenerator> 
</PropertyGroup>

I'm not sure why I had the <UseRazorSourceGenerator> line in there to begin with... but setting the value to true like in the updated code below solved my Hot Reload issue.

<PropertyGroup>
        <TargetFramework>net6.0</TargetFramework>
        <Nullable>enable</Nullable>
        <ImplicitUsings>enable</ImplicitUsings>
        <UseRazorSourceGenerator>true</UseRazorSourceGenerator> 
</PropertyGroup>
Lishalishe answered 19/7, 2022 at 16:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.