Skip to content

Commit b8e44cc

Browse files
Merge pull request #12 from Chris-Wolfgang/develop
Develop Issue #7
2 parents e404318 + 071e11d commit b8e44cc

File tree

10 files changed

+190
-2
lines changed

10 files changed

+190
-2
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// TODO If you are using UseMultiEnvironment then you can delete this file
2+
3+
{
4+
// TODO : Add your connection strings here
5+
"ConnectionStrings": {
6+
7+
},
8+
// TODO : Add your app settings here creating multiple sections as necessary
9+
"SampleConfiguration": {
10+
"CommandTimeout": 1000
11+
}
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// TODO If you are using UseMultiEnvironment then you can delete this file
2+
3+
{
4+
// TODO : Add your connection strings here
5+
"ConnectionStrings": {
6+
7+
},
8+
// TODO : Add your app settings here creating multiple sections as necessary
9+
"SampleConfiguration": {
10+
"CommandTimeout": 2000
11+
}
12+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// TODO If you are using UseMultiEnvironment then you can delete this file
2+
{
3+
// TODO : Add your connection strings here
4+
"ConnectionStrings": {
5+
6+
},
7+
// TODO : Add your app settings here creating multiple sections as necessary
8+
"SampleConfiguration": {
9+
"CommandTimeout": 100
10+
}
11+
}

ConsoleAppTemplate/ConsoleAppTemplate/ConsoleAppTemplate.csproj

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,16 @@
1313
<PackageReference Include="Microsoft.Extensions.Hosting" Version="9.0.4" />
1414
</ItemGroup>
1515

16+
<ItemGroup>
17+
<None Update="AppSettings.Development.json">
18+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
19+
</None>
20+
<None Update="AppSettings.Production.json">
21+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
22+
</None>
23+
<None Update="AppSettings.json">
24+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
25+
</None>
26+
</ItemGroup>
27+
1628
</Project>
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
using ConsoleAppTemplate.Model;
2+
using Microsoft.Extensions.Configuration;
3+
using Microsoft.Extensions.Hosting;
4+
5+
namespace ConsoleAppTemplate.Framework
6+
{
7+
internal static class IHostBuilderExtensions
8+
{
9+
public static IHostBuilder UseSingleEnvironment(this IHostBuilder builder)
10+
{
11+
var config = new SingleEnvironmentConfiguration
12+
{
13+
BasePath = ConfigurationPath.Combine(AppContext.BaseDirectory),
14+
ConfigFile = new ConfigurationFile
15+
{
16+
Name = "AppSettings.json",
17+
Optional = false,
18+
ReloadOnChange = true
19+
}
20+
};
21+
22+
return UseSingleEnvironment(builder, config);
23+
}
24+
25+
26+
27+
public static IHostBuilder UseSingleEnvironment(this IHostBuilder builder, SingleEnvironmentConfiguration config)
28+
{
29+
builder
30+
.ConfigureAppConfiguration((context, configurationBuilder) =>
31+
{
32+
configurationBuilder
33+
.SetBasePath(config.BasePath)
34+
.AddJsonFile(config.ConfigFile.Name, optional: config.ConfigFile.Optional,
35+
reloadOnChange: config.ConfigFile.ReloadOnChange)
36+
.AddEnvironmentVariables();
37+
});
38+
39+
return builder;
40+
}
41+
42+
43+
44+
public static IHostBuilder UseMultiEnvironment(this IHostBuilder builder)
45+
{
46+
var environment = Environment.GetEnvironmentVariable("DOTNET_ENVIRONMENT");
47+
if (string.IsNullOrWhiteSpace(environment))
48+
{
49+
Environment.FailFast("System variable DOTNET_ENVIRONMENT is not set.");
50+
}
51+
52+
var config = new MultiEnvironmentConfiguration
53+
{
54+
BasePath = ConfigurationPath.Combine(AppContext.BaseDirectory),
55+
ConfigFile = new ConfigurationFile
56+
{
57+
Name = $"AppSettings.{environment}.json",
58+
Optional = false,
59+
ReloadOnChange = true
60+
}
61+
};
62+
63+
return UseMultiEnvironment(builder, config);
64+
}
65+
66+
67+
68+
public static IHostBuilder UseMultiEnvironment(this IHostBuilder builder, MultiEnvironmentConfiguration config)
69+
{
70+
builder
71+
.ConfigureAppConfiguration((context, configurationBuilder) =>
72+
{
73+
configurationBuilder
74+
.SetBasePath(config.BasePath)
75+
.AddJsonFile(config.ConfigFile.Name, optional: config.ConfigFile.Optional,
76+
reloadOnChange: config.ConfigFile.ReloadOnChange)
77+
.AddEnvironmentVariables();
78+
});
79+
80+
return builder;
81+
}
82+
83+
84+
85+
}
86+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace ConsoleAppTemplate.Model;
2+
3+
internal record ConfigurationFile
4+
{
5+
public required string Name { get; init; }
6+
public bool Optional { get; init; }
7+
public bool ReloadOnChange { get; init; }
8+
9+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace ConsoleAppTemplate.Model;
2+
3+
internal record MultiEnvironmentConfiguration
4+
{
5+
public required string BasePath { get; init; }
6+
7+
public required ConfigurationFile ConfigFile { get; init; }
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace ConsoleAppTemplate.Model;
2+
3+
internal record SampleConfiguration
4+
{
5+
public int CommandTimeout { get; set; }
6+
7+
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace ConsoleAppTemplate.Model;
2+
3+
internal record SingleEnvironmentConfiguration
4+
{
5+
public required string BasePath { get; init; }
6+
7+
public required ConfigurationFile ConfigFile { get; init; }
8+
}

ConsoleAppTemplate/ConsoleAppTemplate/Program.cs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
using ConsoleAppTemplate.Command;
2+
using ConsoleAppTemplate.Framework;
3+
using ConsoleAppTemplate.Model;
24
using McMaster.Extensions.CommandLineUtils;
5+
using Microsoft.Extensions.Configuration;
36
using Microsoft.Extensions.DependencyInjection;
47
using Microsoft.Extensions.Hosting;
58

@@ -29,20 +32,34 @@ internal class Program
2932
private static async Task<int> Main(string[] args)
3033
{
3134
return await new HostBuilder()
35+
// TODO Uncomment this line to use a single environment configuration file
36+
//.UseSingleEnvironment()
37+
// TODO Uncomment this line to use a multienvironment configuration file
38+
.UseMultiEnvironment()
39+
3240
.ConfigureServices((context, collection) =>
3341
{
3442
collection.AddSingleton<IReporter, ConsoleReporter>();
43+
collection.AddSingleton<SampleConfiguration>(provider =>
44+
{
45+
// Get the configuration from the host builder
46+
var config = provider.GetService<IConfiguration>();
47+
48+
// Load SampleConfiguration section from the config file
49+
var sc = config.GetSection("SampleConfiguration").Get<SampleConfiguration>();
50+
return sc;
51+
});
3552
})
3653
.RunCommandLineApplicationAsync<Program>(args);
3754
}
3855

3956

40-
4157
/// <summary>
4258
/// This method is called if the user does not specify a sub command
4359
/// </summary>
4460
/// <param name="application"></param>
4561
/// <param name="reporter"></param>
62+
/// <param name="sampleConfiguration"></param>
4663
/// <returns>0 on success or any positive number for failure</returns>
4764
/// <remarks>
4865
/// - If you are not using sub commands you can rewrite this method to meet your needs
@@ -53,9 +70,13 @@ private static async Task<int> Main(string[] args)
5370
internal int OnExecute
5471
(
5572
CommandLineApplication<Program> application,
56-
IReporter reporter
73+
IReporter reporter,
74+
SampleConfiguration sampleConfiguration
5775
)
5876
{
77+
78+
reporter.Output($"\nCommandTimeout from config file: {sampleConfiguration.CommandTimeout}\n\n");
79+
5980
// TODO if you are using not using sub commands then you can remove the lines below and replace with your own code
6081
application.ShowHelp();
6182
return ExitCode.Success;
@@ -65,6 +86,7 @@ IReporter reporter
6586
// TODO Add your code here
6687
reporter.Warn("Your code here");
6788

89+
6890
// TODO Return 0 for success and any positive number for failure
6991
return ExitCode.Success;
7092
}

0 commit comments

Comments
 (0)