Browse Source

Log exception of `ReflectionTypeLoadException`.

pull/22983/head
maliming 1 year ago
parent
commit
08aba25c36
No known key found for this signature in database GPG Key ID: A646B9CB645ECEA4
  1. 4
      framework/src/Volo.Abp.Core/Microsoft/Extensions/DependencyInjection/ServiceCollectionLoggingExtensions.cs
  2. 33
      framework/src/Volo.Abp.Core/Volo/Abp/DependencyInjection/ConventionalRegistrarBase.cs
  3. 4
      framework/src/Volo.Abp.Core/Volo/Abp/Internal/InternalServiceCollectionExtensions.cs
  4. 9
      framework/src/Volo.Abp.Core/Volo/Abp/Reflection/AssemblyHelper.cs
  5. 15
      framework/src/Volo.Abp.Core/Volo/Abp/Reflection/TypeFinder.cs
  6. 3
      framework/test/Volo.Abp.Core.Tests/Volo/Abp/Reflection/TypeFinder_Tests.cs

4
framework/src/Volo.Abp.Core/Microsoft/Extensions/DependencyInjection/ServiceCollectionLoggingExtensions.cs

@ -1,4 +1,5 @@
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Volo.Abp.Logging; using Volo.Abp.Logging;
namespace Microsoft.Extensions.DependencyInjection; namespace Microsoft.Extensions.DependencyInjection;
@ -7,6 +8,7 @@ public static class ServiceCollectionLoggingExtensions
{ {
public static ILogger<T> GetInitLogger<T>(this IServiceCollection services) 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>();
} }
} }

33
framework/src/Volo.Abp.Core/Volo/Abp/DependencyInjection/ConventionalRegistrarBase.cs

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Reflection; using System.Reflection;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Volo.Abp.Reflection; using Volo.Abp.Reflection;
namespace Volo.Abp.DependencyInjection; namespace Volo.Abp.DependencyInjection;
@ -11,14 +12,30 @@ public abstract class ConventionalRegistrarBase : IConventionalRegistrar
{ {
public virtual void AddAssembly(IServiceCollection services, Assembly assembly) public virtual void AddAssembly(IServiceCollection services, Assembly assembly)
{ {
var types = AssemblyHelper var logger = services.GetInitLogger<ConventionalRegistrarBase>();
.GetAllTypes(assembly) var types = Array.Empty<Type>();
.Where(
type => type != null && try
type.IsClass && {
!type.IsAbstract && types = AssemblyHelper
!type.IsGenericType .GetAllTypes(assembly)
).ToArray(); .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); AddTypes(services, types);
} }

4
framework/src/Volo.Abp.Core/Volo/Abp/Internal/InternalServiceCollectionExtensions.cs

@ -23,7 +23,6 @@ internal static class InternalServiceCollectionExtensions
{ {
var moduleLoader = new ModuleLoader(); var moduleLoader = new ModuleLoader();
var assemblyFinder = new AssemblyFinder(abpApplication); var assemblyFinder = new AssemblyFinder(abpApplication);
var typeFinder = new TypeFinder(assemblyFinder);
if (!services.IsAdded<IConfiguration>()) if (!services.IsAdded<IConfiguration>())
{ {
@ -36,8 +35,9 @@ internal static class InternalServiceCollectionExtensions
services.TryAddSingleton<IModuleLoader>(moduleLoader); services.TryAddSingleton<IModuleLoader>(moduleLoader);
services.TryAddSingleton<IAssemblyFinder>(assemblyFinder); services.TryAddSingleton<IAssemblyFinder>(assemblyFinder);
services.TryAddSingleton<ITypeFinder>(typeFinder);
services.TryAddSingleton<IInitLoggerFactory>(new DefaultInitLoggerFactory()); services.TryAddSingleton<IInitLoggerFactory>(new DefaultInitLoggerFactory());
var typeFinder = new TypeFinder(services.GetInitLogger<TypeFinder>(), assemblyFinder);
services.TryAddSingleton<ITypeFinder>(typeFinder);
services.AddAssemblyOf<IAbpApplication>(); services.AddAssemblyOf<IAbpApplication>();

9
framework/src/Volo.Abp.Core/Volo/Abp/Reflection/AssemblyHelper.cs

@ -25,13 +25,6 @@ internal static class AssemblyHelper
public static IReadOnlyList<Type> GetAllTypes(Assembly assembly) public static IReadOnlyList<Type> GetAllTypes(Assembly assembly)
{ {
try return assembly.GetTypes();
{
return assembly.GetTypes();
}
catch (ReflectionTypeLoadException ex)
{
return ex.Types!;
}
} }
} }

15
framework/src/Volo.Abp.Core/Volo/Abp/Reflection/TypeFinder.cs

@ -1,19 +1,24 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Reflection;
using System.Threading; using System.Threading;
using Microsoft.Extensions.Logging;
namespace Volo.Abp.Reflection; namespace Volo.Abp.Reflection;
public class TypeFinder : ITypeFinder public class TypeFinder : ITypeFinder
{ {
private readonly ILogger<TypeFinder> _logger;
private readonly IAssemblyFinder _assemblyFinder; private readonly IAssemblyFinder _assemblyFinder;
private readonly Lazy<IReadOnlyList<Type>> _types; private readonly Lazy<IReadOnlyList<Type>> _types;
public TypeFinder(IAssemblyFinder assemblyFinder) public TypeFinder(ILogger<TypeFinder> logger, IAssemblyFinder assemblyFinder)
{ {
_assemblyFinder = assemblyFinder; _assemblyFinder = assemblyFinder;
_logger = logger;
_types = new Lazy<IReadOnlyList<Type>>(FindAll, LazyThreadSafetyMode.ExecutionAndPublication); _types = new Lazy<IReadOnlyList<Type>>(FindAll, LazyThreadSafetyMode.ExecutionAndPublication);
} }
@ -37,9 +42,15 @@ public class TypeFinder : ITypeFinder
allTypes.AddRange(typesInThisAssembly.Where(type => type != null)); 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? //TODO: Trigger a global event?
_logger.LogException(e);
} }
} }

3
framework/test/Volo.Abp.Core.Tests/Volo/Abp/Reflection/TypeFinder_Tests.cs

@ -1,5 +1,6 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Reflection; using System.Reflection;
using Microsoft.Extensions.Logging.Abstractions;
using NSubstitute; using NSubstitute;
using Shouldly; using Shouldly;
using Xunit; using Xunit;
@ -21,7 +22,7 @@ public class TypeFinder_Tests
//Act //Act
var typeFinder = new TypeFinder(fakeAssemblyFinder); var typeFinder = new TypeFinder(NullLogger<TypeFinder>.Instance, fakeAssemblyFinder);
//Assert //Assert

Loading…
Cancel
Save