-
-
Notifications
You must be signed in to change notification settings - Fork 35
/
Program.cs
90 lines (83 loc) · 3.63 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
// 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 System;
using System.IO;
using System.Threading.Tasks;
using Dapplo.Microsoft.Extensions.Hosting.Plugins;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
namespace Dapplo.Hosting.Sample.ConsoleDemo;
/// <summary>
/// This demonstrates loading plugins
/// </summary>
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);
var host = new HostBuilder()
.ConfigureLogging()
.ConfigureConfiguration(args)
.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");
})
.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)
{
return 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);
});
}
}