Browse Source
Log exception of `ReflectionTypeLoadException`.
pull/22983/head
maliming
1 year ago
No known key found for this signature in database
GPG Key ID: A646B9CB645ECEA4
6 changed files with
46 additions and
22 deletions
-
framework/src/Volo.Abp.Core/Microsoft/Extensions/DependencyInjection/ServiceCollectionLoggingExtensions.cs
-
framework/src/Volo.Abp.Core/Volo/Abp/DependencyInjection/ConventionalRegistrarBase.cs
-
framework/src/Volo.Abp.Core/Volo/Abp/Internal/InternalServiceCollectionExtensions.cs
-
framework/src/Volo.Abp.Core/Volo/Abp/Reflection/AssemblyHelper.cs
-
framework/src/Volo.Abp.Core/Volo/Abp/Reflection/TypeFinder.cs
-
framework/test/Volo.Abp.Core.Tests/Volo/Abp/Reflection/TypeFinder_Tests.cs
|
|
|
@ -1,4 +1,5 @@ |
|
|
|
using Microsoft.Extensions.Logging; |
|
|
|
using Microsoft.Extensions.Logging.Abstractions; |
|
|
|
using Volo.Abp.Logging; |
|
|
|
|
|
|
|
namespace Microsoft.Extensions.DependencyInjection; |
|
|
|
@ -7,6 +8,7 @@ public static class ServiceCollectionLoggingExtensions |
|
|
|
{ |
|
|
|
public static ILogger<T> GetInitLogger<T>(this IServiceCollection services) |
|
|
|
{ |
|
|
|
return services.GetSingletonInstance<IInitLoggerFactory>().Create<T>(); |
|
|
|
var loggerFactory = services.GetSingletonInstanceOrNull<IInitLoggerFactory>(); |
|
|
|
return loggerFactory == null ? NullLogger<T>.Instance : loggerFactory.Create<T>(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@ -3,6 +3,7 @@ using System.Collections.Generic; |
|
|
|
using System.Linq; |
|
|
|
using System.Reflection; |
|
|
|
using Microsoft.Extensions.DependencyInjection; |
|
|
|
using Microsoft.Extensions.Logging; |
|
|
|
using Volo.Abp.Reflection; |
|
|
|
|
|
|
|
namespace Volo.Abp.DependencyInjection; |
|
|
|
@ -11,14 +12,30 @@ public abstract class ConventionalRegistrarBase : IConventionalRegistrar |
|
|
|
{ |
|
|
|
public virtual void AddAssembly(IServiceCollection services, Assembly assembly) |
|
|
|
{ |
|
|
|
var types = AssemblyHelper |
|
|
|
.GetAllTypes(assembly) |
|
|
|
.Where( |
|
|
|
type => type != null && |
|
|
|
type.IsClass && |
|
|
|
!type.IsAbstract && |
|
|
|
!type.IsGenericType |
|
|
|
).ToArray(); |
|
|
|
var logger = services.GetInitLogger<ConventionalRegistrarBase>(); |
|
|
|
var types = Array.Empty<Type>(); |
|
|
|
|
|
|
|
try |
|
|
|
{ |
|
|
|
types = AssemblyHelper |
|
|
|
.GetAllTypes(assembly) |
|
|
|
.Where( |
|
|
|
type => type != null && |
|
|
|
type.IsClass && |
|
|
|
!type.IsAbstract && |
|
|
|
!type.IsGenericType |
|
|
|
).ToArray(); |
|
|
|
} |
|
|
|
catch (ReflectionTypeLoadException e) |
|
|
|
{ |
|
|
|
types = e.Types.Select(x => x!).ToArray(); |
|
|
|
logger.LogException(e); |
|
|
|
} |
|
|
|
catch (Exception e) |
|
|
|
{ |
|
|
|
//TODO: Trigger a global event?
|
|
|
|
logger.LogException(e); |
|
|
|
} |
|
|
|
|
|
|
|
AddTypes(services, types); |
|
|
|
} |
|
|
|
|
|
|
|
@ -23,7 +23,6 @@ internal static class InternalServiceCollectionExtensions |
|
|
|
{ |
|
|
|
var moduleLoader = new ModuleLoader(); |
|
|
|
var assemblyFinder = new AssemblyFinder(abpApplication); |
|
|
|
var typeFinder = new TypeFinder(assemblyFinder); |
|
|
|
|
|
|
|
if (!services.IsAdded<IConfiguration>()) |
|
|
|
{ |
|
|
|
@ -36,8 +35,9 @@ internal static class InternalServiceCollectionExtensions |
|
|
|
|
|
|
|
services.TryAddSingleton<IModuleLoader>(moduleLoader); |
|
|
|
services.TryAddSingleton<IAssemblyFinder>(assemblyFinder); |
|
|
|
services.TryAddSingleton<ITypeFinder>(typeFinder); |
|
|
|
services.TryAddSingleton<IInitLoggerFactory>(new DefaultInitLoggerFactory()); |
|
|
|
var typeFinder = new TypeFinder(services.GetInitLogger<TypeFinder>(), assemblyFinder); |
|
|
|
services.TryAddSingleton<ITypeFinder>(typeFinder); |
|
|
|
|
|
|
|
services.AddAssemblyOf<IAbpApplication>(); |
|
|
|
|
|
|
|
|
|
|
|
@ -25,13 +25,6 @@ internal static class AssemblyHelper |
|
|
|
|
|
|
|
public static IReadOnlyList<Type> GetAllTypes(Assembly assembly) |
|
|
|
{ |
|
|
|
try |
|
|
|
{ |
|
|
|
return assembly.GetTypes(); |
|
|
|
} |
|
|
|
catch (ReflectionTypeLoadException ex) |
|
|
|
{ |
|
|
|
return ex.Types!; |
|
|
|
} |
|
|
|
return assembly.GetTypes(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@ -1,19 +1,24 @@ |
|
|
|
using System; |
|
|
|
using System.Collections.Generic; |
|
|
|
using System.Linq; |
|
|
|
using System.Reflection; |
|
|
|
using System.Threading; |
|
|
|
using Microsoft.Extensions.Logging; |
|
|
|
|
|
|
|
namespace Volo.Abp.Reflection; |
|
|
|
|
|
|
|
public class TypeFinder : ITypeFinder |
|
|
|
{ |
|
|
|
private readonly ILogger<TypeFinder> _logger; |
|
|
|
|
|
|
|
private readonly IAssemblyFinder _assemblyFinder; |
|
|
|
|
|
|
|
private readonly Lazy<IReadOnlyList<Type>> _types; |
|
|
|
|
|
|
|
public TypeFinder(IAssemblyFinder assemblyFinder) |
|
|
|
public TypeFinder(ILogger<TypeFinder> logger, IAssemblyFinder assemblyFinder) |
|
|
|
{ |
|
|
|
_assemblyFinder = assemblyFinder; |
|
|
|
_logger = logger; |
|
|
|
|
|
|
|
_types = new Lazy<IReadOnlyList<Type>>(FindAll, LazyThreadSafetyMode.ExecutionAndPublication); |
|
|
|
} |
|
|
|
@ -37,9 +42,15 @@ public class TypeFinder : ITypeFinder |
|
|
|
|
|
|
|
allTypes.AddRange(typesInThisAssembly.Where(type => type != null)); |
|
|
|
} |
|
|
|
catch |
|
|
|
catch (ReflectionTypeLoadException e) |
|
|
|
{ |
|
|
|
allTypes = e.Types.Select(x => x!).ToList(); |
|
|
|
_logger.LogException(e); |
|
|
|
} |
|
|
|
catch (Exception e) |
|
|
|
{ |
|
|
|
//TODO: Trigger a global event?
|
|
|
|
_logger.LogException(e); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@ -1,5 +1,6 @@ |
|
|
|
using System.Collections.Generic; |
|
|
|
using System.Reflection; |
|
|
|
using Microsoft.Extensions.Logging.Abstractions; |
|
|
|
using NSubstitute; |
|
|
|
using Shouldly; |
|
|
|
using Xunit; |
|
|
|
@ -21,7 +22,7 @@ public class TypeFinder_Tests |
|
|
|
|
|
|
|
//Act
|
|
|
|
|
|
|
|
var typeFinder = new TypeFinder(fakeAssemblyFinder); |
|
|
|
var typeFinder = new TypeFinder(NullLogger<TypeFinder>.Instance, fakeAssemblyFinder); |
|
|
|
|
|
|
|
//Assert
|
|
|
|
|
|
|
|
|