Search code examples
asp.netlinux.net-corepublish

.net core 2 application runs successfully but the published app does not run


I'm trying to publish my asp.net core 2 application to debian linux and I'm facing trouble.

While trying to understand the problem, I create an empty console application in bash (windows subsystem for linux, Ubuntu), I run it successfully, I publish it and run the published file, it fails.

/mnt/c/Work/Temp/lin2# dotnet new Console
/mnt/c/Work/Temp/lin2# dotnet run
Hello World!
/mnt/c/Work/Temp/lin2# dotnet publish -c Release --self-contained --runtime linux-x64 -o /mnt/c/Temp/lin2
/mnt/c/Work/Temp/lin2# /mnt/c/Temp/lin2/lin2.dll

Unhandled Exception: System.BadImageFormatException: Could not load file or assembly 'System.Runtime, Version=4.2.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The module was expected to contain an assembly manifest.

The <OutputType>Exe</OutputType> is set (by default) but the compiled assembly has dll file extension. I tried to google how to create exe file, but most responses are related to old project.json format.


Solution

  • For self-contained deployments, the tooling will generate an executable file containing a host which will load the runtime and the dll containing the managed code to run. You tried to invoke the dll file, which is not runnable. Instead there should be an extensionless ELF executable:

    # /mnt/c/Temp/lin2/lin2