You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

69 lines
2.0 KiB
C#

1 year ago
using Microsoft.Extensions.DependencyInjection;
namespace DS.Module.Core.Modules;
public class StartupModuleRunner : ModuleApplicationBase, IStartupModuleRunner
{
/// <summary>
/// 程序启动运行时
/// </summary>
/// <param name="startupModuleType"></param>
/// <param name="services"></param>
public StartupModuleRunner(Type startupModuleType, IServiceCollection services)
: base(startupModuleType, services)
{
services.AddSingleton<IStartupModuleRunner>(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();
}
}
}