-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Auto-refreshing the browser in .NET 5 using dotnet watch run doesn't work with response compression #28293
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Created this issue based on #5456 (comment) |
@danroth27 asked, "Does auto refresh work if you start from a new project? Have you set "launchBrowser": true in launchSettings.json?" Yes, and I do have "launchBrowser": true. |
@szalapski could you run |
With --verbose, my command line shows me: But of course the browser doesn't reload. I still I never see Isn't there some implied middleware or something that is needed for this to work? It seems a bit too black-box for me to troubleshoot. |
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 Alternatively, if you're able to share the app I'd be happy to debug. |
I have Debug on and I don't see any relevant console messages. I'll turn it on more selectively. UPDATE: it seems there are no logevents out of Microsoft.AspNetCore.Watch.BrowserRefresh, at Debug log level. I really think the relevant code isn't even running inside my site. I can't publish the site publicly but I could share over MS Teams. |
Here's my host config. Does anything here raise a flag?
|
Thanks for contacting us. |
I'm making the assumption that @szalapski is running into the same issue I did based on his comments here: 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! |
Interesting that it could be a similar issue as we ran into in Westwind's live reload--good thought. When I enable debug-level logging, I do indeed get I tried commenting out response compression; the message went away, and instead I got I suppose the solution, @pranavkm, is that you must ensure that BrowserRefreshMiddleware can be compatible with UseResponseCompression--I think it must happen after UseResponseCompression in the pipeline, even though we don't have a line for "UseBrowserRefreshMiddleware" in Startup.cs. Do you think you can fix it now? |
Any thoughts, @pranavkm ? |
I have the same experience. Rebuilding happens, and |
Sorry, I missed responding to this earlier. The way dotnet-watch works refreshes the browser is by a) Adding a middleware using a hosting startup to register a middleware Hosting startups only allow registering middlewares at the beginning or end of the middleware pipeline, so we couldn't choose to register a middleware to be invoked before response compression happens. Response compression changes the app's content-type to no longer be plain text, so the dotnet-watch middleware is no longer able to operate. A couple of possible workarounds:
if (!env.IsDevelopment()) // response compression currently conflicts with dotnet watch browser reload
{
app.UseResponseCompression();
}
// _Layout.cshtml
<environment names="Development">
<script src="/_framework/aspnetcore-browser-refresh.js"></script>
</environment> For option (2), please note that if the script was successfully injected by dotnet-watch, things might be broken. So only use if if you're able to verify that watch is unable to inject it. |
OK, that seems a fine workaround, though it is a little surprising to an outsider that these two features wouldn't just work well together. I do indeed recommend documenting the approach you prefer (I think #1). But thanks regardless. |
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 seeFile 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 seedotnet-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
.dotnet --info
VS Pro 16.8.2
-Here's my launchSettings.json:
The text was updated successfully, but these errors were encountered: