Reputation: 4654
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
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.
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
string path = @"test.dll";
byte[] buffer = File.ReadAllBytes(path);
var assembly = AppDomain.CurrentDomain.Load(buffer); //This works
var appDomain = AppDomain.CreateDomain("Test");
assembly = appDomain.Load(buffer); //Exception
Again when I try to load the assembly in the new domain, I get the same exception. Loading it in the CurrentDomain works fine.
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.
I opened the 2 dlls in ILSpy and they look identical
So I have no idea what's wrong with the generated dll. I generate the assembly using the following code.
public class ScriptCompiler
{
public static (byte[], byte[], Assembly) Compile((string Source, string Path)[] sources, string[] references, params Type[] types)
{
var assemblyName = "gen" + Guid.NewGuid().ToString().Replace("-", "") + ".dll";
byte[] rawAssembly;
byte[] rawPdb;
#if DEBUG
var encoding = Encoding.UTF8;
var symbolsName = Path.ChangeExtension(assemblyName, "pdb");
var sourceTexts = sources
.Select(s => new { Buffer = encoding.GetBytes(s.Source), s.Source, s.Path })
.Select(b => new { SourceText = SourceText.From(b.Buffer, b.Buffer.Length, encoding, canBeEmbedded: true), b.Path });
var syntaxTrees = sourceTexts
.Select(s => new { Tree = CSharpSyntaxTree.ParseText(s.SourceText, new CSharpParseOptions(), s.Path), s.Path })
.Select(t => new { Node = t.Tree.GetRoot() as CSharpSyntaxNode, t.Path })
.Select(n => 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 => s.Source)
select CSharpSyntaxTree.ParseText(source);
var options = new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary).WithOptimizationLevel(OptimizationLevel.Release);
#endif
var typeRefs = new List<MetadataReference>();
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 => 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 => 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("\r\n", emitResult.Diagnostics);
throw new ApplicationException(message);
}
}
Anyone got any idea?
Edit: This is the fusionLog. I don't know how to interpret it.
=== 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.
Upvotes: 2
Views: 124