Kaleb Pederson
Kaleb Pederson

Reputation: 46479

Runtime swap out of assembly referenced statically at compile time

Under what conditions can I swap out an assembly that's statically referenced at compile time with a different one for use at runtime? For example:

App Assembly:

Common Assembly:

Service Assembly:

Given the above, I'd like to be able to swap out the Service assembly at install time but I don't know under what conditions the new Service assembly will continue to load.

I believe the following must hold:

  1. Same assembly name,
  2. Weakly named assembly or same version number in a strongly named assembly, and
  3. ServiceProviderFactory signature remains constant.

Note: it seems better to use reflection and search the service assembly for an implementation of IServiceProvider than to rely on some ServiceProviderFactory with a fixed signature being present but I'm still interested in the answer to the above.

Upvotes: 2

Views: 454

Answers (1)

Josh
Josh

Reputation: 69262

If it's strong-named, then you can't really use assembly redirection for anything other than version differences. However, you can use the AppDomain.AssemblyResolve event to supply an assembly in response to a failure to bind the requested assembly.

I actually tried this as a proof of concept when I tried to support .NET 2.0 assemblies that referenced a CodePlex build of MEF in a .NET 4 application that referenced the released build of MEF. Here's a link to my question that someone responded to with that evil trick.

However, this is all very tricky and I wouldn't feel comfortable doing it in a real application. Can you extract your interfaces into a separate assembly then use some kind of dependency injection such as MEF or Unity to dynamically load the types that implement those interfaces?

Upvotes: 1

Related Questions