@@ -81,42 +81,65 @@ public static void AddDefinitionsWithModules(this WebApplicationBuilder builder,
8181 public static void AddDefinitions ( this WebApplicationBuilder builder , params Type [ ] entryPointsAssembly )
8282 {
8383 var logger = builder . Services . BuildServiceProvider ( ) . GetRequiredService < ILogger < IAppDefinition > > ( ) ;
84- var definitions = new List < IAppDefinition > ( ) ;
8584 var appDefinitionInfo = builder . Services . BuildServiceProvider ( ) . GetService < AppDefinitionCollection > ( ) ;
86- var info = appDefinitionInfo ?? new AppDefinitionCollection ( ) ;
85+ var definitionCollection = appDefinitionInfo ?? new AppDefinitionCollection ( ) ;
8786
8887 foreach ( var entryPoint in entryPointsAssembly )
8988 {
90- info . AddEntryPoint ( entryPoint . Name ) ;
89+ definitionCollection . AddEntryPoint ( entryPoint . Name ) ;
9190
9291 var types = entryPoint . Assembly . ExportedTypes . Where ( Predicate ) ;
93- var instances = types . Select ( Activator . CreateInstance ) . Cast < IAppDefinition > ( ) . ToList ( ) ;
92+ var instances = types . Select ( Activator . CreateInstance ) . Cast < IAppDefinition > ( ) . Where ( x => x . Enabled ) . OrderBy ( x => x . OrderIndex ) . ToList ( ) ;
9493
9594 foreach ( var definition in instances )
9695 {
97- info . AddInfo ( new AppDefinitionItem ( definition , entryPoint . Name , definition . Enabled , definition . Exported ) ) ;
96+ definitionCollection . AddInfo ( new AppDefinitionItem ( definition , entryPoint . Name , definition . Enabled , definition . Exported ) ) ;
9897 }
99-
100- var instancesOrdered = instances . Where ( x => x . Enabled ) . OrderBy ( x => x . OrderIndex ) . ToList ( ) ;
101- definitions . AddRange ( instancesOrdered ) ;
10298 }
10399
104- foreach ( var definition in definitions )
100+ if ( logger . IsEnabled ( LogLevel . Information ) )
105101 {
106- definition . ConfigureServices ( builder ) ;
102+ logger . LogInformation ( "[AppDefinitions entry points found]: {@items}" , string . Join ( ", " , definitionCollection . EntryPoints ) ) ;
107103 }
108104
109- if ( logger . IsEnabled ( LogLevel . Debug ) )
110- {
111- logger . LogDebug ( "[AppDefinitions]: From {@items}" , string . Join ( ", " , info . EntryPoints ) ) ;
105+ var items = definitionCollection . GetDistinct ( ) . ToList ( ) ;
112106
113- foreach ( var item in info . Items . OrderBy ( x => x . Definition . GetType ( ) . Name ) )
107+ foreach ( var item in items )
108+ {
109+ if ( logger . IsEnabled ( LogLevel . Debug ) )
114110 {
115- logger . LogDebug ( "[AppDefinitions]: {@AppDefinitionName} ({@AssemblyName}) (Enabled: {@Enabled})" , item . Definition . GetType ( ) . Name , item . AssemblyName , item . Definition . Enabled ? "Yes" : "No" ) ;
111+ logger . LogDebug ( "[AppDefinitions for ConfigureServices]: {@AssemblyName}:{@AppDefinitionName} is {EnabledOrDisabled} {ExportEnabled}" ,
112+ item . AssemblyName ,
113+ item . Definition . GetType ( ) . Name ,
114+ item . Enabled ? "enabled" : "disabled" ,
115+ item . Exported ? "(exported)" : "export disabled" ) ;
116116 }
117+
118+ item . Definition . ConfigureServices ( builder ) ;
119+ }
120+
121+ builder . Services . AddSingleton ( definitionCollection ) ;
122+
123+ if ( ! logger . IsEnabled ( LogLevel . Debug ) )
124+ {
125+ return ;
126+ }
127+
128+ var skipped = definitionCollection . GetEnabled ( ) . Except ( items ) . ToList ( ) ;
129+ if ( ! skipped . Any ( ) )
130+ {
131+ return ;
132+ }
133+
134+ logger . LogWarning ( "[AppDefinitions skipped for ConfigureServices: {Count}" , skipped . Count ) ;
135+ foreach ( var item in skipped )
136+ {
137+ logger . LogWarning ( "[AppDefinitions skipped for ConfigureServices]: {@AssemblyName}:{@AppDefinitionName} is {EnabledOrDisabled}" ,
138+ item . AssemblyName ,
139+ item . Definition . GetType ( ) . Name ,
140+ item . Enabled ? "enabled" : "disabled" ) ;
117141 }
118142
119- builder . Services . AddSingleton ( info ) ;
120143 }
121144
122145 /// <summary>
@@ -130,20 +153,52 @@ public static void AddDefinitions(this WebApplicationBuilder builder, params Typ
130153 public static void UseDefinitions ( this WebApplication source )
131154 {
132155 var logger = source . Services . GetRequiredService < ILogger < AppDefinition > > ( ) ;
133- var definitions = source . Services . GetRequiredService < AppDefinitionCollection > ( ) ;
156+ var definitionCollection = source . Services . GetRequiredService < AppDefinitionCollection > ( ) ;
157+
158+ var items = definitionCollection . GetDistinct ( ) . ToList ( ) ;
134159
135- if ( logger . IsEnabled ( LogLevel . Debug ) )
160+ foreach ( var item in items )
136161 {
137- logger . LogDebug ( "From {Modules} assemblies totally AppDefinitions found: {Count} " , string . Join ( ", " , definitions . EntryPoints ) , definitions . Items . Count ) ;
162+ if ( logger . IsEnabled ( LogLevel . Debug ) )
163+ {
164+ logger . LogDebug ( "[AppDefinitions for ConfigureApplication]: {@AssemblyName}:{@AppDefinitionName} is {EnabledOrDisabled}" ,
165+ item . AssemblyName ,
166+ item . Definition . GetType ( ) . Name ,
167+ item . Enabled
168+ ? "enabled"
169+ : "disabled" ) ;
170+ }
171+
172+ item . Definition . ConfigureApplication ( source ) ;
138173 }
139174
140- var instancesOrdered = definitions . Items . Where ( x => x . Definition . Enabled ) . OrderBy ( x => x . Definition . OrderIndex ) . ToList ( ) ;
175+ if ( ! logger . IsEnabled ( LogLevel . Debug ) )
176+ {
177+ if ( logger . IsEnabled ( LogLevel . Information ) )
178+ {
179+ logger . LogInformation ( "[AppDefinitions applied: {Count} of {Total}" , items . Count , definitionCollection . GetEnabled ( ) . Count ( ) ) ;
180+ }
181+ return ;
182+ }
141183
142- instancesOrdered . ForEach ( x => x . Definition . ConfigureApplication ( source ) ) ;
184+ var skipped = definitionCollection . GetEnabled ( ) . Except ( items ) . ToList ( ) ;
185+ if ( ! skipped . Any ( ) )
186+ {
187+ logger . LogInformation ( "[AppDefinitions applied: {Count} of {Total}" , items . Count , definitionCollection . GetEnabled ( ) . Count ( ) ) ;
188+ return ;
189+ }
143190
144- if ( logger . IsEnabled ( LogLevel . Debug ) )
191+ logger . LogWarning ( "[AppDefinitions skipped for ConfigureApplication: {Count}" , skipped . Count ) ;
192+ foreach ( var item in skipped )
145193 {
146- logger . LogDebug ( "Total AppDefinitions applied: {Count}" , instancesOrdered . Count ) ;
194+ logger . LogWarning ( "[AppDefinitions skipped for ConfigureApplication]: {@AssemblyName}:{@AppDefinitionName} is {EnabledOrDisabled} {ExportEnabled}" ,
195+ item . AssemblyName ,
196+ item . Definition . GetType ( ) . Name ,
197+ item . Enabled ? "enabled" : "disabled" ,
198+ item . Exported ? "(exported)" : "export disabled" ) ;
147199 }
200+
201+ logger . LogInformation ( "[AppDefinitions applied: {Count} of {Total}" , items . Count , definitionCollection . GetEnabled ( ) . Count ( ) ) ;
202+
148203 }
149204}
0 commit comments