diff --git a/framework/src/Volo.Abp.Core/Volo/Abp/AbpApplicationBase.cs b/framework/src/Volo.Abp.Core/Volo/Abp/AbpApplicationBase.cs index c6e86b4af8..2a73b72932 100644 --- a/framework/src/Volo.Abp.Core/Volo/Abp/AbpApplicationBase.cs +++ b/framework/src/Volo.Abp.Core/Volo/Abp/AbpApplicationBase.cs @@ -24,6 +24,8 @@ public abstract class AbpApplicationBase : IAbpApplication public IReadOnlyList Modules { get; } + private bool _configuredServices; + internal AbpApplicationBase( [NotNull] Type startupModuleType, [NotNull] IServiceCollection services, @@ -48,7 +50,7 @@ public abstract class AbpApplicationBase : IAbpApplication Modules = LoadModules(services, options); - if (!options.ManualConfigureServices) + if (!options.SkipConfigureServices) { ConfigureServices(); } @@ -139,6 +141,8 @@ public abstract class AbpApplicationBase : IAbpApplication //TODO: We can extract a new class for this public virtual async Task ConfigureServicesAsync() { + CheckMultipleConfigureServices(); + var context = new ServiceConfigurationContext(Services); Services.AddSingleton(context); @@ -211,11 +215,23 @@ public abstract class AbpApplicationBase : IAbpApplication abpModule.ServiceConfigurationContext = null; } } + + _configuredServices = true; + } + + private void CheckMultipleConfigureServices() + { + if (_configuredServices) + { + throw new AbpInitializationException("Services have already been configured! If you call ConfigureServicesAsync method, you must have set AbpApplicationCreationOptions.SkipConfigureServices tu true before."); + } } //TODO: We can extract a new class for this public virtual void ConfigureServices() { + CheckMultipleConfigureServices(); + var context = new ServiceConfigurationContext(Services); Services.AddSingleton(context); @@ -288,5 +304,7 @@ public abstract class AbpApplicationBase : IAbpApplication abpModule.ServiceConfigurationContext = null; } } + + _configuredServices = true; } } diff --git a/framework/src/Volo.Abp.Core/Volo/Abp/AbpApplicationCreationOptions.cs b/framework/src/Volo.Abp.Core/Volo/Abp/AbpApplicationCreationOptions.cs index 8bf7579843..d18e43275b 100644 --- a/framework/src/Volo.Abp.Core/Volo/Abp/AbpApplicationCreationOptions.cs +++ b/framework/src/Volo.Abp.Core/Volo/Abp/AbpApplicationCreationOptions.cs @@ -19,13 +19,12 @@ public class AbpApplicationCreationOptions [NotNull] public AbpConfigurationBuilderOptions Configuration { get; } - public bool ManualConfigureServices { get; set; } + public bool SkipConfigureServices { get; set; } public AbpApplicationCreationOptions([NotNull] IServiceCollection services) { Services = Check.NotNull(services, nameof(services)); PlugInSources = new PlugInSourceList(); Configuration = new AbpConfigurationBuilderOptions(); - ManualConfigureServices = false; } } diff --git a/framework/src/Volo.Abp.Core/Volo/Abp/AbpApplicationFactory.cs b/framework/src/Volo.Abp.Core/Volo/Abp/AbpApplicationFactory.cs index d904c24ca2..50e5e862ca 100644 --- a/framework/src/Volo.Abp.Core/Volo/Abp/AbpApplicationFactory.cs +++ b/framework/src/Volo.Abp.Core/Volo/Abp/AbpApplicationFactory.cs @@ -15,7 +15,7 @@ public static class AbpApplicationFactory var app = Create(typeof(TStartupModule), options => { optionsAction?.Invoke(options); - options.ManualConfigureServices = true; + options.SkipConfigureServices = true; }); await app.ConfigureServicesAsync(); return app; @@ -28,7 +28,7 @@ public static class AbpApplicationFactory var app = new AbpApplicationWithInternalServiceProvider(startupModuleType, options => { optionsAction?.Invoke(options); - options.ManualConfigureServices = true; + options.SkipConfigureServices = true; }); await app.ConfigureServicesAsync(); return app; @@ -42,7 +42,7 @@ public static class AbpApplicationFactory var app = Create(typeof(TStartupModule), services, options => { optionsAction?.Invoke(options); - options.ManualConfigureServices = true; + options.SkipConfigureServices = true; }); await app.ConfigureServicesAsync(); return app; @@ -56,7 +56,7 @@ public static class AbpApplicationFactory var app = new AbpApplicationWithExternalServiceProvider(startupModuleType, services, options => { optionsAction?.Invoke(options); - options.ManualConfigureServices = true; + options.SkipConfigureServices = true; }); await app.ConfigureServicesAsync(); return app; diff --git a/framework/src/Volo.Abp.Core/Volo/Abp/IAbpApplication.cs b/framework/src/Volo.Abp.Core/Volo/Abp/IAbpApplication.cs index b51fd7ec37..2fb66b735f 100644 --- a/framework/src/Volo.Abp.Core/Volo/Abp/IAbpApplication.cs +++ b/framework/src/Volo.Abp.Core/Volo/Abp/IAbpApplication.cs @@ -23,6 +23,13 @@ public interface IAbpApplication : IModuleContainer, IDisposable /// This can not be used before initialize the application. /// IServiceProvider ServiceProvider { get; } + + /// + /// Calls the Pre/Post/ConfigureServicesAsync methods of the modules. + /// If you use this method, you must have set the + /// option to true before. + /// + Task ConfigureServicesAsync(); /// /// Used to gracefully shutdown the application and all modules. diff --git a/framework/src/Volo.Abp.Core/Volo/Abp/IAbpApplicationWithExternalServiceProvider.cs b/framework/src/Volo.Abp.Core/Volo/Abp/IAbpApplicationWithExternalServiceProvider.cs index b4363e1161..a17bb146e3 100644 --- a/framework/src/Volo.Abp.Core/Volo/Abp/IAbpApplicationWithExternalServiceProvider.cs +++ b/framework/src/Volo.Abp.Core/Volo/Abp/IAbpApplicationWithExternalServiceProvider.cs @@ -10,12 +10,7 @@ public interface IAbpApplicationWithExternalServiceProvider : IAbpApplication /// Sets the service provider, but not initializes the modules. /// void SetServiceProvider([NotNull] IServiceProvider serviceProvider); - - /// - /// Call the Pre/Post/ConfigureServicesAsync method of the modules. - /// - Task ConfigureServicesAsync(); - + /// /// Sets the service provider and initializes all the modules. /// If was called before, the same diff --git a/framework/src/Volo.Abp.Core/Volo/Abp/IAbpApplicationWithInternalServiceProvider.cs b/framework/src/Volo.Abp.Core/Volo/Abp/IAbpApplicationWithInternalServiceProvider.cs index 8a3b4c7ca1..0079608c2e 100644 --- a/framework/src/Volo.Abp.Core/Volo/Abp/IAbpApplicationWithInternalServiceProvider.cs +++ b/framework/src/Volo.Abp.Core/Volo/Abp/IAbpApplicationWithInternalServiceProvider.cs @@ -11,11 +11,6 @@ public interface IAbpApplicationWithInternalServiceProvider : IAbpApplication /// IServiceProvider CreateServiceProvider(); - /// - /// Call the Pre/Post/ConfigureServicesAsync method of the modules. - /// - Task ConfigureServicesAsync(); - /// /// Creates the service provider and initializes all the modules. /// If method was called before,