Move base initialization into separate class

develop
Matija Koželj 2 years ago
parent be3bc5c365
commit 35b4388b3c

@ -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<SysConfiguration> 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;
}
}
Loading…
Cancel
Save