C
C#•3y ago
Surihia

Embedding dlls into a compiled exe [Answered]

I have few dll packages that I am using with my app and I want to embed them all inside a single compiled executable file. I am using the following packages:
System.Buffers
System.Memory
System.Buffers
System.Memory
Now I have added the dll files by right clicking on my Project in the Solution explorer and by selecting the Add item option. the compiled app is larger as a result of this, but when I try running the app, it throws exceptions that the respective dll file is not present or unable to be loaded.
22 Replies
Surihia
SurihiaOP•3y ago
Here is the exception I get:
Unhandled Exception: System.IO.FileNotFoundException: Could not load file or assembly 'System.Memory, Version=4.0.1.2, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' or one of its dependencies. The system cannot find the file specified.
at MyCustomWriter.Program.Main(String[] args)
Unhandled Exception: System.IO.FileNotFoundException: Could not load file or assembly 'System.Memory, Version=4.0.1.2, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' or one of its dependencies. The system cannot find the file specified.
at MyCustomWriter.Program.Main(String[] args)
jcotton42
jcotton42•3y ago
$singlefile
MODiX
MODiX•3y ago
dotnet publish -c Release -r <runtime identifier> -p:PublishSingleFile=true Use of -r|--runtime implies --self-contained true. Add --self-contained false to publish as runtime-dependent. -r ... and -p:PublishSingleFile=true can be moved to .csproj as the following properties:
<RuntimeIdentifier>runtime identifier</RuntimeIdentifier>
<PublishSingleFile>true</PublishSingleFile>
<RuntimeIdentifier>runtime identifier</RuntimeIdentifier>
<PublishSingleFile>true</PublishSingleFile>
but to target multiple RIDs, you have to use dotnet publish with the -r option for each RID. You can also add -p:IncludeNativeLibrariesForSelfExtract=true to include native libraries (like Common Language Runtime dlls) in the output executable. https://docs.microsoft.com/en-us/dotnet/core/deploying/single-file https://docs.microsoft.com/en-us/dotnet/core/rid-catalog https://docs.microsoft.com/en-us/dotnet/core/tools/dotnet-publish
Create a single file for application deployment - .NET
Learn what single file application is and why you should consider using this application deployment model.
.NET Runtime Identifier (RID) catalog
Learn about the Runtime Identifier (RID) and how RIDs are used in .NET.
Surihia
SurihiaOP•3y ago
I am a bit confused. should I use the cli as well as edit the csproj file of my app or should I do only one of them.
I added these two lines to my .csproj file of the program
<RuntimeIdentifier>runtime identifier</RuntimeIdentifier>
<PublishSingleFile>true</PublishSingleFile>
<RuntimeIdentifier>runtime identifier</RuntimeIdentifier>
<PublishSingleFile>true</PublishSingleFile>
Then I Built the app for release and the same issue persists.
jcotton42
jcotton42•3y ago
single file is a publish thing it doesn't affect regular builds, just publishes also
can be moved to .csproj as the following properties:
it's CLI or csproj can you show the contents of your csproj?
Surihia
SurihiaOP•3y ago
sure
jcotton42
jcotton42•3y ago
as $code in the future please
MODiX
MODiX•3y ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat If your code is too long, post it to: https://paste.mod.gg/
jcotton42
jcotton42•3y ago
oh, Framework 4.7.2 🤢 single-file publish is not available for Framework projects
Surihia
SurihiaOP•3y ago
ah So I have no choice but to include the dlls with my exe then ?
jcotton42
jcotton42•3y ago
yes well, there's some libraries that can do this for you one of the Fody ones iirc but it's not as seamless as a single-file publish
Surihia
SurihiaOP•3y ago
I got some weavers something error message with Fody is there like a sample tutorial for using that package to make a single exe ?
jcotton42
jcotton42•3y ago
though, something I've noticed
<Reference Include="System.Memory, Version=4.0.1.2, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>packages\System.Memory.4.5.5\lib\net461\System.Memory.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.Memory, Version=4.0.1.2, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>packages\System.Memory.4.5.5\lib\net461\System.Memory.dll</HintPath>
<Private>True</Private>
</Reference>
I believe the Private thing means it shouldn't be emitted into the build output, as it's only used at build time, not runtime which is not what you want for these packages
dancepanda42
dancepanda42•3y ago
GitHub
GitHub - Fody/Costura: Embed references as resources
Embed references as resources. Contribute to Fody/Costura development by creating an account on GitHub.
dancepanda42
dancepanda42•3y ago
Compile everything in one file
Surihia
SurihiaOP•3y ago
Ok I installed the packages through the NUget manager what do I next ?
jcotton42
jcotton42•3y ago
did you read the github page? it says what to do
Surihia
SurihiaOP•3y ago
I am getting this error
Severity Code Description Project File Line Suppression State Error Fody: Could not read 'U:\Documents\Visual Studio 2022 projects\C# stuff\App projects\MyCustomWriter\FodyWeavers.xml' because it has invalid XML. Message: 'There are multiple root elements. Line 2, position 4.
Severity Code Description Project File Line Suppression State Error Fody: Could not read 'U:\Documents\Visual Studio 2022 projects\C# stuff\App projects\MyCustomWriter\FodyWeavers.xml' because it has invalid XML. Message: 'There are multiple root elements. Line 2, position 4.
this is my Fody Weavers file
<Weavers xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="FodyWeavers.xsd" />
<Costura UseRuntimeReferencePaths='true' />
</Weavers>
<Weavers xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="FodyWeavers.xsd" />
<Costura UseRuntimeReferencePaths='true' />
</Weavers>
I did read the page
jcotton42
jcotton42•3y ago
The first tag is self-closing in your paste The />
Surihia
SurihiaOP•3y ago
Ok that fixed it These are the lines inside the weavers xml file that allowed me to compile a single app. putting it here as it can be of help to someone else:
<Weavers xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="FodyWeavers.xsd">
<Costura UseRuntimeReferencePaths='true' />
</Weavers>
<Weavers xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="FodyWeavers.xsd">
<Costura UseRuntimeReferencePaths='true' />
</Weavers>
Thank you to the both of you for helping out
Accord
Accord•3y ago
✅ This post has been marked as answered!
Want results from more Discord servers?
Add your server