Debugging information and avoiding errors

Aug 4, 2011 at 1:16 PM
Edited Aug 4, 2011 at 1:23 PM

First, thanks for this useful code.

Like others I was getting errors during compilation. I made a couple of hasty additions to avoid some of them:

Sometimes an assembly with an empty name seems to be appended to the referenced assemblies thus generating an eror, so I added the bold red text in DirectCompilerServiceBase.cs Line 59:

.Where(a => !a.IsDynamic && a.Location != "")

To return some debugging information, I modified TemplateCompilationException to return the result output (a list of strings which contains the source of the error):

public TemplateCompilationException(CompilerErrorCollection errors, System.Collections.Specialized.StringCollection output)
 : base("Unable to compile template. Check the Errors list for details.")
 var list = errors.Cast<CompilerError>().ToList();
 Errors = new ReadOnlyCollection<CompilerError>(list);
 Output = new List<string>(output.Cast<string>());

public ReadOnlyCollection<CompilerError> Errors { get; private set; }

public List<string> Output { get; private set; }

and modified accordingly DirectCompilerServiceBase.cs Line 78 to:

throw new TemplateCompilationException(results.Errors, results.Output);

If you are using RazorEngine.Web you will also have to modify RazorBuildProviderBase.cs Line 70 to:

throw new TemplateCompilationException(results.Errors, results.Output);

You will need to catch the TemplateCompilationException and go through the Output list of strings to see where the template error is.

I'm sure the author will find a better way to fix these problems. :-)