File tree Expand file tree Collapse file tree
Cuture.Extensions.Modularity.Hosting Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -22,6 +22,7 @@ public override void ConfigureServices(ServiceConfigurationContext context)
2222 {
2323 var services = context . Services ;
2424 var configure = services . GetConfiguration ( ) ;
25+ var hostBuilder = services . GetRequiredHostBuilder ( ) ;
2526
2627 Console . WriteLine ( $ "Current User: { configure [ "USERNAME" ] ?? configure [ "USER" ] } ") ;
2728 }
Original file line number Diff line number Diff line change 11using System ;
22
3+ using Cuture . Extensions . Modularity . Internal ;
4+
35using Microsoft . Extensions . Configuration ;
46using Microsoft . Extensions . DependencyInjection ;
57using Microsoft . Extensions . Hosting ;
@@ -50,21 +52,29 @@ internal static IHostBuilder InternalAddModuleSource(this IHostBuilder hostBuild
5052
5153 hostBuilder . ConfigureServices ( ( context , services ) =>
5254 {
55+ services . AddObjectAccessor < IHostBuilderContainer > ( new ( hostBuilder ) ) ;
5356 services . AddObjectAccessor < IConfiguration > ( context . Configuration ) ;
5457
55- foreach ( var item in loadContext . ModuleSources )
58+ try
5659 {
57- services . LoadModule ( item . Key , item . Value ) ;
58- }
60+ foreach ( var item in loadContext . ModuleSources )
61+ {
62+ services . LoadModule ( item . Key , item . Value ) ;
63+ }
64+
65+ foreach ( var item in loadContext . OptionActions )
66+ {
67+ services . OptionModuleLoadBuilder ( item ) ;
68+ }
5969
60- foreach ( var item in loadContext . OptionActions )
70+ services . ModuleLoadComplete ( ) ;
71+ }
72+ finally
6173 {
62- services . OptionModuleLoadBuilder ( item ) ;
74+ //TODO 不直接移除IConfiguration
75+ services . RemoveObjectAccessor < IConfiguration > ( ) ;
76+ services . RemoveObjectAccessor < IHostBuilderContainer > ( ) ;
6377 }
64-
65- services . ModuleLoadComplete ( ) ;
66-
67- services . RemoveObjectAccessor < IConfiguration > ( ) ;
6878 } ) ;
6979 }
7080
Original file line number Diff line number Diff line change 1+ using System ;
2+
3+ using Cuture . Extensions . Modularity . Internal ;
4+
5+ using Microsoft . Extensions . Hosting ;
6+
7+ namespace Microsoft . Extensions . DependencyInjection
8+ {
9+ /// <summary>
10+ ///
11+ /// </summary>
12+ public static class HostBuilderServiceCollectionExtensions
13+ {
14+ #region Public 方法
15+
16+ /// <summary>
17+ /// 从<see cref="IServiceCollection"/>中获取<see cref="IHostBuilder"/>
18+ /// </summary>
19+ /// <param name="services"></param>
20+ /// <returns></returns>
21+ public static IHostBuilder ? GetHostBuilder ( this IServiceCollection services ) => services . GetObjectAccessorValue < IHostBuilderContainer > ( ) ? . Value ;
22+
23+ /// <inheritdoc cref="GetHostBuilder(IServiceCollection)"/>
24+ public static IHostBuilder GetRequiredHostBuilder ( this IServiceCollection services ) => services . GetObjectAccessorValue < IHostBuilderContainer > ( ) ? . Value
25+ ?? throw new InvalidOperationException ( $ "Not found { nameof ( IHostBuilder ) } in serviceCollection.") ;
26+
27+ #endregion Public 方法
28+ }
29+ }
Original file line number Diff line number Diff line change 1+ using System ;
2+
3+ using Microsoft . Extensions . Hosting ;
4+
5+ namespace Cuture . Extensions . Modularity . Internal ;
6+
7+ internal class IHostBuilderContainer
8+ {
9+ #region Public 属性
10+
11+ public IHostBuilder Value { get ; set ; }
12+
13+ #endregion Public 属性
14+
15+ #region Public 构造函数
16+
17+ public IHostBuilderContainer ( IHostBuilder value )
18+ {
19+ Value = value ?? throw new ArgumentNullException ( nameof ( value ) ) ;
20+ }
21+
22+ #endregion Public 构造函数
23+ }
Original file line number Diff line number Diff line change 2929
3030 <!-- Package Info-->
3131 <PropertyGroup >
32- <VersionPrefix >1.1.7 </VersionPrefix >
32+ <VersionPrefix >1.1.8 </VersionPrefix >
3333
3434 <PackageIdPrefix >Cuture.Extensions</PackageIdPrefix >
3535 <Authors >Stratos</Authors >
5151 </PropertyGroup >
5252
5353 <ItemGroup Condition =" '$(Configuration)' == 'Release'" >
54- <PackageReference Include =" Microsoft.SourceLink.GitHub" Version =" 1.0.0 " PrivateAssets =" All" />
54+ <PackageReference Include =" Microsoft.SourceLink.GitHub" Version =" 1.* " PrivateAssets =" All" />
5555 </ItemGroup >
5656</Project >
You can’t perform that action at this time.
0 commit comments