question

AndrewFraser-0479 avatar image
3 Votes"
AndrewFraser-0479 asked Bruce-SqlWork answered

Self-Contained Single-File does not produce a single file

I have a small .Net 5 console application with no dependencies on third party components.
I have set it up to publish as a self-contained single-file.
However, in the publish directory is the EXE file and four DLLs. The EXE file will not run without those DLLs being present.
The DLLs are:

clrcompression.dll
clrjit.dll
coreclr.dll
mscordaccore.dll

Why is it Visual Studio does not understand single in Single-File publish ?

Andy

dotnet-cli
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

TimonYang-MSFT avatar image
2 Votes"
TimonYang-MSFT answered PennDavid-3601 edited

You need use IncludeNativeLibrariesForSelfExtract or IncludeAllContentForSelfExtract.

Either use it like this in the .Net CLI:

 dotnet publish -r win-x64 /p:PublishSingleFile=true /p:IncludeNativeLibrariesForSelfExtract=true --self-contained true

Either add it in the projectName.csproj file:

  <IncludeAllContentForSelfExtract>true</IncludeAllContentForSelfExtract>

In this way, there will be only one exe file and one pdb file after release.


If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

· 5
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

The CLI version worked perfectly.
Many thanks

Andy

0 Votes 0 ·

Has .NET 5 changed recently - the dotnet publish command above which worked fine for me last week is now giving me the error:

Required argument missing for option: --runtime

Andy

0 Votes 0 ·

@AndrewFraser-0479
I created a new console application for .Net 5 and published it, but everything was the same as before, and I did not encounter this error.
I did some searches but couldn't find any recent changes made in .Net 5 that might cause this problem.
Maybe you can try to refer to the example in this link to add the runtime option to see if it is useful.
Have you tried the second method I proposed? Open the project folder, find the .csproj file, add this information, and then publish it in Visual Studio in the normal way.

 <Project Sdk="Microsoft.NET.Sdk">
   <PropertyGroup>
     <OutputType>Exe</OutputType>
     <TargetFramework>net5.0</TargetFramework>
     <IncludeAllContentForSelfExtract>true</IncludeAllContentForSelfExtract>
   </PropertyGroup>
 </Project>
0 Votes 0 ·
Show more comments

This simply doesn't work. I have the EXACT .csproj you have below. It creates the .exe with a .dll next to it. If you copy just the .exe to another folder and run it you get:
The application to execute does not exist: 'c:\temp\app.dll'.

The CLI approach does work properly however.

0 Votes 0 ·
Bruce-SqlWork avatar image
0 Votes"
Bruce-SqlWork answered

the <IncludeAllContentForSelfExtract> tell the publish, that you want the dll extracted during publish (required to debug). do not do this if you want a single file. also you need to supply a runtime.

 <Project Sdk="Microsoft.NET.Sdk">
   <PropertyGroup>
     <OutputType>Exe</OutputType>
     <TargetFramework>net6.0</TargetFramework>
     <ImplicitUsings>enable</ImplicitUsings>
     <Nullable>enable</Nullable>
     <PublishSingleFile>true</PublishSingleFile>
     <RuntimeIdentifier>osx.12-arm64</RuntimeIdentifier>
   </PropertyGroup>
 </Project>

Note: for MacOs the Mac app bundler format is used, so there are fewer restrictions than windows.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.