diff --git a/src/Connected.Host/ConnectedPlatformService.cs b/src/Connected.Host/ConnectedPlatformService.cs new file mode 100644 index 0000000..99a8a65 --- /dev/null +++ b/src/Connected.Host/ConnectedPlatformService.cs @@ -0,0 +1,32 @@ +using System.Reflection; +using Connected.Host.Configuration; +using Microsoft.Extensions.Options; + +namespace Connected.Host; + +internal sealed class ConnectedPlatformService : BackgroundService +{ + private SysConfiguration Config { get; } + private IConfiguration GlobalConfig { get; } + public ConnectedPlatformService(IOptions config, IConfiguration globalConfig) + { + Config = config.Value; + GlobalConfig = globalConfig; + } + + protected override async Task ExecuteAsync(CancellationToken stoppingToken) + { + await DependencyLoader.LoadPackages(Config.MicroServices, Config.Repositories); + + if (Type.GetType(Config.Start) is not Type startupType) + throw new Exception($"Cannot resolve startup type. ({Config.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[] { GlobalConfig }) is not Task task) + throw new Exception($"ConfigureAsync of type {startupType} must return Task."); + + await task; + } +} \ No newline at end of file