From d2f76bdae54946cd6e0085aff2b53ae7bf417235 Mon Sep 17 00:00:00 2001 From: Mayuki Sawatari Date: Sun, 14 Apr 2019 23:39:00 +0900 Subject: [PATCH] fix: Use ConfigureHostConfiguration to initialize GenericHost. --- src/MicroBatchFramework/BatchHost.cs | 29 +++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/src/MicroBatchFramework/BatchHost.cs b/src/MicroBatchFramework/BatchHost.cs index 8aedb96..4cbfbbe 100644 --- a/src/MicroBatchFramework/BatchHost.cs +++ b/src/MicroBatchFramework/BatchHost.cs @@ -6,6 +6,7 @@ using System; using System.IO; using System.Reflection; +using System.Collections.Generic; namespace MicroBatchFramework { @@ -64,24 +65,34 @@ public static IHostBuilder CreateDefaultBuilder(bool useSimpleConosoleLogger, Lo { var builder = new HostBuilder(); - builder.UseContentRoot(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)); - ConfigureAppConfigurationDefault(builder, hostEnvironmentVariable); + ConfigureHostConfigurationDefault(builder, hostEnvironmentVariable); + ConfigureAppConfigurationDefault(builder); ConfigureLoggingDefault(builder, useSimpleConosoleLogger, minSimpleConsoleLoggerLogLevel); return builder; } - internal static void ConfigureAppConfigurationDefault(IHostBuilder builder, string hostEnvironmentVariable) + internal static void ConfigureHostConfigurationDefault(IHostBuilder builder, string hostEnvironmentVariable) + { + builder.UseContentRoot(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)); + + builder.ConfigureHostConfiguration(config => + { + config.AddEnvironmentVariables(prefix: "NETCORE_"); + config.AddInMemoryCollection(new[] { new KeyValuePair(HostDefaults.ApplicationKey, Assembly.GetExecutingAssembly().GetName().Name) }); + }); + + if (!string.IsNullOrWhiteSpace(hostEnvironmentVariable)) + { + builder.UseEnvironment(System.Environment.GetEnvironmentVariable(hostEnvironmentVariable) ?? "Production"); + } + } + + internal static void ConfigureAppConfigurationDefault(IHostBuilder builder) { builder.ConfigureAppConfiguration((hostingContext, config) => { var env = hostingContext.HostingEnvironment; - env.ApplicationName = Assembly.GetExecutingAssembly().GetName().Name; - if (string.IsNullOrWhiteSpace(hostEnvironmentVariable)) - { - hostEnvironmentVariable = "NETCORE_ENVIRONMENT"; - } - env.EnvironmentName = System.Environment.GetEnvironmentVariable(hostEnvironmentVariable) ?? "Production"; config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true); config.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true);