Search code examples
c#wpfdllmergefluent

Merge C# DLL's into .EXE


I know there are many reply to this topic, but the example code which I found on this replys dont work for each .dll

I used this example.

public App()
    {
        AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(ResolveAssembly);
    }

    static Assembly ResolveAssembly(object sender, ResolveEventArgs args)
    {
        //We dont' care about System Assemblies and so on...
        if (!args.Name.ToLower().StartsWith("wpfcontrol")) 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;
    }

When i created a little program with only one window and a butten it had works, but with my "big" dll it hadnt work. The settings on the "big" dll are the same like that from my little programm.

I cant imagine why it does work sometimes and sometimes it doesnt work. I tested it with the ICSharp.AvalonEdit.dll too, unsuccessful..

can anyone imagine where the error is?

Edit 1

When i start my program it says that it cant found my dll.

Edit 2

I think i got the core of my issue. If one of the dll's, that i want to merge contains a reference to an other dll, than i become this FileNotFoundException exception. Do somebody know how to load/add also the internal required dll's

Edit 3

When i using the Code from Jiří Polášek it works for some. My Fluent shows the error "Please, attach a ResourceDictionary with styles. But i had done this already in my App.xaml

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="pack://application:,,,/Fluent;Component/Themes/Office2010/Silver.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources> 

Solution

  • If your referenced assembly requires other assemblies, you have to include them with your application as well - either in application folder or as embedded resource. You can determine referenced assemblies using Visual Studio, IL Spy, dotPeek, or write you own tool using method Assembly.GetReferencedAssemblies.

    It's also possible that AssemblyResolve event is fired before you attach ResolveAssembly handler to it. Attaching handler should be the first thing you do in your Main method. In WPF you have to create new class with new Main method a set it as Startup object in project settings.

    public class Program
    {
        [STAThreadAttribute]
        public static void Main()
        {
            AppDomain.CurrentDomain.AssemblyResolve
                += new ResolveEventHandler(ResolveAssembly);
            WpfApplication1.App app = new WpfApplication1.App();
            app.InitializeComponent();
            app.Run();
        }
    
        public static Assembly ResolveAssembly(object sender, ResolveEventArgs args)
        {   
          // check condition on the top of your original implementation
        }
    }
    

    You should also check guard condition on the top of ResolveAssembly to not exclude any referenced assembly.