Once an application has started is there a way to create a binding redirect that will apply to all future assembly loads?
Asked
Active
Viewed 2,499 times
11
-
3Why do you need this? What are you trying to achieve? – Darin Dimitrov Apr 13 '11 at 08:21
-
What do you mean by all future loads? – František Žiačik Apr 13 '11 at 08:32
-
@darin. so is that a "i dont know"? it is a fairly convoluted case and not really worth explaining here. – Simon Apr 13 '11 at 08:35
-
@František assemblies load on domand. So I want to control binding for when assemblies are loaded during the life of the application – Simon Apr 13 '11 at 08:39
-
@Simon I am just asking for clarification on what you are trying to achieve. That's all. – Darin Dimitrov Apr 13 '11 at 08:41
-
@darin i am trying to get around an obscure file lock issue to do with a VS addin. forcing a binding to fail may give me a back door to workaround the issue. this is a greatly summarised description :) – Simon Apr 13 '11 at 08:44
-
1@Simon: is it something you could use `AppDomain.AssemblyResolve` event for? – František Žiačik Apr 13 '11 at 08:51
-
@František AppDomain.AssemblyResolve only fires for an assembly failure. so no it does not help – Simon Apr 13 '11 at 09:52
-
@Simon, I've got this exact same issue now. Did you ever find a solution? – Cory Nelson Oct 21 '13 at 01:40
-
@CoryNelson no. I marked Gaels answer as correct because it seems like the correct way to go. but I was not able to achieve this – Simon Oct 21 '13 at 06:49
2 Answers
12
Sorry for responding to an old post, but this blog has a much better answer to the question. Hope someone finds it useful.
My use case: doing a binding redirect from a COM interop assembly invoked by a classic ASP application.
http://blog.slaks.net/2013-12-25/redirecting-assembly-loads-at-runtime/
This function from the post in question will do what you want:
public static void RedirectAssembly(string shortName, Version targetVersion, string publicKeyToken) {
ResolveEventHandler handler = null;
handler = (sender, args) => {
// Use latest strong name & version when trying to load SDK assemblies
var requestedAssembly = new AssemblyName(args.Name);
if (requestedAssembly.Name != shortName)
return null;
Debug.WriteLine("Redirecting assembly load of " + args.Name
+ ",\tloaded by " + (args.RequestingAssembly == null ? "(unknown)" : args.RequestingAssembly.FullName));
requestedAssembly.Version = targetVersion;
requestedAssembly.SetPublicKeyToken(new AssemblyName("x, PublicKeyToken=" + publicKeyToken).GetPublicKeyToken());
requestedAssembly.CultureInfo = CultureInfo.InvariantCulture;
AppDomain.CurrentDomain.AssemblyResolve -= handler;
return Assembly.Load(requestedAssembly);
};
AppDomain.CurrentDomain.AssemblyResolve += handler;
}

Bryan Slatner
- 1,588
- 2
- 16
- 19
2
It could be possible using ICLRHostBindingPolicyManager::ModifyApplicationPolicy, but I've never tried myself. Note that this is a CLR-level interface, so you can't load policies for individual AppDomains (which is why it is not used by PostSharp yet).

Gael Fraiteur
- 6,759
- 2
- 24
- 31