C
C#ā€¢9mo ago
DjangoCodes

Need help publishing a dotnet 6.0 WPF application as a single exe.

I've tried everything possible, used Costura.Fody to compress together the dlls and tried every single build option in publish.xml
42 Replies
Pobiega
Pobiegaā€¢9mo ago
and you tried the built in CLI thing first, right? $singlefile
MODiX
MODiXā€¢9mo ago
dotnet publish -c Release -r <runtime identifier> -p:PublishSingleFile=true Use of -p:PublishSingleFile=true implies --self-contained true. Add --self-contained false to publish as runtime-dependent. -r RID and -p:PublishSingleFile=true can be moved to .csproj as the following properties:
<RuntimeIdentifier>RID</RuntimeIdentifier>
<PublishSingleFile>true</PublishSingleFile>
<RuntimeIdentifier>RID</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. You might want to instead publish your application compiled Ahead Of Time to native code, see $nativeaot for examples. Single file publishing | Runtime Identifier (RID) catalog | dotnet publish
DjangoCodes
DjangoCodesOPā€¢9mo ago
yep already tried that, I'm using the mongoDB c# Driver for my project and newtonsoft json for deserializing. These dependencies for some reason don't pack into the same exe and are still standalone even after trying to publish single File as true.
Pobiega
Pobiegaā€¢9mo ago
Unrelated to your problem, but I'm a bit worried about the concept of packaging a WPF client with a mongo connector in it. That means you likely have a connectionstring somewhere in your app too.
DjangoCodes
DjangoCodesOPā€¢9mo ago
I do yes, I was trying to find a way to not use the connection string but everywhere on the mongoDb c# document it only reffered to using the connection string as a way of connecting to atlas. I'm a bit of a noob so pls excuse me if im doing something wrong, I'd appreciate the help
Pobiega
Pobiegaā€¢9mo ago
lets get back to that later
DjangoCodes
DjangoCodesOPā€¢9mo ago
sure
Pobiega
Pobiegaā€¢9mo ago
I just made a .net 6 WPF app with mongo driver and newtonsoft and published it as single file worked fine, except the native dlls
Pobiega
Pobiegaā€¢9mo ago
No description
Pobiega
Pobiegaā€¢9mo ago
No description
DjangoCodes
DjangoCodesOPā€¢9mo ago
is there a way to pack those dlls into a single exe like we were able to in .net framework 4.7.2?
Pobiega
Pobiegaā€¢9mo ago
yep I literally just followed the instructions I linked above
DjangoCodes
DjangoCodesOPā€¢9mo ago
I'll try again one sec
Pobiega
Pobiegaā€¢9mo ago
here is my csproj
<Project Sdk="Microsoft.NET.Sdk">

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

<ItemGroup>
<PackageReference Include="MongoDB.Driver" Version="2.25.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>

</Project>
<Project Sdk="Microsoft.NET.Sdk">

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

<ItemGroup>
<PackageReference Include="MongoDB.Driver" Version="2.25.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>

</Project>
and this is the command I ran dotnet publish -c Release -p:IncludeNativeLibrariesForSelfExtract=true thats all I did
DjangoCodes
DjangoCodesOPā€¢9mo ago
the includenativelibraries for self extract, extracts the libraries to temp right?
Pobiega
Pobiegaā€¢9mo ago
not sure exactly where they end up when the program is run tbh but it works šŸ™‚
DjangoCodes
DjangoCodesOPā€¢9mo ago
okay, so I did what you said with just publishing as a single file in the release directory, and I got this. My application is supposed to have an image right there but it doesn't render for some reason
No description
DjangoCodes
DjangoCodesOPā€¢9mo ago
should look like this
No description
Pobiega
Pobiegaā€¢9mo ago
how is the image stored? you can and should be embedding that image if you want a "proper" singlefile.
DjangoCodes
DjangoCodesOPā€¢9mo ago
the build type of the images is content and output to directory set to always
Pobiega
Pobiegaā€¢9mo ago
but look at your file-sizes thats NOT a self-contained singlefile executable
DjangoCodes
DjangoCodesOPā€¢9mo ago
I didn't do self contained, do you recommed self contained for applications?
Pobiega
Pobiegaā€¢9mo ago
open up your terminal of choice and go to your project root directory
DjangoCodes
DjangoCodesOPā€¢9mo ago
alrighty
Pobiega
Pobiegaā€¢9mo ago
if you want the true single-file experience, yes
DjangoCodes
DjangoCodesOPā€¢9mo ago
oh then yes
Pobiega
Pobiegaā€¢9mo ago
if you dont self-contain it, then it needs .net 6 installed on the computer
DjangoCodes
DjangoCodesOPā€¢9mo ago
yeah, definitely want self contained then gimme a min
Pobiega
Pobiegaā€¢9mo ago
you have ~3, then Im going for lunch šŸ˜›
Pobiega
Pobiegaā€¢9mo ago
No description
Pobiega
Pobiegaā€¢9mo ago
its important that the command is run from the project root (in my case C:\code\temp\WpfAppPublish\WpfAppPublish\)
DjangoCodes
DjangoCodesOPā€¢9mo ago
No description
DjangoCodes
DjangoCodesOPā€¢9mo ago
alright there we go
DjangoCodes
DjangoCodesOPā€¢9mo ago
Here's my csproj
Pobiega
Pobiegaā€¢9mo ago
right your images are all "copy to output" you should investigate how to embed them as resources instead
DjangoCodes
DjangoCodesOPā€¢9mo ago
alright I will do that
Pobiega
Pobiegaā€¢9mo ago
I'm not a GUI dude, so can't help, but that would mean your images get baked into the exe file and you wont have path issues šŸ™‚
DjangoCodes
DjangoCodesOPā€¢9mo ago
tysm for your help, you were more helpful than all MSDN docs combined
Pobiega
Pobiegaā€¢9mo ago
also btw, win-x86 is kinda pointless now its like 0.01% of the windows userbase
DjangoCodes
DjangoCodesOPā€¢9mo ago
yeah i was just debugging
Pobiega
Pobiegaā€¢9mo ago
šŸ‘ time for lunch good luck
DjangoCodes
DjangoCodesOPā€¢9mo ago
alright have a great lunch thank you for the help
Want results from more Discord servers?
Add your server