As you may know you cannot use ILMerge to merge assemblies with XAML (WPF application) however there is a little known trick you can use to achieve your goal in a bit different way.
- Add needed assemblies as embedded resources in your application
- Load needed assemblies from embedded resources in runtime
How to do this?
First you need to ensure that all referenced assemblies (except the framework itself) are added properly to the application resources on every application build. To do this you can use this simple MSBuild task (AfterResolveReferences) at the end of your project:
Funny how things happen...
Last night as I was about to leave work I downloaded ILMerge. I've been wanting to use it for forever (over 8 years... that's like forever!) and finally had a real business need to do so. So I downloaded it and planned on using it this morning.
Then I caught Vladimir's above article.
Now the app I need to merge is a command line app, but still I thought the resource approach cool, so I save the article for future reference.
I started down the ILMerge path and it wasn't going so well. Getting some weird errors building the EXE, etc. I head over to the ILMerge site and guess what I see?
A Great Alternative to using ILMerge
If you cannot use ILMerge because you are trying to merge WPF assemblies, then here is a great way for you to get the same effect, courtesy of Jeffrey Richter: embed the dlls you want to merge as resources and load them on demand! In many ways, this is a better technique than using ILMerge, so I strongly urge you to check it out, even if ILMerge is working for your scenario.
Cool! So the resource approach is not only not a hack but actually "good!" And while Jeff's article is good, I thought Vladimir's even better! Almost 100% cut-n-paste and done good!
Why almost? Remember I said I was using this approach with a command line app? Well you have to be careful there.
In short, don't do the resource assembly resource/event hookup in the same block of code where you use those assemblies. The JIT will get you.
So in the Main, do the assembly stuff as shown by Vladimir's and move your code into another routine (so your code isn't JIT'd at the same time, same block.
Once you do that, you should be good. It's worked great for me today helping me greatly streamline the deployment of one of my app's. Talk about simple xcopy deployment! :)
Related Past Post XRef:
ILMerge get's a rev...
Have a bunch of referenced DLL’s cluttering up your deployment? Just say no and ILMerge them…
Automate ILMerge'ing - Using Project Attributes to mark an Assembly for merging and then MSBuild to ILMerge them...
Gilma - GUI for ILMerge Branched for .Net 2.0
VS2005 Power Toys Pack Installer
MSBuild and ILMerge
"Gilma - GUI for ILMerge Application"
The Code Project - Merging .NET assemblies using ILMerge - .NET