-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Hot Reload doen't work with UseResponseCompression Enabled #38832
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
Hot Reload is a hot mess. The reason it doesn't work with response compression enabled, is that there's an injector middleware at play which has to wedge a script tag into your response which loads the JavaScript-side of the hot reload infrastructure. It can't do that properly with response bodies which are compressed. With the SDK shipping with the release version of VS 2022 it actually does still attempt to do that and as a consequence will corrupt the compressed data stream in the response when it can find something resembling the insertion point it is written to check for in the compressed data. It's an input-dependent bug. Depending on how the compressor compresses your HTML output you'll see different misbehavior from this injector middleware: It also completely breaks any and all responses that are type There isn't even a proper way to turn the ASP.NET add-ons off and retain only the server-side C# hot reloading features. Even if you switch off the Hot Reload features under Imho this 'automagical' nature of wiring in a feature which applies potentially destructive mutations to markup and the browser runtime environment is return to WebForms mistakes from the past and whoever decided it was a good idea to include this injector middleware as part of defaults should get a boot up their ass. With the way they chose to integrate this, Microsoft managed to deliver a feature that is so broken you have to disable it and in doing so actually LOSE functionality (namely - the legacy edit & continue) compared to VS 2019 in the process. It's downright stupid. |
Duped to #38454. |
@DamianEdwards You're marking what is clearly a hideous bug in .NET 6 that corrupts response bodies when debugging from VS 2022 with response compression enabled; for which the only simple workaround is to disable and remove feature functionality from Visual Studio 2022, including features that worked fine in VS 2019 and earlier (namely: basic Edit & Continue) -- as a dupe of something that will only potentially be fixed with .NET 7? When .NET 6 is the actual LTS? Wow. Just let me ask here; what is wrong with a very basic Host
.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(builder => builder
.UseHotReload(false) // <--- (!!)
.UseStartup<Startup>()
); to prevent the ASP.NET hot-reload middleware features from being used for applications where they would be incompatible? Defaults; sure. [EDIT] Host
.CreateDefaultBuilder(args)
.ConfigureWebBuilder(builder => builder
.UseSetting(WebHostDefaults.HostingStartupExcludeAssembliesKey, "Microsoft.AspNetCore.Watch.BrowserRefresh")
.UseStartup<Startup>()
); Or if anyone else comes across this and is looking for something a bit more ergonomic: /// <summary>
/// Extends <see cref="IWebHostBuilder" /> with methods to allow or disallow the use
/// of the BrowserRefresh extensions that power the client-side ASP.NET parts of C# hot
/// reloading when connected to a compatible debugging host, like Visual Studio 2022.
/// </summary>
public static class BrowserRefreshWebHostBuilderExtensions
{
private const string ASSEMBLY_NAME = "Microsoft.AspNetCore.Watch.BrowserRefresh";
private static string ExclusionKey { get; } = WebHostDefaults.HostingStartupExcludeAssembliesKey;
/// <summary>
/// Specifies whether to allow use of the BrowserRefresh extensions that power the client-side
/// ASP.NET parts of C# hot reloading when connected to a compatible debugging host, like
/// Visual Studio 2022.
/// </summary>
/// <param name="hostBuilder">The <see cref="IWebHostBuilder"/> to configure.</param>
/// <param name="enabled">Whether the extensions should be enabled or not.</param>
/// <returns>The <see cref="IWebHostBuilder"/> to configure.</returns>
/// <exception cref="ArgumentNullException"><paramref name="hostBuilder"/> is <c>null</c>.</exception>
public static IWebHostBuilder UseBrowserRefresh(this IWebHostBuilder hostBuilder, bool enabled)
{
if (hostBuilder is null) throw new ArgumentNullException(nameof(hostBuilder));
var setting = hostBuilder.GetSetting(ExclusionKey) ?? "";
var excluded = setting
.Split(";", StringSplitOptions.RemoveEmptyEntries)
.ToHashSet(StringComparer.OrdinalIgnoreCase);
if (
( enabled && excluded.Remove(ASSEMBLY_NAME))
|| ( !enabled && excluded.Add(ASSEMBLY_NAME))
)
{
hostBuilder.UseSetting(ExclusionKey, string.Join(";", excluded));
}
return hostBuilder;
}
} |
Please see my comment in #32767 (comment) One can disable Hot Reload in the launch/debug profile of the project so that Edit and Continue keeps working but the browser refresh script is not injected. |
Hot Reload doen't work with UseResponseCompression Enabled.
It took me 3 hours to find it. If you don't plan to fix it please improve the docs.
hot-reload docs
related : 28293
The text was updated successfully, but these errors were encountered: