C
C#15mo ago
Donut5162

❔ Publishing project

Hello, I am trying to publish a WPF project with NuGet dependencies. I am trying to publish to a single executable. I can publish it in visual studio and get a lightweight exe but it requires a few other DLLs. I can use the command line to publish to a single file but it is very large; the
--self-contained
--self-contained
flag is necessary for this, but it seems to include the .NET runtume in the executable. I would like to avoid this to reduce size since i believe windows comes with the runtime already. Any ideas?
16 Replies
JakenVeina
JakenVeina15mo ago
depends what version of the .NET runtime you're targeting a ClickOnce installer might be a good bet
Donut5162
Donut5162OP15mo ago
is .NET backwards compatible with older versions?
JakenVeina
JakenVeina15mo ago
not completely, no
Donut5162
Donut5162OP15mo ago
unfortuante
JakenVeina
JakenVeina15mo ago
but again, depends on the version you're targeting
Donut5162
Donut5162OP15mo ago
.NET 6.0-windows
JakenVeina
JakenVeina15mo ago
.NET Core and .NET 5+ do not come pre-installed in Windows
Donut5162
Donut5162OP15mo ago
oh okay I can do ClickOnce, I would have liked to avoid using an installer but if that's the best bet i guess it can't be helped
JakenVeina
JakenVeina15mo ago
they are available through the Windows Package Manager in Windows 10 and 11, so that doesn't necessarily mean you need to build a self-contained executable a ClickOnce is a decent solution alternatively, you can dive deeper into the self-contained rabbit hole there's a lot of gains to be made for build size when you make the extra step to AoT compilation and trimming
Donut5162
Donut5162OP15mo ago
i will look into it, thanks 👍
Pobiega
Pobiega15mo ago
Sounds like you want to take a look at $singlefile
MODiX
MODiX15mo 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. 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 - .NET
Learn about the runtime identifier (RID) and how RIDs are used in .NET.
Donut5162
Donut5162OP15mo ago
oh shoot, adding --self-contained=false actually worked it's a single file and less than a megabyte well, it works on my machine at least...
Pobiega
Pobiega15mo ago
Yeah, but it will require the runtime to be installed
Donut5162
Donut5162OP15mo ago
yeah
Accord
Accord15mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.

Did you find this page helpful?