dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
https://asp.net
MIT License
35.18k stars 9.93k forks source link

Net7 Blazor + WebAssembly (Client + Server) + SignalR | Hot reload fail #52605

Open Ptipoi-jf opened 9 months ago

Ptipoi-jf commented 9 months ago

Is there an existing issue for this?

Describe the bug

Hot reload did not work using dotnet watch --verbose run on the sample project 7.0/BlazorWebAssemblySignalRApp from https://github.com/dotnet/blazor-samples.git

dotnet watch seems to not able to connect to the browser : Connecting to the browser is taking longer than expected...

The only change i made in this repo (testing and recommendation from old bug) image

dotnet watch logs :

PS D:\projets\DotNet\BlazorExemple\7.0\BlazorWebAssemblySignalRApp\Server> dotnet watch --verbose run
dotnet watch ⌚ Watching with Hot Reload.
dotnet watch πŸ”₯ Hot reload enabled. For a list of supported edits, see https://aka.ms/dotnet/hot-reload.
  πŸ’‘ Press "Ctrl + R" to restart.
dotnet watch πŸ”§ Building...
dotnet watch πŸš€ Started 'C:\Program Files\dotnet\dotnet.exe' 'msbuild /nologo /t:Build /restore' with process id 34152
  Identification des projets Γ  restaurer...
  Tous les projets sont Γ  jour pour la restauration.
  BlazorWebAssemblySignalRApp.Shared -> D:\projets\DotNet\BlazorExemple\7.0\BlazorWebAssemblySignalRApp\Shared\bin\Debu
  g\net7.0\BlazorWebAssemblySignalRApp.Shared.dll
  BlazorWebAssemblySignalRApp.Client -> D:\projets\DotNet\BlazorExemple\7.0\BlazorWebAssemblySignalRApp\Client\bin\Debu
  g\net7.0\BlazorWebAssemblySignalRApp.Client.dll
  BlazorWebAssemblySignalRApp.Client (Blazor output) -> D:\projets\DotNet\BlazorExemple\7.0\BlazorWebAssemblySignalRApp
  \Client\bin\Debug\net7.0\wwwroot
  BlazorWebAssemblySignalRApp.Server -> D:\projets\DotNet\BlazorExemple\7.0\BlazorWebAssemblySignalRApp\Server\bin\Debu
  g\net7.0\BlazorWebAssemblySignalRApp.Server.dll
dotnet watch ⌚ Process id 34152 ran for 8527ms
dotnet watch ⌚ Running MSBuild target 'GenerateWatchList' on 'D:\projets\DotNet\BlazorExemple\7.0\BlazorWebAssemblySignalRApp\Server\BlazorWebAssemblySignalRApp.Server.csproj'
dotnet watch πŸš€ Started 'C:\Program Files\dotnet\dotnet.exe' 'msbuild /nologo D:\projets\DotNet\BlazorExemple\7.0\BlazorWebAssemblySignalRApp\Server\BlazorWebAssemblySignalRApp.Server.csproj /p:_DotNetWatchListFile=C:\Users\fjar\AppData\Local\Temp\tmplnxw1x.tmp /nologo /v:n /t:GenerateWatchList /p:DotNetWatchBuild=true /p:DesignTimeBuild=true /p:CustomAfterMicrosoftCommonTargets=C:\Program Files\dotnet\sdk\8.0.100\DotnetTools\dotnet-watch\8.0.100-rtm.23551.6\tools\net8.0\any\DotNetWatch.targets /p:CustomAfterMicrosoftCommonCrossTargetingTargets=C:\Program Files\dotnet\sdk\8.0.100\DotnetTools\dotnet-watch\8.0.100-rtm.23551.6\tools\net8.0\any\DotNetWatch.targets /p:_DotNetWatchTraceOutput=true' with process id 15436
dotnet watch ⌚ Process id 15436 ran for 944ms
dotnet watch ⌚ Watching 37 file(s) for changes
dotnet watch ⌚ dotnet-watch is configured to launch a browser on ASP.NET Core application startup.
dotnet watch ⌚ Configuring the app to use browser-refresh middleware.
dotnet watch ⌚ Refresh server running at wss://localhost:52366,ws://localhost:52367.
dotnet watch πŸ”₯ HotReloadProfile: BlazorHosted. D:\projets\DotNet\BlazorExemple\7.0\BlazorWebAssemblySignalRApp\Server\BlazorWebAssemblySignalRApp.Server.csproj references BlazorWebAssembly project D:\projets\DotNet\BlazorExemple\7.0\BlazorWebAssemblySignalRApp\Client\BlazorWebAssemblySignalRApp.Client.csproj.
dotnet watch ⌚ Connecting to the application.
dotnet watch ⌚ Connecting to the browser.
dotnet watch ⌚ Running BlazorWebAssemblySignalRApp.Server with the following arguments: ''
dotnet watch πŸš€ Started 'D:\projets\DotNet\BlazorExemple\7.0\BlazorWebAssemblySignalRApp\Server\bin\Debug\net7.0\BlazorWebAssemblySignalRApp.Server.exe' '' with process id 21984
dotnet watch πŸš€ Started
info: Microsoft.Hosting.Lifetime[14]
      Now listening on: http://localhost:5276
dotnet watch ⌚ Launching browser.
info: Microsoft.Hosting.Lifetime[0]
      Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
      Hosting environment: Development
info: Microsoft.Hosting.Lifetime[0]
      Content root path: D:\projets\DotNet\BlazorExemple\7.0\BlazorWebAssemblySignalRApp\Server
dotnet watch ⌚ Connecting to the browser is taking longer than expected ...
dotnet watch ⌚ File changed: D:\projets\DotNet\BlazorExemple\7.0\BlazorWebAssemblySignalRApp\Client\Pages\Counter.razor.
dotnet watch ⌚ Connecting to the browser is taking longer than expected ...
info: Microsoft.Hosting.Lifetime[0]
      Application is shutting down...
dotnet watch πŸ›‘ Shutdown requested. Press Ctrl+C again to force exit.
dotnet watch ⌚ Failed to read Hot Reload capabilities: A task was canceled.
  ❔ Do you want to restart your app - Yes (y) / No (n) / Always (a) / Never (v)?
  ❔
PS D:\projets\DotNet\BlazorExemple\7.0\BlazorWebAssemblySignalRApp\Server>

Expected Behavior

I expect hot reload to work like other projects BlazorWebAssemblyScopesLogger or BlazorServerEFCoreSample in the same repo sample

BlazorWebAssemblyScopesLogger is working with net7 and net8

Steps To Reproduce

run dotnet watch run over 7.0/BlazorWebAssemblySignalRApp/Server

no need to edit something, dotnet watch show continusly the message Connecting to the browser is taking longer than expected...

Exceptions (if any)

No response

.NET Version

8.0.100

Anything else?

commit hash from sample repo : 0ecc2bc297549b620c74efeb0b42b6ad638982c7

dotnet --info output : SDKΒ .NETΒ : Version: 8.0.100 Commit: 57efcf1350 Workload version: 8.0.100-manifests.8d38d0cc

Environnement d'exΓ©cutionΒ : OS Name: Windows OS Version: 10.0.19045 OS Platform: Windows RID: win-x64 Base Path: C:\Program Files\dotnet\sdk\8.0.100\

Charges de travail .NET installΓ©esΒ : Workload version: 8.0.100-manifests.8d38d0cc Il n'y a aucune charge de travail installΓ©e Γ  afficher.

Host: Version: 8.0.0 Architecture: x64 Commit: 5535e31a71

.NET SDKs installed: 3.0.100-preview7-012821 [C:\Program Files\dotnet\sdk] 3.1.426 [C:\Program Files\dotnet\sdk] 5.0.100 [C:\Program Files\dotnet\sdk] 5.0.104 [C:\Program Files\dotnet\sdk] 6.0.319 [C:\Program Files\dotnet\sdk] 8.0.100 [C:\Program Files\dotnet\sdk]

.NET runtimes installed: Microsoft.AspNetCore.All 2.1.22 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All] Microsoft.AspNetCore.App 2.1.22 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 3.0.0-preview7.19365.7 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 3.1.8 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 3.1.13 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 3.1.32 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 5.0.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 5.0.4 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 5.0.17 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 6.0.5 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 6.0.9 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 6.0.11 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 6.0.24 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 6.0.25 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 7.0.14 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 8.0.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.NETCore.App 2.1.22 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 3.0.0-preview7-27912-14 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 3.1.8 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 3.1.13 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 3.1.32 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 5.0.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 5.0.4 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 5.0.17 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 6.0.9 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 6.0.11 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 6.0.24 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 6.0.25 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 7.0.14 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 8.0.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.WindowsDesktop.App 3.0.0-preview7-27912-14 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 3.1.13 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 3.1.32 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 5.0.0 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 5.0.4 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 5.0.17 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 6.0.9 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 6.0.11 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 6.0.24 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 6.0.25 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 7.0.14 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 8.0.0 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

Other architectures found: arm64 [C:\Program Files\dotnet] registered at [HKLM\SOFTWARE\dotnet\Setup\InstalledVersions\arm64\InstallLocation] x86 [C:\Program Files (x86)\dotnet] registered at [HKLM\SOFTWARE\dotnet\Setup\InstalledVersions\x86\InstallLocation]

Environment variables: Not set

global.json file: Not found

Learn more: https://aka.ms/dotnet/info

Download .NET: https://aka.ms/dotnet/download

Ptipoi-jf commented 9 months ago

i forget to say my first try is using hot reload with visual studio but did not work, that why i'm trying to use dotnet watch

CptSpaceToaster commented 7 months ago

I've been sitting here on the sidelines not sure if I'm only going to contribute noise...

We've had nearly identical issue show up while working on our client project.

  1. Hot reload stopped working
  2. Running the project with dotnet watch prints the error: Connecting to the browser is taking longer than expected endlessly.

Unlike the report here... Hot reload had been functioning previously, and then one dev started noticing trouble... and then it "spread" to my machine after some time. It makes me think that something changed in our runtime. Maybe a very subtle change was made to the project (or this is some sort of bug)

It's intriguing to hear that some sample projects seem to work out of the box while others don't.

.NET SDK:
 Version:           8.0.100
 Commit:            57efcf1350
 Workload version:  8.0.100-manifests.6c33ef20

Runtime Environment:
 OS Name:     Mac OS X
 OS Version:  13.4
 OS Platform: Darwin
 RID:         osx-arm64
 Base Path:   /usr/local/share/dotnet/sdk/8.0.100/

.NET workloads installed:
 Workload version: 8.0.100-manifests.6c33ef20
There are no installed workloads to display.

Host:
  Version:      8.0.0
  Architecture: arm64
  Commit:       5535e31a71

.NET SDKs installed:
  6.0.412 [/usr/local/share/dotnet/sdk]
  6.0.413 [/usr/local/share/dotnet/sdk]
  6.0.414 [/usr/local/share/dotnet/sdk]
  7.0.306 [/usr/local/share/dotnet/sdk]
  7.0.307 [/usr/local/share/dotnet/sdk]
  7.0.308 [/usr/local/share/dotnet/sdk]
  7.0.401 [/usr/local/share/dotnet/sdk]
  8.0.100 [/usr/local/share/dotnet/sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.App 6.0.20 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 6.0.21 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 6.0.22 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 7.0.9 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 7.0.10 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 7.0.11 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 8.0.0 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 6.0.20 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 6.0.21 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 6.0.22 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 7.0.9 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 7.0.10 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 7.0.11 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 8.0.0 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]

Other architectures found:
  None

Environment variables:
  Not set

global.json file:
  Not found

Learn more:
  https://aka.ms/dotnet/info

Download .NET:
  https://aka.ms/dotnet/download

I'll spend some time today to see if I can run all 3 listed sample projects on my Mac, then see if I get the same results.

From there... I wonder if we can isolate it to the installed versions of the dotnet SDK, and the installed workloads.

CptSpaceToaster commented 7 months ago

Indeed: 7.0/BlazorWebAssemblySignalRApp/Server doesn't hot-reload on my machine out of the box. (no code changes made)

  1. dotnet watch - Verified the logs: Connecting to the browser is taking longer than expected ...
  2. Made a change to 7.0/BlazorWebAssemblySignalRApp/Client/Pages/Index.razor
  3. Saved, and verified File changed: showed up in logs
  4. Change was not visible in browser πŸ’₯

Curiously, the 8.0 signalR demo seemed to function like we expect: 8.0/BlazorSignalRApp βœ…

Checking the other net7.0 examples 7.0/BlazorWebAssemblyScopesLogger βœ…

7.0/BlazorServerEFCoreSample βœ…

I think this still leaves the workload version, and net7.0-related runtimes as "things to check" (although we don't have any similarities there)

I might take a peak at the different .csproj's to see if any differences stick out as well.

CptSpaceToaster commented 7 months ago

Nothing jumped out to me as out of line... I decided to take a look at the port numbers on a hunch: The 7.0/BlazorWebAssemblySignalRApp/Server opens to 5276 with no hot-reload ❌

8.0/BlazorSignalRApp uses 7034 βœ… 7.0/BlazorServerEFCoreSample uses 5276 βœ… <- same as BlazorWebAssemblySignalRApp

So it's not some sort of port conflict... Hot reload can work on 5276... its more project specific than that.

And all of the .csproj files are all quite thin... I'm not seeing anything jump out to me there... our installed lists of SDK's are different... the only similarities that I can see involce having the latest version of the sdk and workload (workload manifest?) installed...

Perhaps it's something to do with order in program.cs or something otherwise subtle.

CptSpaceToaster commented 7 months ago

I was finally able to unstick my side of things on a project where hot reload was working but mysteriously broke.

  1. dotnet watch
  2. Verify that browser opens (I'm using Chrome) and app loads
  3. Verify that console spams Connecting to the browser is taking longer than expected
  4. Open browser inspector
  5. (In Chrome) Right click the refresh icon and select Empty Cache and Hard Reload
  6. Wait for app to reconnect.

I was able to see chatter in console as the browser reconnected.

I decided to try this in our problematic 7.0/BlazorWebAssemblySignalRApp/Server

This didn't fix the issue immediately however @Ptipoi-jf disabled Response Compression in the example... which I haven't yet. In my logs, I still see:

warn: Microsoft.AspNetCore.Watch.BrowserRefresh.BrowserRefreshMiddleware[4]
      Unable to configure browser refresh script injection on the response. This may have been caused by the response's Content-Encoding: 'br'. Consider disabling response compression.
dotnet watch ⌚ Connecting to the browser is taking longer than expected ...
dotnet watch ⌚ Connecting to the browser is taking longer than expected ...
dotnet watch ⌚ Connecting to the browser is taking longer than expected ...

My bet is both things need to happen:

  1. ResponseCompression needs to be disabled
  2. Browser caches need to be cleaned/cleared thoroughly if you ever have trouble.

When I comment out response compression, the SignalR demo fails to compile... and other demos that AddResponseCompression(...) seem to function... so I'm not confident this is it...

Ptipoi-jf commented 7 months ago

Thanks to your multiples tests !

Response Compression was disabled on my test (see screen diff) but i guess i didn't try to clear cache of my browser (i'm using edge)

i'll try to clear cache like you, i'll give feedback here

paulguz-datapa commented 7 months ago

Using dotnet.exe, I'm experiencing this in Visual Studio 17.8.6, .Net 8.0.1, which began happening after re-installing Visual Studio due to some Wasm errors I could not explain or get rid of otherwise. Disabling response compression, or even SignalR entirely, does not fix it.

Running as dotnet run (or watch) I cannot connect to my web application in the browser - i get "Connection refused" (even though the application has started). I can, however, run the application from Visual Studio.

CptSpaceToaster commented 7 months ago

@paulguz-datapa You may be experiencing something of a different issue:

The original issue states that the application can be served... but no longer "hot-reloads" with a noteworthy message in the console after running dotnet serve

Connecting to the browser is taking longer than expected

If you're getting a "Connection Refused"... that sounds like something new... (or an addition)

That being said, if you're able to get your application up and open in a browser... I have found that I need to both:

It's not great... The fact that I need to do this every time I dotnet watch and open a new tab almost defeats the purpose of having hot-reload in the first place...... but it does seem to get things to work for a little bit. Do you think you have another issue at hand?

paulguz-datapa commented 7 months ago

@CptSpaceToaster Forget the Connection Refused, my app was running on a different port at dotnet.exe, I forgot I had been messing with my ASPNETCORE_URLS environment variable before I had the Wasm trouble.

Have fixed that, but still get Connecting to the browser is taking longer than expected, Hot Reload seems ok at the moment, however. Will update if that changes.