4

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.

Raudel Ravelo
  • 648
  • 2
  • 6
  • 24
Jake
  • 98
  • 2
  • 5
  • I also miss Costura/Fody. It just worked. In contrary to the whole "publish" scheme, which isn't just a pain, but also rarely does what it is supposed to do. :-) – Xan-Kun Clark-Davis Jan 03 '23 at 00:01

1 Answers1

6

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

Martin
  • 3,096
  • 1
  • 26
  • 46
  • 1
    Thank you! It worked Just adds that with this config it embeds the whole .net runtime and the result is heavy exe (58mb) To reduce this size I added true And the exe size became much lighter (18mb) – Jake Jul 23 '21 at 20:58
  • @Jake Just added trimming to the answer to have it a full example. Thank you. – Martin Jul 24 '21 at 09:41
  • would this work with netcoreapp3.1 ? – Raudel Ravelo Jan 20 '22 at 16:33
  • 1
    @RaudelRavelo My example project is on another pc. But you can easily try this :). I expect in total it is just working higher than net 5 – Martin Jan 20 '22 at 16:54