.NET 5 excludes some libraries from single file publication
Asked Answered
G

1

6

I have a little problem with single file executable publish with .NET 5.
Infact, it does not include all libraries in the executable file, and produces multiple files.

In my example I'm using a library for SQLite (Microsoft.Data.Sqlite) and, after compilation, e_sqlite3.dll is not included.
Instead, in the output folder, it produces two files (excluding the pdb file):

> e_sqlite3.dll
> WpfApp1.exe
Gregorygregrory answered 10/11, 2020 at 23:26 Comment(0)
G
18

By reading documentation

Single-file doesn't bundle native libraries by default. On Linux, we prelink the runtime into the bundle and only application native libraries are deployed to the same directory as the single-file app. On Windows, we prelink only the hosting code and both the runtime and application native libraries are deployed to the same directory as the single-file app. This is to ensure a good debugging experience, which requires native files to be excluded from the single file. There is an option to set a flag, IncludeNativeLibrariesForSelfExtract, to include native libraries in the single file bundle, but these files will be extracted to a temporary directory in the client machine when the single file application is run.

So (in my case e_sqlite3.dll) native libraries are not included by default to ensure a good debugging experience.
If you want to include them anyway in the application executable, you can simply add this line to the project (.csproj) file.

<IncludeNativeLibrariesForSelfExtract>true</IncludeNativeLibrariesForSelfExtract>

Example:

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

  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>net5.0-windows</TargetFramework>
    <UseWPF>true</UseWPF>
    <IncludeNativeLibrariesForSelfExtract>true</IncludeNativeLibrariesForSelfExtract>
    <StartupObject>WpfApp1.App</StartupObject>
    <Description>WpfApp1</Description>
  </PropertyGroup>

...

</Project>
Gregorygregrory answered 10/11, 2020 at 23:26 Comment(1)
But in this case, ALL files (including settings and configuration files) that should not exist as separate files and folderes and are marked as "Copy to output" will also be integrated into the exe file, rather than stand alone.Serous

© 2022 - 2024 — McMap. All rights reserved.