Просмотр исходного кода

Fix diff checks for ContentRoot and WebRoot (#40095)

feiyun0112 4 лет назад
Родитель
Сommit
edca74858d

+ 3 - 1
src/DefaultBuilder/src/ConfigureHostBuilder.cs

@@ -62,6 +62,7 @@ public sealed class ConfigureHostBuilder : IHostBuilder, ISupportsConfigureWebHo
         var previousApplicationName = _configuration[HostDefaults.ApplicationKey];
         var previousApplicationName = _configuration[HostDefaults.ApplicationKey];
         // Use the real content root so we can compare paths
         // Use the real content root so we can compare paths
         var previousContentRoot = _context.HostingEnvironment.ContentRootPath;
         var previousContentRoot = _context.HostingEnvironment.ContentRootPath;
+        var previousContentRootConfig = _configuration[HostDefaults.ContentRootKey];
         var previousEnvironment = _configuration[HostDefaults.EnvironmentKey];
         var previousEnvironment = _configuration[HostDefaults.EnvironmentKey];
 
 
         // Run these immediately so that they are observable by the imperative code
         // Run these immediately so that they are observable by the imperative code
@@ -74,7 +75,8 @@ public sealed class ConfigureHostBuilder : IHostBuilder, ISupportsConfigureWebHo
             throw new NotSupportedException($"The application name changed from \"{previousApplicationName}\" to \"{_configuration[HostDefaults.ApplicationKey]}\". Changing the host configuration using WebApplicationBuilder.Host is not supported. Use WebApplication.CreateBuilder(WebApplicationOptions) instead.");
             throw new NotSupportedException($"The application name changed from \"{previousApplicationName}\" to \"{_configuration[HostDefaults.ApplicationKey]}\". Changing the host configuration using WebApplicationBuilder.Host is not supported. Use WebApplication.CreateBuilder(WebApplicationOptions) instead.");
         }
         }
 
 
-        if (!string.Equals(previousContentRoot, HostingPathResolver.ResolvePath(_configuration[HostDefaults.ContentRootKey]), StringComparison.OrdinalIgnoreCase))
+        if (!string.Equals(previousContentRootConfig, _configuration[HostDefaults.ContentRootKey], StringComparison.OrdinalIgnoreCase)
+            && !string.Equals(previousContentRoot, HostingPathResolver.ResolvePath(_configuration[HostDefaults.ContentRootKey]), StringComparison.OrdinalIgnoreCase))
         {
         {
             throw new NotSupportedException($"The content root changed from \"{previousContentRoot}\" to \"{HostingPathResolver.ResolvePath(_configuration[HostDefaults.ContentRootKey])}\". Changing the host configuration using WebApplicationBuilder.Host is not supported. Use WebApplication.CreateBuilder(WebApplicationOptions) instead.");
             throw new NotSupportedException($"The content root changed from \"{previousContentRoot}\" to \"{HostingPathResolver.ResolvePath(_configuration[HostDefaults.ContentRootKey])}\". Changing the host configuration using WebApplicationBuilder.Host is not supported. Use WebApplication.CreateBuilder(WebApplicationOptions) instead.");
         }
         }

+ 6 - 2
src/DefaultBuilder/src/ConfigureWebHostBuilder.cs

@@ -37,7 +37,9 @@ public sealed class ConfigureWebHostBuilder : IWebHostBuilder, ISupportsStartup
     public IWebHostBuilder ConfigureAppConfiguration(Action<WebHostBuilderContext, IConfigurationBuilder> configureDelegate)
     public IWebHostBuilder ConfigureAppConfiguration(Action<WebHostBuilderContext, IConfigurationBuilder> configureDelegate)
     {
     {
         var previousContentRoot = _context.HostingEnvironment.ContentRootPath;
         var previousContentRoot = _context.HostingEnvironment.ContentRootPath;
+        var previousContentRootConfig = _configuration[WebHostDefaults.ContentRootKey];
         var previousWebRoot = _context.HostingEnvironment.WebRootPath;
         var previousWebRoot = _context.HostingEnvironment.WebRootPath;
+        var previousWebRootConfig = _configuration[WebHostDefaults.WebRootKey];
         var previousApplication = _configuration[WebHostDefaults.ApplicationKey];
         var previousApplication = _configuration[WebHostDefaults.ApplicationKey];
         var previousEnvironment = _configuration[WebHostDefaults.EnvironmentKey];
         var previousEnvironment = _configuration[WebHostDefaults.EnvironmentKey];
         var previousHostingStartupAssemblies = _configuration[WebHostDefaults.HostingStartupAssembliesKey];
         var previousHostingStartupAssemblies = _configuration[WebHostDefaults.HostingStartupAssembliesKey];
@@ -46,7 +48,8 @@ public sealed class ConfigureWebHostBuilder : IWebHostBuilder, ISupportsStartup
         // Run these immediately so that they are observable by the imperative code
         // Run these immediately so that they are observable by the imperative code
         configureDelegate(_context, _configuration);
         configureDelegate(_context, _configuration);
 
 
-        if (!string.Equals(HostingPathResolver.ResolvePath(previousWebRoot, previousContentRoot), HostingPathResolver.ResolvePath(_configuration[WebHostDefaults.WebRootKey], previousContentRoot), StringComparison.OrdinalIgnoreCase))
+        if (!string.Equals(previousWebRootConfig, _configuration[WebHostDefaults.WebRootKey], StringComparison.OrdinalIgnoreCase)
+            && !string.Equals(HostingPathResolver.ResolvePath(previousWebRoot, previousContentRoot), HostingPathResolver.ResolvePath(_configuration[WebHostDefaults.WebRootKey], previousContentRoot), StringComparison.OrdinalIgnoreCase))
         {
         {
             // Diasllow changing the web root for consistency with other types.
             // Diasllow changing the web root for consistency with other types.
             throw new NotSupportedException($"The web root changed from \"{HostingPathResolver.ResolvePath(previousWebRoot, previousContentRoot)}\" to \"{HostingPathResolver.ResolvePath(_configuration[WebHostDefaults.WebRootKey], previousContentRoot)}\". Changing the host configuration using WebApplicationBuilder.WebHost is not supported. Use WebApplication.CreateBuilder(WebApplicationOptions) instead.");
             throw new NotSupportedException($"The web root changed from \"{HostingPathResolver.ResolvePath(previousWebRoot, previousContentRoot)}\" to \"{HostingPathResolver.ResolvePath(_configuration[WebHostDefaults.WebRootKey], previousContentRoot)}\". Changing the host configuration using WebApplicationBuilder.WebHost is not supported. Use WebApplication.CreateBuilder(WebApplicationOptions) instead.");
@@ -56,7 +59,8 @@ public sealed class ConfigureWebHostBuilder : IWebHostBuilder, ISupportsStartup
             // Disallow changing any host configuration
             // Disallow changing any host configuration
             throw new NotSupportedException($"The application name changed from \"{previousApplication}\" to \"{_configuration[WebHostDefaults.ApplicationKey]}\". Changing the host configuration using WebApplicationBuilder.WebHost is not supported. Use WebApplication.CreateBuilder(WebApplicationOptions) instead.");
             throw new NotSupportedException($"The application name changed from \"{previousApplication}\" to \"{_configuration[WebHostDefaults.ApplicationKey]}\". Changing the host configuration using WebApplicationBuilder.WebHost is not supported. Use WebApplication.CreateBuilder(WebApplicationOptions) instead.");
         }
         }
-        else if (!string.Equals(previousContentRoot, HostingPathResolver.ResolvePath(_configuration[WebHostDefaults.ContentRootKey]), StringComparison.OrdinalIgnoreCase))
+        else if (!string.Equals(previousContentRootConfig, _configuration[WebHostDefaults.ContentRootKey], StringComparison.OrdinalIgnoreCase)
+            && !string.Equals(previousContentRoot, HostingPathResolver.ResolvePath(_configuration[WebHostDefaults.ContentRootKey]), StringComparison.OrdinalIgnoreCase))
         {
         {
             // Disallow changing any host configuration
             // Disallow changing any host configuration
             throw new NotSupportedException($"The content root changed from \"{previousContentRoot}\" to \"{HostingPathResolver.ResolvePath(_configuration[WebHostDefaults.ContentRootKey])}\". Changing the host configuration using WebApplicationBuilder.WebHost is not supported. Use WebApplication.CreateBuilder(WebApplicationOptions) instead.");
             throw new NotSupportedException($"The content root changed from \"{previousContentRoot}\" to \"{HostingPathResolver.ResolvePath(_configuration[WebHostDefaults.ContentRootKey])}\". Changing the host configuration using WebApplicationBuilder.WebHost is not supported. Use WebApplication.CreateBuilder(WebApplicationOptions) instead.");

+ 30 - 0
src/DefaultBuilder/test/Microsoft.AspNetCore.Tests/WebApplicationTests.cs

@@ -1690,6 +1690,36 @@ public class WebApplicationTests
         Assert.Equal(0b00000111, callbacks);
         Assert.Equal(0b00000111, callbacks);
     }
     }
 
 
+    [Fact]
+    public void EmptyAppConfiguration()
+    {
+        var wwwroot = Path.Combine(AppContext.BaseDirectory, "wwwroot");
+        bool createdDirectory = false;
+        if (!Directory.Exists(wwwroot))
+        {
+            createdDirectory = true;
+            Directory.CreateDirectory(wwwroot);
+        }
+
+        try
+        {
+            var builder = WebApplication.CreateBuilder();
+
+            builder.WebHost.ConfigureAppConfiguration((ctx, config) => { });
+
+            using var app = builder.Build();
+            var hostEnv = app.Services.GetRequiredService<Hosting.IWebHostEnvironment>();
+            Assert.Equal(wwwroot, hostEnv.WebRootPath);
+        }
+        finally
+        {
+            if (createdDirectory)
+            {
+                Directory.Delete(wwwroot);
+            }
+        }
+    }
+
     [Fact]
     [Fact]
     public void HostConfigurationNotAffectedByConfiguration()
     public void HostConfigurationNotAffectedByConfiguration()
     {
     {