Skip to content

Commit 556d824

Browse files
committed
modified DI to use he hostbuilder in order to add appsettings.json for app configurations
1 parent 20d9d42 commit 556d824

File tree

3 files changed

+35
-7
lines changed

3 files changed

+35
-7
lines changed

src/HyperMC/HyperMC.csproj

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
<ItemGroup>
1717
<PackageReference Include="LibGit2Sharp" Version="0.26.2" />
1818
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="5.0.2" />
19+
<PackageReference Include="Microsoft.Extensions.Hosting" Version="5.0.0" />
1920
<PackageReference Include="Microsoft.Toolkit.Uwp.Notifications" Version="7.0.1" />
2021
</ItemGroup>
2122

@@ -38,4 +39,10 @@
3839
<ProjectReference Include="..\HyperMC.CurseForge\HyperMC.CurseForge.csproj" />
3940
</ItemGroup>
4041

42+
<ItemGroup>
43+
<None Update="appsettings.json">
44+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
45+
</None>
46+
</ItemGroup>
47+
4148
</Project>

src/HyperMC/Program.cs

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
using Hypermc.Services;
22
using Hypermc.Settings;
33
using Hypermc.UI.Views;
4+
using Microsoft.Extensions.Configuration;
45
using Microsoft.Extensions.DependencyInjection;
6+
using Microsoft.Extensions.Hosting;
57
using System;
68
using System.Collections.Generic;
9+
using System.IO;
710
using System.Linq;
811
using System.Threading.Tasks;
912
using System.Windows.Forms;
@@ -12,26 +15,33 @@ namespace Hypermc
1215
{
1316
static class Program
1417
{
15-
private static IServiceProvider Provider;
1618
/// <summary>
1719
/// The main entry point for the application.
1820
/// </summary>
1921
[STAThread]
2022
static async Task Main()
2123
{
22-
Provider = ConfigureService();
23-
await Provider.GetRequiredService<IUserSettings>().Initialize();
24+
var builder = new ConfigurationBuilder();
25+
BuildConfig(builder);
26+
builder.Build();
27+
28+
var host = Host.CreateDefaultBuilder()
29+
.ConfigureServices((context, services) =>
30+
{
31+
ConfigureServices(services);
32+
})
33+
.Build();
34+
35+
await host.Services.GetRequiredService<IUserSettings>().Initialize();
2436

2537
Application.SetHighDpiMode(HighDpiMode.SystemAware);
2638
Application.EnableVisualStyles();
2739
Application.SetCompatibleTextRenderingDefault(false);
28-
Application.Run(Provider.GetRequiredService<HyperMcView>());
40+
Application.Run(host.Services.GetRequiredService<HyperMcView>());
2941
}
3042

31-
private static IServiceProvider ConfigureService()
43+
private static IServiceProvider ConfigureServices(IServiceCollection services)
3244
{
33-
IServiceCollection services = new ServiceCollection();
34-
3545
services.AddSingleton<HyperMcView>()
3646
.AddSingleton<IUserSettings, UserSettings>();
3747

@@ -42,5 +52,13 @@ private static IServiceProvider ConfigureService()
4252

4353
return services.BuildServiceProvider();
4454
}
55+
56+
static void BuildConfig(IConfigurationBuilder builder)
57+
{
58+
builder.SetBasePath(Directory.GetCurrentDirectory())
59+
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
60+
.AddJsonFile($"appsettings.{ Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Production" }.json", optional: true)
61+
.AddEnvironmentVariables();
62+
}
4563
}
4664
}

src/HyperMC/appsettings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"test": 15
3+
}

0 commit comments

Comments
 (0)