title: Help needed with unloading .DLL's from AppDomain - Still not working even with ShadowCopy
body: <p>I am trying to do the following. App A is the "mother app". It stays open. App B is just a .DLL where I write some classes that are derived from an interface specified in App A. </p>
  
  <p>Then, from App A, I will "import" classes from App B and run methods within them. I want to be able to dynamically change App B (change code and recompile) and use the new code in App A.</p>
  
  <p>I have a post-compile command in App B that copies the new .DLL to the App A directory. App A creates a new AppDomain and uses ShadowCopying. I thought this would be sufficient, but when I try to recompile &amp; copy App B's new .DLL, it says that the file is in use and can't be overwritten. </p>
  
  <p>Here is the code I have as of now:</p>
  
  <p><strong>App A (TestServer in code):</strong></p>
  
  <pre><code>namespace TestServer
  {
      public interface IRunnable
      {
          void Run();        
      }
  
      class Program
      {        
          static void Main(string[] args)
          {
              AppDomainSetup setup = new AppDomainSetup();
              setup.ApplicationName = "DemoApp";
              setup.ApplicationBase = Environment.CurrentDirectory;
              setup.ShadowCopyDirectories = Environment.CurrentDirectory;
              setup.ShadowCopyFiles = "true";
              int _domain = 1;
  
              while (true)
              {
                  string typeName = Console.ReadLine();
  
              AppDomain appDomain = AppDomain.CreateDomain("DemoDomain" + _domain, null, setup);
  
              IRunnable runner = appDomain.CreateInstanceFromAndUnwrap("TestClient.dll", typeName) as IRunnable;
  
              runner.Run();
  
              AppDomain.Unload(appDomain);
              _domain++;
              }
          }   
      }   
  }
  </code></pre>
  
  <p><strong>App B (TestClient in code):</strong></p>
  
  <pre><code>namespace TestClient
  {    
      public class TestRunner : TestServer.IRunnable
      {
          public void Run()
          {
              Console.WriteLine("RUNNING");
          }
      }
  
      public class Bob : TestServer.IRunnable
      {
          public void Run()
          {
              Console.WriteLine("BOB");
          }
      }
  }
  </code></pre>
  
  <p>I have read that if you use stuff from other app domains, those app domains could automatically load the .DLL or something along those lines. In this case, I fear that using the Interface is causing the base AppDomain to load the .DLL thereby locking it up.</p>
  
  <p>How can I resolve this issue / is there a better setup??</p>
  
  <p>NOTE: I have update my code and it still yields the same result.</p>