enhancement to load dependencies from the /bin if they are not loaded automatically

This commit is contained in:
Shaun Walker 2020-06-22 16:58:41 -04:00
parent ff23865711
commit f81ef89c61

View File

@ -1,15 +1,12 @@
using System; using System;
using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Reflection; using System.Reflection;
using System.Runtime.Loader; using System.Runtime.Loader;
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting;
using Oqtane.Extensions;
using Oqtane.Infrastructure; using Oqtane.Infrastructure;
using Oqtane.Modules; using Oqtane.Modules;
using Oqtane.Services; using Oqtane.Services;
using Oqtane.Shared;
using Oqtane.UI; using Oqtane.UI;
// ReSharper disable once CheckNamespace // ReSharper disable once CheckNamespace
@ -64,22 +61,23 @@ namespace Microsoft.Extensions.DependencyInjection
if (runtime == Runtime.Server) if (runtime == Runtime.Server)
{ {
assembly.GetInstances<IClientStartup>() assembly.GetInstances<IClientStartup>()
.ToList() .ToList()
.ForEach(x => x.ConfigureServices(services)); .ForEach(x => x.ConfigureServices(services));
} }
} }
return services; return services;
} }
private static void LoadAssemblies() private static void LoadAssemblies()
{ {
var assemblyPath = Path.GetDirectoryName(Assembly.GetEntryAssembly()?.Location); var assemblyPath = Path.GetDirectoryName(Assembly.GetEntryAssembly()?.Location);
if (assemblyPath == null) return; 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 ) // 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())) foreach (var dll in assembliesFolder.EnumerateFiles($"*.dll", SearchOption.TopDirectoryOnly).Where(f => f.IsOqtaneAssembly()))
@ -95,7 +93,6 @@ namespace Microsoft.Extensions.DependencyInjection
continue; continue;
} }
var assemblies = AppDomain.CurrentDomain.GetAssemblies();
if (!assemblies.Any(a => AssemblyName.ReferenceMatchesDefinition(assemblyName, a.GetName()))) if (!assemblies.Any(a => AssemblyName.ReferenceMatchesDefinition(assemblyName, a.GetName())))
{ {
try 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;
}
}
} }
} }