title: "Could not load file or assembly 'System.Core, Version=2.0.5.0,..." exception when loading Portable Class Library dynamically
body: <p>First of all I need to emphasize that this is slightly different question than the one in <a href="https://stackoverflow.com/questions/13871267/unable-to-resolve-assemblies-that-use-portable-class-libraries">this thread</a>. Additionally, installing <a href="https://www.microsoft.com/en-us/download/details.aspx?id=3556" rel="nofollow noreferrer">KB2468871</a> doesn't help.</p>
  
  <p>I tried to simplify this problem as much as possible. In general it about loading PCL assemblies in Desktop application with Assembly.LoadFile(...).</p>
  
  <p>Let's say there is a .NET 4.0 Console Application (called "C"). It references .NET 4.0 assembly (called "N4") and PCL assembly (called "PCL"). </p>
  
  <p>where N4 looks like this:</p>
  
  <pre><code>using System.Linq;
  
  namespace N4
  {
      public class ClassInN4
      {
          public static string Greet()
          {
              return new string(
                  "hello from N4"
                  .ToCharArray()
                  .Select(char.ToUpper)
                  .ToArray()
              );
          }
      }
  }
  </code></pre>
  
  <p>PCL looks like this:</p>
  
  <pre><code>using System.Linq;
  
  namespace PCL
  {
      public class ClassInPCL
      {
          public static string Greet()
          {
              return new string(
                  "hello from pcl"
                  .ToCharArray()
                  .Select(char.ToUpper)
                  .ToArray()
              );
          }
      }
  }
  </code></pre>
  
  <p>and C look like this:</p>
  
  <pre><code>using System;
  using System.IO;
  using System.Reflection;
  using N4;
  using PCL;
  
  namespace C
  {
      internal class Program
      {
          private static void Main(string[] args)
          {
              Test();
              Console.ReadLine();
          }
  
          private static void Test()
          {
              Test("N4", ClassInN4.Greet);
              Test("PCL", ClassInPCL.Greet);
          }
  
          private static void Test(
              string title, 
              Func&lt;string&gt; generator)
          {
              try
              {
                  Console.WriteLine(
                      "{0}: {1}", title, generator());
              }
              catch (Exception e)
              {
                  Console.WriteLine(
                      "{0}: {1} -&gt; {2}", title, e.GetType(), e.Message);
              }
          }
      }
  }
  </code></pre>
  
  <p>When you run this application you get absolutely correct results:</p>
  
  <pre><code>N4: HELLO FROM N4
  PCL: HELLO FROM PCL
  </code></pre>
  
  <p>Let's add AssemblyResolve event to CurrentDomain in Program.Main:</p>
  
  <pre><code>AppDomain.CurrentDomain.AssemblyResolve += (_, a) =&gt; {
      var fileName = Path.GetFullPath(
          new AssemblyName(a.Name).Name + ".data");
      Console.WriteLine("Probing '{0}'", fileName);
      return 
          File.Exists(fileName) 
          ? Assembly.LoadFile(fileName) 
          : null;
  };
  </code></pre>
  
  <p>So, what it does if assembly cannot be found it tries to load it from ".data" file.</p>
  
  <p>Let's go application folder and rename "N4.dll" to "N4.data" and run "C.exe".</p>
  
  <pre><code>Probing 'C:\xxx\C\bin\Debug\N4.data'
  N4: HELLO FROM N4
  PCL: HELLO FROM PCL
  </code></pre>
  
  <p>So it goes through AssemblyResolve and finally loads "N4.data" and works as good as original.</p>
  
  <p>Let's revert "N4.data" to "N4.dll" and rename "PCL.dll" to "PCL.data" and...</p>
  
  <pre><code>Probing 'C:\xxx\C\bin\Debug\PCL.data'
  N4: HELLO FROM N4
  Probing 'C:\xxx\C\bin\Debug\System.Core.data'
  Probing 'C:\xxx\C\bin\Debug\System.Core.data'
  Probing 'C:\xxx\C\bin\Debug\System.Core.data'
  PCL: System.IO.FileNotFoundException -&gt; 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.
  </code></pre>
  
  <p>Please note that PCL assembly was loaded just fine, the problem is, it only can't find its dependencies (System.Core) anymore.</p>
  
  <p>It's like Assembly.LoadFile(fileName) is-a no-no if loaded assembly is portable.</p>
  
  <p>Did anyone have this problem? Did anyone solved this problem?</p>
  
  <p>You can find all files <a href="https://dl.dropboxusercontent.com/u/317815/PCLResolveBug.zip" rel="nofollow noreferrer">here</a>.</p>
  
  <p><strong>EDIT:</strong>
  Thanks to leppie for forcing me to check other options. I actually wanted to be sure that I'm not lying while I answer "Yeah, yeah, I tried". Apparently it was worth checking.</p>
  
  <p>From <a href="http://blogs.msdn.com/b/suzcook/archive/2003/09/19/57248.aspx" rel="nofollow noreferrer">Suzanne Cook's .NET CLR Notes</a>:</p>
  
  <blockquote>
    <p>Be careful - these aren't the same thing.</p>
    
    <p>LoadFrom() goes through Fusion and can be redirected to another assembly at a different path but with that same identity if one is already loaded in the LoadFrom context.
    LoadFile() doesn't bind through Fusion at all - the loader just goes ahead and loads exactly* what the caller requested. It doesn't use either the Load or the LoadFrom context.</p>
  </blockquote>