-
-
Notifications
You must be signed in to change notification settings - Fork 35
/
Program.cs
102 lines (94 loc) · 4.59 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// Copyright (c) Dapplo and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using Dapplo.Microsoft.Extensions.Hosting.Plugins;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using System;
using System.IO;
using System.Threading.Tasks;
using Dapplo.Microsoft.Extensions.Hosting.AppServices;
using Dapplo.Microsoft.Extensions.Hosting.Wpf;
using Microsoft.Extensions.DependencyInjection;
namespace Dapplo.Hosting.Sample.WpfDemo;
public static class Program
{
private const string AppSettingsFilePrefix = "appsettings";
private const string HostSettingsFile = "hostsettings.json";
private const string Prefix = "PREFIX_";
public static Task Main(string[] args)
{
var executableLocation = Path.GetDirectoryName(typeof(Program).Assembly.Location) ?? throw new NotSupportedException("Can't start without location.");
var host = new HostBuilder()
.ConfigureLogging()
.ConfigureConfiguration(args)
.ConfigureSingleInstance(builder =>
{
builder.MutexId = "{C3CC6C8F-B40C-4EC2-A540-1D4B8FFFB60D}";
builder.WhenNotFirstInstance = (hostingEnvironment, logger) =>
{
// This is called when an instance was already started, this is in the second instance
logger.LogWarning("Application {applicationName} already running.", hostingEnvironment.ApplicationName);
};
})
.ConfigurePlugins(pluginBuilder =>
{
var runtime = Path.GetFileName(executableLocation);
var parentDirectory = Directory.GetParent(executableLocation).FullName;
var configuration = Path.GetFileName(parentDirectory);
var basePath = Path.Combine(executableLocation, @"..\..\..\..\");
// Specify the location from where the Dll's are "globbed"
pluginBuilder.AddScanDirectories(basePath);
// Add the framework libraries which can be found with the specified globs
pluginBuilder.IncludeFrameworks(@$"**\bin\{configuration}\netstandard2.0\*.FrameworkLib.dll");
// Add the plugins which can be found with the specified globs
pluginBuilder.IncludePlugins(@$"**\bin\{configuration}\{runtime}\*.Sample.Plugin*.dll");
})
.ConfigureServices(serviceCollection =>
// Make OtherWindow available for DI to the MainWindow, but not as singleton
serviceCollection.AddTransient<OtherWindow>())
.ConfigureWpf(wpfBuilder =>
{
wpfBuilder.UseApplication<MyApplication>();
wpfBuilder.UseWindow<MainWindow>();
})
.UseWpfLifetime()
.UseConsoleLifetime()
.Build();
Console.WriteLine("Run!");
return host.RunAsync();
}
/// <summary>
/// Configure the loggers
/// </summary>
/// <param name="hostBuilder">IHostBuilder</param>
/// <returns>IHostBuilder</returns>
private static IHostBuilder ConfigureLogging(this IHostBuilder hostBuilder) =>
hostBuilder.ConfigureLogging((hostContext, configLogging) =>
configLogging
.AddConfiguration(hostContext.Configuration.GetSection("Logging"))
.AddConsole()
.AddDebug());
/// <summary>
/// Configure the configuration
/// </summary>
/// <param name="hostBuilder"></param>
/// <param name="args"></param>
/// <returns></returns>
private static IHostBuilder ConfigureConfiguration(this IHostBuilder hostBuilder, string[] args) =>
hostBuilder.ConfigureHostConfiguration(configHost =>
configHost.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile(HostSettingsFile, optional: true)
.AddEnvironmentVariables(prefix: Prefix)
.AddCommandLine(args))
.ConfigureAppConfiguration((hostContext, configApp) =>
{
configApp.AddJsonFile(AppSettingsFilePrefix + ".json", optional: true);
if (!string.IsNullOrEmpty(hostContext.HostingEnvironment.EnvironmentName))
{
configApp.AddJsonFile(AppSettingsFilePrefix + $".{hostContext.HostingEnvironment.EnvironmentName}.json", optional: true);
}
configApp.AddEnvironmentVariables(prefix: Prefix)
.AddCommandLine(args);
});
}