-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
78 lines (57 loc) · 2.25 KB
/
Program.cs
File metadata and controls
78 lines (57 loc) · 2.25 KB
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
using System;
using System.Collections.Immutable;
using System.Linq;
using System.Text.Json;
using System.Threading;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using SDK.CSharp.Example;
using Serilog;
var hostBuilder = Host.CreateApplicationBuilder();
var loggerConfiguration = new LoggerConfiguration()
.MinimumLevel.Verbose()
.MinimumLevel.Override("Microsoft", Serilog.Events.LogEventLevel.Information)
.WriteTo.Console(
outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3}] [{SourceContext}] {Message:lj}{NewLine}{Exception}");
Log.Logger = loggerConfiguration.CreateLogger();
hostBuilder.Logging.ClearProviders();
hostBuilder.Logging.AddSerilog();
var exampleType = typeof(IExample);
typeof(Program).Assembly.GetTypes().Where(x => x.IsClass && x.IsAssignableTo(exampleType)).ToList().ForEach(x =>
{
hostBuilder.Services.TryAddEnumerable(new ServiceDescriptor(exampleType, x, ServiceLifetime.Singleton));
});
var config = hostBuilder.Configuration.Get<ExampleConfig>()!;
hostBuilder.Services.AddSingleton<ExampleConfig>(config);
Console.WriteLine(JsonSerializer.Serialize(config, new JsonSerializerOptions { WriteIndented = true }));
var host = hostBuilder.Build();
var console = new Thread(async () =>
{
Console.WriteLine("OpenShock Example Program...");
var examples = host.Services.GetServices<IExample>().ToImmutableArray();
Console.WriteLine($"Available Examples:");
for (int i = 0; i < examples.Length; i++)
{
var example = examples[i];
Console.WriteLine($"[{i}] {example.GetType().Name}");
}
while (true)
{
var input = Console.ReadLine();
if (!int.TryParse(input, out var choice))
{
Console.WriteLine("Invalid choice");
continue;
}
var example = examples[choice];
if (examples == null) Console.WriteLine("Invalid choice, example doesnt exist");
Console.WriteLine($"Starting example {example.GetType().Name}");
example.Start().Wait();
}
});
console.IsBackground = true;
console.Start();
await host.RunAsync();