title: Is Assembly.Load(byte[]) not thread safe?
body: <p>I am attaching to the <a href="http://msdn.microsoft.com/en-us/library/zwdd50tz%28v=vs.100%29.aspx" rel="nofollow">AppDomain.AssemblyResolve</a> event and then calling <a href="http://msdn.microsoft.com/en-us/library/h538bck7.aspx" rel="nofollow">Assembly.Load(byte[])</a> to load the assembly.</p>
  
  <p>It seems that if this is done from multiple Threads I can end up with duplicate assemblies loaded. However if I call <a href="http://msdn.microsoft.com/en-us/library/1009fa28.aspx" rel="nofollow">Assembly.LoadFrom</a> or <a href="http://msdn.microsoft.com/en-us/library/b61s44e8.aspx" rel="nofollow">Assembly.LoadFile</a> this problem does not occur. </p>
  
  <p>I was just wondering if this is a known qwerk of using Assembly.Load? Is it not thread safe and I need to add extra code to handle this scenario?</p>
  
  <p>Full code follows...</p>
  
  <pre><code>[TestFixture]
  public class Tester
  {
      [Test]
      public void Run()
      {
          var currentDomain = AppDomain.CurrentDomain;
          currentDomain.AssemblyResolve += ResolveAssembly;
  
          var thread1 = new Thread(LoadAssembly);
          var thread2 = new Thread(LoadAssembly);
  
          thread1.Start();
          thread2.Start();
          thread1.Join();
          thread2.Join();
          var assemblies = currentDomain.GetAssemblies();
          Assert.AreEqual(1, assemblies.Count(x =&gt; x.GetName().Name == "AssemblyToReference"));
      }
  
      Assembly ResolveAssembly(object sender, ResolveEventArgs args)
      {
          //This works
          //return Assembly.LoadFile(@"PathToAssembly");
  
          //This works
          //return Assembly.LoadFrom(@"PathToAssembly");
  
          //This does not work
          return Assembly.Load(File.ReadAllBytes(@"PathToAssembly"));
      }
  
      void LoadAssembly()
      {
          Assembly.Load("AssemblyToReference");
      }
  }
  </code></pre>