12

We're using Microsoft's Unity framework for dependency injection in some new class libraries. The main app is using some of the other enterprise libraries (Common and Logging) which are expecting Unity v2.0.414 but we implemented our libraries using Unity v2.1.515.

To get around the version differences I created an app.Config for the main app and placed an bindingRedirect entry into the config file and that has been working great. However we just learned that the app to this point has never used an app.config and mgmt prefers it this way.

So is it possible to programmatically implement an assembly redirect (i.e. in code)? Thanks!

Freddy V
  • 364
  • 3
  • 11

1 Answers1

17

Did you try with the AppDomain.AssemblyResolve event?

This post may help you as well How to use Assembly Binding Redirection to ignore revision and build numbers

public void Load(string assembly)
{
    AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(Method);
    Assembly assembly = Assembly.LoadFrom(assemblyFile);
    // ...
}
Community
  • 1
  • 1
Bidou
  • 7,378
  • 9
  • 47
  • 70
  • This worked great, thanks! I check the args.Name to see if contains "microsoft.practices.unity" and if it does then I return typeof(UnityContainer).Assembly otherwise I return null - thanks again for the help! – Freddy V Jun 05 '13 at 16:54
  • This is the way to bind assemblies for C# cmdlets. They don't run as an exe so they don't use their own app.config. If you need to redirect a binding you have to add the above event in the BeginProcessing() override. – Tom Padilla Jul 17 '20 at 13:52