33using Cuture . Extensions . Modularity . Internal ;
44
55using Microsoft . Extensions . Configuration ;
6+ using Microsoft . Extensions . DependencyInjection ;
67using Microsoft . Extensions . Hosting ;
78
8- namespace Microsoft . Extensions . DependencyInjection
9+ namespace Cuture . Extensions . Modularity ;
10+
11+ /// <summary>
12+ ///
13+ /// </summary>
14+ public static class ConfigurationServiceCollectionExtensions
915{
16+ #region Public 方法
17+
1018 /// <summary>
11- ///
19+ /// 从<see cref="IServiceCollection"/>中获取<see cref="IConfiguration"/>
1220 /// </summary>
13- public static class ConfigurationServiceCollectionExtensions
21+ /// <param name="services"></param>
22+ /// <returns></returns>
23+ public static IConfiguration ? GetConfiguration ( this IServiceCollection services )
1424 {
15- #region Public 方法
16-
17- /// <summary>
18- /// 从<see cref="IServiceCollection"/>中获取<see cref="IConfiguration"/>
19- /// </summary>
20- /// <param name="services"></param>
21- /// <returns></returns>
22- public static IConfiguration ? GetConfiguration ( this IServiceCollection services )
25+ if ( services . TryGetObjectAccessorValue < IConfigurationContainer > ( out var iConfigurationContainer )
26+ && iConfigurationContainer is not null )
2327 {
24- if ( services . TryGetObjectAccessorValue < IConfigurationContainer > ( out var iConfigurationContainer )
25- && iConfigurationContainer is not null )
26- {
27- return iConfigurationContainer . Value ;
28- }
29- var hostBuilderContext = services . GetSingletonServiceInstance < HostBuilderContext > ( ) ;
30-
31- return hostBuilderContext ? . Configuration as IConfigurationRoot
32- ?? services . GetSingletonServiceInstance < IConfiguration > ( )
33- ?? ( services . TryGetObjectAccessorValue < IConfiguration > ( out var configuration ) ? configuration : null ) ;
28+ return iConfigurationContainer . Value ;
3429 }
30+ var hostBuilderContext = services . GetSingletonServiceInstance < HostBuilderContext > ( ) ;
31+
32+ return hostBuilderContext ? . Configuration as IConfigurationRoot
33+ ?? services . GetSingletonServiceInstance < IConfiguration > ( )
34+ ?? ( services . TryGetObjectAccessorValue < IConfiguration > ( out var configuration ) ? configuration : null ) ;
35+ }
3536
36- /// <inheritdoc cref="GetConfiguration(IServiceCollection)"/>
37- public static IConfiguration GetRequiredConfiguration ( this IServiceCollection services ) => services . GetConfiguration ( )
38- ?? throw new InvalidOperationException ( $ "Not found { nameof ( IConfiguration ) } in serviceCollection.") ;
37+ /// <inheritdoc cref="GetConfiguration(IServiceCollection)"/>
38+ public static IConfiguration GetRequiredConfiguration ( this IServiceCollection services ) => services . GetConfiguration ( )
39+ ?? throw new InvalidOperationException ( $ "Not found { nameof ( IConfiguration ) } in serviceCollection.") ;
3940
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 )
41+ /// <summary>
42+ /// 将用于<see cref="GetConfiguration(IServiceCollection)"/>获取的<see cref="IConfiguration"/>添加到<paramref name="services"/>中
43+ /// </summary>
44+ /// <param name="services"></param>
45+ /// <param name="configuration"></param>
46+ /// <returns>是否为新添加</returns>
47+ public static bool SetConfiguration ( this IServiceCollection services , IConfiguration configuration )
48+ {
49+ if ( services . TryGetObjectAccessor < IConfigurationContainer > ( out var container )
50+ && container is not null )
4751 {
48- if ( services . TryGetObjectAccessor < IConfigurationContainer > ( out var container )
49- && container is not null )
52+ if ( container . Value is null )
5053 {
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 ;
54+ container . Value = new ( configuration ) ;
6055 }
6156 else
6257 {
63- services . AddObjectAccessor < IConfigurationContainer > ( new ( configuration ) ) ;
64- return true ;
58+ container . Value . Value = configuration ;
6559 }
60+ return false ;
61+ }
62+ else
63+ {
64+ services . AddObjectAccessor < IConfigurationContainer > ( new ( configuration ) ) ;
65+ return true ;
6666 }
67-
68- #endregion Public 方法
6967 }
68+
69+ #endregion Public 方法
7070}
0 commit comments