From 4ef0d1798b29d555adf3405b804a93ed25938bba Mon Sep 17 00:00:00 2001 From: Stephen Halter Date: Tue, 11 May 2021 16:03:19 -0700 Subject: [PATCH 1/2] Use WebRootKey to populate WebRootPath --- src/DefaultBuilder/samples/SampleApp/Program.cs | 10 +++++----- src/DefaultBuilder/src/ConfigureHostBuilder.cs | 2 +- src/DefaultBuilder/src/WebHostEnvironment.cs | 6 +++--- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/DefaultBuilder/samples/SampleApp/Program.cs b/src/DefaultBuilder/samples/SampleApp/Program.cs index a3d0028efaf1..51a4e81a0ef6 100644 --- a/src/DefaultBuilder/samples/SampleApp/Program.cs +++ b/src/DefaultBuilder/samples/SampleApp/Program.cs @@ -2,7 +2,6 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; -using System.Threading.Tasks; using Microsoft.AspNetCore; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; @@ -15,13 +14,14 @@ namespace SampleApp { public class Program { - public static async Task Main(string[] args) + public static void Main(string[] args) { - await using var webApp = WebApplication.Create(args); + var app = WebApplication.Create(args); - webApp.MapGet("/", (Func)(() => "Hello, World!")); + app.UseStaticFiles(); + app.MapGet("/", (Func)(() => "Hello, World!")); - await webApp.RunAsync(); + app.Run(); } private static void HelloWorld() diff --git a/src/DefaultBuilder/src/ConfigureHostBuilder.cs b/src/DefaultBuilder/src/ConfigureHostBuilder.cs index 00fae15083e8..73c9b7e03598 100644 --- a/src/DefaultBuilder/src/ConfigureHostBuilder.cs +++ b/src/DefaultBuilder/src/ConfigureHostBuilder.cs @@ -61,7 +61,7 @@ public IHostBuilder ConfigureHostConfiguration(Action con configureDelegate(_hostConfiguration); _environment.ApplyConfigurationSettings(_hostConfiguration.Build()); - Configuration.ChangeBasePath(_environment.ContentRootPath); + Configuration.ChangeFileProvider(_environment.ContentRootFileProvider); _operations += b => b.ConfigureHostConfiguration(configureDelegate); return this; diff --git a/src/DefaultBuilder/src/WebHostEnvironment.cs b/src/DefaultBuilder/src/WebHostEnvironment.cs index 828a59c6f62c..8a87a262be76 100644 --- a/src/DefaultBuilder/src/WebHostEnvironment.cs +++ b/src/DefaultBuilder/src/WebHostEnvironment.cs @@ -41,9 +41,9 @@ public WebHostEnvironment(Assembly? callingAssembly) public void ApplyConfigurationSettings(IConfiguration configuration) { ApplicationName = configuration[WebHostDefaults.ApplicationKey] ?? ApplicationName; - ContentRootPath = configuration[WebHostDefaults.ContentRootKey] ?? ContentRootPath; EnvironmentName = configuration[WebHostDefaults.EnvironmentKey] ?? EnvironmentName; - WebRootPath = configuration[WebHostDefaults.ContentRootKey] ?? WebRootPath; + ContentRootPath = configuration[WebHostDefaults.ContentRootKey] ?? ContentRootPath; + WebRootPath = configuration[WebHostDefaults.WebRootKey] ?? WebRootPath; ResolveFileProviders(configuration); } @@ -65,7 +65,7 @@ public void ResolveFileProviders(IConfiguration configuration) if (Directory.Exists(WebRootPath)) { - WebRootFileProvider = new PhysicalFileProvider(Path.Combine(ContentRootPath, WebRootPath)); + WebRootFileProvider = new PhysicalFileProvider(WebRootPath); } if (this.IsDevelopment()) From 7d69f21f8b8a0584c3158357765565097ee27dda Mon Sep 17 00:00:00 2001 From: Stephen Halter Date: Tue, 11 May 2021 17:07:03 -0700 Subject: [PATCH 2/2] Add WebHostEnvironmentTests --- .../src/Properties/AssemblyInfo.cs | 6 ++ src/DefaultBuilder/src/WebHostEnvironment.cs | 19 +++- .../WebHostEnvironmentTests.cs | 102 ++++++++++++++++++ 3 files changed, 125 insertions(+), 2 deletions(-) create mode 100644 src/DefaultBuilder/src/Properties/AssemblyInfo.cs create mode 100644 src/DefaultBuilder/test/Microsoft.AspNetCore.Tests/WebHostEnvironmentTests.cs diff --git a/src/DefaultBuilder/src/Properties/AssemblyInfo.cs b/src/DefaultBuilder/src/Properties/AssemblyInfo.cs new file mode 100644 index 000000000000..97ac339e54a9 --- /dev/null +++ b/src/DefaultBuilder/src/Properties/AssemblyInfo.cs @@ -0,0 +1,6 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System.Runtime.CompilerServices; + +[assembly: InternalsVisibleTo("Microsoft.AspNetCore.Tests, PublicKey=0024000004800000940000000602000000240000525341310004000001000100f33a29044fa9d740c9b3213a93e57c84b472c84e0b8a0e1ae48e67a9f8f6de9d5f7f3d52ac23e48ac51801f1dc950abe901da34d2a9e3baadb141a17c77ef3c565dd5ee5054b91cf63bb3c6ab83f72ab3aafe93d0fc3c2348b764fafb0b1c0733de51459aeab46580384bf9d74c4e28164b7cde247f891ba07891c9d872ad2bb")] diff --git a/src/DefaultBuilder/src/WebHostEnvironment.cs b/src/DefaultBuilder/src/WebHostEnvironment.cs index 8a87a262be76..052764c2ea63 100644 --- a/src/DefaultBuilder/src/WebHostEnvironment.cs +++ b/src/DefaultBuilder/src/WebHostEnvironment.cs @@ -38,14 +38,29 @@ public WebHostEnvironment(Assembly? callingAssembly) ResolveFileProviders(new Configuration()); } + // For testing + internal WebHostEnvironment() + { + ApplicationName = default!; + EnvironmentName = default!; + ContentRootPath = default!; + WebRootPath = default!; + ContentRootFileProvider = default!; + WebRootFileProvider = default!; + } + public void ApplyConfigurationSettings(IConfiguration configuration) + { + ReadConfigurationSettings(configuration); + ResolveFileProviders(configuration); + } + + internal void ReadConfigurationSettings(IConfiguration configuration) { ApplicationName = configuration[WebHostDefaults.ApplicationKey] ?? ApplicationName; EnvironmentName = configuration[WebHostDefaults.EnvironmentKey] ?? EnvironmentName; ContentRootPath = configuration[WebHostDefaults.ContentRootKey] ?? ContentRootPath; WebRootPath = configuration[WebHostDefaults.WebRootKey] ?? WebRootPath; - - ResolveFileProviders(configuration); } public void ApplyEnvironmentSettings(IWebHostBuilder genericWebHostBuilder) diff --git a/src/DefaultBuilder/test/Microsoft.AspNetCore.Tests/WebHostEnvironmentTests.cs b/src/DefaultBuilder/test/Microsoft.AspNetCore.Tests/WebHostEnvironmentTests.cs new file mode 100644 index 000000000000..369a11a61837 --- /dev/null +++ b/src/DefaultBuilder/test/Microsoft.AspNetCore.Tests/WebHostEnvironmentTests.cs @@ -0,0 +1,102 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System; +using System.Collections.Generic; +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Hosting; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Xunit; + +namespace Microsoft.AspNetCore.Tests +{ + public class WebHostEnvironmentTests + { + [Fact] + public void ApplyConfigurationSettingsUsesTheCorrectKeys() + { + var configBuilder = new ConfigurationBuilder(); + configBuilder.AddInMemoryCollection(new[] + { + new KeyValuePair(WebHostDefaults.ApplicationKey, WebHostDefaults.ApplicationKey), + new KeyValuePair(WebHostDefaults.EnvironmentKey, WebHostDefaults.EnvironmentKey), + new KeyValuePair(WebHostDefaults.ContentRootKey, WebHostDefaults.ContentRootKey), + new KeyValuePair(WebHostDefaults.WebRootKey, WebHostDefaults.WebRootKey), + }); + + var env = new WebHostEnvironment(); + + // Basically call ApplyConfigurationSettings(config) but without creating PhysicalFileProviders. + env.ReadConfigurationSettings(configBuilder.Build()); + + Assert.Equal(WebHostDefaults.ApplicationKey, env.ApplicationName); + Assert.Equal(WebHostDefaults.EnvironmentKey, env.EnvironmentName); + Assert.Equal(WebHostDefaults.ContentRootKey, env.ContentRootPath); + Assert.Equal(WebHostDefaults.WebRootKey, env.WebRootPath); + } + + [Fact] + public void ApplyEnvironmentSettingsUsesTheCorrectKeys() + { + var env = new WebHostEnvironment + { + ApplicationName = WebHostDefaults.ApplicationKey, + EnvironmentName = WebHostDefaults.EnvironmentKey, + ContentRootPath = WebHostDefaults.ContentRootKey, + WebRootPath = WebHostDefaults.WebRootKey, + }; + + var settings = new Dictionary(); + + env.ApplyEnvironmentSettings(new TestWebHostBuilder(settings)); + + Assert.Equal(WebHostDefaults.ApplicationKey, settings[WebHostDefaults.ApplicationKey]); + Assert.Equal(WebHostDefaults.EnvironmentKey, settings[WebHostDefaults.EnvironmentKey]); + Assert.Equal(WebHostDefaults.ContentRootKey, settings[WebHostDefaults.ContentRootKey]); + Assert.Equal(WebHostDefaults.WebRootKey, settings[WebHostDefaults.WebRootKey]); + } + + private class TestWebHostBuilder : IWebHostBuilder + { + private readonly Dictionary _settings; + + public TestWebHostBuilder(Dictionary settingsDictionary) + { + _settings = settingsDictionary; + } + + public IWebHost Build() + { + throw new NotImplementedException(); + } + + public IWebHostBuilder ConfigureAppConfiguration(Action configureDelegate) + { + throw new NotImplementedException(); + } + + public IWebHostBuilder ConfigureServices(Action configureServices) + { + throw new NotImplementedException(); + } + + public IWebHostBuilder ConfigureServices(Action configureServices) + { + throw new NotImplementedException(); + } + + public string GetSetting(string key) + { + _settings.TryGetValue(key, out var value); + return value; + } + + public IWebHostBuilder UseSetting(string key, string value) + { + _settings[key] = value; + return this; + } + } + } +}