@@ -22,43 +22,62 @@ public static class AppDefinitionExtensions
2222 /// <param name="entryPointsAssembly"></param>
2323 public static void AddDefinitionsWithModules ( this WebApplicationBuilder builder , string modulesFolderPath , params Type [ ] entryPointsAssembly )
2424 {
25- var modulesFolder = Path . Combine ( builder . Environment . ContentRootPath , modulesFolderPath ) ;
25+ var logger = builder . Services . BuildServiceProvider ( ) . GetRequiredService < ILogger < IAppDefinition > > ( ) ;
2626
27- if ( ! Directory . Exists ( modulesFolder ) )
27+ try
2828 {
29- throw new DirectoryNotFoundException ( modulesFolder ) ;
30- }
29+ var modulesFolder = Path . Combine ( builder . Environment . ContentRootPath , modulesFolderPath ) ;
3130
32- var types = new List < Type > ( ) ;
33- types . AddRange ( entryPointsAssembly ) ;
31+ if ( ! Directory . Exists ( modulesFolder ) )
32+ {
33+ if ( logger . IsEnabled ( LogLevel . Debug ) )
34+ {
35+ logger . LogDebug ( "[Error]: Directory not exists {ModuleName}" , modulesFolder ) ;
36+ }
37+ throw new DirectoryNotFoundException ( modulesFolder ) ;
38+ }
3439
35- var modulesDirectory = new DirectoryInfo ( modulesFolderPath ) ;
36- var modules = modulesDirectory . GetFiles ( "*.dll" ) ;
37- if ( ! modules . Any ( ) )
38- {
39- return ;
40- }
40+ var types = new List < Type > ( ) ;
41+ types . AddRange ( entryPointsAssembly ) ;
4142
42- foreach ( var fileInfo in modules )
43- {
44- var module = Assembly . LoadFile ( fileInfo . FullName ) ;
45- var typesAll = module . GetExportedTypes ( ) ;
46- var typesDefinition = typesAll
47- . Where ( Predicate )
48- . ToList ( ) ;
49-
50- var instances = typesDefinition . Select ( Activator . CreateInstance )
51- . Cast < IAppDefinition > ( )
52- . Where ( x => x . Enabled && x . Exported )
53- . Select ( x => x . GetType ( ) )
54- . ToList ( ) ;
55-
56- types . AddRange ( instances ) ;
57- }
43+ var modulesDirectory = new DirectoryInfo ( modulesFolderPath ) ;
44+ var modules = modulesDirectory . GetFiles ( "*.dll" ) ;
45+ if ( ! modules . Any ( ) )
46+ {
47+ if ( logger . IsEnabled ( LogLevel . Debug ) )
48+ {
49+ logger . LogDebug ( "[Warning]: No modules found in folder {ModuleName}" , modulesFolder ) ;
50+ }
51+ return ;
52+ }
53+
54+ foreach ( var fileInfo in modules )
55+ {
56+ var module = Assembly . LoadFile ( fileInfo . FullName ) ;
57+ var typesAll = module . GetExportedTypes ( ) ;
58+ var typesDefinition = typesAll
59+ . Where ( Predicate )
60+ . ToList ( ) ;
61+
62+ var instances = typesDefinition . Select ( Activator . CreateInstance )
63+ . Cast < IAppDefinition > ( )
64+ . Where ( x => x . Enabled && x . Exported )
65+ . Select ( x => x . GetType ( ) )
66+ . ToList ( ) ;
67+
68+ types . AddRange ( instances ) ;
69+ }
5870
59- if ( types . Any ( ) )
71+ if ( types . Any ( ) )
72+ {
73+ AddDefinitions ( builder , types . ToArray ( ) ) ;
74+ }
75+
76+ }
77+ catch ( Exception exception )
6078 {
61- AddDefinitions ( builder , types . ToArray ( ) ) ;
79+ logger . LogError ( exception , exception . Message ) ;
80+ throw ;
6281 }
6382 }
6483
@@ -81,65 +100,72 @@ public static void AddDefinitionsWithModules(this WebApplicationBuilder builder,
81100 public static void AddDefinitions ( this WebApplicationBuilder builder , params Type [ ] entryPointsAssembly )
82101 {
83102 var logger = builder . Services . BuildServiceProvider ( ) . GetRequiredService < ILogger < IAppDefinition > > ( ) ;
84- var appDefinitionInfo = builder . Services . BuildServiceProvider ( ) . GetService < AppDefinitionCollection > ( ) ;
85- var definitionCollection = appDefinitionInfo ?? new AppDefinitionCollection ( ) ;
86-
87- foreach ( var entryPoint in entryPointsAssembly )
103+ try
88104 {
89- definitionCollection . AddEntryPoint ( entryPoint . Name ) ;
90-
91- var types = entryPoint . Assembly . ExportedTypes . Where ( Predicate ) ;
92- var instances = types . Select ( Activator . CreateInstance ) . Cast < IAppDefinition > ( ) . Where ( x => x . Enabled ) . OrderBy ( x => x . OrderIndex ) . ToList ( ) ;
105+ var appDefinitionInfo = builder . Services . BuildServiceProvider ( ) . GetService < AppDefinitionCollection > ( ) ;
106+ var definitionCollection = appDefinitionInfo ?? new AppDefinitionCollection ( ) ;
93107
94- foreach ( var definition in instances )
108+ foreach ( var entryPoint in entryPointsAssembly )
95109 {
96- definitionCollection . AddInfo ( new AppDefinitionItem ( definition , entryPoint . Name , definition . Enabled , definition . Exported ) ) ;
110+ definitionCollection . AddEntryPoint ( entryPoint . Name ) ;
111+
112+ var types = entryPoint . Assembly . ExportedTypes . Where ( Predicate ) ;
113+ var instances = types . Select ( Activator . CreateInstance ) . Cast < IAppDefinition > ( ) . Where ( x => x . Enabled ) . OrderBy ( x => x . OrderIndex ) . ToList ( ) ;
114+
115+ foreach ( var definition in instances )
116+ {
117+ definitionCollection . AddInfo ( new AppDefinitionItem ( definition , entryPoint . Name , definition . Enabled , definition . Exported ) ) ;
118+ }
97119 }
98- }
99120
100- if ( logger . IsEnabled ( LogLevel . Information ) )
101- {
102- logger . LogInformation ( "[AppDefinitions entry points found]: {@items}" , string . Join ( ", " , definitionCollection . EntryPoints ) ) ;
103- }
121+ if ( logger . IsEnabled ( LogLevel . Information ) )
122+ {
123+ logger . LogInformation ( "[AppDefinitions entry points found]: {@items}" , string . Join ( ", " , definitionCollection . EntryPoints ) ) ;
124+ }
104125
105- var items = definitionCollection . GetDistinct ( ) . ToList ( ) ;
126+ var items = definitionCollection . GetDistinct ( ) . ToList ( ) ;
106127
107- foreach ( var item in items )
108- {
109- if ( logger . IsEnabled ( LogLevel . Debug ) )
128+ foreach ( var item in items )
110129 {
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" ) ;
130+ if ( logger . IsEnabled ( LogLevel . Debug ) )
131+ {
132+ logger . LogDebug ( "[AppDefinitions for ConfigureServices]: {@AssemblyName}:{@AppDefinitionName} is {EnabledOrDisabled} {ExportEnabled}" ,
133+ item . AssemblyName ,
134+ item . Definition . GetType ( ) . Name ,
135+ item . Enabled ? "enabled" : "disabled" ,
136+ item . Exported ? "(exported)" : "export disabled" ) ;
137+ }
138+
139+ item . Definition . ConfigureServices ( builder ) ;
116140 }
117141
118- item . Definition . ConfigureServices ( builder ) ;
119- }
142+ builder . Services . AddSingleton ( definitionCollection ) ;
120143
121- builder . Services . AddSingleton ( definitionCollection ) ;
144+ if ( ! logger . IsEnabled ( LogLevel . Debug ) )
145+ {
146+ return ;
147+ }
122148
123- if ( ! logger . IsEnabled ( LogLevel . Debug ) )
124- {
125- return ;
126- }
149+ var skipped = definitionCollection . GetEnabled ( ) . Except ( items ) . ToList ( ) ;
150+ if ( ! skipped . Any ( ) )
151+ {
152+ return ;
153+ }
127154
128- var skipped = definitionCollection . GetEnabled ( ) . Except ( items ) . ToList ( ) ;
129- if ( ! skipped . Any ( ) )
130- {
131- return ;
155+ logger . LogWarning ( "[AppDefinitions skipped for ConfigureServices: {Count}" , skipped . Count ) ;
156+ foreach ( var item in skipped )
157+ {
158+ logger . LogWarning ( "[AppDefinitions skipped for ConfigureServices]: {@AssemblyName}:{@AppDefinitionName} is {EnabledOrDisabled}" ,
159+ item . AssemblyName ,
160+ item . Definition . GetType ( ) . Name ,
161+ item . Enabled ? "enabled" : "disabled" ) ;
162+ }
132163 }
133-
134- logger . LogWarning ( "[AppDefinitions skipped for ConfigureServices: {Count}" , skipped . Count ) ;
135- foreach ( var item in skipped )
164+ catch ( Exception exception )
136165 {
137- logger . LogWarning ( "[AppDefinitions skipped for ConfigureServices]: {@AssemblyName}:{@AppDefinitionName} is {EnabledOrDisabled}" ,
138- item . AssemblyName ,
139- item . Definition . GetType ( ) . Name ,
140- item . Enabled ? "enabled" : "disabled" ) ;
166+ logger . LogError ( exception , exception . Message ) ;
167+ throw ;
141168 }
142-
143169 }
144170
145171 /// <summary>
0 commit comments