From 38ac21bb5a6c07780dcb15f110eee65b9dcc287c Mon Sep 17 00:00:00 2001 From: Sandro Ciervo Date: Mon, 28 Oct 2024 16:23:27 +0100 Subject: [PATCH 1/3] Consistently use AppContext.BaseDirectory as the configuration builder base path --- .../DotNetConsoleBuilder.cs | 24 +++++++++++-------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/Neolution.DotNet.Console/DotNetConsoleBuilder.cs b/Neolution.DotNet.Console/DotNetConsoleBuilder.cs index 27d635b..0f12938 100644 --- a/Neolution.DotNet.Console/DotNetConsoleBuilder.cs +++ b/Neolution.DotNet.Console/DotNetConsoleBuilder.cs @@ -1,4 +1,4 @@ -namespace Neolution.DotNet.Console +namespace Neolution.DotNet.Console { using System; using System.Collections.Generic; @@ -94,8 +94,13 @@ public DotNetConsole Build() /// internal static DotNetConsoleBuilder CreateBuilderInternal(Assembly assembly, Type[]? verbTypes, string[] args) { + // Create read-only configuration and environment that are only valid before the host is built, because we want to expose these as properties of our DotNetConsoleBuilder. + var environment = CreateConsoleEnvironment(args); + var configuration = CreateConsoleConfiguration(assembly, args, environment); + // Create a HostBuilder var builder = Host.CreateDefaultBuilder(args) + .UseContentRoot(environment.ContentRootPath) .ConfigureLogging((context, logging) => { AdjustDefaultBuilderLoggingProviders(logging); @@ -109,10 +114,6 @@ internal static DotNetConsoleBuilder CreateBuilderInternal(Assembly assembly, Ty .AsImplementedInterfaces()); }); - // Manually build configuration and environment again, because we unfortunately can't access them from the host builder we just created, but want to provide them in the ConsoleApplicationBuilder. - var environment = CreateConsoleEnvironment(args); - var configuration = ApplyDefaultConfiguration(assembly, args, environment); - // If verb types were not specified, compile all available verbs for this run by looking for classes with the Verb attribute in the specified assembly verbTypes ??= assembly.GetTypes() .Where(t => t.GetCustomAttribute() != null) @@ -172,12 +173,15 @@ private static DotNetConsoleEnvironment CreateConsoleEnvironment(string[] args) .AddCommandLine(args) .Build(); + // The apps root directory is where the appsettings.json are located + var appRootDirectory = AppContext.BaseDirectory; + return new DotNetConsoleEnvironment { - EnvironmentName = configuration[HostDefaults.EnvironmentKey] ?? "Production", + EnvironmentName = configuration[HostDefaults.EnvironmentKey] ?? Environments.Production, ApplicationName = AppDomain.CurrentDomain.FriendlyName, - ContentRootPath = AppContext.BaseDirectory, - ContentRootFileProvider = new PhysicalFileProvider(AppContext.BaseDirectory), + ContentRootPath = appRootDirectory, + ContentRootFileProvider = new PhysicalFileProvider(appRootDirectory), }; } @@ -188,10 +192,10 @@ private static DotNetConsoleEnvironment CreateConsoleEnvironment(string[] args) /// The arguments. /// The environment. /// The . - private static IConfiguration ApplyDefaultConfiguration(Assembly assembly, string[] args, IHostEnvironment environment) + private static IConfiguration CreateConsoleConfiguration(Assembly assembly, string[] args, IHostEnvironment environment) { var configurationBuilder = new ConfigurationBuilder() - .SetBasePath(AppContext.BaseDirectory) + .SetBasePath(environment.ContentRootPath) .AddEnvironmentVariables(prefix: "DOTNET_"); AddCommandLineConfig(configurationBuilder, args); From c6a69254296cc07917caba7dd4f730ded63f1f23 Mon Sep 17 00:00:00 2001 From: Sandro Ciervo Date: Mon, 28 Oct 2024 16:28:25 +0100 Subject: [PATCH 2/3] wording --- Neolution.DotNet.Console/DotNetConsoleBuilder.cs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Neolution.DotNet.Console/DotNetConsoleBuilder.cs b/Neolution.DotNet.Console/DotNetConsoleBuilder.cs index 0f12938..419c4b6 100644 --- a/Neolution.DotNet.Console/DotNetConsoleBuilder.cs +++ b/Neolution.DotNet.Console/DotNetConsoleBuilder.cs @@ -1,4 +1,4 @@ -namespace Neolution.DotNet.Console +namespace Neolution.DotNet.Console { using System; using System.Collections.Generic; @@ -94,7 +94,8 @@ public DotNetConsole Build() /// internal static DotNetConsoleBuilder CreateBuilderInternal(Assembly assembly, Type[]? verbTypes, string[] args) { - // Create read-only configuration and environment that are only valid before the host is built, because we want to expose these as properties of our DotNetConsoleBuilder. + // Create configuration and environment instances that are only valid before the host is built. + // We want to expose these as read-only properties in the DotNetConsoleBuilder. var environment = CreateConsoleEnvironment(args); var configuration = CreateConsoleConfiguration(assembly, args, environment); From bba64877dcb979c7d42b7187f34198cb12aaed46 Mon Sep 17 00:00:00 2001 From: Sandro Ciervo Date: Mon, 28 Oct 2024 16:40:02 +0100 Subject: [PATCH 3/3] Edit changelog --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1035e9e..dc11671 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Fixed + +- Resolved another case where appsettings.json was being loaded from the current working directory instead of the executable's directory. + ## [3.0.3] - 2024-10-09 ### Fixed