Search code examples
c#.netdllmergeilmerge

.NET assembly merge troubleshooting ilmerge


searching for a tool to merge .NET assemblies I always endup in trying ILMerge. But I don't get it working. Now I tried just a very simple example that doesn't work.

My library containing only this interface:

namespace CommonLib
{
    public interface IPlugin
    {
        string Name { get; }
        string Version { get; }
        void Load();
    }
}

My console application contains the class:

using System;
using CommonLib;

namespace ILMergeTest
{
    internal class DirectTestPlugin : IPlugin
    {
        public string Name => "DirectTestPlugin";
        public string Version => "4711.0.8.15";
        public void Load()
        {
            Console.WriteLine("Loading Plugin: " + Name + " [Version " + Version + "]");
        }
    }
}

and the Program.cs

using System;

namespace ILMergeTest
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            var plugin = new DirectTestPlugin();
            plugin.Load();
            Console.ReadKey();
        }
    }
}

After compiling the application everything works fine.

Now I copy ILMerge.exe to the output directory, where my binaries are and create the following batch to perform ILMerge

mkdir Merged

ilmerge /union /ndebug /t:exe /copyattrs /closed /out:Merged\MergedProgram.exe /targetplatform:v4,"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6.1" "ILMergeTest.exe" "CommonLib.dll"
pause

My projects target Framework is .NET 4.6.1.

I always get the following error:

ILMerge.Merge: The assembly 'CommonLib' was not merged in correctly. It is still listed as an external reference in the target assembly.
bei ILMerging.ILMerge.Merge() bei ILMerging.ILMerge.Main(String[] args)

How do I fix this? The documentation of ILMerge didn't help me at all.

Thanks


Solution

  • Now I got my fault! Using the /union causes this error in my case.

    Now I use the following commandline and everything works as expected:

    mkdir Merged
    ilmerge /ndebug /t:exe /copyattrs /closed /out:Merged\MergedProgram.exe /targetplatform:v4,"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6.1" "ILMergeTest.exe" "CommonLib.dll"
    pause