using Microsoft.Extensions.DependencyInjection;
namespace DS.Module.Core.Modules;
public class StartupModuleRunner : ModuleApplicationBase, IStartupModuleRunner
{
///
/// 程序启动运行时
///
///
///
public StartupModuleRunner(Type startupModuleType, IServiceCollection services)
: base(startupModuleType, services)
{
services.AddSingleton(this);
}
public void ConfigureServices(IServiceCollection services)
{
DSIocManage.Instance.SetServiceCollection(services);
var context = new ConfigureServicesContext(services);
services.AddSingleton(context);
foreach (var module in Modules)
{
//如果是继承了DSAppModule
if (module is DSAppModule appModule)
{
appModule.ConfigureServicesContext = context;
}
}
foreach (var config in Modules)
{
services.AddSingleton(config);
config.ConfigureServices(context);
}
foreach (var module in Modules)
{
if (module is DSAppModule appModule)
{
appModule.ConfigureServicesContext = null;
}
}
}
public void Initialize(IServiceProvider service)
{
DSIocManage.Instance.SetApplicationServiceProvider(service);
SetServiceProvider(service);
using var scope = ServiceProvider.CreateScope();
//using var scope = service.CreateScope();
var ctx = new ApplicationContext(scope.ServiceProvider);
foreach (var cfg in Modules)
{
cfg.ApplicationInitialization(ctx);
}
}
public override void Dispose()
{
base.Dispose();
if (ServiceProvider is IDisposable disposableServiceProvider)
{
disposableServiceProvider.Dispose();
}
}
}