File tree Expand file tree Collapse file tree
Cuture.Extensions.Modularity.Hosting/Extensions Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -52,7 +52,12 @@ internal static IHostBuilder InternalAddModuleSource(this IHostBuilder hostBuild
5252 hostBuilder . ConfigureServices ( ( context , services ) =>
5353 {
5454 services . AddObjectAccessor < IHostBuilderContainer > ( new ( hostBuilder ) ) ;
55- services . AddObjectAccessor < IConfigurationContainer > ( new ( context . Configuration ) ) ;
55+ var hasConfiguration = services . GetConfiguration ( ) is not null ;
56+ var removeIConfigurationContainer = false ;
57+ if ( ! hasConfiguration )
58+ {
59+ removeIConfigurationContainer = services . SetConfiguration ( context . Configuration ) ;
60+ }
5661
5762 try
5863 {
@@ -70,7 +75,10 @@ internal static IHostBuilder InternalAddModuleSource(this IHostBuilder hostBuild
7075 }
7176 finally
7277 {
73- services . RemoveObjectAccessor < IConfigurationContainer > ( ) ;
78+ if ( removeIConfigurationContainer )
79+ {
80+ services . RemoveObjectAccessor < IConfigurationContainer > ( ) ;
81+ }
7482 services . RemoveObjectAccessor < IHostBuilderContainer > ( ) ;
7583 }
7684 } ) ;
Original file line number Diff line number Diff line change @@ -37,6 +37,34 @@ public static class ConfigurationServiceCollectionExtensions
3737 public static IConfiguration GetRequiredConfiguration ( this IServiceCollection services ) => services . GetConfiguration ( )
3838 ?? throw new InvalidOperationException ( $ "Not found { nameof ( IConfiguration ) } in serviceCollection.") ;
3939
40+ /// <summary>
41+ /// 将用于<see cref="GetConfiguration(IServiceCollection)"/>获取的<see cref="IConfiguration"/>添加到<paramref name="services"/>中
42+ /// </summary>
43+ /// <param name="services"></param>
44+ /// <param name="configuration"></param>
45+ /// <returns>是否为新添加</returns>
46+ public static bool SetConfiguration ( this IServiceCollection services , IConfiguration configuration )
47+ {
48+ if ( services . TryGetObjectAccessor < IConfigurationContainer > ( out var container )
49+ && container is not null )
50+ {
51+ if ( container . Value is null )
52+ {
53+ container . Value = new ( configuration ) ;
54+ }
55+ else
56+ {
57+ container . Value . Value = configuration ;
58+ }
59+ return false ;
60+ }
61+ else
62+ {
63+ services . AddObjectAccessor < IConfigurationContainer > ( new ( configuration ) ) ;
64+ return true ;
65+ }
66+ }
67+
4068 #endregion Public 方法
4169 }
4270}
Original file line number Diff line number Diff line change 2929
3030 <!-- Package Info-->
3131 <PropertyGroup >
32- <VersionPrefix >1.1.9 </VersionPrefix >
32+ <VersionPrefix >1.1.10 </VersionPrefix >
3333
3434 <PackageIdPrefix >Cuture.Extensions</PackageIdPrefix >
3535 <Authors >Stratos</Authors >
You can’t perform that action at this time.
0 commit comments