Browse Source

Check multiple configure services calls and rename ManualConfigureServices to SkipConfigureServices.

pull/10928/head
Halil İbrahim Kalkan 5 years ago
parent
commit
d9a5b86505
  1. 20
      framework/src/Volo.Abp.Core/Volo/Abp/AbpApplicationBase.cs
  2. 3
      framework/src/Volo.Abp.Core/Volo/Abp/AbpApplicationCreationOptions.cs
  3. 8
      framework/src/Volo.Abp.Core/Volo/Abp/AbpApplicationFactory.cs
  4. 7
      framework/src/Volo.Abp.Core/Volo/Abp/IAbpApplication.cs
  5. 7
      framework/src/Volo.Abp.Core/Volo/Abp/IAbpApplicationWithExternalServiceProvider.cs
  6. 5
      framework/src/Volo.Abp.Core/Volo/Abp/IAbpApplicationWithInternalServiceProvider.cs

20
framework/src/Volo.Abp.Core/Volo/Abp/AbpApplicationBase.cs

@ -24,6 +24,8 @@ public abstract class AbpApplicationBase : IAbpApplication
public IReadOnlyList<IAbpModuleDescriptor> Modules { get; } public IReadOnlyList<IAbpModuleDescriptor> Modules { get; }
private bool _configuredServices;
internal AbpApplicationBase( internal AbpApplicationBase(
[NotNull] Type startupModuleType, [NotNull] Type startupModuleType,
[NotNull] IServiceCollection services, [NotNull] IServiceCollection services,
@ -48,7 +50,7 @@ public abstract class AbpApplicationBase : IAbpApplication
Modules = LoadModules(services, options); Modules = LoadModules(services, options);
if (!options.ManualConfigureServices) if (!options.SkipConfigureServices)
{ {
ConfigureServices(); ConfigureServices();
} }
@ -139,6 +141,8 @@ public abstract class AbpApplicationBase : IAbpApplication
//TODO: We can extract a new class for this //TODO: We can extract a new class for this
public virtual async Task ConfigureServicesAsync() public virtual async Task ConfigureServicesAsync()
{ {
CheckMultipleConfigureServices();
var context = new ServiceConfigurationContext(Services); var context = new ServiceConfigurationContext(Services);
Services.AddSingleton(context); Services.AddSingleton(context);
@ -211,11 +215,23 @@ public abstract class AbpApplicationBase : IAbpApplication
abpModule.ServiceConfigurationContext = null; 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 //TODO: We can extract a new class for this
public virtual void ConfigureServices() public virtual void ConfigureServices()
{ {
CheckMultipleConfigureServices();
var context = new ServiceConfigurationContext(Services); var context = new ServiceConfigurationContext(Services);
Services.AddSingleton(context); Services.AddSingleton(context);
@ -288,5 +304,7 @@ public abstract class AbpApplicationBase : IAbpApplication
abpModule.ServiceConfigurationContext = null; abpModule.ServiceConfigurationContext = null;
} }
} }
_configuredServices = true;
} }
} }

3
framework/src/Volo.Abp.Core/Volo/Abp/AbpApplicationCreationOptions.cs

@ -19,13 +19,12 @@ public class AbpApplicationCreationOptions
[NotNull] [NotNull]
public AbpConfigurationBuilderOptions Configuration { get; } public AbpConfigurationBuilderOptions Configuration { get; }
public bool ManualConfigureServices { get; set; } public bool SkipConfigureServices { get; set; }
public AbpApplicationCreationOptions([NotNull] IServiceCollection services) public AbpApplicationCreationOptions([NotNull] IServiceCollection services)
{ {
Services = Check.NotNull(services, nameof(services)); Services = Check.NotNull(services, nameof(services));
PlugInSources = new PlugInSourceList(); PlugInSources = new PlugInSourceList();
Configuration = new AbpConfigurationBuilderOptions(); Configuration = new AbpConfigurationBuilderOptions();
ManualConfigureServices = false;
} }
} }

8
framework/src/Volo.Abp.Core/Volo/Abp/AbpApplicationFactory.cs

@ -15,7 +15,7 @@ public static class AbpApplicationFactory
var app = Create(typeof(TStartupModule), options => var app = Create(typeof(TStartupModule), options =>
{ {
optionsAction?.Invoke(options); optionsAction?.Invoke(options);
options.ManualConfigureServices = true; options.SkipConfigureServices = true;
}); });
await app.ConfigureServicesAsync(); await app.ConfigureServicesAsync();
return app; return app;
@ -28,7 +28,7 @@ public static class AbpApplicationFactory
var app = new AbpApplicationWithInternalServiceProvider(startupModuleType, options => var app = new AbpApplicationWithInternalServiceProvider(startupModuleType, options =>
{ {
optionsAction?.Invoke(options); optionsAction?.Invoke(options);
options.ManualConfigureServices = true; options.SkipConfigureServices = true;
}); });
await app.ConfigureServicesAsync(); await app.ConfigureServicesAsync();
return app; return app;
@ -42,7 +42,7 @@ public static class AbpApplicationFactory
var app = Create(typeof(TStartupModule), services, options => var app = Create(typeof(TStartupModule), services, options =>
{ {
optionsAction?.Invoke(options); optionsAction?.Invoke(options);
options.ManualConfigureServices = true; options.SkipConfigureServices = true;
}); });
await app.ConfigureServicesAsync(); await app.ConfigureServicesAsync();
return app; return app;
@ -56,7 +56,7 @@ public static class AbpApplicationFactory
var app = new AbpApplicationWithExternalServiceProvider(startupModuleType, services, options => var app = new AbpApplicationWithExternalServiceProvider(startupModuleType, services, options =>
{ {
optionsAction?.Invoke(options); optionsAction?.Invoke(options);
options.ManualConfigureServices = true; options.SkipConfigureServices = true;
}); });
await app.ConfigureServicesAsync(); await app.ConfigureServicesAsync();
return app; return app;

7
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. /// This can not be used before initialize the application.
/// </summary> /// </summary>
IServiceProvider ServiceProvider { get; } IServiceProvider ServiceProvider { get; }
/// <summary>
/// Calls the Pre/Post/ConfigureServicesAsync methods of the modules.
/// If you use this method, you must have set the <see cref="AbpApplicationCreationOptions.SkipConfigureServices"/>
/// option to true before.
/// </summary>
Task ConfigureServicesAsync();
/// <summary> /// <summary>
/// Used to gracefully shutdown the application and all modules. /// Used to gracefully shutdown the application and all modules.

7
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. /// Sets the service provider, but not initializes the modules.
/// </summary> /// </summary>
void SetServiceProvider([NotNull] IServiceProvider serviceProvider); void SetServiceProvider([NotNull] IServiceProvider serviceProvider);
/// <summary>
/// Call the Pre/Post/ConfigureServicesAsync method of the modules.
/// </summary>
Task ConfigureServicesAsync();
/// <summary> /// <summary>
/// Sets the service provider and initializes all the modules. /// Sets the service provider and initializes all the modules.
/// If <see cref="SetServiceProvider"/> was called before, the same /// If <see cref="SetServiceProvider"/> was called before, the same

5
framework/src/Volo.Abp.Core/Volo/Abp/IAbpApplicationWithInternalServiceProvider.cs

@ -11,11 +11,6 @@ public interface IAbpApplicationWithInternalServiceProvider : IAbpApplication
/// </summary> /// </summary>
IServiceProvider CreateServiceProvider(); IServiceProvider CreateServiceProvider();
/// <summary>
/// Call the Pre/Post/ConfigureServicesAsync method of the modules.
/// </summary>
Task ConfigureServicesAsync();
/// <summary> /// <summary>
/// Creates the service provider and initializes all the modules. /// Creates the service provider and initializes all the modules.
/// If <see cref="CreateServiceProvider"/> method was called before, /// If <see cref="CreateServiceProvider"/> method was called before,

Loading…
Cancel
Save