title: Shared domain for app domain while trying to run microsoft reportviwer in legacy mode
body: <p>I am aiming to have Microsoft report viewer on .net 4.x with .net 3.5 with NetFx40_LegacySecurityPolicy as noted <a href="https://social.msdn.microsoft.com/Forums/sqlserver/en-US/6d89e2ce-3528-465f-9740-7e22aa7b7aae/slow-performance-with-dynamic-grouping-and-reportviewer-in-local-mode?referrer=https://social.msdn.microsoft.com/Forums/sqlserver/en-US/6d89e2ce-3528-465f-9740-7e22aa7b7aae/slow-performance-with-dynamic-grouping-and-reportviewer-in-local-mode?forum=sqlreportingservices" rel="nofollow noreferrer">here</a> </p>
  
  <p>With full trust I got </p>
  
  <pre><code>Loading this assembly would produce a different grant set from other instances.
  </code></pre>
  
  <p>When try with relevant permissions by <a href="https://stackoverflow.com/a/8231360/492258">signing assemblies</a>:</p>
  
  <pre><code>PermissionSet permissions = new PermissionSet(PermissionState.None);
  permissions.AddPermission(new SecurityPermission(SecurityPermissionFlag.Execution));
  permissions.AddPermission(new FileIOPermission(PermissionState.None) { AllFiles = FileIOPermissionAccess.Read });
  </code></pre>
  
  <p>it says <code>Local mode requires full trust.</code></p>
  
  <p><a href="https://stackoverflow.com/questions/33892955/error-while-loading-assembly-on-separated-appdomain#comment55553802_33892955">Here</a> Shared domain is mentioned:</p>
  
  <blockquote>
    <p>The problem is that some assemblies (most notably, interop assemblies)
    are not domain-specific. They use a shared domain, and thus they can
    only be loaded once in a process. You need to make sure that all the
    domains use the exact same assembly when that's the case. Or use full
    trust, but that comes with its own can of worms, of course :)</p>
  </blockquote>
  
  <p>For shared domain, <a href="https://blogs.msdn.microsoft.com/tess/2006/08/02/asp-net-case-study-lost-session-variables-and-appdomain-recycles/" rel="nofollow noreferrer">Here</a> there is a listing like this:</p>
  
  <blockquote>
    <p>…where each of the subwebs HrWeb, EmployeeServices etc. are set up as
    an application in the internet service manager, you will have the
    following application domains (appdomains) in your asp.net process</p>
    
    <p>System Domain<br>
    Shared Domain<br>
    Default Domain<br>
    Root<br>
    HrWeb<br>
    EmployeeServices<br>
    FinanceWeb<br>
    SalesWeb</p>
  </blockquote>
  
  <p>and <a href="https://stackoverflow.com/a/1362832/492258">here</a> said:</p>
  
  <blockquote>
    <p>Any assembly can be loaded by the CLR either into the shared domain or
    the domain that triggered the assembly load depending on the
    configuration of the CLR instance that is loading the assembly.</p>
  </blockquote>
  
  <p><del><strong>My first question, what is "Shared domain", is it related to shared hosting?</strong></del></p>
  
  <p><a href="http://geekswithblogs.net/akraus1/archive/2012/07/25/150301.aspx" rel="nofollow noreferrer">This link</a> explains about "shared domain" by depicting with illustration with arguement:</p>
  
  <blockquote>
    <p>When you load an assembly into your (default) AppDomain you will load
    it only for your current AppDomain. The types defined there are not
    shared anywhere. There is one exception though: The types defined in
    mscorlib are always shared between all AppDomains. The mscorlib
    assembly is loaded into a so called Shared Domain. This is not a real
    AppDomain but simply a placeholder domain where all assemblies are
    loaded which can be shared between AppDomains. An assembly loaded into
    the Shared Domain is loaded therefore AppDomain neutral. Assemblies
    loaded AppDomain neutral have one special behavior:</p>
  </blockquote>
  
  <p><a href="https://i.stack.imgur.com/6prFg.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/6prFg.png" alt="enter image description here"></a></p>
  
  <p>So my understanding, Shared domain is on top of appdomain. But how is s it related to shared hosting? Is it w3wp.exe process has appdomains and shared domain?</p>
  
  <p>On the other hand, if I play with set the <code>AppDomainSetup.LoaderOptimization</code> property, I am stuck as well because:  </p>
  
  <p><a href="https://stackoverflow.com/a/1362832/492258">Here</a>, it is said LoaderOptimization attribute <em>can not be</em> changed for iis.</p>
  
  <p>However to verify I both tried with </p>
  
  <pre><code>        AppDomainSetup setup = new AppDomainSetup { ApplicationBase = Environment.CurrentDirectory, LoaderOptimization = LoaderOptimization.SingleDomain };
  </code></pre>
  
  <p>and </p>
  
  <pre><code>        AppDomainSetup setup = new AppDomainSetup { ApplicationBase = Environment.CurrentDirectory, LoaderOptimization = LoaderOptimization.MultiDomain };
  </code></pre>
  
  <p>but always result with:</p>
  
  <pre><code>Exception has been thrown by the target of an invocation.|[!] Exception has been thrown by the target of an invocation.
  The type initializer for 'Microsoft.Reporting.WebForms.ReportViewer' threw an exception.
  Loading this assembly would produce a different grant set from other instances. (Exception from HRESULT: 0x80131401) 
  </code></pre>
  
  <p><strong>Second question is: I can produce report in dev machine however in test machine it fails with error message above with full trust issues. How can I reproduce the same error on my dev machine?</strong></p>
  
  <p><strong>So finally How can I produce properly my report on test machine? Is it cause to use multiple application on the same domain? Do I need dedicated iis domain for this?</strong></p>