il-repack icon indicating copy to clipboard operation
il-repack copied to clipboard

Can't pack .NET core assemblies / executables

Open ghost opened this issue 8 years ago • 5 comments

System.InvalidOperationException: ILRepack does not support merging non-.NET libraries (e.g.: native libraries) ---> Sys
tem.BadImageFormatException: Format of the executable (.exe) or library (.dll) is invalid.
   at Mono.Cecil.PE.ImageReader.ReadOptionalHeaders(UInt16& subsystem, UInt16& dll_characteristics)
   at Mono.Cecil.PE.ImageReader.ReadImage()
   at Mono.Cecil.PE.ImageReader.ReadImageFrom(Stream stream)
   at Mono.Cecil.ModuleDefinition.ReadModule(Stream stream, ReaderParameters parameters)
   at Mono.Cecil.ModuleDefinition.ReadModule(String fileName, ReaderParameters parameters)
   at Mono.Cecil.AssemblyDefinition.ReadAssembly(String fileName, ReaderParameters parameters)
   at ILRepacking.ILRepack.ReadInputAssembly(String assembly, Boolean isPrimary)
   --- End of inner exception stack trace ---
   at ILRepacking.ILRepack.ReadInputAssembly(String assembly, Boolean isPrimary)
   at ILRepacking.ILRepack.ReadInputAssemblies()
   at ILRepacking.ILRepack.Repack()
   at ILRepacking.Application.Main(String[] args)

ghost avatar Sep 30 '17 05:09 ghost

I don't think Microsoft changed the PE header with .Net core, so this error sounds strange. Which assemblies are you trying to merge ?

gluck avatar Oct 02 '17 09:10 gluck

Created a new .NET Core 2.0 console app.

Added win10-x64 as a RuntimeTarget.

dotnet publish -c Release -r win10-x64 --self-contained

PS C:\Users\Adam\Documents\Visual Studio 2017\Projects\ConsoleApp1\ConsoleApp1\bin\Release\netcoreapp2.0\win10-x64> ilre
pack /out:test.exe .\ConsoleApp1.dll .\ConsoleApp1.exe
INFO: IL Repack - Version 2.0.13
INFO: ------------- IL Repack Arguments -------------
/out:test.exe  .\ConsoleApp1.dll .\ConsoleApp1.exe
-----------------------------------------------
INFO: Adding assembly for merge: .\ConsoleApp1.dll
INFO: Failed to load debug information for .\ConsoleApp1.dll
INFO: Adding assembly for merge: .\ConsoleApp1.exe
ERROR: Failed to load assembly .\ConsoleApp1.exe
System.InvalidOperationException: ILRepack does not support merging non-.NET libraries (e.g.: native libraries) ---> Sys
tem.BadImageFormatException: Format of the executable (.exe) or library (.dll) is invalid.
   at Mono.Cecil.PE.ImageReader.ReadOptionalHeaders(UInt16& subsystem, UInt16& dll_characteristics)
   at Mono.Cecil.PE.ImageReader.ReadImage()
   at Mono.Cecil.PE.ImageReader.ReadImageFrom(Stream stream)
   at Mono.Cecil.ModuleDefinition.ReadModule(Stream stream, ReaderParameters parameters)
   at Mono.Cecil.ModuleDefinition.ReadModule(String fileName, ReaderParameters parameters)
   at Mono.Cecil.AssemblyDefinition.ReadAssembly(String fileName, ReaderParameters parameters)
   at ILRepacking.ILRepack.ReadInputAssembly(String assembly, Boolean isPrimary)
   --- End of inner exception stack trace ---
   at ILRepacking.ILRepack.ReadInputAssembly(String assembly, Boolean isPrimary)
   at ILRepacking.ILRepack.ReadInputAssemblies()
   at ILRepacking.ILRepack.Repack()
   at ILRepacking.Application.Main(String[] args)
PS C:\Users\Adam\Documents\Visual Studio 2017\Projects\ConsoleApp1\ConsoleApp1\bin\Release\netcoreapp2.0\win10-x64>

ConsoleApp1.zip

ghost avatar Oct 04 '17 05:10 ghost

Did you try w/o --self-contained ? I don't know what kind of magic dotnet does to make a Self Contained app, but probably it involves creating a native (non-managed) executable, you probably won't be able to merge/repack something like this.

gluck avatar Oct 04 '17 06:10 gluck

Without --self-contained, only a DLL is created. I'll have a play around though.

ghost avatar Oct 04 '17 07:10 ghost

The --self-contained flag includes the needed bits of the .NET Core framework so that the application can run on a machine that doesn't otherwise have .NET Core installed. The application still exists in the DLL, but a native EXE is created to bootstrap the DLL.

You can possibly merge the DLLs with your application's DLL, and ship that alongside the EXE, but I don't know if the EXE references the other DLLs directly.

Update: Doing some testing, most of the framework DLL files seem to be native DLLs, so there may not be much savings to be had by merging only the application and NuGet package DLLs.

I think your options are to leave it as is, or require that the host machine has .NET Core installed already, or package everything up into a Docker image and require that the host machine has Docker.

I still would like to see the ability to merge everything into one EXE file, though.

mcattle avatar Oct 25 '17 00:10 mcattle