Runtime assembly binding redirect

211 Views Asked by At

I have a pluginA for application, pluginA executes pluginB, pluginB is referencing library and execute some method from it.

First, i am loading library with Assembly.Load(byte[]), then loading pluginB with Assembly.Load(byte[]), store assembly in memory, then creating startup class from stored assembly with Assembly.CreateInstance() and execute some startup method.

Everything fine until i am trying to update library at runtime, i am loading new version of library with Assembly.Load(byte[]), the new version of library is loaded, a new instance of pluginB startup class is created, but on execute it is using old version of library.

I tried to use AppDomain.CurrentDomain.AssemblyResolve event but after the first load of library it is never fired again.

I added

<GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects> 

to pluginA and pluginB projects, also i set SpecificVersion property to false, in those projects.

But this didn't work.

What can i do to force pluginB to use a new version of library?

1

There are 1 best solutions below

0
On BEST ANSWER

After some research, i ended up with this approach

There is a library (https://github.com/0xd4d/dnlib) that lets you edit metadata of assembly and write a new assembly with edited metadata at runtime (and much more).

After loading pluginB, when I need to update library, I am loading a new version of library with Assembly.Load(bytes[]) get its new version, after that with dnlib I change assemblyRef of pluginB to reference to a new version of library, and writing new pluginB assembly to bytes array, then reload it with Assembly.Load(bytes[]), after that pluginB will be using a new version of library.