I'm writing a program in C#. I'd like to compile the program to a DLL file, which can be easily be done with the dotnet
cli tool. However, I would then like to pack this dll, along with all of it's dependencies, into a single executable file for the specified platform, along with some configuration values which could be accessed in the C# code. I do not want to use Publish single file because the configuration values will vary and I don't want to need the source code every time I need a new executable. Is this possible? I can't find anything of the sort in Microsoft documentation or on Google.
Asked
Active
Viewed 426 times
1

Elijah
- 57
- 6
-
Add them as binary resources, then hook in to `AppDomain.AssemblyResolve` and load them from the resource binary – Charlieface Jul 26 '22 at 01:54
-
@Charlieface Do you have an example of how to do that? I'm not exactly sure what you mean by resource binary. I want to emulate the function of `dotnet publish` in single-file mode except separately from the DLL compilation – Elijah Jul 26 '22 at 02:00
-
Are these third-party DLLs, or are they other projects in the solution? – Charlieface Jul 26 '22 at 02:01
-
@Charlieface There's one main program dll written by me and then some nuget dependency dlls. – Elijah Jul 26 '22 at 02:11
-
Does this answer your question? [Loading dll library from Resource to Current Domain(Embedding dll in main exe file)](https://stackoverflow.com/questions/13443890/loading-dll-library-from-resource-to-current-domainembedding-dll-in-main-exe-fi) – Charlieface Jul 26 '22 at 02:13
-
@Charlieface No. That seems to be some legacy .NET framework problem. The functionality seems to already exist in .NET core as seen when you publish in single-file mode, except I want to do it at a later date to the existing dll without republishing – Elijah Jul 26 '22 at 02:16
-
You can do this when you compile the EXE rather than at the point you compile the DLL – Charlieface Jul 26 '22 at 02:22
-
@Charlieface Do you know of an example? – Elijah Jul 26 '22 at 02:28
-
I just linked you one, not sure what is difficult to understand. It works the same way on .NET Core as on Framework: you add the DLLs as resources in your EXE project, then attach a `AssemblyResolve` handler to read the DLL from the resource and load it. – Charlieface Jul 26 '22 at 02:30
2 Answers
1
You can simply use Fody.Costura
to automate the process of adding dependencies into your .exe as Resources, and adding the AssemblyResolve
stub.
Simply run
PM> Install-Package Fody
PM> Install-Package Costura.Fody
On your executable project.

Aron
- 15,464
- 3
- 31
- 64
-1
So what you actually want is to publish as a single exe, but exclude appsettings.json from the bundle?
<ItemGroup>
<None Include="appsettings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<ExcludeFromSingleFile>true</ExcludeFromSingleFile>
</None>
</ItemGroup>

Jeremy Lakeman
- 9,515
- 25
- 29
-
where did that come from? I want to publish as just DLLs, and then link those into EXEs at a later time – Elijah Jul 26 '22 at 18:11
-
If you want a single executable, then you do want to use the documented process to publish a single exe. Keeping the config out of the exe is not hard and shouldn't be a reason to attempt any other weird packaging method. – Jeremy Lakeman Jul 27 '22 at 01:07
-
I don't want the config out of the exe. I have other reasons for wanting to do it afterward – Elijah Jul 27 '22 at 19:40