views:

26

answers:

1

I'm using C# and VS2010. I have a dll that I reference in my project (as a dll reference not a project reference). That dll (a.dll) references another dll that my project doesn't directly use, let's call it b.dll. None of these are in the GAC.

My project compiles fine, but when I run it from Visual Studio, I get an exception that b.dll can't be found. It's not being copied to the bin directory when my project is compiled.

What is the best way to get b.dll into the bin directory so that it can be found at run time. I've thought of four options.

  1. Use a post compile step to copy b.dll to the bin directory
  2. Add b.dll to my project (as a file) and specify copy to output directory if newer
  3. Add b.dll as a dll reference to my project.
  4. Use ILMerge to combine b.dll with a.dll

I don't like 3 at all because it makes b.dll visible to my project, the other two seem like hacks. Am I missing other solutions? Which is the "right" way? Would a dependency injection framework be able to resolve and load b.dll?

+1  A: 

You can add the b.dll file to your project and set Copy to Output Directory.

If b.dll will already be (somewhere) on the end-users' computers, you can handle the AssemblyResolve event and load it yourself.

If you want to, you can compile it as an embedded resource in your project (Add the file, then set Build Action to Embedded Resource), then load it in an AssemblyResolve handler)

EDIT: Example:

static Assembly OnAssemblyResolve(object sender, ResolveEventArgs args) {
    if (args.Name == "b")
        return Assembly.Load(path);
}
SLaks
I would prefer that my project, which doesn't directly use b.dll, not have b.dll in it's project. b.dll is located in the same directory as a.dll is referenced from.Could you give a sample of using the AssemblyResolve event to deal with this?
automatic
Where is `b.dll` _on the end-user's machine_?
SLaks
On the end users machine I'll make sure it's in the same directory as the exe. My question is more about development. I have a ReferencedDlls directory that holds both a.dll and b.dll a.dll is referenced from this directory in my project. When I run/debug my project b.dll is not loaded because it's not in the /bin directory.
automatic
If the DLL is in the same directory as the EXE, you don't need an AssemblyResolve handler. You should probably put an AssemblyResolve handler inside `#if DEBUG` to load it from your directory when debugging.
SLaks