C
C#ā€¢3mo ago
DjangoTweaks

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ā€¢3mo ago
and you tried the built in CLI thing first, right? $singlefile
MODiX
MODiXā€¢3mo 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
DjangoTweaks
DjangoTweaksā€¢3mo 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ā€¢3mo 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.
DjangoTweaks
DjangoTweaksā€¢3mo 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ā€¢3mo ago
lets get back to that later
DjangoTweaks
DjangoTweaksā€¢3mo ago
sure
Pobiega
Pobiegaā€¢3mo 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ā€¢3mo ago
No description
Pobiega
Pobiegaā€¢3mo ago
No description
DjangoTweaks
DjangoTweaksā€¢3mo 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ā€¢3mo ago
yep I literally just followed the instructions I linked above
DjangoTweaks
DjangoTweaksā€¢3mo ago
I'll try again one sec
Pobiega
Pobiegaā€¢3mo 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
DjangoTweaks
DjangoTweaksā€¢3mo ago
the includenativelibraries for self extract, extracts the libraries to temp right?
Pobiega
Pobiegaā€¢3mo ago
not sure exactly where they end up when the program is run tbh but it works šŸ™‚
DjangoTweaks
DjangoTweaksā€¢3mo 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
DjangoTweaks
DjangoTweaksā€¢3mo ago
should look like this
No description
Pobiega
Pobiegaā€¢3mo ago
how is the image stored? you can and should be embedding that image if you want a "proper" singlefile.
DjangoTweaks
DjangoTweaksā€¢3mo ago
the build type of the images is content and output to directory set to always
Pobiega
Pobiegaā€¢3mo ago
but look at your file-sizes thats NOT a self-contained singlefile executable
DjangoTweaks
DjangoTweaksā€¢3mo ago
I didn't do self contained, do you recommed self contained for applications?
Pobiega
Pobiegaā€¢3mo ago
open up your terminal of choice and go to your project root directory
DjangoTweaks
DjangoTweaksā€¢3mo ago
alrighty
Pobiega
Pobiegaā€¢3mo ago
if you want the true single-file experience, yes
DjangoTweaks
DjangoTweaksā€¢3mo ago
oh then yes
Pobiega
Pobiegaā€¢3mo ago
if you dont self-contain it, then it needs .net 6 installed on the computer
DjangoTweaks
DjangoTweaksā€¢3mo ago
yeah, definitely want self contained then gimme a min
Pobiega
Pobiegaā€¢3mo ago
you have ~3, then Im going for lunch šŸ˜›
Pobiega
Pobiegaā€¢3mo ago
No description
Pobiega
Pobiegaā€¢3mo ago
its important that the command is run from the project root (in my case C:\code\temp\WpfAppPublish\WpfAppPublish\)
DjangoTweaks
DjangoTweaksā€¢3mo ago
No description
DjangoTweaks
DjangoTweaksā€¢3mo ago
alright there we go
DjangoTweaks
DjangoTweaksā€¢3mo ago
Here's my csproj
Pobiega
Pobiegaā€¢3mo ago
right your images are all "copy to output" you should investigate how to embed them as resources instead
DjangoTweaks
DjangoTweaksā€¢3mo ago
alright I will do that
Pobiega
Pobiegaā€¢3mo 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 šŸ™‚
DjangoTweaks
DjangoTweaksā€¢3mo ago
tysm for your help, you were more helpful than all MSDN docs combined
Pobiega
Pobiegaā€¢3mo ago
also btw, win-x86 is kinda pointless now its like 0.01% of the windows userbase
DjangoTweaks
DjangoTweaksā€¢3mo ago
yeah i was just debugging
Pobiega
Pobiegaā€¢3mo ago
šŸ‘ time for lunch good luck
DjangoTweaks
DjangoTweaksā€¢3mo ago
alright have a great lunch thank you for the help
Want results from more Discord servers?
Add your server
More Posts