From fe69ececb2ff04f81aa7bc1bf3f583a50ca1c008 Mon Sep 17 00:00:00 2001 From: Florian Rappl Date: Thu, 2 May 2024 08:24:31 +0200 Subject: [PATCH] Improved MFLC --- .../MicrofrontendLoadContext.cs | 33 +++++++++++++++---- src/Piral.Blazor.Sdk/Emulator.cs | 1 + 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/src/Piral.Blazor.Orchestrator/MicrofrontendLoadContext.cs b/src/Piral.Blazor.Orchestrator/MicrofrontendLoadContext.cs index 9a4167c..8bdf72e 100644 --- a/src/Piral.Blazor.Orchestrator/MicrofrontendLoadContext.cs +++ b/src/Piral.Blazor.Orchestrator/MicrofrontendLoadContext.cs @@ -5,14 +5,35 @@ namespace Piral.Blazor.Orchestrator; internal class MicrofrontendLoadContext(string name) : AssemblyLoadContext(name, true) { + private readonly AssemblyLoadContext _root = All.FirstOrDefault(m => m.Name == "root") ?? Default; + protected override Assembly? Load(AssemblyName assemblyName) { - //string assemblyPath = _resolver.ResolveAssemblyToPath(assemblyName); - //if (assemblyPath != null) - //{ - // return LoadFromAssemblyPath(assemblyPath); - //} - //return null; + var existing = GetExisting(_root.Assemblies, assemblyName); + + if (existing is not null) + { + return existing; + } + + // root and default are different; we must be in an emulator + if (_root != Default) + { + // Let's see if we find this in the default ALC + var globalAssembly = GetExisting(Default.Assemblies, assemblyName); + + if (globalAssembly is not null) + { + // In case we found it we return null to trigger the not found flow + return null; + } + } + return base.Load(assemblyName); } + + private static Assembly? GetExisting(IEnumerable assemblies, AssemblyName name) + { + return assemblies.FirstOrDefault(m => AssemblyName.ReferenceMatchesDefinition(m.GetName(), name)); + } } diff --git a/src/Piral.Blazor.Sdk/Emulator.cs b/src/Piral.Blazor.Sdk/Emulator.cs index 4e868b0..8130528 100644 --- a/src/Piral.Blazor.Sdk/Emulator.cs +++ b/src/Piral.Blazor.Sdk/Emulator.cs @@ -1,4 +1,5 @@ using System; +using System.Diagnostics; using System.IO; using System.Reflection; using System.Runtime.Loader;