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

Andrew Fraser 101 Reputation points
2021-03-03T11:31:44.523+00:00

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

.NET CLI
.NET CLI
A cross-platform toolchain for developing, building, running, and publishing .NET applications.
337 questions
0 comments No comments
{count} votes

Accepted answer
  1. Timon Yang-MSFT 9,591 Reputation points
    2021-03-04T02:03:02.497+00:00

    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.

    4 people found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 66,706 Reputation points
    2022-06-10T17:36:29.047+00:00

    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.

    0 comments No comments

  2. Rajib Chy 1 Reputation point
    2022-09-21T08:34:59.98+00:00

    try as following with .Net core 6.0.*

    <Project Sdk="Microsoft.NET.Sdk">  
      <PropertyGroup>  
        <OutputType>Exe</OutputType>  
        <TargetName>trade-plus</TargetName>  
        <TargetFramework>net6.0-windows</TargetFramework>  
        <RuntimeIdentifier>win-x64</RuntimeIdentifier>  
        <PlatformTarget>x64</PlatformTarget>  
        <AssemblyName>app-assembly-name</AssemblyName>  
        <Version>1.0.0.1</Version>  
        <FileVersion>1.0.0.1</FileVersion>  
        <Platforms>x64</Platforms>  
      </PropertyGroup>  
      <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">  
        <PlatformTarget>x64</PlatformTarget>  
        <Optimize>True</Optimize>  
        <DebugType>none</DebugType>  
      </PropertyGroup>  
      <PropertyGroup>  
        <ProduceReferenceAssembly>False</ProduceReferenceAssembly>  
      </PropertyGroup>  
    </Project>  
    

    and run this command at respective project directory dotnet publish -r win-x64 -c Release -o bin/win-x64/native /p:SelfContained=true /p:PublishSingleFile=true /p:PublishReadyToRun=true
    read more Single-file deployment and executable

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.