Search code examples
asp.netassembly-resolution

Using a 32bit- or 64bit specific dll depending on the process bitness


I need to reference a DLL which is available in 2 versions (one for 32bit and one for 64bit). My goal is to build an web application that works on both 32 and 64 bit systems.

I thought about referencing the 32bit assembly by default and using the AssemblyResolve event to load the 64bit version (if loading the 32bit version failed):

static void Main(string[] args)
{
    AppDomain.CurrentDomain.AssemblyResolve += _AssemblyResolve;
    // Try LoadAssembly ...
}

static System.Reflection.Assembly _AssemblyResolve(object sender, ResolveEventArgs args)
{
    var path = string.Format(@"...\lib_x64\{0}.dll", args.Name);
    return Assembly.LoadFrom(path);
}

But even when a BadImageFormatException occurs, the _AssemblyResolve handler will not be called. Is there any other way to achieve the proposed behavior?


Solution

  • See answers for dealing with this for System.Data.SQLite.

    I think your proposed method should work but you need to move the 32-bit version so it can't be found by default, so _AssemblyResolve is always called for that dll. That's just a guess.