How to merge multiple assemblies into one?
Asked Answered
G

7

96

I consuming my service stack using EXE project (startup task for azure application) in that I have copied following service stack's DLL & some Azure's DLLs in to EXE project.

dlls

When I build this EXE project then Azure DLLs will be bundled with my EXE but service stack's DLL will not be bundled with EXE, because to run my EXE on any machine I need to copy all service stack's DLL manually.

I have used this service stack's dll to use

JsonServiceClient client = new JsonServiceClient(servicepath);

What should I have to do to bundled all these DLLs in to my EXE?

Guria answered 10/11, 2011 at 9:43 Comment(0)
A
135

You have several options:

OR

  • use some tool like SmartAssembly (commercial)
    it can embed and merge among other things (no need to change your source code)

OR

  • code that yourself in less than 10 lines (free but minimal source code change)
    mark all needed dependencies as "embedded resource" - this way they are included in the EXE file... you need to setup an AssemblyResolve handler which at runtime reads from Resources and returns the needed DLLs to the .NET runtime...
Apostrophe answered 10/11, 2011 at 12:4 Comment(2)
Thanks for reminding me of Jeffrey Richter's solution of embedding assemblies! See my answer mentioning Costura for a tool that combines this with injecting the necessary code.Insalivate
Going ahead with .NET 5.0, ILRepack is the replacement of ILMerge.Zenger
I
31

A great tool to include referenced assemblies as embedded resources is Costura (a Fody add-in). The author Simon Kropp describes it as follows:

[...] a combination of two methods:

The result is a super simple solution which merely requires to fetch Costura.Fody from NuGet.

Features:

  • Including debug symbols
  • Compression of embedded assemblies
  • Including/excluding specific assemblies
  • Others (see Readme)
Insalivate answered 23/9, 2015 at 4:56 Comment(6)
OMG Costura.Fody is the right answer. Installed the package from nuget, rebuilt the project and now I have a standalone EXE AWESOME!!Onder
I can't believe how easy it is. Just install packages from nuget and your are done.Milieu
I'm using Costura.Fody as well. It's trivial to setup and dead easy to include NuGet packages as resources. Get project.Interpellation
Is it possible to not lump all assemblies into the main executable but group them as DLLs such as Gui.dll assembly for all GUI related stuff etc?Eartha
@user3700562, this technique does not create new assemblies but rather embeds one or more referenced assemblies into an existing one.Insalivate
Great solution! I use it since a year and had no problem. But please note it only includes assemblies referenced by your top project. If your app reference another project which references a third party assembly you need to add it to your top level project.Irriguous
B
30

The tool you are looking for is called ILMerge . It is a command line tool and can be used like this:

ilmerge /target:winexe /out:MyApp.exe 
        MyExe.exe ServiceStack.dll ServiceStack.Interfaces.dll ServiceStack.ServiceInterface.dll  ServiceStack.Text.dll

There is also an article that describes how to include ILMerge into your VS project setup here

Brunet answered 10/11, 2011 at 11:41 Comment(2)
Hi, can I add external resource (file/image) into executable?Rattling
@EmdadulSawon maybe you can include them as resources in one of your libs?Shown
P
7

Try ILMerge-GUI, the .NET merger. It's a GUI based Ilmerge which avoids all command line work.

Psychosexual answered 26/6, 2015 at 12:48 Comment(1)
Codeplex is shutting down, so here is the moved project link: bitbucket.org/wvd-vegt/ilmergeguiRom
C
5

If you have WPF dependencies your options may be more limited..... ILMerge doesn't appear to deal with these. Costura.Fody (as mentioned by Codefox above) worked perfectly for us however and took about 5 minutes to get going... a very good experience.

Install with Nuget (selecting the correct default project in the Package Manager Console).

It merges the all DLLs marked "Copy Local" = true and produces a merged .EXE (alongside the standard output, most of which is now not necessary) which is also compressed. This can then be used standalone.

The license is MIT as so you can modify/distribute as required.

https://github.com/Fody/Costura/

Cauda answered 13/4, 2016 at 14:21 Comment(0)
W
5

.net core 3 introduces two new options in the project configuration, called single file publish and trimming.

You can find more details on docs here, project configuration copied here for reference.

  1. Project Configuration:
    <PropertyGroup>
      <RuntimeIdentifier>win10-x64</RuntimeIdentifier>
      <PublishSingleFile>true</PublishSingleFile>
    </PropertyGroup>

    <PropertyGroup>
      <PublishTrimmed>true</PublishTrimmed>
    </PropertyGroup>
  1. Using CLI:
    dotnet publish -r win10-x64 -p:PublishSingleFile=true
    dotnet publish -r <rid> -c Release

It is fully supported to combine the two options together to get a trimmed single assembly for your application.

Whoso answered 29/11, 2019 at 14:42 Comment(1)
This does not give you are single assembly. It simply compresses your files into a zip archive and then unpacks it to a user-folder when you try to run the application.Express
V
2

Checkout the ServiceStack.Gap project which shows several examples of howto ILMerge ServiceStack into a single cross-platform .exe.

ServiceStack also includes a number of other features that's particularly well suited for these creating embedded apps where it:

  • Allows your services to be self-hosted using .NET's HTTP Listener
  • Supports pre-compiled Razor Views
  • Supports Embedded Resources
  • Supports an embedded database in Sqlite and OrmLite
  • Can be ILMerged into a single .exe
Viscacha answered 26/6, 2015 at 13:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.