Skip to content

Commit f1907e1

Browse files
committed
AssemblyLoadPal is used instead of AppDomain in order to cover both AppDomain and AssemblyLoadContext scenarios.
1 parent 2c21516 commit f1907e1

File tree

3 files changed

+13
-9
lines changed

3 files changed

+13
-9
lines changed

src/Dotnet.Script.Core/Dotnet.Script.Core.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
<EmbeddedResource Include="**/*.template" />
2424
</ItemGroup>
2525
<ItemGroup>
26+
<PackageReference Include="Gapotchenko.FX.Reflection.Loader" Version="2021.2.6-beta" />
2627
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Scripting" Version="3.9.0" />
2728
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
2829
<PackageReference Include="ReadLine" Version="2.0.1" />

src/Dotnet.Script.Core/ScriptRunner.cs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using Dotnet.Script.DependencyModel.Environment;
88
using Dotnet.Script.DependencyModel.Logging;
99
using Dotnet.Script.DependencyModel.Runtime;
10+
using Gapotchenko.FX.Reflection;
1011
using Microsoft.CodeAnalysis.CSharp.Scripting.Hosting;
1112
using Microsoft.CodeAnalysis.Scripting;
1213
using Microsoft.CodeAnalysis.Scripting.Hosting;
@@ -30,13 +31,15 @@ public ScriptRunner(ScriptCompiler scriptCompiler, LogFactory logFactory, Script
3031

3132
public async Task<TReturn> Execute<TReturn>(string dllPath, IEnumerable<string> commandLineArgs)
3233
{
34+
var assemblyLoaderPal = AssemblyLoadPal.ForCurrentAppDomain;
35+
3336
var runtimeDeps = ScriptCompiler.RuntimeDependencyResolver.GetDependenciesForLibrary(dllPath);
3437
var runtimeDepsMap = ScriptCompiler.CreateScriptDependenciesMap(runtimeDeps);
35-
var assembly = Assembly.LoadFrom(dllPath); // this needs to be called prior to 'AppDomain.CurrentDomain.AssemblyResolve' event handler added
38+
var assembly = assemblyLoaderPal.LoadFrom(dllPath); // this needs to be called prior to 'AppDomain.CurrentDomain.AssemblyResolve' event handler added
3639

37-
Assembly OnResolve(object sender, ResolveEventArgs args) => ResolveAssembly(args, runtimeDepsMap);
40+
Assembly OnResolve(AssemblyLoadPal sender, AssemblyLoadPal.ResolvingEventArgs args) => ResolveAssembly(sender, args, runtimeDepsMap);
3841

39-
AppDomain.CurrentDomain.AssemblyResolve += OnResolve;
42+
assemblyLoaderPal.Resolving += OnResolve;
4043
try
4144
{
4245
var type = assembly.GetType("Submission#0");
@@ -64,7 +67,7 @@ public async Task<TReturn> Execute<TReturn>(string dllPath, IEnumerable<string>
6467
}
6568
finally
6669
{
67-
AppDomain.CurrentDomain.AssemblyResolve -= OnResolve;
70+
assemblyLoaderPal.Resolving -= OnResolve;
6871
}
6972
}
7073

@@ -97,12 +100,11 @@ public virtual async Task<TReturn> Execute<TReturn, THost>(ScriptCompilationCont
97100
return ProcessScriptState(scriptResult);
98101
}
99102

100-
internal Assembly ResolveAssembly(ResolveEventArgs args, Dictionary<string, RuntimeAssembly> runtimeDepsMap)
103+
internal Assembly ResolveAssembly(AssemblyLoadPal sender, AssemblyLoadPal.ResolvingEventArgs args, Dictionary<string, RuntimeAssembly> runtimeDepsMap)
101104
{
102-
var assemblyName = new AssemblyName(args.Name);
103-
var result = runtimeDepsMap.TryGetValue(assemblyName.Name, out RuntimeAssembly runtimeAssembly);
105+
var result = runtimeDepsMap.TryGetValue(args.Name.Name, out RuntimeAssembly runtimeAssembly);
104106
if (!result) return null;
105-
var loadedAssembly = Assembly.LoadFrom(runtimeAssembly.Path);
107+
var loadedAssembly = sender.LoadFrom(runtimeAssembly.Path);
106108
return loadedAssembly;
107109
}
108110

src/Dotnet.Script.Tests/ScriptRunnerTests.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using Dotnet.Script.Core;
44
using Dotnet.Script.DependencyModel.Runtime;
55
using Dotnet.Script.Shared.Tests;
6+
using Gapotchenko.FX.Reflection;
67
using Moq;
78
using Xunit;
89

@@ -15,7 +16,7 @@ public void ResolveAssembly_ReturnsNull_WhenRuntimeDepsMapDoesNotContainAssembly
1516
{
1617
var scriptRunner = CreateScriptRunner();
1718

18-
var result = scriptRunner.ResolveAssembly(new ResolveEventArgs("AnyAssemblyName"), new Dictionary<string, RuntimeAssembly>());
19+
var result = scriptRunner.ResolveAssembly(AssemblyLoadPal.ForCurrentAppDomain, new AssemblyLoadPal.ResolvingEventArgs(null, "AnyAssemblyName"), new Dictionary<string, RuntimeAssembly>());
1920

2021
Assert.Null(result);
2122
}

0 commit comments

Comments
 (0)