Skip to content

Instantly share code, notes, and snippets.

@thoemmi
Created September 14, 2012 19:58
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thoemmi/3724333 to your computer and use it in GitHub Desktop.
Save thoemmi/3724333 to your computer and use it in GitHub Desktop.
Embed referenced assemblies, because ILMerge won't work with WPF applications
public partial class App : Application {
public App() {
AppDomain.CurrentDomain.AssemblyResolve += OnResolveAssembly;
}
private static Assembly OnResolveAssembly(object sender, ResolveEventArgs args) {
var executingAssembly = Assembly.GetExecutingAssembly();
var assemblyName = new AssemblyName(args.Name);
var path = assemblyName.Name + ".dll";
if (assemblyName.CultureInfo.Equals(CultureInfo.InvariantCulture) == false) {
path = String.Format(@"{0}\{1}", assemblyName.CultureInfo, path);
}
using (var stream = executingAssembly.GetManifestResourceStream(path)) {
if (stream == null)
return null;
var assemblyRawBytes = new byte[stream.Length];
stream.Read(assemblyRawBytes, 0, assemblyRawBytes.Length);
return Assembly.Load(assemblyRawBytes);
}
}
}
<?xml version="1.0" encoding="utf-8" ?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<!--
Originally written by Daniel Chambers (http://www.digitallycreated.net)
http://www.digitallycreated.net/Blog/61/combining-multiple-assemblies-into-a-single-exe-for-a-wpf-application
-->
<Target Name="EmbedReferencedAssemblies" AfterTargets="ResolveAssemblyReferences">
<ItemGroup>
<!-- get list of assemblies marked as CopyToLocal -->
<AssembliesToEmbed Include="@(ReferenceCopyLocalPaths)" Condition="'%(Extension)' == '.dll'"/>
<!-- add these assemblies to the list of embedded resources -->
<EmbeddedResource Include="@(AssembliesToEmbed)">
<LogicalName>%(Filename)%(Extension)</LogicalName>
</EmbeddedResource>
<!-- no need to copy the assmblies locally anymore -->
<ReferenceCopyLocalPaths Remove="@(AssembliesToEmbed)"/>
</ItemGroup>
<Message Importance="high" Text="Embedding: @(AssembliesToEmbed->'%(Filename)%(Extension)', ', ')"/>
</Target>
</Project>
@gouranga
Copy link

gouranga commented Jul 7, 2013

Nice snippet. Just one suggestion, this block:

<!-- no need to copy the assmblies locally anymore -->
<ReferenceCopyLocalPaths Remove="@(AssembliesToEmbed)"/>

can be replaced by simply removing the _CopyFilesMarkedCopyLocal target. Like that:

  <Target Name="EmbedReferencedAssemblies" AfterTargets="ResolveAssemblyReferences">
    <ItemGroup>
      <!-- get list of assemblies marked as CopyToLocal -->
      <AssembliesToEmbed Include="@(ReferenceCopyLocalPaths)" Condition="'%(Extension)' == '.dll'" />
      <!-- add these assemblies to the list of embedded resources -->
      <EmbeddedResource Include="@(AssembliesToEmbed)">
        <LogicalName>%(Filename)%(Extension)</LogicalName>
      </EmbeddedResource>
    </ItemGroup>
    <Message Importance="high" Text="Embedding: @(AssembliesToEmbed->'%(Filename)%(Extension)', ', ')" />
  </Target>
  <!-- no need to copy the assemblies locally anymore -->
  <Target Name="_CopyFilesMarkedCopyLocal" />

This will also prevent from copying pdb and xml files.

@gouranga
Copy link

I edited EmbeddedResource action a bit. So it will properly handle localized dlls:

  <Target Name="EmbedReferencedAssemblies" AfterTargets="ResolveAssemblyReferences">
    <ItemGroup>
      <!-- get list of assemblies marked as CopyToLocal -->
      <AssembliesToEmbed Include="@(ReferenceCopyLocalPaths)" Condition="'%(Extension)' == '.dll'" />
      <!-- add these assemblies to the list of embedded resources -->
      <EmbeddedResource Include="@(AssembliesToEmbed)">
        <LogicalName>%(AssembliesToEmbed.DestinationSubDirectory)%(AssembliesToEmbed.Filename)%(AssembliesToEmbed.Extension)</LogicalName>
      </EmbeddedResource>
    </ItemGroup>
    <Message Importance="high" Text="Embedding: @(AssembliesToEmbed->'%(DestinationSubDirectory)%(Filename)%(Extension)', ', ')" />
  </Target>
  <!-- no need to copy the assemblies locally anymore -->
  <Target Name="_CopyFilesMarkedCopyLocal" />

You have all the necessary code in ResolveAssembly already, so it does not need any fixes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment