Skip to content

Commit

Permalink
Fixes assembly loading for multiple execution contexts
Browse files Browse the repository at this point in the history
  • Loading branch information
jameschch committed Jan 27, 2022
1 parent 447be1b commit 2478ec3
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
10 changes: 7 additions & 3 deletions AlgorithmFactory/Loader.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/*
* QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
* Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
*
Expand Down Expand Up @@ -237,13 +237,17 @@ private bool TryCreateILAlgorithm(string assemblyPath, out IAlgorithm algorithmI
if (debugInformationBytes == null)
{
Log.Trace("Loader.TryCreateILAlgorithm(): Loading only the algorithm assembly");
assembly = Assembly.LoadFrom(assemblyPath);
//this is needed to isolate multiple execution contexts
assembly = System.Runtime.Loader.AssemblyLoadContext.CurrentContextualReflectionContext
.LoadFromAssemblyPath(assemblyPath);
}
else
{
Log.Trace("Loader.TryCreateILAlgorithm(): Loading debug information with algorithm");
var assemblyBytes = File.ReadAllBytes(assemblyPath);
assembly = Assembly.Load(assemblyBytes, debugInformationBytes);
//this is needed to isolate multiple execution contexts
assembly = System.Runtime.Loader.AssemblyLoadContext.CurrentContextualReflectionContext
.LoadFromStream(new MemoryStream(assemblyBytes), new MemoryStream(debugInformationBytes));
}

//Get the list of extention classes in the library:
Expand Down
4 changes: 3 additions & 1 deletion Common/Util/Composer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,9 @@ public Composer()
try
{
foreach (var type in
Assembly.LoadFrom(file).ExportedTypes.Where(type => !type.IsAbstract && !type.IsInterface && !type.IsEnum))
//this is needed to isolate multiple execution contexts
System.Runtime.Loader.AssemblyLoadContext.CurrentContextualReflectionContext
.LoadFromAssemblyPath(file).ExportedTypes.Where(type => !type.IsAbstract && !type.IsInterface && !type.IsEnum))
{
exportedTypes.Add(type);
}
Expand Down

0 comments on commit 2478ec3

Please sign in to comment.