Search code examples
c#.netvisual-studio-2019.net-5fody-costura

Can't get costura.fody to embed dll into exe


I try to embed the dll of a class library into my exe. I use visual studio 2019 and .net 5. I created two projects in one solution one is class library (dll), and the second is console application both targeted for .net core 5. I selected the console application as startup project. the class library contain only public static hello function which print out hello. I referenced the project of the class library into the console application then in the console application i only called the ClassNamespace.library.hello function. when I compile it, it workes fine. then I installed costura.fody as described in their readme, i added the to the console project by:

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

Then I FodyWeavers.xml into project folder

<Weavers>
  <Costura/>
</Weavers>

After that i rebuilt the project, and it built, and the exe is running, but when I delete the .dll from the output directory the .exe isn't running.


Solution

  • This can be accomplished without any additional package. Since NET 5 you have to set two options.

    <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>
    

    With setting IncludeNativeLibrariesForSelfExtract to false

    enter image description here

    With setting IncludeNativeLibrariesForSelfExtract to true

    enter image description here

    Documentation for publish single file

    Documentation for trimming