问题
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
回答1:
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>
来源:https://stackoverflow.com/questions/64778283/net-5-excludes-some-libraries-from-single-file-publication