Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to merge multiple assemblies into one?

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?

like image 611
Arun Rana Avatar asked Nov 10 '11 09:11

Arun Rana


People also ask

Can you combine DLL files?

Yes, it is impossible to merge dll files; there is no tool to do it and it cannot be done manually either. You must modify the source code.

How do I insert a DLL into another DLL?

Add DLL As Embedded Resource First, add the DLL as Reference. Then, add the same DLL as file into the project. Right click the project's name > Add > Existing Item... The same DLL will exist twice in different folder in the project.


2 Answers

You have several options:

  • use ILMerge (free)
    For howto see here and here

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...
like image 58
Yahia Avatar answered Sep 25 '22 21:09

Yahia


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)
like image 32
CodeFox Avatar answered Sep 21 '22 21:09

CodeFox