diff --git a/framework/src/Volo.Abp.AspNetCore/Microsoft/AspNetCore/Hosting/AbpHostingEnvironmentExtensions.cs b/framework/src/Volo.Abp.AspNetCore/Microsoft/AspNetCore/Hosting/AbpHostingEnvironmentExtensions.cs index 49eb511fd9..7e8bd32055 100644 --- a/framework/src/Volo.Abp.AspNetCore/Microsoft/AspNetCore/Hosting/AbpHostingEnvironmentExtensions.cs +++ b/framework/src/Volo.Abp.AspNetCore/Microsoft/AspNetCore/Hosting/AbpHostingEnvironmentExtensions.cs @@ -1,32 +1,27 @@ -using Microsoft.Extensions.Configuration; +using System; +using Microsoft.Extensions.Configuration; namespace Microsoft.AspNetCore.Hosting { public static class AbpHostingEnvironmentExtensions { - public static IConfigurationRoot BuildConfiguration(this IHostingEnvironment env, AbpAspNetCoreConfigurationOptions options = null) + public static IConfigurationRoot BuildConfiguration( + this IHostingEnvironment env, + ConfigurationBuilderOptions options = null) { - options = options ?? new AbpAspNetCoreConfigurationOptions(); + options = options ?? new ConfigurationBuilderOptions(); - var builder = new ConfigurationBuilder() - .SetBasePath(env.ContentRootPath) - .AddJsonFile(options.FileName + ".json", optional: true, reloadOnChange: true) - .AddJsonFile($"{options.FileName}.{env.EnvironmentName}.json", optional: true, reloadOnChange: true) - .AddEnvironmentVariables(); + if (options.BasePath.IsNullOrEmpty()) + { + options.BasePath = env.ContentRootPath; + } - if (env.IsDevelopment()) + if (options.EnvironmentName.IsNullOrEmpty()) { - if (options.UserSecretsId != null) - { - builder.AddUserSecrets(options.UserSecretsId); - } - else if (options.UserSecretsAssembly != null) - { - builder.AddUserSecrets(options.UserSecretsAssembly, true); - } + options.EnvironmentName = env.EnvironmentName; } - return builder.Build(); + return ConfigurationHelper.BuildConfiguration(options); } } } diff --git a/framework/src/Volo.Abp.AspNetCore/Microsoft/Extensions/DependencyInjection/AbpAspNetCoreServiceCollectionExtensions.cs b/framework/src/Volo.Abp.AspNetCore/Microsoft/Extensions/DependencyInjection/AbpAspNetCoreServiceCollectionExtensions.cs index 735d3cb802..859bdcdbaf 100644 --- a/framework/src/Volo.Abp.AspNetCore/Microsoft/Extensions/DependencyInjection/AbpAspNetCoreServiceCollectionExtensions.cs +++ b/framework/src/Volo.Abp.AspNetCore/Microsoft/Extensions/DependencyInjection/AbpAspNetCoreServiceCollectionExtensions.cs @@ -10,12 +10,12 @@ namespace Microsoft.Extensions.DependencyInjection return services.GetSingletonInstance(); } - public static IConfigurationRoot BuildConfiguration(this IServiceCollection services, AbpAspNetCoreConfigurationOptions options = null) + public static IConfigurationRoot BuildConfiguration(this IServiceCollection services, ConfigurationBuilderOptions options = null) { return services.GetHostingEnvironment().BuildConfiguration(options); } - public static IConfigurationRoot AddConfiguration(this IServiceCollection services, AbpAspNetCoreConfigurationOptions options = null) + public static IConfigurationRoot AddConfiguration(this IServiceCollection services, ConfigurationBuilderOptions options = null) { var configuration = services.BuildConfiguration(options); services.SetConfiguration(configuration); diff --git a/framework/src/Volo.Abp.AspNetCore/Volo.Abp.AspNetCore.csproj b/framework/src/Volo.Abp.AspNetCore/Volo.Abp.AspNetCore.csproj index 0ad65c5dea..1dbc2938c7 100644 --- a/framework/src/Volo.Abp.AspNetCore/Volo.Abp.AspNetCore.csproj +++ b/framework/src/Volo.Abp.AspNetCore/Volo.Abp.AspNetCore.csproj @@ -30,8 +30,6 @@ - - diff --git a/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/AbpAspNetCoreModule.cs b/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/AbpAspNetCoreModule.cs index 7bf8d305b3..f7cc0710e7 100644 --- a/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/AbpAspNetCoreModule.cs +++ b/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/AbpAspNetCoreModule.cs @@ -1,5 +1,4 @@ using Microsoft.AspNetCore.Builder; -using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Volo.Abp.AspNetCore.Auditing; using Volo.Abp.Auditing; @@ -30,6 +29,11 @@ namespace Volo.Abp.AspNetCore )] public class AbpAspNetCoreModule : AbpModule { + public override void PreConfigureServices(ServiceConfigurationContext context) + { + context.Services.AddConfiguration(); + } + public override void ConfigureServices(ServiceConfigurationContext context) { Configure(options => @@ -39,10 +43,6 @@ namespace Volo.Abp.AspNetCore AddAspNetServices(context.Services); context.Services.AddObjectAccessor(); - - context.Services.AddConfiguration( - context.Services.ExecutePreConfiguredActions() - ); } private static void AddAspNetServices(IServiceCollection services) diff --git a/framework/src/Volo.Abp.AspNetCore/Microsoft/Extensions/Configuration/AbpAspNetCoreConfigurationOptions.cs b/framework/src/Volo.Abp.Core/Microsoft/Extensions/Configuration/ConfigurationBuilderOptions.cs similarity index 64% rename from framework/src/Volo.Abp.AspNetCore/Microsoft/Extensions/Configuration/AbpAspNetCoreConfigurationOptions.cs rename to framework/src/Volo.Abp.Core/Microsoft/Extensions/Configuration/ConfigurationBuilderOptions.cs index ec8757d8a1..101dad5c95 100644 --- a/framework/src/Volo.Abp.AspNetCore/Microsoft/Extensions/Configuration/AbpAspNetCoreConfigurationOptions.cs +++ b/framework/src/Volo.Abp.Core/Microsoft/Extensions/Configuration/ConfigurationBuilderOptions.cs @@ -2,7 +2,7 @@ namespace Microsoft.Extensions.Configuration { - public class AbpAspNetCoreConfigurationOptions + public class ConfigurationBuilderOptions { /// /// Used to set assembly which is used to get the user secret id for the application. @@ -20,5 +20,15 @@ namespace Microsoft.Extensions.Configuration /// Default value: "appsettings". /// public string FileName { get; set; } = "appsettings"; + + /// + /// Environment name. Generally used "Development", "Staging" or "Production". + /// + public string EnvironmentName { get; set; } + + /// + /// Base path to read the configuration file indicated by . + /// + public string BasePath { get; set; } } -} +} \ No newline at end of file diff --git a/framework/src/Volo.Abp.Core/Microsoft/Extensions/Configuration/ConfigurationHelper.cs b/framework/src/Volo.Abp.Core/Microsoft/Extensions/Configuration/ConfigurationHelper.cs new file mode 100644 index 0000000000..b2d005685e --- /dev/null +++ b/framework/src/Volo.Abp.Core/Microsoft/Extensions/Configuration/ConfigurationHelper.cs @@ -0,0 +1,43 @@ +using System; +using System.IO; + +namespace Microsoft.Extensions.Configuration +{ + public static class ConfigurationHelper + { + public static IConfigurationRoot BuildConfiguration(ConfigurationBuilderOptions options = null) + { + options = options ?? new ConfigurationBuilderOptions(); + + if (options.BasePath.IsNullOrEmpty()) + { + options.BasePath = Directory.GetCurrentDirectory(); + } + + var builder = new ConfigurationBuilder() + .SetBasePath(options.BasePath) + .AddJsonFile(options.FileName + ".json", optional: true, reloadOnChange: true); + + if (!options.EnvironmentName.IsNullOrEmpty()) + { + builder = builder.AddJsonFile($"{options.FileName}.{options.EnvironmentName}.json", optional: true, reloadOnChange: true); + } + + builder = builder.AddEnvironmentVariables(); + + if (options.EnvironmentName == "Development") + { + if (options.UserSecretsId != null) + { + builder.AddUserSecrets(options.UserSecretsId); + } + else if (options.UserSecretsAssembly != null) + { + builder.AddUserSecrets(options.UserSecretsAssembly, true); + } + } + + return builder.Build(); + } + } +} diff --git a/framework/src/Volo.Abp.Core/Volo.Abp.Core.csproj b/framework/src/Volo.Abp.Core/Volo.Abp.Core.csproj index 8ef5624f04..b1f3ed1fda 100644 --- a/framework/src/Volo.Abp.Core/Volo.Abp.Core.csproj +++ b/framework/src/Volo.Abp.Core/Volo.Abp.Core.csproj @@ -18,6 +18,8 @@ + + diff --git a/framework/src/Volo.Abp.Core/Volo/Abp/AbpApplicationBase.cs b/framework/src/Volo.Abp.Core/Volo/Abp/AbpApplicationBase.cs index 785b66e98e..a4862789f2 100644 --- a/framework/src/Volo.Abp.Core/Volo/Abp/AbpApplicationBase.cs +++ b/framework/src/Volo.Abp.Core/Volo/Abp/AbpApplicationBase.cs @@ -39,7 +39,7 @@ namespace Volo.Abp services.AddSingleton(this); services.AddCoreServices(); - services.AddCoreAbpServices(this); + services.AddCoreAbpServices(this, options); Modules = LoadModules(services, options); } diff --git a/framework/src/Volo.Abp.Core/Volo/Abp/AbpApplicationCreationOptions.cs b/framework/src/Volo.Abp.Core/Volo/Abp/AbpApplicationCreationOptions.cs index 296c7a4326..005471d9b9 100644 --- a/framework/src/Volo.Abp.Core/Volo/Abp/AbpApplicationCreationOptions.cs +++ b/framework/src/Volo.Abp.Core/Volo/Abp/AbpApplicationCreationOptions.cs @@ -1,4 +1,5 @@ using JetBrains.Annotations; +using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Volo.Abp.Modularity.PlugIns; @@ -12,10 +13,14 @@ namespace Volo.Abp [NotNull] public PlugInSourceList PlugInSources { get; } + [NotNull] + public ConfigurationBuilderOptions Configuration {get; } + public AbpApplicationCreationOptions([NotNull] IServiceCollection services) { Services = Check.NotNull(services, nameof(services)); PlugInSources = new PlugInSourceList(); + Configuration = new ConfigurationBuilderOptions(); } } } \ No newline at end of file diff --git a/framework/src/Volo.Abp.Core/Volo/Abp/Internal/InternalServiceCollectionExtensions.cs b/framework/src/Volo.Abp.Core/Volo/Abp/Internal/InternalServiceCollectionExtensions.cs index c18d640883..b653f8d2a1 100644 --- a/framework/src/Volo.Abp.Core/Volo/Abp/Internal/InternalServiceCollectionExtensions.cs +++ b/framework/src/Volo.Abp.Core/Volo/Abp/Internal/InternalServiceCollectionExtensions.cs @@ -16,13 +16,22 @@ namespace Volo.Abp.Internal services.AddLocalization(); } - internal static void AddCoreAbpServices(this IServiceCollection services, IAbpApplication abpApplication) + internal static void AddCoreAbpServices(this IServiceCollection services, + IAbpApplication abpApplication, + AbpApplicationCreationOptions applicationCreationOptions) { var moduleLoader = new ModuleLoader(); var assemblyFinder = new AssemblyFinder(abpApplication); var typeFinder = new TypeFinder(assemblyFinder); - services.TryAddSingleton(DefaultConfigurationAccessor.Empty); + services.TryAddSingleton( + new DefaultConfigurationAccessor( + ConfigurationHelper.BuildConfiguration( + applicationCreationOptions.Configuration + ) + ) + ); + services.TryAddSingleton(moduleLoader); services.TryAddSingleton(assemblyFinder); services.TryAddSingleton(typeFinder); diff --git a/framework/src/Volo.Abp.Data/Volo/Abp/Data/AbpDataModule.cs b/framework/src/Volo.Abp.Data/Volo/Abp/Data/AbpDataModule.cs index 1a5fa8bab9..c7fa93a664 100644 --- a/framework/src/Volo.Abp.Data/Volo/Abp/Data/AbpDataModule.cs +++ b/framework/src/Volo.Abp.Data/Volo/Abp/Data/AbpDataModule.cs @@ -7,6 +7,10 @@ namespace Volo.Abp.Data { public override void ConfigureServices(ServiceConfigurationContext context) { + var configuration = context.Services.GetConfiguration(); + + Configure(configuration); + context.Services.AddSingleton(typeof(IDataFilter<>), typeof(DataFilter<>)); } } diff --git a/framework/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/AbpHttpClientModule.cs b/framework/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/AbpHttpClientModule.cs index 5e9400994d..0331c51fff 100644 --- a/framework/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/AbpHttpClientModule.cs +++ b/framework/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/AbpHttpClientModule.cs @@ -1,4 +1,5 @@ -using Volo.Abp.Castle; +using Microsoft.Extensions.DependencyInjection; +using Volo.Abp.Castle; using Volo.Abp.Modularity; namespace Volo.Abp.Http.Client @@ -7,6 +8,11 @@ namespace Volo.Abp.Http.Client [DependsOn(typeof(AbpCastleCoreModule))] public class AbpHttpClientModule : AbpModule { + public override void ConfigureServices(ServiceConfigurationContext context) + { + var configuration = context.Services.GetConfiguration(); + Configure(configuration); + } } } diff --git a/framework/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/DynamicProxying/DynamicHttpProxyInterceptor.cs b/framework/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/DynamicProxying/DynamicHttpProxyInterceptor.cs index 350a88a17a..f6be5ac630 100644 --- a/framework/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/DynamicProxying/DynamicHttpProxyInterceptor.cs +++ b/framework/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/DynamicProxying/DynamicHttpProxyInterceptor.cs @@ -126,7 +126,7 @@ namespace Volo.Abp.Http.Client.DynamicProxying var accessToken = await _accessTokenProvider.GetOrNullAsync(); if (accessToken != null) { - //TODO: "Bearer" should not by static. + //TODO: "Bearer" should not be static. client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken); }