Auto-refreshing the browser in .NET 5 using dotnet watch run doesn't work with response compression
See original GitHub issueDescribe the bug
Hmmm, auto-refreshing the browser in .NET 5 using dotnet watch run doesn’t seem to be working for me. I used to use Westwind.AspNetCore.LiveReload, but I have removed it to try this out. When I make a change, dotnet watch rebuilds and restarts the web server for me, but the browser doesn’t refresh.
I am using dotnet watch --project MyProject\Server\MyProject.Server.csproj run
.
I never see dotnet-watch reload socket connected
in the console. Of course, I also never see File changes detected. Waiting for application to rebuild.
.
This is a project I upgraded from .NET Core 3.1. Is there anything that needs to be done in program.cs or startup.cs to enable this? What else could be interfering? How can I troubleshoot?
To Reproduce
On my project, I can reproduce by running it using dotnet watch --project MyProject\Server\MyProject.Server.csproj run
and noticing that I never see dotnet-watch reload socket connected
in the console.
I cannot reproduce it on a new project; auto-reload seems to work when I do dotnet watch run
.
- ASP.NET Core version 5.0.0
- The output of
dotnet --info
.NET SDK (reflecting any global.json):
Version: 5.0.100
Commit: 5044b93829
Runtime Environment:
OS Name: Windows
OS Version: 10.0.18363
OS Platform: Windows
RID: win10-x64
Base Path: C:\Program Files\dotnet\sdk\5.0.100\
Host (useful for support):
Version: 5.0.0
Commit: cf258a14b7
.NET SDKs installed:
5.0.100 [C:\Program Files\dotnet\sdk]
.NET runtimes installed:
Microsoft.AspNetCore.All 2.1.23 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.App 2.1.23 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 3.1.9 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 5.0.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.NETCore.App 2.1.23 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 3.1.9 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 5.0.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.WindowsDesktop.App 3.1.9 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 5.0.0 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
- The IDE (VS / VS Code/ VS4Mac) you’re running on, and it’s version VS Pro 16.8.2
-Here’s my launchSettings.json:
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:49342",
"sslPort": 44324
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"inspectUri": "{wsProtocol}://{url.hostname}:{url.port}/_framework/debug/ws-proxy?browser={browserInspectUri}",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"MyProject": {
"commandName": "Project",
"launchBrowser": true,
"inspectUri": "{wsProtocol}://{url.hostname}:{url.port}/_framework/debug/ws-proxy?browser={browserInspectUri}",
"applicationUrl": "https://localhost:5001;http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
Issue Analytics
- State:
- Created 3 years ago
- Reactions:2
- Comments:15 (5 by maintainers)
I’m making the assumption that @szalapski is running into the same issue I did based on his comments here: https://github.com/RickStrahl/Westwind.AspnetCore.LiveReload/issues/38#issuecomment-701872458
If not, please pardon the interruption and I’ll create a new issue.
Using Response Compression causes the following to appear in the dotnet watch debugging output
I’m guessing this is due to ResponseCompressionBody obfuscating the body close tag WebSocketScriptInjection is looking for.
As a workaround, reloading is working fine with the following in the Startup.cs Configure method
Also, thank you for the ongoing efforts to improve the “frontend” developer inner-loop in ASP.Net Core, it’s greatly appreciated!
The middleware gets wired up using startup hooks and hosting startup (👻 magic) so you don’t really need to do anything to set it up.
The middleware specifically looks for the closing
body
tag to determine where to inject the WebSocket script block that listens todotnet watch
. The code is very much inspired from Westwind.AspNetCore.LiveReload, but it’s possible that there’s a bug in our implementation. The middleware runs as part of your app and includesDebug
logs. You could try changing the log level for theMicrosoft.AspNetCore.Watch.BrowserRefresh
filter toDebug
to have it print it out.Alternatively, if you’re able to share the app I’d be happy to debug.