title: How to pre-load all deployed assemblies for an AppDomain
body: <p><strong>UPDATE:</strong> I now have a solution I'm much happier with that, whilst not solving all the problems I ask about, it does leave the way clear to do so.  I've updated my own answer to reflect this.</p>
  
  <p><em>Original Question</em></p>
  
  <p>Given an App Domain, there are many different locations that Fusion (the .Net assembly loader) will probe for a given assembly.  Obviously, we take this functionality for granted and, since the probing appears to be embedded within the .Net runtime (<code>Assembly._nLoad</code> internal method seems to be the entry-point when Reflect-Loading - and I assume that implicit loading is probably covered by the same underlying algorithm), as developers we don't seem to be able to gain access to those search paths.</p>
  
  <p>My problem is that I have a component that does a lot of dynamic type resolution, and which needs to be able to ensure that all user-deployed assemblies for a given AppDomain are pre-loaded before it starts its work.  Yes, it slows down startup - but the benefits we get from this component totally outweight this.</p>
  
  <p>The basic loading algorithm I've already written is as follows.  It deep-scans a set of folders for any .dll (.exes are being excluded <em>at the moment</em>), and uses Assembly.LoadFrom to load the dll if it's AssemblyName cannot be found in the set of assemblies already loaded into the AppDomain (this is implemented inefficiently, but it can be optimized later):</p>
  
  <pre><code>void PreLoad(IEnumerable&lt;string&gt; paths)
  {
    foreach(path p in paths)
    {
      PreLoad(p);
    }
  }
  
  void PreLoad(string p)
  {
    //all try/catch blocks are elided for brevity
    string[] files = null;
  
    files = Directory.GetFiles(p, "*.dll", SearchOption.AllDirectories);
  
    AssemblyName a = null;
    foreach (var s in files)
    {
      a = AssemblyName.GetAssemblyName(s);
      if (!AppDomain.CurrentDomain.GetAssemblies().Any(
          assembly =&gt; AssemblyName.ReferenceMatchesDefinition(
          assembly.GetName(), a)))
        Assembly.LoadFrom(s);
    }    
  }
  </code></pre>
  
  <p>LoadFrom is used because I've found that using Load() can lead to duplicate assemblies being loaded by Fusion if, when it probes for it, it doesn't find one loaded from where it expects to find it.</p>
  
  <p>So, with this in place, all I now have to do is to get a list in precedence order (highest to lowest) of the search paths that Fusion is going to be using when it searches for an assembly.  Then I can simply iterate through them.</p>
  
  <p>The GAC is irrelevant for this, and I'm not interested in any environment-driven fixed paths that Fusion might use - only those paths that can be gleaned from the AppDomain which contain assemblies expressly deployed for the app.</p>
  
  <p>My first iteration of this simply used AppDomain.BaseDirectory.  This works for services, form apps and console apps.</p>
  
  <p>It doesn't work for an Asp.Net website, however, since there are at least two main locations - the AppDomain.DynamicDirectory (where Asp.Net places it's dynamically generated page classes and any assemblies that the Aspx page code references), and then the site's Bin folder - which can be discovered from the AppDomain.SetupInformation.PrivateBinPath property.</p>
  
  <p>So I now have working code for the most basic types of apps now (Sql Server-hosted AppDomains are another story since the filesystem is virtualised) - but I came across an interesting issue a couple of days ago where this code simply doesn't work: the nUnit test runner.</p>
  
  <p>This uses both Shadow Copying (so my algorithm would need to be discovering and loading them from the shadow-copy drop folder, not from the bin folder) and it sets up the PrivateBinPath as being relative to the base directory.</p>
  
  <p>And of course there are loads of other hosting scenarios that I probably haven't considered; but which must be valid because otherwise Fusion would choke on loading the assemblies.</p>
  
  <p>I want to stop feeling around and introducing hack upon hack to accommodate these new scenarios as they crop up - what I want is, given an AppDomain and its setup information, the ability to produce this list of Folders that I should scan in order to pick up all the DLLs that are going to be loaded; regardless of how the AppDomain is setup.  If Fusion can see them as all the same, then so should my code.</p>
  
  <p>Of course, I might have to alter the algorithm if .Net changes its internals - that's just a cross I'll have to bear.  Equally, I'm happy to consider SQL Server and any other similar environments as edge-cases that remain unsupported for now.</p>
  
  <p>Any ideas!?</p>