Question

When trying to access assemblies from a C# assembly, there are several methods:

  1. Implement a AssemblyResolve event handler http://msdn.microsoft.com/en-us/library/system.appdomain.assemblyresolve(v=vs.110).aspx

  2. Use the property in the .config file to redirect assembly requests

As stated in http://msdn.microsoft.com/en-us/library/system.resolveeventargs.name(v=vs.110).aspx for the property of ResolveEventArgs of the event in (1): "Name is the assembly name before policy is applied."

However, I couldn't find any documentation about the order of these methods. Is the AssemblyResolve event handler called before the redirects are probed? Or the other way around?

Second, is there a possibility to somehow apply the policy to the ResolveEventArgs? For example to request the redirected version range from the .config file?

Was it helpful?

Solution

I see two questions here. Correct me if I'm no providing enough information.

  1. AssermblyResolve event is invoked only if loader doesn't manage to find assembly it is looking for. So, first assembly load locations are probed, and then if assembly is not found, AssermblyResolve event is invoked. If all assemblies are loaded correctly, AssemblyResolve event will not fire at all.

  2. It is possible to load assembly manually to default AppDomain if that is what you mean. When assembly doesn't load correctly, and AssemblyResolve of the AppDomain fires, you have a chance to resolve it manually.

First you attach to the event to get informed that loading an assembly has failed

AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;

And then try to load replacement assembly from a different place, depending on your criteria:

static System.Reflection.Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
    if (args.Name == "ClassLibrary1, Version=2.0.0.0, Culture=neutral, PublicKeyToken=e261024fcc198a53")
        return Assembly.LoadFile("d:\\differentPath\\ClassLibrary1.dll");
    else
        return null;
}

Binding redirects as far as I know are useful for redirecting to a different version of assembly, but not redirecting loader to specific path where to look for assemblies.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top