Skip to content

Commit 970adc7

Browse files
authored
Merge branch 'main' into Copyright-dynamic-update-added
2 parents ae81cc3 + fcc7f26 commit 970adc7

File tree

4 files changed

+106
-76
lines changed

4 files changed

+106
-76
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44

55
## Что нового
66

7+
### Версия 2.4.1
8+
9+
* Nuget-пакеты обновлены для использования `NET8`
10+
* Был добавлен перехват ошибок при поиске модулей определений (AppDefinition).
11+
712
### Версия 2.4.0
813

914
На ряду с версией для NET6.0 добавлена версия для NET8.0. В параметры пакетов добавлены следующие настройки:

src/Calabonga.AspNetCore.AppDefinitions/AppDefinitionExtensions.cs

Lines changed: 99 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -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>

src/Calabonga.AspNetCore.AppDefinitions/AppDefinitionItem.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
namespace Calabonga.AspNetCore.AppDefinitions;
22

3-
43
/// <summary>
54
/// Information about <see cref="IAppDefinition"/>
65
/// </summary>

src/Calabonga.AspNetCore.AppDefinitions/Calabonga.AspNetCore.AppDefinitions.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<Nullable>enable</Nullable>
77
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
88
<Title>Calabonga.AspNetCore.AppDefinitions</Title>
9-
<Version>2.4.0</Version>
9+
<Version>2.4.1</Version>
1010
<Authors>Calabonga</Authors>
1111
<Company>Calabonga Soft</Company>
1212
<Product>Calabonga.AspNetCore.AppDefinitions</Product>
@@ -18,7 +18,7 @@
1818
<RepositoryUrl>https://github.com/Calabonga/Calabonga.AspNetCore.AppDefinitions</RepositoryUrl>
1919
<RepositoryType>git</RepositoryType>
2020
<PackageTags>calabonga;architecture;application;definitions;minimal-api;nimble-framework</PackageTags>
21-
<PackageReleaseNotes>Processing for dublicates was removed. Some additional information added for DEBUG logging.</PackageReleaseNotes>
21+
<PackageReleaseNotes>Additional information added for DEBUG logging. Some errors catching added.</PackageReleaseNotes>
2222
<PackageLicenseExpression>MIT</PackageLicenseExpression>
2323
<IncludeSymbols>true</IncludeSymbols>
2424
<IncludeSource>true</IncludeSource>

0 commit comments

Comments
 (0)