Bad IL Format when loading exe in .NET Core 3.0

Viewed 4412

I have two .NET Core 3.0 WPF projects. One of them, I published into an executable called "DefaultPlugin.exe".

In my second WPF project, I'm trying to load the first project using Assembly.Load and read the types. However, when I do so, I get an exception, "Bad IL Format" from System.Private.CoreLib. It works when I load the DefaultPlugin.dll from the bin folder I also checked my CPU configuration to make sure they match.

///Works:
string path = @"C:\Users\S-D81\source\repos\DefaultPlugin\DefaultPlugin\bin\Debug\netcoreapp3.0\win-x64\DefaultPlugin.dll";
///Does not work:
//string path = @"C:\Users\S-D81\source\repos\DefaultPlugin\DefaultPlugin\bin\Debug\netcoreapp3.0\win-x64\DefaultPlugin.exe";
//string path = @"C:\Users\S-D81\source\repos\DefaultPlugin\DefaultPlugin\bin\Release\netcoreapp3.0\win-x64\publish\DefaultPlugin.exe";

Assembly assembly = Assembly.LoadFrom(path);

Is there a way to use Assembly.Load on a published .NET Core 3.0 assembly or is this just a limitation of the new framework?

1 Answers

The generated .exe file in .NET Core 3.0 applications is not an IL assembly but a native host executable that finds and boots the runtime before loading the actual IL application assembly which has a .dll extension.

You should be able to load your plugin by loading the .dll file next to the .exe file with the same project name.

Related