At work, I am currently working on a small WPF app that we want to keep as a simple standalone exe. However, it has dependencies on several third-party assemblies: SharpZipLib, Unity, ServiceLocator, etc.
Microsoft has handy tool called ILMerge that merges multiple .NET assemblies into a single dll or exe. Unfortunately, it doesn’t support WPF applications, because of the way XAML is compiled.
Instead, you can use another approach — include all your referenced third-party assemblies as embedded resources of the exe:
public partial class App : Application
{
private void OnStartup(object sender, StartupEventArgs e)
{
AppDomain.CurrentDomain.AssemblyResolve +=
new ResolveEventHandler(ResolveAssembly);
// proceed starting app...
}
static Assembly ResolveAssembly(object sender, ResolveEventArgs args)
{
Assembly parentAssembly = Assembly.GetExecutingAssembly();
var name = args.Name.Substring(0, args.Name.IndexOf(',')) + ".dll";
var resourceName = parentAssembly.GetManifestResourceNames()
.First(s => s.EndsWith(name));
using (Stream stream = parentAssembly.GetManifestResourceStream(resourceName))
{
byte[] block = new byte[stream.Length];
stream.Read(block, 0, block.Length);
return Assembly.Load(block);
}
}
}
Whenever .NET can’t find a referenced assembly, it will call our code and we can provide an Assembly instance ourselves. Note this code expects a sane DLL-naming convention :)
May 14th, 2009 | 2 Comments


