Load satellite assemblies on startup
This commit is contained in:
		@ -5,6 +5,7 @@ using System.Reflection;
 | 
			
		||||
using System.Runtime.Loader;
 | 
			
		||||
using Microsoft.Extensions.Hosting;
 | 
			
		||||
using Oqtane.Infrastructure;
 | 
			
		||||
using Oqtane.Infrastructure.Localization;
 | 
			
		||||
using Oqtane.Modules;
 | 
			
		||||
using Oqtane.Services;
 | 
			
		||||
using Oqtane.UI;
 | 
			
		||||
@ -14,10 +15,14 @@ namespace Microsoft.Extensions.DependencyInjection
 | 
			
		||||
{
 | 
			
		||||
    public static class OqtaneServiceCollectionExtensions
 | 
			
		||||
    {
 | 
			
		||||
        private static readonly string StalliteAssemblyExtension = ".resources.dll";
 | 
			
		||||
 | 
			
		||||
        public static IServiceCollection AddOqtaneParts(this IServiceCollection services, Runtime runtime)
 | 
			
		||||
        {
 | 
			
		||||
            LoadAssemblies();
 | 
			
		||||
            LoadSatelliteAssemblies();
 | 
			
		||||
            services.AddOqtaneServices(runtime);
 | 
			
		||||
 | 
			
		||||
            return services;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
@ -119,6 +124,46 @@ namespace Microsoft.Extensions.DependencyInjection
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private static void LoadSatelliteAssemblies()
 | 
			
		||||
        {
 | 
			
		||||
            var assemblies = AppDomain.CurrentDomain.GetAssemblies();
 | 
			
		||||
            var assemblyPath = Path.GetDirectoryName(Assembly.GetEntryAssembly()?.Location);
 | 
			
		||||
            if (assemblyPath == null)
 | 
			
		||||
            {
 | 
			
		||||
                return;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            AssemblyLoadContext.Default.Resolving += ResolveDependencies;
 | 
			
		||||
 | 
			
		||||
            foreach (var culture in LocalizationSettings.SupportedCultures)
 | 
			
		||||
            {
 | 
			
		||||
                var assembliesFolder = new DirectoryInfo(Path.Combine(assemblyPath, culture));
 | 
			
		||||
                foreach (var assemblyFile in assembliesFolder.EnumerateFiles(StalliteAssemblyExtension))
 | 
			
		||||
                {
 | 
			
		||||
                    AssemblyName assemblyName;
 | 
			
		||||
                    try
 | 
			
		||||
                    {
 | 
			
		||||
                        assemblyName = AssemblyName.GetAssemblyName(assemblyFile.FullName);
 | 
			
		||||
                    }
 | 
			
		||||
                    catch
 | 
			
		||||
                    {
 | 
			
		||||
                        Console.WriteLine($"Not Satellite Assembly : {assemblyFile.Name}");
 | 
			
		||||
                        continue;
 | 
			
		||||
                    }
 | 
			
		||||
 | 
			
		||||
                    try
 | 
			
		||||
                    {
 | 
			
		||||
                        Assembly assembly = AssemblyLoadContext.Default.LoadFromStream(new MemoryStream(File.ReadAllBytes(assemblyFile.FullName)));
 | 
			
		||||
                        Console.WriteLine($"Loaded : {assemblyName}");
 | 
			
		||||
                    }
 | 
			
		||||
                    catch (Exception e)
 | 
			
		||||
                    {
 | 
			
		||||
                        Console.WriteLine($"Failed : {assemblyName}\n{e}");
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private static Assembly ResolveDependencies(AssemblyLoadContext context, AssemblyName name)
 | 
			
		||||
        {
 | 
			
		||||
            var assemblyPath = Path.GetDirectoryName(Assembly.GetEntryAssembly()?.Location) + "\\" + name.Name + ".dll";
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user