Skip to content

Commit 0fddec0

Browse files
authored
Added support of per-extension assembly isolation (#1431)
* Added support of per-extension assembly isolation * Added some comments
1 parent b3a49f0 commit 0fddec0

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System.Reflection;
2+
using System.Runtime.Loader;
3+
4+
namespace DevToys.Core.Mef;
5+
6+
// See https://learn.microsoft.com/en-us/dotnet/core/dependency-loading/understanding-assemblyloadcontext
7+
internal sealed class AssemblyIsolation : AssemblyLoadContext
8+
{
9+
private readonly AssemblyDependencyResolver _resolver;
10+
11+
internal AssemblyIsolation(string entryPointAssemblyPath)
12+
{
13+
_resolver = new AssemblyDependencyResolver(entryPointAssemblyPath);
14+
}
15+
16+
protected override Assembly? Load(AssemblyName assemblyName)
17+
{
18+
string? assemblyPath = _resolver.ResolveAssemblyToPath(assemblyName);
19+
if (assemblyPath != null)
20+
{
21+
return LoadFromAssemblyPath(assemblyPath);
22+
}
23+
24+
return null;
25+
}
26+
}

src/app/dev/DevToys.Core/Mef/RecursiveDirectoryCatalog.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ internal sealed partial class RecursiveDirectoryCatalog : ComposablePartCatalog,
1414
{
1515
private readonly ILogger _logger;
1616
private readonly string _path;
17+
private AssemblyIsolation? _assemblyIsolation;
1718
private AggregateCatalog? _aggregateCatalog;
1819

1920
/// <summary>
@@ -122,7 +123,13 @@ private bool AddLibraryToMefCatalog(string filePath)
122123
try
123124
{
124125
AssemblyCount++;
125-
var asmCat = new AssemblyCatalog(filePath);
126+
127+
// Create an isolation context for the plugin.
128+
// This allows to load dependencies version that the plugin asks, even if DevToys uses a different version of that same dependency.
129+
_assemblyIsolation ??= new AssemblyIsolation(filePath);
130+
131+
Assembly assembly = _assemblyIsolation.LoadFromAssemblyPath(filePath);
132+
var asmCat = new AssemblyCatalog(assembly);
126133

127134
// Force MEF to load the plugin and figure out if there are any exports
128135
// good assemblies will not throw the RTLE exception and can be added to the catalog

0 commit comments

Comments
 (0)