@@ -94,8 +94,14 @@ public DotNetConsole Build()
9494 /// </returns>
9595 internal static DotNetConsoleBuilder CreateBuilderInternal ( Assembly assembly , Type [ ] ? verbTypes , string [ ] args )
9696 {
97+ // Create configuration and environment instances that are only valid before the host is built.
98+ // We want to expose these as read-only properties in the DotNetConsoleBuilder.
99+ var environment = CreateConsoleEnvironment ( args ) ;
100+ var configuration = CreateConsoleConfiguration ( assembly , args , environment ) ;
101+
97102 // Create a HostBuilder
98103 var builder = Host . CreateDefaultBuilder ( args )
104+ . UseContentRoot ( environment . ContentRootPath )
99105 . ConfigureLogging ( ( context , logging ) =>
100106 {
101107 AdjustDefaultBuilderLoggingProviders ( logging ) ;
@@ -109,10 +115,6 @@ internal static DotNetConsoleBuilder CreateBuilderInternal(Assembly assembly, Ty
109115 . AsImplementedInterfaces ( ) ) ;
110116 } ) ;
111117
112- // 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.
113- var environment = CreateConsoleEnvironment ( args ) ;
114- var configuration = ApplyDefaultConfiguration ( assembly , args , environment ) ;
115-
116118 // 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
117119 verbTypes ??= assembly . GetTypes ( )
118120 . Where ( t => t . GetCustomAttribute < VerbAttribute > ( ) != null )
@@ -172,12 +174,15 @@ private static DotNetConsoleEnvironment CreateConsoleEnvironment(string[] args)
172174 . AddCommandLine ( args )
173175 . Build ( ) ;
174176
177+ // The apps root directory is where the appsettings.json are located
178+ var appRootDirectory = AppContext . BaseDirectory ;
179+
175180 return new DotNetConsoleEnvironment
176181 {
177- EnvironmentName = configuration [ HostDefaults . EnvironmentKey ] ?? " Production" ,
182+ EnvironmentName = configuration [ HostDefaults . EnvironmentKey ] ?? Environments . Production ,
178183 ApplicationName = AppDomain . CurrentDomain . FriendlyName ,
179- ContentRootPath = AppContext . BaseDirectory ,
180- ContentRootFileProvider = new PhysicalFileProvider ( AppContext . BaseDirectory ) ,
184+ ContentRootPath = appRootDirectory ,
185+ ContentRootFileProvider = new PhysicalFileProvider ( appRootDirectory ) ,
181186 } ;
182187 }
183188
@@ -188,10 +193,10 @@ private static DotNetConsoleEnvironment CreateConsoleEnvironment(string[] args)
188193 /// <param name="args">The arguments.</param>
189194 /// <param name="environment">The environment.</param>
190195 /// <returns>The <see cref="IConfiguration" />.</returns>
191- private static IConfiguration ApplyDefaultConfiguration ( Assembly assembly , string [ ] args , IHostEnvironment environment )
196+ private static IConfiguration CreateConsoleConfiguration ( Assembly assembly , string [ ] args , IHostEnvironment environment )
192197 {
193198 var configurationBuilder = new ConfigurationBuilder ( )
194- . SetBasePath ( AppContext . BaseDirectory )
199+ . SetBasePath ( environment . ContentRootPath )
195200 . AddEnvironmentVariables ( prefix : "DOTNET_" ) ;
196201
197202 AddCommandLineConfig ( configurationBuilder , args ) ;
0 commit comments