Merging .NET assemblies using ILMerge

Today I had a problem about multiple dll registration plugin for MSCRM. This plugin was registered on Database and I could not update this one because I needed the others libraries. I had two soltions: First, I could register it on GAC. I don’t like because I had problem on production during the installation of the new solution. The second one was to use ILMERGE to merge all dll and create an unique dll to register. I choose the second solution. ILMerge is useful tool on Microsoft. This little gem makes it possible to link multiple assemblies to a single one.

First, you would compile your libraries to DLLs and your program to an EXE referencing the DLLs. This is exactly what Visual Studio would do if you had multiple libraries and a program referencing these libraries, so there is no need to do this on the command line.

Later you can link these parts to a single self-contained EXE, DLL, using ILMerge:

echo %1
%1Build\ILMerge.exe /keyfile:%1Properties\keys.snk /target:library /copyattrs /targetplatform:v4,"C:\Windows\Microsoft.NET\Framework\v4.0.30319" /out:%1Build\final\namedll %1Build\library.dll %1Build\Castle.Core.dll %1Build\Castle.Windsor.dll %1Build\Microsoft.WITDataStore.dll %1Build\Newtonsoft.Json.dll %1Build\Microsoft.TeamFoundation.WorkItemTracking.Client.DataStoreLoader.dll

The nice thing about this is that you can also merge third party assemblies like commercial class libraries into your program. And you do not have to modify your build process.

I found ILMerge tremendously useful, and I think that something like this should be a part of the .NET framework SDK. Maybe just enhance al.exe so that it can also link DLLs.

I have only scratched the surface of the .NET build process and the capabilities of ILMerge, and this article might contain many inaccuracies or even errors. But I found ilmerge.exe so useful that I just had to write about it.

I found a GUID for MERGE,

image_thumb_681250B1

but I prefer the line command. I insert on build process this rows:

copy /Y "$(TargetDir)" "$(ProjectDir)Build"
call "$(ProjectDir)Build\Merge.bat" $(ProjectDir)
cd "$(ProjectDir)Build"
del *.dll *.pdb Winvs*

In this way, after the build, all dlls are copied on Build folder and the Merge command is called. The merge.bat is the following:

echo %1
%1Build\ILMerge.exe /keyfile:%1Properties\keys.snk /target:library /copyattrs /targetplatform:v4,"C:\Windows\Microsoft.NET\Framework\v4.0.30319" /out:%1Build\final\namedll %1Build\library.dll %1Build\Castle.Core.dll %1Build\Castle.Windsor.dll %1Build\Microsoft.WITDataStore.dll %1Build\Newtonsoft.Json.dll %1Build\Microsoft.TeamFoundation.WorkItemTracking.Client.DataStoreLoader.dll

Lascia un commento

Inserisci i tuoi dati qui sotto o clicca su un'icona per effettuare l'accesso:

Logo WordPress.com

Stai commentando usando il tuo account WordPress.com. Chiudi sessione / Modifica )

Foto Twitter

Stai commentando usando il tuo account Twitter. Chiudi sessione / Modifica )

Foto di Facebook

Stai commentando usando il tuo account Facebook. Chiudi sessione / Modifica )

Google+ photo

Stai commentando usando il tuo account Google+. Chiudi sessione / Modifica )

Connessione a %s...