From 39a1dbf1d1b70710d16d0c290cc387821a2bd9a4 Mon Sep 17 00:00:00 2001 From: Qstick Date: Sat, 15 Aug 2020 00:13:42 -0400 Subject: [PATCH] Used ReflectionOnly and/or public types where possible to avoid loading related assemblies unnecessarily Fixes #4763 Co-Authored-By: taloth --- src/NzbDrone.Common/Composition/ContainerBuilderBase.cs | 4 ++-- src/NzbDrone.Common/Reflection/ReflectionExtensions.cs | 6 +++--- src/NzbDrone.Core/Datastore/DbFactory.cs | 1 + src/NzbDrone.Host/CancelHandler.cs | 4 ++-- 4 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/NzbDrone.Common/Composition/ContainerBuilderBase.cs b/src/NzbDrone.Common/Composition/ContainerBuilderBase.cs index 09f7ec8c3..03a2b58a2 100644 --- a/src/NzbDrone.Common/Composition/ContainerBuilderBase.cs +++ b/src/NzbDrone.Common/Composition/ContainerBuilderBase.cs @@ -30,14 +30,14 @@ protected ContainerBuilderBase(IStartupContext args, List assemblies) #if !NETCOREAPP foreach (var assembly in assemblies) { - _loadedTypes.AddRange(Assembly.Load(assembly).GetTypes()); + _loadedTypes.AddRange(Assembly.Load(assembly).GetExportedTypes()); } #else var startupPath = AppDomain.CurrentDomain.BaseDirectory; foreach (var assemblyName in assemblies) { - _loadedTypes.AddRange(AssemblyLoadContext.Default.LoadFromAssemblyPath(Path.Combine(startupPath, $"{assemblyName}.dll")).GetTypes()); + _loadedTypes.AddRange(AssemblyLoadContext.Default.LoadFromAssemblyPath(Path.Combine(startupPath, $"{assemblyName}.dll")).GetExportedTypes()); } var toRegisterResolver = new List { "System.Data.SQLite" }; diff --git a/src/NzbDrone.Common/Reflection/ReflectionExtensions.cs b/src/NzbDrone.Common/Reflection/ReflectionExtensions.cs index 6280e1c56..12749df9e 100644 --- a/src/NzbDrone.Common/Reflection/ReflectionExtensions.cs +++ b/src/NzbDrone.Common/Reflection/ReflectionExtensions.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.Linq; using System.Reflection; @@ -17,7 +17,7 @@ public static List GetSimpleProperties(this Type type) public static List ImplementationsOf(this Assembly assembly) { - return assembly.GetTypes().Where(c => typeof(T).IsAssignableFrom(c)).ToList(); + return assembly.GetExportedTypes().Where(c => typeof(T).IsAssignableFrom(c)).ToList(); } public static bool IsSimpleType(this Type type) @@ -68,7 +68,7 @@ public static T[] GetAttributes(this MemberInfo member) public static Type FindTypeByName(this Assembly assembly, string name) { - return assembly.GetTypes().SingleOrDefault(c => c.Name.Equals(name, StringComparison.InvariantCultureIgnoreCase)); + return assembly.GetExportedTypes().SingleOrDefault(c => c.Name.Equals(name, StringComparison.InvariantCultureIgnoreCase)); } public static bool HasAttribute(this Type type) diff --git a/src/NzbDrone.Core/Datastore/DbFactory.cs b/src/NzbDrone.Core/Datastore/DbFactory.cs index cd8de2622..9f1d2da53 100644 --- a/src/NzbDrone.Core/Datastore/DbFactory.cs +++ b/src/NzbDrone.Core/Datastore/DbFactory.cs @@ -37,6 +37,7 @@ private static void InitializeEnvironment() Environment.SetEnvironmentVariable("No_Expand", "true"); Environment.SetEnvironmentVariable("No_SQLiteXmlConfigFile", "true"); Environment.SetEnvironmentVariable("No_PreLoadSQLite", "true"); + Environment.SetEnvironmentVariable("No_SQLiteFunctions", "true"); } public static void RegisterDatabase(IContainer container) diff --git a/src/NzbDrone.Host/CancelHandler.cs b/src/NzbDrone.Host/CancelHandler.cs index ad7a3f328..fbe142c81 100644 --- a/src/NzbDrone.Host/CancelHandler.cs +++ b/src/NzbDrone.Host/CancelHandler.cs @@ -1,4 +1,4 @@ -using System; +using System; using NLog; using NzbDrone.Core.Lifecycle; @@ -9,7 +9,7 @@ public interface ICancelHandler void Attach(); } - internal class CancelHandler : ICancelHandler + public class CancelHandler : ICancelHandler { private readonly ILifecycleService _lifecycleService; private object _syncRoot;