Question

Calling

Assembly.Load("System.Core, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e, Retargetable=Yes");

inside a .net 4.03 application should redirect to the correct 4.0.0.0 System.Core

It works on my machine for a Console App and inside an ASPX page.

However calling it from inside a Dynamics MS CRM Plugin fails with

System.IO.FileNotFoundException: Could not load file or assembly 'System.Core, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e, Retargetable=Yes' or one of its dependencies. The system cannot find the file specified.

There are no FUSION errors either. What is special about the way plugins are executed that the redirects are skipped?

Was it helpful?

Solution

I think I know what's happening. CRM very likely calling Assembly.LoadFile on your plug-in assembly. This is telling the CLR binder that it wants to handle all the logic that Fusion would normally handle (including the understanding of portable libraries, binding redirects, publisher policy, etc).

As you can see this is problematic - and calling this API is almost always the wrong thing to do, unless you really know what you are doing. Instead, they should probably be calling Assembly.LoadFrom which automatically applies this logic.

What can you do it about?

Without getting them to change, you should be able to hook up to AppDomain.Current.AssemblyResolve and apply the logic yourself that fusion would normally apply:

    static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
    {
        string name = AppDomain.CurrentDomain.ApplyPolicy(args.Name);

        try
        {
            return Assembly.Load(name);
        }
        catch (FileNotFoundException)
        {
        }
        catch (FileLoadException)
        {
        }
        catch (BadImageFormatException)
        {
        }

        return null;
    }

The problem with above is that you aren't going to be able to do this from a portable library. Either you need to this dynamically using Reflection, or have some sort of .NET Framework-specific entry point that runs before your portable assembly is loaded.

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