aspnetcore: dotnet watch stops detecting changes, crashes and corrupts terminal
Description
I have a solution consisting of 3 projects:
- Secret.Api (Asp.NET webapi running on https://localhost:5001)
- Secret.Shared (classlib)
- Secret.WebApp (blazorwasm hosted by Secret.Api)
I develop running
dotnet watch runrunning in a terminal next to my editor. Sometimes when I change files (does not matter of which project), the dotnet watch process hangs up. The last output often looks something like this:
watch : Files changed: /path/to/repo/Secret/Secret.Api/SomeThing/SomeClass.cs, /path/to/repo/Secret.Api/Something/4913
The second file with the 4 digit numeric filename is only displayed when I use Vim I think, but the hangup happens independently of the editor. I think its filename is always 4913 and it apparently comes from the same directory as the file that got changed. When I hit Ctrl-C, this gets printed:
Unhandled exception. System.Threading.Tasks.TaskCanceledException: A task was canceled.
at Microsoft.DotNet.Watcher.Tools.BlazorWebAssemblyDeltaApplier.<>c__DisplayClass6_0.<<GetApplyUpdateCapabilitiesAsync>g__GetApplyUpdateCapabilitiesCoreAsync|0>d.MoveNext()
--- End of stack trace from previous location ---
at Microsoft.DotNet.Watcher.Tools.BlazorWebAssemblyHostedDeltaApplier.GetApplyUpdateCapabilitiesAsync(DotNetWatchContext context, CancellationToken cancellationToken)
at Microsoft.DotNet.Watcher.Tools.CompilationWorkspaceProvider.CreateProject(TaskCompletionSource`1 taskCompletionSource, Task`1 hotReloadCapabilitiesTask, String projectPath, IReporter reporter, CancellationToken cancellationToken)
at System.Threading.Tasks.Task.<>c.<ThrowAsync>b__127_1(Object state)
at System.Threading.QueueUserWorkItemCallbackDefaultContext.Execute()
at System.Threading.ThreadPoolWorkQueue.Dispatch()
at System.Threading.PortableThreadPool.WorkerThread.WorkerThreadStart()
at System.Threading.Thread.StartCallback()
Typing “reset” (while no characters are appearing in the console) fixes the broken terminal problem, but it happens quite frequently. Sometimes it happens many times in a row, and at other times I can make ~5 changes without it hanging up.
Configuration
dotnet 6.0.100-rc.1.21463.6 Tested with Linux Mint 20.2, Debian 12 (bookworm, testing), Windows 10, MacBook pro 2017 using Vim, VS Code, VS 2022 Preview All are x64 Since this is a blazor project: This happens independently of the browser (Firefox or Chromium).
Regression?
Since dotnet 5 did not have hot-reload capabilities, these problems did not exist back when we were using dotnet 5.
When I create a new blazorwasm project using dotnet new blazorwasm -o TestProject hot reloading and everything works as expected.
I have rewritten Properties/launchSettings.json, copied it over from a freshly created blazor WASM project because my only guess was the problems originated from there.
Also I tried to set hotReloadProfile in launchSettings.json to any possible value I could find in the web, used the profile explicitly with dotnet watch run --profile ..., but none of that had an effect.
Other information
When dotnet watch run detects a change and rebuilds the backend, the blazor WASM application does not reload at all.
The blazor error UI appears at the bottom of the page and this exception gets printed to the browser console:
Error: System.NullReferenceException: Object reference not set to an instance of an object.
at Microsoft.AspNetCore.Components.WebAssembly.HotReload.WebAssemblyHotReload.ApplyHotReloadDelta(String moduleIdString, Byte[] metadataDelta, Byte[] ilDeta)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
--- End of stack trace from previous location ---
at Microsoft.JSInterop.Infrastructure.DotNetDispatcher.InvokeSynchronously(JSRuntime jsRuntime, DotNetInvocationInfo& callInfo, IDotNetObjectReference objectReference, String argsJson)
at Microsoft.JSInterop.Infrastructure.DotNetDispatcher.Invoke(JSRuntime jsRuntime, DotNetInvocationInfo& invocationInfo, String argsJson)
at Microsoft.AspNetCore.Components.WebAssembly.Services.DefaultWebAssemblyJSRuntime.InvokeDotNet(String assemblyName, String methodIdentifier, String dotNetObjectId, String argsJson)
_convert_exception_for_method_call https://localhost:5001/_framework/dotnet.6.0.0-rc.1.21451.13.js:1
_handle_exception_for_call https://localhost:5001/_framework/dotnet.6.0.0-rc.1.21451.13.js:1
managed__Microsoft_AspNetCore_Components_WebAssembly__Microsoft_AspNetCore_Components_WebAssembly_Services_DefaultWebAssemblyJSRuntime_InvokeDotNet https://mono-wasm.invalid/managed__Microsoft_AspNetCore_Components_WebAssembly__Microsoft_AspNetCore_Components_WebAssembly_Services_DefaultWebAssemblyJSRuntime_InvokeDotNet:20
invokeDotNetFromJS https://localhost:5001/_framework/blazor.webassembly.js:1
g https://localhost:5001/_framework/blazor.webassembly.js:1
invokeMethod https://localhost:5001/_framework/blazor.webassembly.js:1
applyHotReload https://localhost:5001/_framework/blazor.webassembly.js:1
applyBlazorDeltas https://localhost:5001/_framework/aspnetcore-browser-refresh.js:122
applyBlazorDeltas https://localhost:5001/_framework/aspnetcore-browser-refresh.js:120
BlazorHotReloadDeltav1 https://localhost:5001/_framework/aspnetcore-browser-refresh.js:38
onmessage
Edit:
I should mention that I am running dotnet watch run in the directory of Secret.Api (which is hosting Secret.WebApp).
When running dotnet watch run in Secret.WebApp (which shows some CORS errors for obvious reasons but they don’t matter so far for testing), the page at least reloads when a razor page or component is modified.
But still no hot reload and still throwing exceptions.
This gets then printed to the browser console (now it is a index out of bounds error, not NullReferenceException):
RuntimeError: index out of bounds
_mono_wasm_invoke_method https://localhost:7233/_framework/dotnet.6.0.0-rc.1.21451.13.js:1
managed__Microsoft_AspNetCore_Components_WebAssembly__Microsoft_AspNetCore_Components_WebAssembly_Services_DefaultWebAssemblyJSRuntime_InvokeDotNet https://mono-wasm.invalid/managed__Microsoft_AspNetCore_Components_WebAssembly__Microsoft_AspNetCore_Components_WebAssembly_Services_DefaultWebAssemblyJSRuntime_InvokeDotNet:19
invokeDotNetFromJS https://localhost:7233/_framework/blazor.webassembly.js:1
g https://localhost:7233/_framework/blazor.webassembly.js:1
invokeMethod https://localhost:7233/_framework/blazor.webassembly.js:1
applyHotReload https://localhost:7233/_framework/blazor.webassembly.js:1
applyBlazorDeltas https://localhost:7233/_framework/aspnetcore-browser-refresh.js:122
applyBlazorDeltas https://localhost:7233/_framework/aspnetcore-browser-refresh.js:120
BlazorHotReloadDeltav1 https://localhost:7233/_framework/aspnetcore-browser-refresh.js:38
onmessage
Before any error is shown, this gets printed to the browser console:
Firefox can’t establish a connection to the server at wss://localhost:42689/.
Where the port in the connection error seems to be random. Immediately following this:
WebSocket failed to connect.
About this issue
- Original URL
- State: open
- Created 5 years ago
- Reactions: 10
- Comments: 15 (2 by maintainers)
I‘ve been experiencing similar behavior on my 2017 Mac Book Pro, running macOS 11.6.
@pranavkm rwobig93
In my case this did not change the result. I could see in the network tools that aspnetcore-browser-refresh.js was already being injected, prior to adding. After adding, it was just downloaded twice.
I have the same problem with my Setup
Adding script
<script src="/_framework/aspnetcore-browser-refresh.js"></script>worked for me!I was able to solve this issue for me with the following two steps:
1 Trust the dev certificate if you don’t have done so:
dotnet dev-certs https --trust2 Firefox users like me, need to create a policy to trust IIS Express/Kestrel developer certificates, instructions hereI ended up running into the same issue when migrating my Blazor wasm app over to .net 6. I stumbled upon the dotnet watch tutorial page for .net 6: https://docs.microsoft.com/en-us/aspnet/core/tutorials/dotnet-watch?view=aspnetcore-6.0 At the very bottom is a section talking about browser refresh and dotnet watch injecting a .js script (which I ended up putting into index.html) and this got hot reload working via dotnet watch for me, it however did not fix hot reload working in VS 2022 directly so my workaround atm is to explicitly use dotnet watch via the debug profile by using the SwitchStartupProject VS extension.
The script section they refer to that worked for me was this:
And call dotnet watch in the launchsettings.json (w/o run, adding run seems to break hot reload again by thinking no files are actually changed for whatever reason):