title: Assembly.Load throws FileNotFoundException on an assembly created through Roslyn with Added References
body: <p>I have a problem loading an assembly (from a memory stream) that was emitted from Roslyn compilation.</p>
  
  <p>While trying to load the assembly i'm getting a FileNotFound Exception <code>RazorLight, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified</code>.</p>
  
  <p>In the compilation ExternalReferences there is 261 items which includes <code>C:\Users\favishg\source\repos\Empeon_Reports\Reports\bin\Debug\net461\RazorLight.dll</code>, so i assume that the roslyn compiler knows were to find this assembly, but it still gives this file not found exception.</p>
  
  <p><strong>More details</strong>: i'm dynamically loading a <em>Sample.dll</em> (late binding), this dll is using RazorLight to compile a <em>schtml</em> Template, in order for RazorLight to know about the <em>Sample.dll</em> and it's type's i needed to add MetadataReferences to the <em>Sample.dll</em> which worked fine, but i still got a FileNotFound Exception.</p>
  
  <p>To debug it i downloaded the RazorLight source code and it seems that the exception is when trying to get <code>assembly.GetCustomAttribute&lt;RazorLightTemplateAttribute&gt;()</code> from the Roslyn compiled assembly.</p>
  
  <p><strong>Code</strong>
  Report reunner (<em>Sample.dll</em>)</p>
  
  <pre><code>MetadataReference metadata =MetadataReference.CreateFromFile(PayrollReport);
  MetadataReference metadataRazor=MetadataReference.CreateFromFile(RazorLight);
  
  IRazorLightEngine engine;
  
  engine = new RazorLightEngineBuilder()
      .AddMetadataReferences(metadata, metadataRazor)
      .UseFilesystemProject(templatePath)
      .UseMemoryCachingProvider()
      .Build();
  
  PayrollSummary payrollSummary = repo.GetPayrollData();
  
  return await engine.CompileRenderAsync("Report.cshtml", payrollSummary);
  </code></pre>
  
  <p>Call the <em>sample.dll</em> dynamically</p>
  
  <pre><code>Assembly asmPayroll = Assembly.LoadFrom(ParollReportDll);
  
  //Get the correct type from dll..
  
  IHardcodedReport rpt = (IHardcodedReport)Activator.CreateInstance(type);
  string html = await rpt.GetReportHtmlAsync(bundle);
  </code></pre>