.NET Core 6.0 独立二进制编译

.NET Core 6.0 standalone binary compilation

我正在尝试创建一个独立的 .NET Core 6.0 二进制文件,所有依赖项都打包在一个 .exe 文件中。

我遇到的问题是,虽然二进制编译正常,但它依赖于放置在 Release/Debug 文件夹中的 DLL。

我试过使用

从命令行编译
dotnet publish --standalone

但在那种情况下,我只是遇到了一个类似的问题,加载更多 DLL 并且二进制文件本身大小相同,需要位于 运行.

的文件夹中

我正在寻找的东西是否可能实现?如果可以,如何实现?到目前为止,我已经尝试使用 Visual Studio、dotnet cli 和 Rider。

有许多旧解决方案提到了 ilmerge 等解决方案,但这似乎早已被弃用并且不再维护。

-- 为未来的我编辑:

最终的解决方案是这样的,多亏了下面安德鲁的回答。

我的最终 project.csproj 文件看起来像这样基于 MS Docs

<PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>net6.0-windows</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
    <PublishSingleFile>true</PublishSingleFile>
    <EnableCompressionInSingleFile>true</EnableCompressionInSingleFile>
    <RuntimeIdentifier>win-x64</RuntimeIdentifier>
    <PublishReadyToRun>true</PublishReadyToRun>
</PropertyGroup>

通过 Visual Studio GUI 或:

发布
dotnet publish -c release -r win-x64

我建议查看 https://docs.microsoft.com/en-us/dotnet/core/deploying/single-file,希望它是最新的。我相信关键位是 .csproj.

中的 <PublishSingleFile>true</PublishSingleFile>