ILMerge is a tool for merging assemblies together, which is very useful for easy deployments. Instead of deploying 10 dlls you can deploy one. When distributing applications out to customers you really want to make the application as simple as possible, and the most simple application is one that consist of only one exe.
ILMerge in .NET 4
I had some problems using ILMerge with .NET 4 dlls, but managed to get around my problems with some extra parameters. Here's how you merge dlls into a single DLL using ILMerge.
ilmerge.exe /lib:"C:\Windows\Microsoft.NET\Framework\v4.0.30319" /lib:"C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\PublicAssemblies" /t:dll /targetplatform:v4,C:\Windows\Microsoft.NET\Framework\v4.0.30319 /out:MyApplication.merged.dll MyApplication.dll ReferenceAssembly1.dll ReferenceAssembly2.dll ReferenceAssembly3.dll
It's easy to merge dlls into an exe, just by changing target type. Use your original exe as the first argument after option parameters.
ilmerge.exe /lib:"C:\Windows\Microsoft.NET\Framework\v4.0.30319" /lib:"C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\PublicAssemblies" /t:exe /targetplatform:v4,C:\Windows\Microsoft.NET\Framework\v4.0.30319 /out:MyApplication.merged.exe MyApplication.exe ReferenceAssembly1.dll ReferenceAssembly2.dll ReferenceAssembly3.dll
In your build process
You can easily apply this in your build process.
- Copy ilmerge.exe to somewhere in your project path. I placed mine in a folder called External Tools.
- Right click your main project in Visual Studio and select Properties / Build Events.

- I use the following to merge assemblies for TogglChart into one dll.
"$(SolutionDir)External Tools\ilmerge" /lib:"C:\Windows\Microsoft.NET\Framework\v4.0.30319" /lib:"D:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\PublicAssemblies" /t:dll /targetplatform:v4,C:\Windows\Microsoft.NET\Framework\v4.0.30319 /out:"$(TargetDir)$(SolutionName).merged.dll" "$(TargetDir)TogglChart.Lib.dll" "$(TargetDir)Newtonsoft.Json.dll" "$(TargetDir)ZedGraph.dll"

3 Comments
Pablo Bozzi said
Hello I'm trying to put all together with ILMerge and Fluent NHibernate. After created a Persistence Layer I ran the command line code above to create a DataAccessMerged.dll so when I create each new application in VS2010 (.NET 4) I just add the reference to this dll and it's just use my DAO's. ILMerge Command-Line used: ILMerge.exe /lib:C:\Windows\Microsoft.NET\Framework\v4.0.30319 /lib:"C:\Program Files\Microsoft Visual Studio 10.0\Common7\IDE\PublicAssemblies" /t:dll /closed /targetplatform:v4,"C:\Program Files\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0" /out:DataAccessMerged.dll DataAccess.dll Antlr3.Runtime.dll Castle.Core.dll Castle.DynamicProxy2.dll FluentNHibernate.dll Iesi.Collections.dll log4net.dll NHibernate.ByteCode.Castle.dll NHibernate.dll The problem is that I'm getting the following error: ------------------------ Message: An invalid or incomplete configuration was used while creating a SessionFactory. Check PotentialReasons collection, and InnerException for more detail. Inner Exception: Unable to load type 'NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle' during configuration of proxy factory class. Possible causes are: - The NHibernate.Bytecode provider assembly was not deployed. - The typeName used to initialize the 'proxyfactory.factory_class' property of the session-factory section is not well formed. Solution: Confirm that your deployment folder contains one of the following assemblies: NHibernate.ByteCode.LinFu.dll NHibernate.ByteCode.Castle.dll -------------------------- And If I add the reference of my DataAccess.dll and the DLL of Fluent NHibernate on a new project everything works fine. Could you help me with this? Thank's! Pablo Bozzi.
Deploy Orchard CMS to Azure « Mint said
[...] will decrease startup time, but you can trim it even more if you ILmerge the modules prior to deploy. Make it a part of your automated build/deploy script and get a lot of startup [...]
Asghar Panahy said
Hi, Thanks for this great article. It took me an hour to find your notes and finally got my answers. Great job