Error trying out the sample code

Nov 23, 2010 at 5:58 PM
Edited Nov 23, 2010 at 6:04 PM

This code

  string template = "Hello @Model.Name! Welcome to Razor!";
  string result = Razor.Parse(template, new { Name = "World" });

generates the following error

Unhandled Exception: RazorEngine.Templating.TemplateCompilationException: Unable to compile template.  
at RazorEngine.Compilation.RazorCompiler.CreateTemplate(String template, Type modelType)  
at RazorEngine.Templating.TemplateService.GetTemplate(String template, Type modelType, String name)  
at RazorEngine.Templating.TemplateService.Parse[T](String template, T model, String name)  
at RazorEngine.Razor.Parse[T](String template, T model, String name)  
at SK.Wiki.Server.Program.Main(String[] args) in G:\Repository\Cash Track\branches\2.0\SK.Wiki.Server\Program.cs:line 30

I am using 1.1 version from nuget from a .Net 4.0 console application.

Nov 23, 2010 at 11:04 PM

I've just created a new console application and referenced the Release 1.1 build via NuGet and it compiles and runs fine.  Can you email me your complete project so I can take a look? My email is matt [at] fidelitydesign [dot] net

Nov 24, 2010 at 1:28 AM

In addition a clean Visual Studio Installation without MVC using Nuget works as it should so there must be some unique problem we're running into with this project. If you could post the complete error in the Exception: it's listed under Exception.Errors which should give us the compiler errors you're experiencing.


AntarisZX - we should probably combine the errors into the exception. What do you think?

Nov 24, 2010 at 8:23 AM

I put a screenshot of the error here

It runs OK within a debugger but it fails when I run it on the console. There is nothing special about the project and they are barely 5 lines of code.

Nov 24, 2010 at 8:28 AM

Where can I access "Exception.Errors" ?

Nov 24, 2010 at 9:20 AM
Edited Nov 24, 2010 at 9:21 AM

When you are running it from the console, it looks like the System.Web.Razor.dll assembly is missing from the output folder.  Select the reference in Visual Studio's Solution Explorer, select Properties and ensure the Copy Local property is set to true and do a rebuild.


The TemplateCompilationException has an Errors property, e.g.:

} catch (TemplateCompilationException tcEx) {  foreach (var error in tcEx.Errors)  {    Console.WriteLine(error);  }}

Nov 24, 2010 at 2:04 PM

Still error

Nov 24, 2010 at 2:31 PM

Based on that last screenshot it looks like you need to add a reference to Microsoft.CSharp.dll and possibly System.Core.dll

Please try and add those and let us know if you still have an error - we might need to update our documentation or the project itself to include those.

Nov 24, 2010 at 4:52 PM

Microsoft.CSharp and System.Core are referenced automatically for all C# console .Net 4.0 application. And they are always in the GAC anyway. I will try to replicate it in other computers. This is bizarre. 

Nov 24, 2010 at 8:18 PM

I believe this is due to the compiler omitting references from the RazorEngine.Core.dll's manifest.  When you are using DynamicObject, the runtime binder from Microsoft.CSharp.dll is required, but as we don't explicity load anything from Microsoft.CSharp.dll at compilation time, it falls over because Microsoft.CSharp.dll hasn't been referenced.

Can you download and build Changeset 3689 and try your unit tests again? I had it working with NUnit here...

Nov 25, 2010 at 9:06 AM

It works now. Thanks.

Dec 20, 2010 at 7:15 PM

I just grabbed this project with NuGet, and I'm having the same problem as nirataro -- debugging the sample app works fine, but running the app outside the debugger crashes it with the same unhandled exception.  I can't seem find changeset 3689 on the site.  Do I need to check it out of SVN?

Dec 20, 2010 at 7:17 PM

Nevermind; I need to navigate more before I post.  (

Dec 22, 2010 at 5:38 PM

It doesnt work with visual Please test in as I really need this for a new project.

Dec 25, 2010 at 8:22 AM
Edited Dec 25, 2010 at 8:37 AM

It has to do with the code generation for the template classes is hard coded for c# and doesn't take into account visual basic. If you don't pass in a model or anonymous type it will work fine. I've published an update that fixes this issue, however, it still does not work with classes that are subclasses



Public Class SomeClass

Public Class SomeChildClass

End Class

End Class



SomeClass.SomeChildClass will not work properly - I'm a bit confused as to why but don't do it for now! :)