无法让costura.fody将dll嵌入到exe中

2024-03-13

我尝试将类库的 dll 嵌入到我的 exe 中。 我使用 Visual Studio 2019 和 .net 5。 我在一个解决方案中创建了两个项目 一是类库(dll),二是控制台应用程序 两者都针对 .net core 5。我选择控制台应用程序作为启动项目。 类库仅包含公共静态 hello 函数,该函数打印出 hello。 我将类库的项目引用到控制台应用程序中,然后在控制台应用程序中我只调用了 ClassNamespace.library.hello 函数。 当我编译它时,它工作正常。 然后我按照自述文件中的描述安装了 costura.fody,我通过以下方式将其添加到控制台项目中:

PM> Install-Package Fody
PM> Install-Package Costura.Fody

然后我将FodyWeavers.xml放入项目文件夹中

<Weavers>
  <Costura/>
</Weavers>

之后,我重建了项目,它构建了,并且 exe 正在运行,但是当我从输出目录中删除 .dll 时,.exe 没有运行。


这无需任何附加包即可完成。 从 NET 5 开始你必须设置two选项。

<PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net5.0</TargetFramework>
    <!-- To publish just a single *.exe file -->
    <PublishSingleFile>true</PublishSingleFile>
    <!-- Specify for which runtime you want to publish -->
    <RuntimeIdentifier>win10-x64</RuntimeIdentifier>
    <!-- Since NET 5 specify this if you want to also pack all external *.dll to your file -->
    <IncludeNativeLibrariesForSelfExtract>true</IncludeNativeLibrariesForSelfExtract>
    <!-- Add trimming for a smaller file size if possible--->
    <PublishTrimmed>true</PublishTrimmed>
</PropertyGroup>

有设定IncludeNativeLibrariesForSelfExtract to false

有设定IncludeNativeLibrariesForSelfExtract to true

发布单个文件的文档 https://learn.microsoft.com/en-us/dotnet/core/deploying/single-file

修剪文档 https://devblogs.microsoft.com/dotnet/app-trimming-in-net-5/

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

无法让costura.fody将dll嵌入到exe中 的相关文章

随机推荐