Skip to content

Commit

Permalink
Merge pull request #12 from mayuki/UseConfigureHostConfiguration
Browse files Browse the repository at this point in the history
Use ConfigureHostConfiguration to initialize GenericHost.
  • Loading branch information
neuecc authored Apr 15, 2019
2 parents 43660e1 + d2f76bd commit 4546b64
Showing 1 changed file with 20 additions and 9 deletions.
29 changes: 20 additions & 9 deletions src/MicroBatchFramework/BatchHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System;
using System.IO;
using System.Reflection;
using System.Collections.Generic;

namespace MicroBatchFramework
{
Expand Down Expand Up @@ -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<string, string>(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);
Expand Down

0 comments on commit 4546b64

Please sign in to comment.