How do I link a .NET Core self-contained deployment?
Asked Answered
T

2

9

I've created a self-contained .NET Core console application following this good tutorial.

This process produces a /publish folder with 123(!) files, including my executable. Now, since all these dlls are unlikely to ever change, I'd like to statically link them to the executable and get a single, clean executable.

I'm mostly interested in linking a win64 target on my build server, however instructions on how to build a macosx executable or a linux one are going to be useful to me and to future readers.

I remember doing this under .Net classic a few years back. Has anyone succeeded in doing it under .Net Core?

See also this related corefx issue

Tergiversate answered 14/3, 2017 at 10:59 Comment(7)
Why do you want that? What's the benefit?Cesspool
@Cesspool why is that a concern? I guess I prefer it. I don't think explaining why would help anyone answer, as I am not looking for alternative solutions, nor indulging in a discussion on whether this is a good thing or not. I can accept a "is not possible, here's why..." answer though, if that is the case.Tergiversate
Have you looked at CoreRT? It does produce a self-contained executable, though it also has some other trade-offs. As far as I know, there's no current plan to add linking to .NET Core itself.Alcoholic
@Tergiversate Don't take it so bad, I just didn't wanted to loose the chance to learn something new... Thanks for answering anyway...Cesspool
@Cesspool sorry about the tone of my comment. Every time I ask a hard question someone answers "why? I wouldn't bother" which I hoped to avoid this time. It's just because it's cool to learn new stuff :-)Tergiversate
@Tergiversate Thank you. I think that knowledge is what move us both ;-)Cesspool
It must be getting on for 15 years since .net come out, and it still can't create a simple single file exe that "just works" on all windows computers unlike MFC! However I think it is still worth it so as to avoid C++.Atomy
P
3
  1. Right click on the .Net project (not the solution) and go to Publish.

  2. Click on "Show all settings"

  3. Select the required configuration and target framework and target runtime

  4. Keep Deployment mode as self-contained

  5. Go to "File publish options" at the bottom, and select the following options:

    i. Produce Single File

    ii. Enable ReadyToRun Compilation

  6. Save the settings and publish your project.

This will package all the dlls in the executable itself.

Pyrognostics answered 18/2, 2022 at 14:49 Comment(0)
R
1

Self-contained needs a reference to .Net Core CLI and CoreFX, so you cannot simply create "one DLL" with all. You can try to minimize the amount of DLLs by adding libs function into your main project but I cant recommend this way

It sounds like the feature you want is CoreRT (.NET Native compiler toolchain) but it is still under heavy development and it only works for UWP out of the box.

Remake answered 14/3, 2017 at 13:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.