Skip to content

Commit 79066f1

Browse files
committed
ScriptRunner can be configured with a custom AssemblyLoadContext.
1 parent f1907e1 commit 79066f1

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<Description>A cross platform library allowing you to run C# (CSX) scripts with support for debugging and inline NuGet packages. Based on Roslyn.</Description>
55
<VersionPrefix>1.1.0</VersionPrefix>
66
<Authors>filipw</Authors>
7-
<TargetFrameworks>netstandard2.0</TargetFrameworks>
7+
<TargetFrameworks>netstandard2.0;netcoreapp2.1</TargetFrameworks>
88
<AssemblyName>Dotnet.Script.Core</AssemblyName>
99
<PackageId>Dotnet.Script.Core</PackageId>
1010
<PackageTags>script;csx;csharp;roslyn</PackageTags>
@@ -16,13 +16,15 @@
1616
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute>
1717
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute>
1818
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
19+
<LangVersion>9.0</LangVersion>
1920
<SignAssembly>true</SignAssembly>
2021
<AssemblyOriginatorKeyFile>../dotnet-script.snk</AssemblyOriginatorKeyFile>
2122
</PropertyGroup>
2223
<ItemGroup>
2324
<EmbeddedResource Include="**/*.template" />
2425
</ItemGroup>
2526
<ItemGroup>
27+
<PackageReference Include="Gapotchenko.FX" Version="2021.1.5" />
2628
<PackageReference Include="Gapotchenko.FX.Reflection.Loader" Version="2021.2.6-beta" />
2729
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Scripting" Version="3.9.0" />
2830
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />

src/Dotnet.Script.Core/ScriptRunner.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
using System.Collections.Immutable;
44
using System.Linq;
55
using System.Reflection;
6+
#if NETCOREAPP
7+
using System.Runtime.Loader;
8+
#endif
69
using System.Threading.Tasks;
710
using Dotnet.Script.DependencyModel.Environment;
811
using Dotnet.Script.DependencyModel.Logging;
@@ -29,9 +32,23 @@ public ScriptRunner(ScriptCompiler scriptCompiler, LogFactory logFactory, Script
2932
_scriptEnvironment = ScriptEnvironment.Default;
3033
}
3134

35+
#if NETCOREAPP
36+
#nullable enable
37+
/// <summary>
38+
/// Gets or sets a custom assembly load context to use for script execution.
39+
/// </summary>
40+
public AssemblyLoadContext? AssemblyLoadContext { get; init; }
41+
#nullable restore
42+
#endif
43+
3244
public async Task<TReturn> Execute<TReturn>(string dllPath, IEnumerable<string> commandLineArgs)
3345
{
34-
var assemblyLoaderPal = AssemblyLoadPal.ForCurrentAppDomain;
46+
#if NETCOREAPP
47+
var assemblyLoadContext = AssemblyLoadContext;
48+
var assemblyLoaderPal = assemblyLoadContext != null ? new AssemblyLoadPal(assemblyLoadContext) : AssemblyLoadPal.ForCurrentAppDomain;
49+
#else
50+
var assemblyLoaderPal = AssemblyLoadPal.ForCurrentAppDomain;
51+
#endif
3552

3653
var runtimeDeps = ScriptCompiler.RuntimeDependencyResolver.GetDependenciesForLibrary(dllPath);
3754
var runtimeDepsMap = ScriptCompiler.CreateScriptDependenciesMap(runtimeDeps);

0 commit comments

Comments
 (0)