Move base initialization into separate class
This commit is contained in:
		
							parent
							
								
									be3bc5c365
								
							
						
					
					
						commit
						35b4388b3c
					
				
							
								
								
									
										32
									
								
								src/Connected.Host/ConnectedPlatformService.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										32
									
								
								src/Connected.Host/ConnectedPlatformService.cs
									
									
									
									
									
										Normal file
									
								
							@ -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…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user