From f81ef89c6195981c2ff421fb3c10c94a9d289b2d Mon Sep 17 00:00:00 2001 From: Shaun Walker Date: Mon, 22 Jun 2020 16:58:41 -0400 Subject: [PATCH] enhancement to load dependencies from the /bin if they are not loaded automatically --- .../OqtaneServiceCollectionExtensions.cs | 33 ++++++++++++------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/Oqtane.Server/Extensions/OqtaneServiceCollectionExtensions.cs b/Oqtane.Server/Extensions/OqtaneServiceCollectionExtensions.cs index 3e778869..f49d189a 100644 --- a/Oqtane.Server/Extensions/OqtaneServiceCollectionExtensions.cs +++ b/Oqtane.Server/Extensions/OqtaneServiceCollectionExtensions.cs @@ -1,15 +1,12 @@ using System; -using System.Collections.Generic; using System.IO; using System.Linq; using System.Reflection; using System.Runtime.Loader; using Microsoft.Extensions.Hosting; -using Oqtane.Extensions; using Oqtane.Infrastructure; using Oqtane.Modules; using Oqtane.Services; -using Oqtane.Shared; using Oqtane.UI; // ReSharper disable once CheckNamespace @@ -55,31 +52,32 @@ namespace Microsoft.Extensions.DependencyInjection services.AddSingleton(hostedServiceType, serviceType); } } - + var startUps = assembly.GetInstances(); foreach (var startup in startUps) { startup.ConfigureServices(services); } - + if (runtime == Runtime.Server) { - assembly.GetInstances() - .ToList() - .ForEach(x => x.ConfigureServices(services)); + assembly.GetInstances() + .ToList() + .ForEach(x => x.ConfigureServices(services)); } } return services; } - private static void LoadAssemblies() { var assemblyPath = Path.GetDirectoryName(Assembly.GetEntryAssembly()?.Location); if (assemblyPath == null) return; - var assembliesFolder = new DirectoryInfo(assemblyPath); + AssemblyLoadContext.Default.Resolving += ResolveDependencies; + var assembliesFolder = new DirectoryInfo(assemblyPath); + var assemblies = AppDomain.CurrentDomain.GetAssemblies(); // iterate through Oqtane assemblies in /bin ( filter is narrow to optimize loading process ) foreach (var dll in assembliesFolder.EnumerateFiles($"*.dll", SearchOption.TopDirectoryOnly).Where(f => f.IsOqtaneAssembly())) @@ -95,7 +93,6 @@ namespace Microsoft.Extensions.DependencyInjection continue; } - var assemblies = AppDomain.CurrentDomain.GetAssemblies(); if (!assemblies.Any(a => AssemblyName.ReferenceMatchesDefinition(assemblyName, a.GetName()))) { try @@ -121,5 +118,19 @@ namespace Microsoft.Extensions.DependencyInjection } } } + + private static Assembly ResolveDependencies(AssemblyLoadContext context, AssemblyName name) + { + var assemblyPath = Path.GetDirectoryName(Assembly.GetEntryAssembly()?.Location) + "\\" + name.Name + ".dll"; + if (File.Exists(assemblyPath)) + { + return context.LoadFromStream(new MemoryStream(File.ReadAllBytes(assemblyPath))); + } + else + { + return null; + } + } + } }