Could not load file or assembly mscorlib

1

I'm new in NetCore and I try to load a dll dynamically with this code :

public void LoadDll()
{
    try
    {
        var dllPath = @"C:\Temp\dynamic.dll";

        var asl = new AssemblyLoader();
        var asm = asl.LoadFromAssemblyPath(dllPath);

        var type = asm.GetType("MyNamespace.MyClass");
        dynamicInstance = Activator.CreateInstance(type);
    }
    catch (Exception ex)
    {

    }
}

public class AssemblyLoader : AssemblyLoadContext
{
    // Not exactly sure about this
    protected override Assembly Load(AssemblyName assemblyName)
    {
        var deps = DependencyContext.Default;
        var res = deps.CompileLibraries.Where(d => d.Name.Contains(assemblyName.Name)).ToList();
        var assembly = Assembly.Load(new AssemblyName(res.First().Name));
        return assembly;
    }
}

When I call asm.GetType method a exception is thrown :

"Could not load file or assembly 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. An operation is not legal in the current state. (Exception from HRESULT: 0x80131509)" Source : System.Private.CoreLib

Here is my project.json :

{
    "dependencies": {
    "Microsoft.NETCore.App": {
      "version": "1.0.1",
      "type": "platform"
    },
    "Microsoft.ApplicationInsights.AspNetCore": "1.0.0",
    "Microsoft.AspNetCore.Mvc": "1.0.1",
    "Microsoft.AspNetCore.Routing": "1.0.1",
    "Microsoft.AspNetCore.Server.IISIntegration": "1.0.0",
    "Microsoft.AspNetCore.Server.Kestrel": "1.0.1",
    "Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0",
    "Microsoft.Extensions.Configuration.FileExtensions": "1.0.0",
    "Microsoft.Extensions.Configuration.Json": "1.0.0",
    "Microsoft.Extensions.Logging": "1.0.0",
    "Microsoft.Extensions.Logging.Console": "1.0.0",
    "Microsoft.Extensions.Logging.Debug": "1.0.0",
    "Microsoft.Extensions.Options.ConfigurationExtensions": "1.0.0" 
    },

    "tools": {
      "Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.0.0-preview2-final"
    },

    "frameworks": {
    "netcoreapp1.0": {
      "imports": [
        "dotnet5.6",
        "portable-net45+win8"
        ]
      }
    },

    "buildOptions": {
    "emitEntryPoint": true,
    "preserveCompilationContext": true
    },

    "runtimeOptions": {
    "configProperties": {
      "System.GC.Server": true
      }
    },

    "publishOptions": {
    "include": [
      "wwwroot",
      "**/*.cshtml",
      "appsettings.json",
      "web.config"
      ]
    },

    "scripts": {
    "postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ]
    }
}

Can someone help me ?

dll
.net-core
mscorlib
asked on Stack Overflow Dec 2, 2016 by Edenia • edited Dec 2, 2016 by Bassie

1 Answer

1

SThis is a very common problem. As of .NET Core 1.1 you cannot load libraries built for the .NET Framework (they are mscorlib based) but only ones which are build using PCL.

Check the assembly you try to load.

.NET Core 2.0 will address this topic.

answered on Stack Overflow Dec 3, 2016 by Thomas • edited May 30, 2017 by Thomas

User contributions licensed under CC BY-SA 3.0