title: shadow copying in order not to lock assembly
body: <p>I have created a test library </p>
  
  <pre><code>public class Test
  {
      public int Add(int val1, int val2)
      {
          return val1 + val2;
      }
  }
  </code></pre>
  
  <p>And a project to call it :</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Reflection;
  using System.IO;
  
  namespace Loader
  {
      class Program
      {
          static void Main(string[] args)
          {
              String path = @"...Lib.dll"; // path to lib
              var name = Path.GetFileName(path);
  
              AppDomainSetup setup = new AppDomainSetup  
              {
                  ApplicationBase = @"...", // directory where Lib.dll is 
                  ShadowCopyFiles = "true",
                  ShadowCopyDirectories = @"..."// directory where Lib.dll is 
              };  
  
              var appdomain = AppDomain.CreateDomain("Loader." + name, null, setup);
              Assembly ass = Assembly.LoadFile(path); // &lt;--- I think here is the problem, here where assembly is locked
              Assembly assembly = appdomain.Load(ass.FullName);
  
              dynamic a = assembly.CreateInstance("Lib.Test");
  
              Console.WriteLine(a.Add(1, 5));
          }
      }
  }
  </code></pre>
  
  <p>Please help to find what I have done wrong ? Why my assembly is locked ?</p>
  
  <p><strong>EDIT :</strong> with hardcoded assembly name :</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Reflection;
  using System.IO;
  
  namespace Loader
  {
      class Program
      {
          static void Main(string[] args)
          {
              AppDomainSetup ads = new AppDomainSetup();
  
              String fullPath = @"c:\users\myuser\documents\visual studio 2010\Projects\ShadowCopy\Loader\bin\Debug\Lib.dll";
  
              ads.ShadowCopyFiles = "true";
              ads.ApplicationName = "AppName";
              ads.ShadowCopyDirectories = Path.GetDirectoryName(fullPath);
              //ads.ApplicationBase = Path.GetDirectoryName(fullPath);
              //ads.PrivateBinPath = Path.GetDirectoryName(fullPath);
              ads.CachePath = @"c:\users\myuser\documents\visual studio 2010\Projects\ShadowCopy\Loader\bin\Debug\Cache\";
  
              AppDomain ad = AppDomain.CreateDomain("myName" + ads.ApplicationName, null, ads);
  
              ad.AssemblyResolve += new ResolveEventHandler( ad_AssemblyResolve );
  
              Console.WriteLine(ad.ShadowCopyFiles);
              Console.WriteLine(ad.SetupInformation.ShadowCopyDirectories);
  
              try
              {
                  //Assembly assembly = ad.Load(AssemblyName.GetAssemblyName(fullPath));
                  //dynamic obj = ad.CreateInstanceAndUnwrap(assembly.GetName().Name, "Lib.Test");
  
                  dynamic obj = ad.CreateInstanceAndUnwrap("Lib", "Lib.Test");
  
                  Console.WriteLine(obj.Add(1, 7));
  
                  Console.ReadKey();
  
                  Console.WriteLine(obj.Add(1, 90));
              }
              catch( Exception e)
              {
                  Console.WriteLine( e.Message );
              }
          }
  
          static Assembly ad_AssemblyResolve(object sender, ResolveEventArgs args)
          {
              return Assembly.LoadFile(@"c:\users\myuser\documents\visual studio 2010\Projects\ShadowCopy\Loader\bin\Debug\Lib.dll");
          }
      }
  }
  </code></pre>
  
  <p>It is interesting that cached assebly is also locked.</p>
  
  <p><strong>EDIT 2:</strong></p>
  
  <p>Here is the line of code to block the assembly </p>
  
  <pre><code>Console.WriteLine(obj.Add(1, 7));
  </code></pre>
  
  <p>So once a method in Lib.Test is accessed the assenbly is blocked.</p>
  
  <p>What can be the solution ?</p>