Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PublishSingleFile does not produce a single executable

I have a .Net Core 3 console application that i'm trying to publish as a self contained single executable. I've been able to do this in the past but to my suprise it no longer works. The project structure is a console application with two assemblies, all in Core 3.

If i use dotnet publish -r win-x64 -c Release /p:PublishSingleFile=true /p:PublishTrimmed=true i expect the output to be a single executable of several mb's in size. However the publish folder contains the executable (few hundred kb) and a .dll file together with .cache files and the pdb.

The config for my console app is as follows:

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp3.0</TargetFramework>
    <LangVersion>latest</LangVersion>
    <TreatWarningsAsErrors>true</TreatWarningsAsErrors>
    <Nullable>enable</Nullable>
    <RuntimeIdentifier>win-x64</RuntimeIdentifier>
    <PublishSingleFile>true</PublishSingleFile>
    <PublishTrimmed>true</PublishTrimmed>
  </PropertyGroup>

If i publish the app directly from Visual Studio i get the same results as above.

So my question boils down to: Why doesnt this configuration or publish statement result in a self contained single executable?

like image 237
RoelA Avatar asked Jul 15 '20 07:07

RoelA


People also ask

Is a non self-contained executable a non self-contained executable Cannot be referenced by a self-contained executable?

The referenced project is a non self-contained executable. A non self-contained executable cannot be referenced by a self-contained executable.

What is self-contained app?

A self-contained application consists of a single, installable bundle that contains your application and a copy of the JRE needed to run the application. When the application is installed, it behaves the in the same way as any native application.


2 Answers

I can get my 3.1 console app to publish with the following:

<?xml version="1.0" encoding="utf-8"?>
<!--
https://go.microsoft.com/fwlink/?LinkID=208121. 
-->
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <Configuration>Release</Configuration>
    <Platform>Any CPU</Platform>
    <PublishDir>..\..\Binaries\</PublishDir>
    <PublishProtocol>FileSystem</PublishProtocol>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <RuntimeIdentifier>win-x64</RuntimeIdentifier>
    <PublishSingleFile>True</PublishSingleFile>
    <PublishReadyToRun>False</PublishReadyToRun>
    <DebugType>None</DebugType>
  </PropertyGroup>
</Project>

If I set PublishReadyToRun to True (or add PublishTrimmed as True) it fails. Adding DebugType None prevents it publishing the pdb file for the exe.

UPDATE: removing the SelfContained tag stops it creating the 'dotnetcoreapp3.1' folder.

like image 123
Simon Storr Avatar answered Oct 16 '22 06:10

Simon Storr


According to the command line release, I tested successfully. A single executable file was generated. If you have always failed to publish, I suggest you use VS to publish.The publishing process is as follows:

1.Right click on the project->publish enter image description here

2.Change configuration

enter image description here

3.Save the configuration and click Publish

enter image description here

like image 2
Jack J Jun - MSFT Avatar answered Oct 16 '22 08:10

Jack J Jun - MSFT