title: Can't load assembly generated with code in a new Application Domain
body: <p>I have an asp.net application, which generates assemblies on the fly from user scripts.
  It's all working good, but now I want to run the assembly in a SandBox application domain with reduced permissions.
  When I load the assembly in a new application domain, I get the Exception</p>
  <blockquote>
  <p>Could not load file or assembly 'genb3336c1d82074079911fb70bd8bd7e65.dll, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.</p>
  </blockquote>
  <p>In an effort to see what's happening, I saved the generated assembly to disk and just wrote a simple application with the following code</p>
  <pre><code>string path = @&quot;test.dll&quot;;
  byte[] buffer = File.ReadAllBytes(path);
  
  var assembly = AppDomain.CurrentDomain.Load(buffer); //This works
  
  var appDomain = AppDomain.CreateDomain(&quot;Test&quot;);
  assembly = appDomain.Load(buffer); //Exception
  </code></pre>
  <p>Again when I try to load the assembly in the new domain, I get the same exception. Loading it in the CurrentDomain works fine.</p>
  <p>Then I created a class library with just a class and the source code I generated the dll from.
  Source code is minimal, 2 lines of code in a method. Now when I build and load this dll in the new application domain, it loads just fine.</p>
  <p>I opened the 2 dlls in ILSpy and they look identical
  <a href="https://i.stack.imgur.com/hghRm.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/hghRm.png" alt="enter image description here" /></a></p>
  <p>So I have no idea what's wrong with the generated dll.
  I generate the assembly using the following code.</p>
  <pre><code>public class ScriptCompiler
  {
      public static (byte[], byte[], Assembly) Compile((string Source, string Path)[] sources, string[] references, params Type[] types)
      {
          var assemblyName = &quot;gen&quot; + Guid.NewGuid().ToString().Replace(&quot;-&quot;, &quot;&quot;) + &quot;.dll&quot;;
          byte[] rawAssembly;
          byte[] rawPdb;
  
  #if DEBUG
          var encoding = Encoding.UTF8;
          var symbolsName = Path.ChangeExtension(assemblyName, &quot;pdb&quot;);
  
          var sourceTexts = sources
              .Select(s =&gt; new { Buffer = encoding.GetBytes(s.Source), s.Source, s.Path })
              .Select(b =&gt; new { SourceText = SourceText.From(b.Buffer, b.Buffer.Length, encoding, canBeEmbedded: true), b.Path });
  
          var syntaxTrees = sourceTexts
              .Select(s =&gt; new { Tree = CSharpSyntaxTree.ParseText(s.SourceText, new CSharpParseOptions(), s.Path), s.Path })
              .Select(t =&gt; new { Node = t.Tree.GetRoot() as CSharpSyntaxNode, t.Path })
              .Select(n =&gt; CSharpSyntaxTree.Create(n.Node, null, n.Path, encoding));
  
          var options = new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary).WithOptimizationLevel(OptimizationLevel.Debug);
  #else
          var syntaxTrees =
              from source in sources.Select(s =&gt; s.Source)
              select CSharpSyntaxTree.ParseText(source);
  
          var options = new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary).WithOptimizationLevel(OptimizationLevel.Release);
  #endif
  
          var typeRefs = new List&lt;MetadataReference&gt;();
          foreach (var type in types)
          {
              typeRefs.Add(MetadataReference.CreateFromFile(type.Assembly.Location));
          }
  
          var compilation = CSharpCompilation.Create(assemblyName,
              options: options,
              syntaxTrees: syntaxTrees,
              references: typeRefs.Union(references.Select(r =&gt; MetadataReference.CreateFromFile(r))));
  
          EmitResult emitResult;
  
          using (var peStream = new MemoryStream())
          using (var pdbStream = new MemoryStream())
          {
  #if DEBUG
              emitResult = compilation.Emit(
                  peStream: peStream,
                  pdbStream: pdbStream,
                  embeddedTexts: sourceTexts.Select(s =&gt; EmbeddedText.FromSource(s.Path, s.SourceText)),
                  options: new EmitOptions(debugInformationFormat: DebugInformationFormat.PortablePdb, pdbFilePath: symbolsName)
              );
  #else
              emitResult = compilation.Emit(peStream: peStream);
  #endif
  
              if (emitResult.Success)
              {
                  rawAssembly = peStream.GetBuffer();
                  rawPdb = pdbStream.GetBuffer();
                  var assembly = Assembly.Load(rawAssembly, rawPdb);
                  return (rawAssembly, rawPdb, assembly);
              }
          }
  
          var message = string.Join(&quot;\r\n&quot;, emitResult.Diagnostics);
          throw new ApplicationException(message);
      }
  }
  </code></pre>
  <p>Anyone got any idea?</p>
  <p><strong>Edit:</strong>
  This is the fusionLog. I don't know how to interpret it.</p>
  <pre><code>=== Pre-bind state information ===
  LOG: DisplayName = genb3336c1d82074079911fb70bd8bd7e65.dll, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
   (Fully-specified)
  LOG: Appbase = file:///C:/Users/Admin/source/repos/LoadAssembly/LoadAssembly/bin/Debug/
  LOG: Initial PrivatePath = NULL
  Calling assembly : (Unknown).
  ===
  LOG: This bind starts in default load context.
  LOG: Using application configuration file: C:\Users\Admin\source\repos\LoadAssembly\LoadAssembly\bin\Debug\LoadAssembly.exe.Config
  LOG: Using host configuration file: 
  LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config.
  LOG: Policy not being applied to reference at this time (private, custom, partial, or location-based assembly bind).
  LOG: Attempting download of new URL file:///C:/Users/Admin/source/repos/LoadAssembly/LoadAssembly/bin/Debug/genb3336c1d82074079911fb70bd8bd7e65.dll.DLL.
  LOG: Attempting download of new URL file:///C:/Users/Admin/source/repos/LoadAssembly/LoadAssembly/bin/Debug/genb3336c1d82074079911fb70bd8bd7e65.dll/genb3336c1d82074079911fb70bd8bd7e65.dll.DLL.
  LOG: Attempting download of new URL file:///C:/Users/Admin/source/repos/LoadAssembly/LoadAssembly/bin/Debug/genb3336c1d82074079911fb70bd8bd7e65.dll.EXE.
  LOG: Attempting download of new URL file:///C:/Users/Admin/source/repos/LoadAssembly/LoadAssembly/bin/Debug/genb3336c1d82074079911fb70bd8bd7e65.dll/genb3336c1d82074079911fb70bd8bd7e65.dll.EXE.
  </code></pre>