title: Add shadow copy directory to ASP.NET AppDomain
body: <p>I already saw <a href="https://stackoverflow.com/questions/4392841/add-directories-to-asp-net-shadow-copy-via-webconfig">THIS</a> and <a href="https://stackoverflow.com/questions/4060528/add-directories-to-asp-net-shadow-copy">THIS</a> question, but they are both a few years old and in my case there is maybe another solution at all:</p>
  <p>I have an ASP.NET MVC 5 application with a plugin system based on <a href="http://shazwazza.com/post/Developing-a-plugin-framework-in-ASPNET-with-medium-trust.aspx" rel="noreferrer">THIS</a>.<br />
  So my <code>~/bin</code> folder is shadow copied by the framework and my <code>~/Plugins</code> folder is using the &quot;manual shadow copy&quot; to <code>~/Plugins/ShadowCopy</code> on application pre-init as describen in the link above.<br />
  This worked fine so far until now, as I have the following case now:</p>
  <p>I create a separate restricted <code>AppDomain</code> to execute some sandboxed code.<br />
  To be able to pass objects with types defined in my assemblies to this domain I set this domains <code>ApplicationBase</code> to the shadow-copy directory of the main domain (if you do not load them from there you get a <code>SerializationException</code> when passing the type/object to the second domain, because the assembly defining this type is loaded from different locations).</p>
  <p>Now I can pass types defined in my &quot;base assemblies&quot; to this second domain.<br />
  But as soon as I want to use types defined in a plugin this fails, because the plugin assemblies are not in the default shadow copy dir, but in my &quot;manual shadow copy directory&quot;.<br />
  And as this directory is also in a completely different location I can't add it as a <code>PrivateBinPath</code> as they are required to be underneath the base path.</p>
  <p>At the moment I use the following &quot;workaround&quot;:</p>
  <pre><code>#pragma warning disable 0618
          AppDomain.CurrentDomain.SetShadowCopyPath(String.Join(&quot;;&quot;, 
             HostingEnvironment.MapPath(&quot;~/bin&quot;), 
             HostingEnvironment.MapPath(&quot;~/Plugins/ShadowCopy&quot;)));
  #pragma warning restore 0618
  </code></pre>
  <p>in the pre-init method. This adds the &quot;manual shadow copy dir&quot; to the default shadow copy dirs and my plugins are also shadow copied by the framework and my second <code>AppDomain</code> is able to load them and everything is working more or less.</p>
  <p>But as <code>SetShadowCopyPath</code> is depricated I am now getting to my actual question:<br />
  <strong>I am searching for a better solution to avoid the depricated method.</strong><br />
  I thought of the following solutions, but was not able to find out if/how that would be possible:</p>
  <ol>
  <li>Configure IIS to create an <code>AppDomain</code> with the <code>AppDomainSetup.ShadowCopyDirectories</code> property set to <code>~/bin</code> and <code>~/Plugins/ShadowCopy</code> via <code>web.config</code></li>
  <li>Move the shadow copy dir to e.g. <code>~/bin/Plugins</code> and tell IIS to NOT recycle the main <code>AppDomain</code> on changes in this folder (otherwise the app restarts on every request as the pre-init method changes the files in there every time)</li>
  </ol>
  <p>Another idea would be to set the &quot;manual shadow copy dir&quot; to be underneath the default shadow copy directory, so instead of <code>~/Plugins/ShadowCopy</code> something like <code>C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\root\69da84c5\cd056104\Plugins</code>, but is it wise to &quot;interfere with framework folders&quot;?</p>
  <p>I am open to a solution to this problem as well as a different approach to solve my base problem.</p>