title: AppDomains and configSections
body: <p>We are using CSLA (a rather ancient version) in our .NET 3.5 applications, and we make use of it's NetRun application loading for some of our users.  For those not familiar with NetRun, NetRun.exe is basically an application "runner" that is installed on user computers (e.g. to c:\Program Files\NetRun\NetRun.exe).  The user simply starts the application by launching NetRun.exe.</p>
  
  <p>What NetRun.exe does is the following:</p>
  
  <p>(1) Creates a new AppDomainSetup</p>
  
  <pre><code>Dim setupDomain As New AppDomainSetup()
  setupDomain.ApplicationBase = CurrentDomainPath() ' this will be C:\Program Files\NetRun\
  setupDomain.ConfigurationFile = "http://www.ourdomain.com/TheApp.xml" ' The app.config file is actually named TheApp.xml on the server because it has NetRun-specific config settings that don't belong in the standard TheApp.config that is used when the app is running directly from the server.
  </code></pre>
  
  <p>(2) Then a new AppDomain is created with that AppDomainSetup</p>
  
  <pre><code>' create new application domain 
  Dim newDomain As AppDomain = AppDomain.CreateDomain("TheApp", Nothing, setupDomain)
  </code></pre>
  
  <p>(3) Then NetRun.Launcher (a launch helper class -- mainly used for a common splash screen) is instantiated within that new AppDomain via:</p>
  
  <pre><code>' create launcher object in new appdomain
  Dim launcher As Launcher = CType(newDomain.CreateInstanceAndUnwrap("NetRun", "NetRun.Launcher"), Launcher)
  </code></pre>
  
  <p>(4) Then the luncher helper class runs the app within the new AppDomain via</p>
  
  <pre><code>' use launcher object from the new domain to launch the remote app in that appdomain
  launcher.RunApp()
  </code></pre>
  
  <p>(5) Which, after all the splash-screen stuff, the application is finally launched via</p>
  
  <pre><code>Dim ass = Assembly.LoadFrom("http://www.ourdomain.com/TheApp.exe")
  ass.EntryPoint.Invoke(ass.EntryPoint, Nothing)
  </code></pre>
  
  <p>So, to recap, the actual running application's AppDomain's ApplicationBase is C:\Program Files\NetRun\, while the EntryPoint to the actual app is found in "<a href="http://www.ourdomain.com/TheApp.exe" rel="nofollow noreferrer">http://www.ourdomain.com/TheApp.exe</a>". So far, so good.</p>
  
  <p>The application itself, in addition to TheApp.exe, also as a dependency on ALibrary.dll.  To date, this has worked wonderful for us.  Even when ALibrary.dll has config entries that were pulled into TheApp.xml file, those config entries have always been read fine (and continue to do so).</p>
  
  <p>With our upcoming release, we added a new custom config file section, which is defined in ALibrary.dll.  We added the new section into TheApp.xml and TheApp.config files</p>
  
  <pre><code>&lt;configSections&gt;
      &lt;sectionGroup name="applicationSettings" type="System.Configuration.Applicat..."&gt;
        &lt;section name="TheApp.My.MySettings" type="System.Configuration.ClientSettingsS..."/&gt;
        &lt;section name="ALibrary.My.MySettings" type="System.Configuration.ClientSettingsS..."/&gt;
      &lt;/sectionGroup&gt;
      &lt;!-- NEW CONFIG SECTION --&gt;
      &lt;section name="fixedPriceExceptionModel" type="ALibrary.Configuration.FixedPrices.FixedPriceExceptionModelSection, ALibrary"/&gt;
  &lt;/configSections&gt;
  </code></pre>
  
  <p>(parts were ...'ed out to conserve space in this post)</p>
  
  <p>However, when we try to access the new section in a NetRun launched TheApp.exe in the "normal" way via</p>
  
  <pre><code>CType(ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None) _
                               .GetSection("fixedPriceExceptionModel"), FixedPriceExceptionModelSection)
  </code></pre>
  
  <p>We get the following exception:</p>
  
  <blockquote>
    <p>An error occurred creating the
    configuration section handler for
    fixedPriceExceptionModel: Could not
    load file or assembly 'ALibrary' or
    one of its dependencies. The system
    cannot find the file specified.
    (<a href="http://www.ourdomain.com/TheApp.xml" rel="nofollow noreferrer">http://www.ourdomain.com/TheApp.xml</a>
    line 8)</p>
  </blockquote>
  
  <p>Doing some research, this is due to the fact that the assembly resolver for the .NET configuration libraries are looking for ALibrary.dll in all the usual places; ApplicationBase, specific sub directories, .NET core directories, and then the GAC.  Unfortunatly, ALibrary.dll is never going to be found in any of those locations.  When this application is run directly on the server, without NetRun, the application does not throw the exception and properly reads the configuration section.</p>
  
  <p>One thing I tried was to set the AppDomainSetup differently, in that its ApplicationBase would be set to <a href="http://www.ourdomain.com/" rel="nofollow noreferrer">http://www.ourdomain.com/</a>, but then the subsequent call to CType(newDomain.CreateInstanceAndUnwrap("NetRun", "NetRun.Launcher"), Launcher) bombs because NetRun.exe is not on the server.</p>
  
  <p>This is probably a lot to take in and I hope I described it well enough (and didn't bore you).  Maybe there is no easy solution, but I've exhausted my limited knowledge of this area and hope someone else here on SO might have the magic fix, allowing me to reliably access custom config sections while running the application in both NetRun and local.</p>