0

I am using the following code to load System.Net.Http.dll from the .NET Core runtime directory (systemNetHttpPath) and save it to another directory (newSystemNetHttpPath). I do not make any changes to the assembly. I pick the System.Net.Http.dll version based on the target framework of the application that will use it.

var module = ModuleDefMD.Load(File.ReadAllBytes(systemNetHttpPath));

if (module.IsILOnly)
{
    module.Write(newSystemNetHttpPath);
}
else
{
    module.NativeWrite(newSystemNetHttpPath);
}

I am getting the following exception on .NET Core 3.1 when I call module.NativeWrite: dnlib.DotNet.Writer.ModuleWriterException: 'Could not create header'.

On .NET 6.0 I am getting the following exception: dnlib.DotNet.Writer.ModuleWriterException: 'Invalid section RVA'.

I found a similar issue and according to the conversation I tried to do the following:

if (!module.IsILOnly)
{
   module.Cor20HeaderFlags &= ~ComImageFlags.ILLibrary;
}

 module.Write(newSystemNetHttpPath);

In this case, the module is saved successfully but I am getting a runtime exception when I try to run the application that uses saved System.Net.Http.dll: 'System.BadImageFormatException': 'Could not load file or assembly 'System.Net.Http, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. An attempt was made to load a program with an incorrect format.'

Note: I am experiencing this issue only on Linux (tested on Ubuntu 22.04) On Windows both cases work fine.

Could you please suggest how to save mixed-mode assembly (System.Net.Http.dll) using dnlib on Linux?

Andrii
  • 689
  • 1
  • 6
  • 12

0 Answers0