0

I have a C# console app written under net 6.0. And I ran it complied exe file (I complied it in Windows and upload whole published folder to Linux VM) using mono, and get following message:

file does not contain a valid CIL image.

I checked the other thread and re-install dotnet package. But it still does not work out.

Please provide some help. Thanks.

  • 2
    Why not compile directly for linux? See [this for an example to target Ubuntu](https://stackoverflow.com/a/41533671/6621862) – Luke Oct 03 '22 at 19:48
  • How did you compile it? You can't run Windows exe on Linux. – DavidG Oct 03 '22 at 19:56
  • .NET Framework-style exe's can be run in Linux with Mono, because they contain the .NET program code. However, starting with .NET 5 -- or even perhaps already starting with .NET Core -- the exe file typically (albeit depending on the project's publishing settings) does not contain the .NET program code anymore. Rather, projects are built into an exe with _native_ code only plus a separate DLL with the actual .NET code. So, instead of trying to run the exe, try to run the DLL that's accompanying the exe... –  Oct 03 '22 at 20:09
  • Did you download the runtime version of core for linux : https://dotnet.microsoft.com/en-us/download/dotnet/6.0 – jdweng Oct 03 '22 at 23:35

2 Answers2

1

I finally found the answer:

  1. Mono is not compatible with .net 6 so far.
  2. The correct command line to run the code is:

dotnet program.dll

(exe file is not working here)

Thanks for you guys' help.

0

.NET 6 is not compatible with Mono, so if you want your .NET 6 console app to run on Linux, you should need to strictly follow the .NET Core/.NET 6 guides,

Running a self-contained ASP.NET Core application on Ubuntu

The "complied exe file" generated for a .NET 6 console app by dotnet publish, is strictly a native Windows executable (equivalent of .NET CLI executable) that contains no MSIL. That's why when you force Mono runtime to load it "file does not contain a valid CIL image" is reported. Don't check any other threads, as Mono is going away and not what you should resort to at this moment, https://halfblood.pro/the-end-of-mono/

Lex Li
  • 60,503
  • 9
  • 116
  • 147