Embed referenced assemblies, because ILMerge won't work with WPF applications
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
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.
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
Originally written by Daniel Chambers, see Combining multiple assemblies into a single EXE for a WPF application