Reputation: 46747
How does .NET resolve assemblies referenced by dynamically compiled in-memory assemblies.
I'm hosting Razor view engine and using it to compile views on the fly, but am having problems getting it to reference the main project's assembly. In the compile parameters I add a reference to the project assembly, something like this:
// Add references to all currently loaded assemblies
foreach (var a in AppDomain.CurrentDomain.GetAssemblies())
{
compileParams.ReferencedAssemblies.Add(a.Location);
}
The code compiles and runs fine, so long as I don't reference any types in the project DLL - in which case I get the following error:
Could not load file or assembly 'MyProject, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.
I've double checked and this assembly is already loaded at the point where the exception is being thrown. What am I missing?
Upvotes: 4
Views: 2771
Reputation: 46747
Figured it out. The main project is a dll loaded by an exe in a different directory. .NET assembly loader by default searches the exe directory but not the dll's directory. Resolved it with an AppDomain.CurrentDomain.AssemblyResolve event that searches the folder of the project dll.
Upvotes: 2
Reputation: 13246
From the code it appears as though you are creating a separate AppDomain to host the dynamically compiled in-memory assembly.
It may be that your dynamically compiled assembly is referencing an assembly that your main app is not. See if casting your exception to ReflectionTypeLoadException
and checking the LoaderExceptions
doesn't provide a clue.
Upvotes: 1