Merging two dll files and it's use in Tridion

Posted on December 31, 2014 at 11:32 PM

Need for merging single-file assemblies in Tridion is not that rare. I came across two scenarios where this was useful:

  1. When referencing third party dll in C# assembly template building block
  2. When referencing third party dll in tridion event system

Why would you choose merging dll over simply registering assembly in GAC or even loading the DLL file dynamically from your own code?

Of course the answer is obvious to the second question, dynamically loading from your code is too complicated and not worth it. However installing third party dll to GAC seems the most convenient. After all GAC is part of a solution to manage exactly such issues. But general guidelines for GAC say:

“Scrum (n): A framework within which people can address complex adaptive problems, while productively and creatively delivering products of the highest possible value “

Also when introducing third party assemblies in Tridion projects consider the clients rules. Most of the clients are uncomfortable when adding third party assemblies to the GAC or even to the system it self, sometimes they have strict rules for this. Make sure you read the licence agreement of the third party dll so you do not break any copyright laws by merging your code with someone else's.

Lets go back to the topic of this blog. How can I merge two dll files? Use ILMerge. There are two options how to use ILMerge:

  1. Execute ILMerge command manually from Command Prompt.
  2. Automate the process in Visual Studio post build event.

As far as the first choice I would rather use ILMergeGui than do this manually. Second choice will be explained in this post.

Create ILMerge.bat file:

COPY YourMain.dll temp.dll
"C:\Program Files (x86)\Microsoft\ILMerge\ILMerge.exe" /out:YourOutput.dll YourMain.dll YourSecondary.dll
DEL temp.dll

This file executes ILMerge command on dll files that you choose.

Copy the file in your projects path and add it to your project. When you add the file select it to get the File Properties in Visual Studio. Set "Copy to Output Directory" field to "Copy Always". You still need to add following code to "Post-build event command line" (Project Properties/Build Events):

"$(TargetDir)ILMerge.bat" "$(TargetDir)"

This command executes ILMerge.bat file after successful build.

Now you are ready to build your project and automatically merge the files in bin/Debug folder of your project.

Site search