using Connected.Interop; using Microsoft.AspNetCore.Builder; namespace Connected.Instance { public static class Instance { internal static WebApplication Host { get; private set; } internal static async Task StartAsync(Dictionary args) { var builder = WebApplication.CreateBuilder(UnpackArguments(args)); var startups = Assemblies.QueryImplementations(); foreach (var assembly in Assemblies.All) builder.Services.AddMicroService(assembly); foreach (var startup in startups) { if (startup.CreateInstance() is IStartup start) start.ConfigureServices(builder.Services); } Host = builder.Build(); foreach (var startup in startups) { if (startup.CreateInstance() is IStartup start) start.Configure(Host); } foreach (var startup in startups) { if (startup.CreateInstance() is IStartup start) await start.Initialize(args); } foreach (var startup in startups) { if (startup.CreateInstance() is IStartup start) await start.Start(args); } if (args.TryGetValue("entitySynchronization", out string? entities)) await EntitySynchronizer.Synchronize(Host.Services, entities); await Host.RunAsync(); } private static string[] UnpackArguments(Dictionary args) { var result = new string[args.Count]; for (var i = 0; i < args.Count; i++) { var arg = args.ElementAt(i); result[i] = string.IsNullOrWhiteSpace(arg.Value) ? arg.Key : $"{arg.Key}={arg.Value}"; } return result; } } }