在尝试编译利用Microsoft.Data.Sqlite的单个文件二进制文件时,我始终会看到的两个文件,这两个文件都是应用程序工作所必需的。
{ProjectName}.exe
e_sqlite3.dll
是否可以将e_sqlite3.dll
包含到exe中?
看来,System.Data.Sqlite表现出了同样的行为,而是一个名为
SQLite.Interop.dll
的文件。
样本代码
注意:我意识到与SQLite没有实际的互操作,这段代码纯粹是为了演示编译。
ProjectName.fsproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<PublishSingleFile>true</PublishSingleFile>
<SelfContained>true</SelfContained>
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
<PublishReadyToRun>true</PublishReadyToRun>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Data.Sqlite" version="7.*" />
</ItemGroup>
<ItemGroup>
<Compile Include="Program.fs" />
</ItemGroup>
</Project>
Program.fs
module ProjectName.Program
open System
[<EntryPoint>]
let main (argv : string[]) =
printfn "Hello world"
0
该项目汇编如下:
dotnet publish .\ProjectName.fsproj -c Release
发布于 2022-12-03 01:56:35
解决方案
事实证明,在net6、net7以及(想必)之外,通过将IncludeNativeLibrariesForSelfExtract
设置为true
,很容易做到这一点。
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<SelfContained>true</SelfContained>
<PublishSingleFile>true</PublishSingleFile>
<PublishReadyToRun>true</PublishReadyToRun>
<PublishTrimmed>true</PublishTrimmed>
<!-- Must include this line -->
<IncludeNativeLibrariesForSelfExtract>true</IncludeNativeLibrariesForSelfExtract>
<DebuggerSupport>false</DebuggerSupport>
<EnableUnsafeUTF7Encoding>false</EnableUnsafeUTF7Encoding>
<HttpActivityPropagationSupport>false</HttpActivityPropagationSupport>
<InvariantGlobalization>true</InvariantGlobalization>
<UseNativeHttpHandler>true</UseNativeHttpHandler>
<UseSystemResourceKeys>true</UseSystemResourceKeys>
<EnableCompressionInSingleFile>true</EnableCompressionInSingleFile>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Data.Sqlite" version="7.*" />
</ItemGroup>
<ItemGroup>
<Compile Include="Program.fs" />
</ItemGroup>
</Project>
发布于 2022-12-02 23:04:51
不幸的是,似乎不可能将e_sqlite3.dll包含到exe文件中。拥有单个文件可执行文件的唯一方法是使用.NET本机工具链,可以通过将属性设置为true来启用该工具链。
https://stackoverflow.com/questions/74661400
复制相似问题