Skip to content

ExecutionEngineException in System.Private.CoreLib.ni.dll after System.Runtime.Loader.AssemblyLoadContext.Default.Resolving called #6279

@ewinnington

Description

@ewinnington

I'm trying to load a DLL from a byte stream dynamically in .net core rtm.
Tested on Windows 10, VS 2015 Community, version 1.0.0-preview2-003121

I have a DLL containing this single object.

    //compiled into core-minimal-dynamic.dll         
            namespace eric.coreminimal.dynamic
            {
                public class DemoDynamicClass
                {
                    public int x { get; set; }

                    public DemoDynamicClass()
                    {
                        x = 5; 
                    }
                }
            }

If I load it with from outside the folder where it's executing, this works.

            FileStream fs = File.OpenRead(@"..\..\..\projects\core-minimal\dynamic\core-minimal-dynamic.dll");

            Assembly asm = System.Runtime.Loader.AssemblyLoadContext.Default.LoadFromStream(fs);

            var c = Type.GetType("eric.coreminimal.dynamic.DemoDynamicClass,core-minimal-dynamic");

            dynamic d = Activator.CreateInstance(c);

            Console.WriteLine(d.x); 

If I have it in the folder where the code is executing, I don't even need to load it with the Filestream and AssembyLoadContext, because the .net core probes the path and finds the DLL automatically.

But, If I try to load it in response to the System.Runtime.Loader.AssemblyLoadContext.Default.Resolving += Assembly_Resolve; event, then I trigger the

    System.ExecutionEngineException was unhandled

    HResult=-2146233082

    Message=Exception of type 'System.ExecutionEngineException' was thrown.

    InnerException: null 

The Assembly_Resolve routine is:

private static System.Reflection.Assembly Assembly_Resolve(System.Runtime.Loader.AssemblyLoadContext loadContext, System.Reflection.AssemblyName TargetAssembly)
        {
            if (TargetAssembly.Name == "core-minimal-dynamic")
            {
                FileStream fs = File.OpenRead(@"..\..\..\projects\core-minimal\dynamic\core-minimal-dynamic.dll");
                Assembly asm = System.Runtime.Loader.AssemblyLoadContext.Default.LoadFromStream(fs);
                return asm; 
            }
    return null;
}

What am I doing wrong in the Assembly_Resolve routine? Why is the System.ExecutionEngineException firing?

I have attached the minimal code to trigger the issue and the code used to generate the DLL.

TestIssue.zip
core-minimal-dynamic.zip

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions