1

Background : Merging dlls into a single .exe with wpf

How shall i merge a .dll reference into the .exe file, i read the above post, got principle behind it, but i am not able to figure out how to do it?( i am newbie, sorry) The reference file is HtmlagilityPack.dll

Currently my App.xaml.cs contains :

public partial class App : Application
    {
       public App(){
            AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(ResolveAssembly);

            // proceed starting app...
        }

        static Assembly ResolveAssembly(object sender, ResolveEventArgs args)
        {
            //We dont' care about System Assembies and so on...
            if (!args.Name.ToLower().StartsWith("Html")) return null;

            Assembly thisAssembly = Assembly.GetExecutingAssembly();

            //Get the Name of the AssemblyFile
            var name = args.Name.Substring(0, args.Name.IndexOf(',')) + ".dll";

            //Load form Embedded Resources - This Function is not called if the Assembly is in the Application Folder
            var resources = thisAssembly.GetManifestResourceNames().Where(s => s.EndsWith(name));
            if (resources.Count() > 0)
            {
                var resourceName = resources.First();
                using (Stream stream = thisAssembly.GetManifestResourceStream(resourceName))
                {
                    if (stream == null) return null;
                    var block = new byte[stream.Length];
                    stream.Read(block, 0, block.Length);
                    return Assembly.Load(block);
                }
            }
            return null;
        }
    }

Where else am i supposed to make changes?, i have being trying past an hour with an example of http://blog.mahop.net/post/Merge-WPF-Assemblies.aspx But not able to figure out how to do it with HtmlAgilityPack.

Community
  • 1
  • 1
TarunG
  • 602
  • 5
  • 21
  • what is the error? what's happening in your app? – Glenn Ferrie Jul 06 '11 at 10:09
  • the app compiles, but the dll file is not included in the exe, when i try to run the .exe as a standalone(deleting HtmlAgilityPack.dll from the folder), it quits stating some error.i have tried setting up break-points in above code and my program never enters the above wriiten code. Why? – TarunG Jul 06 '11 at 10:13
  • I think you should debug the initial load of this application more closely and see what other assemblies the AssemblyResolve event is looking for. This line is most likely the key to your problems: if (!args.Name.ToLower().StartsWith("Html")) return null; -- Upon loading htmlAgilitypack, you may need to load additional dependencies. You shouldnt simply return null, any execution of "AssemblyResolve" means the current AppDomain is looking for a reference. – Glenn Ferrie Jul 06 '11 at 10:21
  • But the code never enters the ResolveAssembly Block!!. Shall I add something in App.xaml also?? – TarunG Jul 06 '11 at 10:24
  • Got it. Can you proactively load the HtmlAgilityPack.dll assembly? Instead of deferring the load into a "resolve" handler? The only way I know to get that event to fire is to reference the 'HtmlAgilityPack.dll' at compile-time, build, and then delete the DLL. – Glenn Ferrie Jul 06 '11 at 10:32
  • okay, so you mean to say that i shall hard-code it into the project and then delete it after compilation.? – TarunG Jul 06 '11 at 10:43
  • I guess I dont understand what you're using the agility pack for exactly. If it was referenced in your code then it would force the resolve. and if you just want to load it programmatically and access it using Reflection then there is no need to introduce the "AssemblyResolve" event to this equation. – Glenn Ferrie Jul 06 '11 at 10:49
  • Try putting the AssemblyResolve EventHandler assignment in a static constructor instead of the normal constructor: public static App(){...}? Put a breakpoint on it to see if it is called? – Louis Somers Jul 06 '11 at 10:56
  • To force it, i set the "Local Copy" property of HtmlAgilityPack to false, Now it enters the Code but get thrown out at resources.count>0, returning null only. @Louis: trying that one currently. Can anybody post their sample project on mediafire on something so so that i am able to take a look. Or better post some STeps that one should follow while migrating your standard project to a one like this. – TarunG Jul 06 '11 at 11:10
  • When it returns back due to if(resources.count>0) it returns an exception "File not Found".o.O? – TarunG Jul 06 '11 at 11:17
  • @Louis: Access modifiers not allowed on static. – TarunG Jul 06 '11 at 11:31

2 Answers2

1

Okay, finally had to use the SmartAssembly program. But still looking for a solution to do it by code.

TarunG
  • 602
  • 5
  • 21
  • I'd also like to see a working example. I guess the problem lies in the way you retrieve the resource. – Louis Somers Jul 06 '11 at 12:12
  • Here is a working example : http://blog.mahop.net/post/Merge-WPF-Assemblies.aspx , but sadly wasnt of much help to me. – TarunG Jul 06 '11 at 12:17
0

Your code looks slightly off, it should look more like this:

public class App : Application
{
    [STAThreadAttribute()]
    public static void Main()
    {
        AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(ResolveAssembly);
        // etc...
    }

    // etc...

You then also need to change the "Startup object" setting in the properties page of your project to use the App class (i.e. the above code) - you should then see the Main method of this class being the first code executed when you start debugging.

Justin
  • 84,773
  • 49
  • 224
  • 367