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 diff --git a/Neolution.DotNet.Console/DotNetConsoleBuilder.cs b/Neolution.DotNet.Console/DotNetConsoleBuilder.cs index 27d635b..419c4b6 100644 --- a/Neolution.DotNet.Console/DotNetConsoleBuilder.cs +++ b/Neolution.DotNet.Console/DotNetConsoleBuilder.cs @@ -94,8 +94,14 @@ public DotNetConsole Build() /// internal static DotNetConsoleBuilder CreateBuilderInternal(Assembly assembly, Type[]? verbTypes, string[] args) { + // 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); + // Create a HostBuilder var builder = Host.CreateDefaultBuilder(args) + .UseContentRoot(environment.ContentRootPath) .ConfigureLogging((context, logging) => { AdjustDefaultBuilderLoggingProviders(logging); @@ -109,10 +115,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 +174,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 +193,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);