4

I published a small C# .Net Core 2.2 console application as executable using the following command from this post:

dotnet publish -c Release -r win10-x64

The generated files contain both the classic ConsoleApp.dll, but also the executable that I was expecting to be generated ConsoleApp.exe.

My question is why there was still the DLL generated, since all its code, I suppose, could have been compiled to the .exe, as in a .Net Framework application?

On the other hand, I tried to decompile the .exe file with ILSpy but the content from it does not seem to be managed code. In this case I also suppose that the .exe file is just calling the DLL using the dotnet command. Is this assumption right?

Below is the Console Application .csproj file content:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp2.2</TargetFramework>
  </PropertyGroup>

  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
    <PlatformTarget>AnyCPU</PlatformTarget>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="System.Management" Version="4.7.0-preview3.19551.4" />
  </ItemGroup>

  <ItemGroup>
    <None Update="input.txt">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </None>
  </ItemGroup>

</Project>
meJustAndrew
  • 6,011
  • 8
  • 50
  • 76

2 Answers2

2

Very interesting question. I'll try to cover it as much as I possibly can.

With the introduction of .net core 3.0 came the Single file publish as described in the design documentation of .Net. So If you have a .net 3.0 application then you can use the /p:PublishSingleFile=true to bundle everything in a single executable.

Watch out for the /p:PublishTrimmed=true option, as treeshaking can and probably will cause problems with reflection code, as access to it is not covered by the tree shaking.

For previous versions of net core, you need to use one of the packagers like wrap or Costura.

As per the exe file, I'll mention somthing from the wrap documentation:

The final self-contained single binary application consists of two parts: 1) runner and 2) the compressed target application executable and dependencies.

The dll that is created by the standard publisher is multi platform and non specific to windows. So the executable file has all the code needed to create the process in windows and call the actual code that is in the dll that can be used on any platform. It's just a wrapper.

Wrap application

More information about the executable Microsoft .net core deploying docs

Self-contained deployment. Unlike FDD, a self-contained deployment (SCD) doesn't rely on the presence of shared components on the target system. All components, including both the .NET Core libraries and the .NET Core runtime, are included with the application and are isolated from other .NET Core applications. SCDs include an executable (such as app.exe on Windows platforms for an application named app), which is a renamed version of the platform-specific .NET Core host, and a .dll file (such as app.dll), which is the actual application.

Athanasios Kataras
  • 25,191
  • 4
  • 32
  • 61
  • But the question is about .NET Core 2.2 – Pavel Anikhouski Nov 26 '19 at 10:43
  • Check https://github.com/dgiagio/warp#windows-1 for .net core 2.2 – Athanasios Kataras Nov 26 '19 at 10:47
  • *the executable file has all the code needed to create the process in windows and call the actual code that is in the dll* - is this also the case for the .exe and .dll resulting after the `dotnet publish` command in .Net Core 2.2? – meJustAndrew Nov 26 '19 at 10:48
  • It is, for all net core applications. All your actual application code will be included in the dll file, in a way that you can use the dll file even without the exe. – Athanasios Kataras Nov 26 '19 at 10:51
  • Check the updated answer for more documentation about the exe file (from the official microsoft documentation) – Athanasios Kataras Nov 26 '19 at 10:59
  • @AthanasiosKataras I have read that part of the documentation, but I believe it is more about the self contained deployment and it focuses on the fact that the components of the .Net Core runtime are included into the application, so it will not rely on the presence of .Net Core runtime on the target machine. What I haven't seen on the MSDN is what actually this .exe is doing and whether it's only calling the DLL or not. – meJustAndrew Nov 26 '19 at 11:15
  • Your answer is the one that comes closest to the actual question. I just want to see some reference of what is the .exe file doing in the current deployment. – meJustAndrew Nov 26 '19 at 11:17
  • I started a new github question to get some official answer as I couldn't find any more information other than what I included in the question, so I can only make assumptions from here: https://github.com/dotnet/docs/issues/15986 I will let you know as soon as I've got some extra information. Thanks for the great question. – Athanasios Kataras Nov 26 '19 at 13:23
0

I am not sure about your assumption. But use the following command it will generate only two files, one is .exe and other is .pdb in the publish directory inside win10-x64 directory.

dotnet publish -r win10-x64 -c Release /p:PublishSingleFile=true /p:PublishTrimmed=true
Keyur Ramoliya
  • 1,900
  • 2
  • 16
  • 17
  • I am using .Net Core 2.2 to publish. Are the arguments you used in your commands specific to .Net Core 3? I am asking because I still get generated both the DLL and the exe. – meJustAndrew Nov 26 '19 at 10:26