Including a DLL as an embedded resource in a WPF project

I follow http://blogs.msdn.com/b/microsoft_press/archive/2010/02/03/jeffrey-richter-excerpt-2-from-clr-via-c-third-edition.aspx

I added WPFToolkit.Extended.dll to my solution and installed its Build Embedded Resource action.

In App.OnStartup (StartupEventArgs e) I have the following code:

AppDomain.CurrentDomain.AssemblyResolve += (sender, args) => { String resourceName = "AssemblyLoadingAndReflection." + new AssemblyName(args.Name).Name + ".dll"; String assemblyName = Assembly.GetExecutingAssembly().FullName; Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName); using (stream) { Byte[] assemblyData = new Byte[stream.Length]; stream.Read(assemblyData, 0, assemblyData.Length); return Assembly.Load(assemblyData); } }; 

The debugger hits this block of code twice.

First time:

 resourceName is "AssemblyLoadingAndReflection.StatusUtil.resources.dll" assemblyName is "StatusUtil, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" stream is null 

Second time:

 resourceName is "AssemblyLoadingAndReflection.WPFToolkit.Extended.resources.dll" assemblyName is "StatusUtil, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" stream is null 

The code throws an exception when it hits stream.Length, because it is null.

I cannot use ILMerge because it is a WPF project.

+4
source share
5 answers

You must change the string "AssemblyLoadingAndReflection" to the name of your application assembly.

One thing you can do to make this code more general is by using some more reflection:

 Assembly.GetExecutingAssembly().FullName.Split(',').First() 

Remember to add a point. This, of course, will not work if the DLL is not in the application build resources.

+6
source

The answer from HB is actually not quite right. We need a prefix not of the assembly name, but the default namespace for the project. This probably cannot be done completely reliably, but the following code will be much more reliable than assuming it is the same as the assembly name.

 Assembly thisAssembly = Assembly.GetEntryAssembly(); String resourceName = string.Format("{0}.{1}.dll", thisAssembly.EntryPoint.DeclaringType.Namespace, new AssemblyName(args.Name).Name); 
+2
source

Nathan Philip Answer worked like a charm for me ..

This is the whole method that worked for me (and it works for several DLLs as well)

 public MainWindow() { InitializeComponent(); AppDomain.CurrentDomain.AssemblyResolve += (sender, args) => { Assembly thisAssembly = Assembly.GetEntryAssembly(); String resourceName = string.Format("{0}.{1}.dll", thisAssembly.EntryPoint.DeclaringType.Namespace, new AssemblyName(args.Name).Name); using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName)) { Byte[] assemblyData = new Byte[stream.Length]; stream.Read(assemblyData, 0, assemblyData.Length); return Assembly.Load(assemblyData); } }; } 
+2
source

I tried all of the above answers and they did not work for me. I found this post and it worked like a charm.

Message: http://www.digitallycreated.net/Blog/61/combining-multiple-assemblies-into-a-single-exe-for-a-wpf-application

I found that the .csproj file also needs to be edited. The post explains how to do all this.

0
source

I have a full explanation of how to dynamically load inline assemblies into a StackOverflow VB.NET inline DLL question in another DLL as an inline resource?

0
source

All Articles