Skip to content

Commit 4546b64

Browse files
authored
Merge pull request #12 from mayuki/UseConfigureHostConfiguration
Use ConfigureHostConfiguration to initialize GenericHost.
2 parents 43660e1 + d2f76bd commit 4546b64

File tree

1 file changed

+20
-9
lines changed

1 file changed

+20
-9
lines changed

src/MicroBatchFramework/BatchHost.cs

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System;
77
using System.IO;
88
using System.Reflection;
9+
using System.Collections.Generic;
910

1011
namespace MicroBatchFramework
1112
{
@@ -64,24 +65,34 @@ public static IHostBuilder CreateDefaultBuilder(bool useSimpleConosoleLogger, Lo
6465
{
6566
var builder = new HostBuilder();
6667

67-
builder.UseContentRoot(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location));
68-
ConfigureAppConfigurationDefault(builder, hostEnvironmentVariable);
68+
ConfigureHostConfigurationDefault(builder, hostEnvironmentVariable);
69+
ConfigureAppConfigurationDefault(builder);
6970
ConfigureLoggingDefault(builder, useSimpleConosoleLogger, minSimpleConsoleLoggerLogLevel);
7071

7172
return builder;
7273
}
7374

74-
internal static void ConfigureAppConfigurationDefault(IHostBuilder builder, string hostEnvironmentVariable)
75+
internal static void ConfigureHostConfigurationDefault(IHostBuilder builder, string hostEnvironmentVariable)
76+
{
77+
builder.UseContentRoot(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location));
78+
79+
builder.ConfigureHostConfiguration(config =>
80+
{
81+
config.AddEnvironmentVariables(prefix: "NETCORE_");
82+
config.AddInMemoryCollection(new[] { new KeyValuePair<string, string>(HostDefaults.ApplicationKey, Assembly.GetExecutingAssembly().GetName().Name) });
83+
});
84+
85+
if (!string.IsNullOrWhiteSpace(hostEnvironmentVariable))
86+
{
87+
builder.UseEnvironment(System.Environment.GetEnvironmentVariable(hostEnvironmentVariable) ?? "Production");
88+
}
89+
}
90+
91+
internal static void ConfigureAppConfigurationDefault(IHostBuilder builder)
7592
{
7693
builder.ConfigureAppConfiguration((hostingContext, config) =>
7794
{
7895
var env = hostingContext.HostingEnvironment;
79-
env.ApplicationName = Assembly.GetExecutingAssembly().GetName().Name;
80-
if (string.IsNullOrWhiteSpace(hostEnvironmentVariable))
81-
{
82-
hostEnvironmentVariable = "NETCORE_ENVIRONMENT";
83-
}
84-
env.EnvironmentName = System.Environment.GetEnvironmentVariable(hostEnvironmentVariable) ?? "Production";
8596

8697
config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
8798
config.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true);

0 commit comments

Comments
 (0)