RazorLight: 'object' does not contain a definition for 'Name'

I’m trying to convert an old project with RazorEngine to .netcore and RazorLight but with the old working template and code I get always this error: ‘object’ does not contain a definition for ‘Name’

SomeMethod("\r\n<h1>Hello @Model.Name, welcome to RazorEngine!</h1>", new { Name = "pippo" });

.....

public string SomeMethod(string templateText, object model)
{
    var engine = new RazorLightEngineBuilder().UseMemoryCachingProvider().Build();
    Task<string> messageTask = Task.Run(async () => await engine.CompileRenderAsync("TemplateKey", templateText, model));
    var message = messageTask.Result;
    return message;
}

full exception:

"System.AggregateException: One or more errors occurred. ('object' does not contain a definition for 'Name') ---> Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: 'object' does not contain a definition for 'Name'
   at CallSite.Target(Closure , CallSite , Object )
   at System.Dynamic.UpdateDelegates.UpdateAndExecute1[T0,TRet](CallSite site, T0 arg0)
   at RazorLight.CompiledTemplates.GeneratedTemplate.<ExecuteAsync>d__0.MoveNext() in testtemplate:line 2
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at RazorLight.TemplateRenderer.<RenderPageCoreAsync>d__9.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at RazorLight.TemplateRenderer.<RenderPageAsync>d__8.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at RazorLight.TemplateRenderer.<RenderAsync>d__7.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at RazorLight.RazorLightEngine.<RenderTemplateAsync>d__17.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at RazorLight.RazorLightEngine.<RenderTemplateAsync>d__16.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at RazorLight.RazorLightEngine.<CompileRenderAsync>d__11.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at TestProject.Mailer.Domain.MailTemplate.<>c__DisplayClass17_0.<<WithParameters>b__1>d.MoveNext() in D:\\Projects\\TestProject.Mailer\\TestProject.Mailer\\Domain\\MailTemplate.cs:line 61
   --- End of inner exception stack trace ---
   at System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification)
   at TestProject.Mailer.Domain.MailTemplate.WithParameters(Object model) in D:\\Projects\\TestProject.Mailer\\TestProject.Mailer\\Domain\\MailTemplate.cs:line 62
   at TestProject.Mailer.CoreConsole.Program.Main(String[] args) in D:\\Projects\\TestProject.Mailer\\TestProject.Mailer.CoreConsole\\Program.cs:line 21
---> (Inner Exception #0) Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: 'object' does not contain a definition for 'Name'
   at CallSite.Target(Closure , CallSite , Object )
   at System.Dynamic.UpdateDelegates.UpdateAndExecute1[T0,TRet](CallSite site, T0 arg0)
   at RazorLight.CompiledTemplates.GeneratedTemplate.<ExecuteAsync>d__0.MoveNext() in testtemplate:line 2
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at RazorLight.TemplateRenderer.<RenderPageCoreAsync>d__9.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at RazorLight.TemplateRenderer.<RenderPageAsync>d__8.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at RazorLight.TemplateRenderer.<RenderAsync>d__7.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at RazorLight.RazorLightEngine.<RenderTemplateAsync>d__17.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at RazorLight.RazorLightEngine.<RenderTemplateAsync>d__16.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at RazorLight.RazorLightEngine.<CompileRenderAsync>d__11.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at TestProject.Mailer.Domain.MailTemplate.<>c__DisplayClass17_0.<<WithParameters>b__1>d.MoveNext() in D:\\Projects\\TestProject.Mailer\\TestProject.Mailer\\Domain\\MailTemplate.cs:line 61<---

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 16 (3 by maintainers)

Most upvoted comments

You are casting anonymous object to system.object. Exception message is self explanatory. Don’t generics instead of object in your method. Closing

@MarcoNicolodi if the class ViewModel isn’t public it breaks. If it is public then everything in the sample works just fine.

This code also breaks:

var engine = new RazorLightEngineBuilder()
              .UseMemoryCachingProvider()
              .Build();

string template = "Hello, @Model.Name. Welcome to RazorLight repository";
ViewModel model = new ViewModel() { Name = "John Doe" };

string result = await engine.CompileRenderAsync("templateKey", template, model);

@giammin it works if you use template as a file instead of a in memory template string 😃

This issue should still be reopened though, since the readme.md sample doesnt work (with typed model), at least in my environment (.netcore 2.1.300 on ubuntu 18.04). It only works with anonymously typed model.