This repository was archived by the owner on Dec 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 101
/
Copy pathApp.cs
125 lines (109 loc) · 4.7 KB
/
App.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
using NLog;
using NLog.Config;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using System.Xml;
using XOutput.Configuration;
using XOutput.DependencyInjection;
using XOutput.Notifications;
using XOutput.Resources;
using XOutput.Versioning;
namespace XOutput
{
public class App
{
private static readonly ILogger logger = LogManager.GetCurrentClassLogger();
private readonly string appVersion;
public static void Main()
{
string exePath = Assembly.GetExecutingAssembly().Location;
string cwd = Path.GetDirectoryName(exePath);
Directory.SetCurrentDirectory(cwd);
new App(Assembly.GetExecutingAssembly().GetName().Version.ToString(3)).Run().Wait();
}
public App(string appVersion)
{
this.appVersion = appVersion;
}
private async Task Run()
{
SetLoggerConfiguration();
// AppDomain.CurrentDomain.FirstChanceException += (object sender, FirstChanceExceptionEventArgs e) => UnhandledException(e.Exception, LogLevel.Info);
AppDomain.CurrentDomain.UnhandledException += (object sender, UnhandledExceptionEventArgs e) => UnhandledException(e.ExceptionObject as Exception, LogLevel.Error);
TaskScheduler.UnobservedTaskException += (object sender, UnobservedTaskExceptionEventArgs e) => UnhandledException(e.Exception, LogLevel.Error);
logger.Info($"Starting XOutput server version: {appVersion}");
var globalContext = ApplicationContext.Global;
globalContext.AddFromConfiguration(typeof(CoreConfiguration));
globalContext.AddFromConfiguration(typeof(ServerConfiguration));
globalContext.Discover(GetOrLoadAssemblies("XOutput.Core", "XOutput.Api", "XOutput.Mapping", "XOutput.Emulation", "XOutput.Server"));
logger.Info("Configuration classes are loaded");
var configurationManager = globalContext.Resolve<ConfigurationManager>();
var notificationService = globalContext.Resolve<NotificationService>();
await CheckUpdate(globalContext.Resolve<UpdateChecker>(), notificationService);
var server = globalContext.Resolve<HttpServer>();
server.Run();
globalContext.Close();
}
public IHost BuildWebHost()
{
return Host.CreateDefaultBuilder(Array.Empty<string>())
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
})
.Build();
}
private void SetLoggerConfiguration()
{
try
{
if (File.Exists("nlog.config"))
{
LogManager.Configuration = new XmlLoggingConfiguration(XmlReader.Create(File.OpenRead("nlog.config")));
return;
}
}
catch
{
// Cannot create logger
}
LogManager.Configuration = new XmlLoggingConfiguration(XmlReader.Create(AssemblyResourceManager.GetResourceStream("XOutput.Server.nlog.config")));
}
private Task CheckUpdate(UpdateChecker updateChecker, NotificationService notificationService)
{
return updateChecker.CompareReleaseAsync(appVersion).ContinueWith(t => {
switch (t.Result.Result)
{
case VersionCompareValues.NeedsUpgrade:
notificationService.Add(Notifications.Notifications.NeedsVersionUpgrade, new List<string>() { t.Result.LatestVersion });
break;
case VersionCompareValues.Error:
notificationService.Add(Notifications.Notifications.VersionCheckError, new List<string>() { }, NotificationTypes.Warning);
break;
}
});
}
public static void UnhandledException(Exception exceptionObject, LogLevel level)
{
logger.Log(level, exceptionObject);
}
private static IEnumerable<Assembly> GetOrLoadAssemblies(params string[] assemblyNames)
{
return assemblyNames.Select(assemblyName =>
{
var assembly = AppDomain.CurrentDomain.GetAssemblies().FirstOrDefault(a => a.FullName == assemblyName);
if (assembly != null)
{
return assembly;
}
return AppDomain.CurrentDomain.Load(assemblyName);
}).ToList();
}
}
}