Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to programmatically perform an assembly redirect

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!

like image 975
Freddy V Avatar asked Jun 04 '13 21:06

Freddy V


People also ask

How to add assembly redirect?

To redirect one assembly version to another, use the <bindingRedirect> element. The oldVersion attribute can specify a single assembly version or a range of versions. The newVersion attribute should specify a single version. For example, <bindingRedirect oldVersion="1.1.

What is auto generate binding redirects?

Binding redirects are added if your app or its components reference more than one version of the same assembly, even if you manually specify binding redirects in the configuration file for your app. The automatic binding redirection feature affects desktop apps that target . NET Framework 4.5. 1 or a later version.


1 Answers

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);
    // ...
}
like image 111
Bidou Avatar answered Sep 27 '22 19:09

Bidou