question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Auto-refreshing the browser in .NET 5 using dotnet watch run doesn't work with response compression

See original GitHub issue

Describe 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:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:15 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
harvey-kcommented, Dec 23, 2020

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

dbug: Microsoft.AspNetCore.Watch.BrowserRefresh.BrowserRefreshMiddleware[3]
      Unable to configure browser refresh script injection on the response.

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

if(!env.IsDevelopment()) // response compression currently conflicts with dotnet watch browser reload
{
     app.UseResponseCompression();
}

Also, thank you for the ongoing efforts to improve the “frontend” developer inner-loop in ASP.Net Core, it’s greatly appreciated!

1reaction
pranavkmcommented, Dec 2, 2020

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 to dotnet 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 includes Debug logs. You could try changing the log level for the Microsoft.AspNetCore.Watch.BrowserRefresh filter to Debug to have it print it out.

Alternatively, if you’re able to share the app I’d be happy to debug.

Read more comments on GitHub >

github_iconTop Results From Across the Web

dotnet watch command - .NET CLI
The dotnet watch command is a file watcher that runs a dotnet command when changes in source code are detected.
Read more >
dotnet watch run doesnt reload browser - Umbraco 9
The only thing that it doesn't do on .NET 5 is refresh your browser, after you see File changes detected. Waiting for application...
Read more >
How to automatically reload .NET Core project in Visual ...
1) Create.Net core application. 2) Open a command Window in your Web project's folder. 3) Type dotnet watch run. 4) Open your browser...
Read more >
Auto refresh with dotnet watch for ASP .NET Core projects
1) Go to Tools > ⚙ Options > Projects and Solutions > ASP .NET Core · 2) Select Auto build and refresh browser...
Read more >
How “dotnet watch run” auto refreshes your site? - flauberjp
Bad news first, “dotnet watch run” doesn't auto refresh your site, at least not yet. Good ones: we have a solution for that...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found