automatic
automatic

Reputation: 2737

Satisfying indirect references at runtime

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?

Upvotes: 4

Views: 1231

Answers (1)

SLaks
SLaks

Reputation: 887453

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);
}

Upvotes: 1

Related Questions