public ModuleInstance( ModuleDescriptor moduleDescriptor, string moduleInstanceId, PathString pathBase, IServiceCollection sharedServices, IServiceProvider appServiceProvider, ModuleInstanceOptions options) { ModuleDescriptor = moduleDescriptor; ModuleInstanceId = moduleInstanceId; PathBase = pathBase; AddSharedServices(sharedServices, appServiceProvider); ModuleServiceCollection.Add(moduleDescriptor.ModuleServiceCollection); ModuleServiceCollection.AddSingleton <ModuleInstanceIdProvider>(new ModuleInstanceIdProvider(moduleInstanceId)); ModuleServiceCollection.AddSingleton <IRootServiceProvider>(new RootServiceProvider(appServiceProvider)); if (options != null) { foreach (var configureServices in options.ConfigureServices) { configureServices(ModuleServiceCollection); } } ModuleServices = ModuleServiceCollection.BuildServiceProvider(); }
void AddSharedServices(IServiceCollection sharedServices, IServiceProvider appServiceProvider) { foreach (var sd in sharedServices) { if (!sd.ServiceType.GetTypeInfo().IsGenericTypeDefinition&& sd.Lifetime != ServiceLifetime.Transient) { ModuleServiceCollection.Add(ServiceDescriptor.Describe( sd.ServiceType, sp => appServiceProvider.GetRequiredService(sd.ServiceType), sd.Lifetime)); } else { // TODO: How to preserve lifetime for generic services? ModuleServiceCollection.Add(sd); } } }
void AddHostingServices() { ModuleServiceCollection.Add(_hostingServices); ModuleServiceCollection.AddSingleton(ModuleHostingEnvironment); }