Problem compiling using RazorEngine in asp.net site

Sep 27, 2011 at 12:01 PM

Hello guys,

i had a problem compiling a razor template (the content doesnt matter) in a mvc project. it gave an error during compilation saying that the same assembly had been added already to the list of references sent to the codedom.

i traced the problem to the CompilerResults Compile(TypeContext context) method in DirectCompilerServiceBase where it takes all the loaded assemblies and add them as references. i modified the following to get it working, [there are probably better ways to do this]


Dictionary<string, string> references = new Dictionary<string, string>();

var assemblies = CompilerServices
    .GetLoadedAssemblies()
    .Where(a => !a.IsDynamic)
    .Select(a => a.Location)
    .ToArray();

foreach (var a in assemblies)
{
    references[Path.GetFileName(a)] = a;
}

@params.ReferencedAssemblies.AddRange(references.Values.ToArray());

i hope you can add something similar to the main code base :)