Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.Net 5 Publish Single File - Produces exe and dlls

Tags:

I am using VS 2019 and .Net 5 to build a simple console application. I wanted to share this app with a friend so I tried to publish it as a single file but I keep getting some extra DLLs that the executable needs to run correctly.

Edit: Switching this project to .net core 3.1 works as expected I am able to export a single Exe file without any required DLLs.

Dotnet Cli: dotnet publish -c Release -o publish -p:PublishReadyToRun=true -p:PublishSingleFile=true -p:PublishTrimmed=true --self-contained true

Csproj:

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

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net5.0</TargetFramework>
    <PublishSingleFile>true</PublishSingleFile>
    <RuntimeIdentifier>win-x64</RuntimeIdentifier>
    <PlatformTarget>x64</PlatformTarget>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="HtmlAgilityPack" Version="1.11.28" />
  </ItemGroup>
</Project>

Output

like image 743
Bailey Miller Avatar asked Dec 06 '20 16:12

Bailey Miller


People also ask

How do I publish self-contained?

Produce an executableWhen publishing your app and creating an executable, you can publish the app as self-contained or framework-dependent. Publishing an app as self-contained includes the . NET runtime with the app, and users of the app don't have to worry about installing . NET before running the app.


1 Answers

Its known issue that described here: https://github.com/dotnet/runtime/issues/36590

And new dev experience provided here: https://github.com/dotnet/designs/blob/main/accepted/2020/single-file/design.md#user-experience

So in your case you need use p:IncludeNativeLibrariesForSelfExtract=true additionaly.

Full command:

dotnet publish -c Release -o publish -p:PublishReadyToRun=true -p:PublishSingleFile=true -p:PublishTrimmed=true --self-contained true -p:IncludeNativeLibrariesForSelfExtract=true

or include this flag in .csproj file

<PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net5.0</TargetFramework>
    <PublishSingleFile>true</PublishSingleFile>
    <RuntimeIdentifier>win-x64</RuntimeIdentifier>
    <PlatformTarget>x64</PlatformTarget>
    
   <IncludeNativeLibrariesForSelfExtract>true</IncludeNativeLibrariesForSelfExtract>

</PropertyGroup>
like image 109
Anton Komyshan Avatar answered Sep 17 '22 13:09

Anton Komyshan