I have a project in .net Core and I need to load an Assembly (compiled with Roslyn) to a sandbox, in order to isolate the code execution.
My first tought was use an AppDomain, but it's not possible in .net Core. So, the solution is to use AssemblyLoadContext.
The following code is my Assembly Loader:
public class AssemblyContext : AssemblyLoadContext
{
public Assembly Load(Stream stream)
{
this.Resolving += ResolvingHandler;
return this.LoadFromStream(stream);
}
public Assembly ResolvingHandler(AssemblyLoadContext context, AssemblyName assemblyName)
{
var assembly = context.LoadFromAssemblyName(assemblyName);
Console.WriteLine("Resolving: " + assemblyName.FullName);
return assembly;
}
}
My problem here is after the load of the Assembly, the Resolving method is not called and the dependencies are not loaded, making my compiled code not working.
Is necessary to do any additional steps to the ResolvingHandler be called? Or this isn't possible to do in Core?
As from documentation it is not called by design:
AssemblyLoadContext.Resolving Event
Occurs when the resolution of an assembly fails when attempting to load into this assembly load context.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With