From c67b11eec357928de705308f5e484f81c6940130 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matija=20Ko=C5=BEelj?= Date: Wed, 4 Jan 2023 15:24:41 +0100 Subject: [PATCH] Rewrite host to use standard hosting patterns --- src/Connected.Host/Program.cs | 91 +++++++++++++---------------------- 1 file changed, 33 insertions(+), 58 deletions(-) diff --git a/src/Connected.Host/Program.cs b/src/Connected.Host/Program.cs index 8eaa4fa..2c935aa 100644 --- a/src/Connected.Host/Program.cs +++ b/src/Connected.Host/Program.cs @@ -1,67 +1,42 @@ -using System.Reflection; using Connected.Host.Configuration; namespace Connected.Host { - public static class Program + public static partial class Program { - public static void Main(params string[] args) + public static async Task Main(params string[] args) { - SysConfiguration.Build(); - - Start(ParseArguments(args)); - } - - private static void Start(Dictionary args) - { - foreach (var microService in SysConfiguration.MicroServices) - LoadMicroServiceAssembly(microService); - - if (Type.GetType(SysConfiguration.Start) is not Type startupType) - throw new Exception($"Cannot resolve startup type. ({SysConfiguration.Start})."); - - if (startupType.GetMethod("ConfigureAsync", BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic) is not MethodInfo configureMethod) - throw new Exception($"ConfigureAsync does not exists in the startup type {startupType}. Startup must have static 'async Task ConfigureAsync(params string[] args)' method."); - - if (configureMethod.Invoke(null, new object[] { args }) is not Task task) - throw new Exception($"ConfigureAsync of type {startupType} must return Task."); - - task.ConfigureAwait(false); - - task.GetType().GetProperty("Result")?.GetValue(task); - } - - private static Assembly? LoadMicroServiceAssembly(MicroServiceDescriptor descriptor) - { - var assemblyFile = descriptor.Name.EndsWith(".dll") ? descriptor.Name : $"{descriptor.Name}.dll"; - - foreach (var location in SysConfiguration.Locations) - { - var assemblyPath = Path.Combine(location, assemblyFile); - - if (File.Exists(assemblyPath)) - return Assembly.LoadFrom(assemblyPath); - } - - return Assembly.Load(AssemblyName.GetAssemblyName(assemblyFile)); + using IHost host = Microsoft.Extensions.Hosting.Host + .CreateDefaultBuilder(args) + .ConfigureAppConfiguration((_, cfg) => + { + var appPath = AppContext.BaseDirectory; + var segments = new Uri(appPath).Segments; + + segments = segments.Select(e => e.Replace("%20", " ")).ToArray(); + + for (var i = 1; i <= segments.Length; i++) + { + var pathBase = Path.Combine(segments[0..i]); + var filePath = Path.Combine(pathBase, "sys.json"); + + if (File.Exists(filePath)) + { + cfg.AddJsonFile(filePath, true, false); + break; + } + } + + cfg.AddEnvironmentVariables(); + }) + .ConfigureServices((context, services) => + { + services.Configure(context.Configuration); + services.AddHostedService(); + }) + .Build(); + + await host.RunAsync(); } - - private static Dictionary ParseArguments(params string[] args) - { - var result = new Dictionary(StringComparer.OrdinalIgnoreCase); - - foreach (var i in args) - { - var tokens = i.Split('=', 2); - - if (tokens.Length == 1) - result.Add(tokens[0].Trim(), string.Empty); - else - result.Add(tokens[0].Trim(), tokens[1].Trim()); - } - - return result; - } - } } \ No newline at end of file