Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 14 additions & 9 deletions Neolution.DotNet.Console/DotNetConsoleBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,14 @@ public DotNetConsole Build()
/// </returns>
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);
Expand All @@ -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<VerbAttribute>() != null)
Expand Down Expand Up @@ -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),
};
}

Expand All @@ -188,10 +193,10 @@ private static DotNetConsoleEnvironment CreateConsoleEnvironment(string[] args)
/// <param name="args">The arguments.</param>
/// <param name="environment">The environment.</param>
/// <returns>The <see cref="IConfiguration" />.</returns>
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);
Expand Down