Error when trying to render from .NET 6 project
See original GitHub issueDescribe the bug I have RazorLight set up in a shared project that multitargets both net48 and net6.0. I have a web project that targets 4.8, and when using RazorLight via the previously mentioned shared project it works fine. I also have a console project that targets net6.0. When attempting to use the same shared project it fails with the below error.
I see that there are several bugs describing a similar error. I’ve tried all of the suggestions on these other issues in addition to the troubleshooting steps outlined in the readme.
Thanks in advance for the help!
To Reproduce
Setup:
RazorLightEngine razorLightEngine = new RazorLightEngineBuilder() .SetOperatingAssembly(type.Assembly) .UseFileSystemProject(templatePath) .UseMemoryCachingProvider() .Build();
Execution:
string renderedContent = await razorLightEngine.CompileRenderAsync(key, model, (ExpandoObject)ViewBag);
Expected behavior The template is rendered without error.
Information (please complete the following information):
- OS: Windows 10
- Platform: .NET Framework 4.8, .NET 6
- RazorLight version: 2.3.0
- Are you using the OFFICIAL RazorLight package? Yes
- Visual Studio version: Visual Studio Enterprise 17.3.6
Additional context Quartz.SchedulerException: Job threw an unhandled exception. —> RazorLight.Compilation.TemplateCompilationException: Failed to compile generated Razor template: - (3:29) The type or namespace name ‘Razor’ does not exist in the namespace ‘RazorLight’ (are you missing an assembly reference?) - (1:6) The type or namespace name ‘GDIT’ could not be found (are you missing a using directive or an assembly reference?) - (25:37) The type or namespace name ‘TemplatePage<>’ could not be found (are you missing a using directive or an assembly reference?) - (25:50) The type or namespace name ‘LeaveNotification’ could not be found (are you missing a using directive or an assembly reference?) - (28:66) ‘GeneratedTemplate.ExecuteAsync()’: no suitable method found to override - (4:4) The name ‘Layout’ does not exist in the current context - (5:30) The name ‘Model’ does not exist in the current context - (6:8) The name ‘Model’ does not exist in the current context - (6:55) The name ‘Model’ does not exist in the current context - (6:98) The name ‘Model’ does not exist in the current context - (8:38) The name ‘Model’ does not exist in the current context - (43:12) The name ‘WriteLiteral’ does not exist in the current context - (12:38) The name ‘Write’ does not exist in the current context - (12:44) The name ‘Model’ does not exist in the current context - (51:12) The name ‘WriteLiteral’ does not exist in the current context - (12:99) The name ‘Write’ does not exist in the current context - (59:12) The name ‘WriteLiteral’ does not exist in the current context - (60:12) The name ‘BeginWriteAttribute’ does not exist in the current context - (15:0) The name ‘WriteAttributeValue’ does not exist in the current context - (15:67) The name ‘ViewBag’ does not exist in the current context - (15:84) The name ‘Model’ does not exist in the current context - (68:12) The name ‘EndWriteAttribute’ does not exist in the current context - (69:12) The name ‘WriteLiteral’ does not exist in the current context - (15:131) The name ‘Write’ does not exist in the current context - (15:175) The name ‘ViewBag’ does not exist in the current context - (15:193) The name ‘Model’ does not exist in the current context - (77:12) The name ‘WriteLiteral’ does not exist in the current context
See CompilationErrors for detailed information
at RazorLight.Compilation.RoslynCompilationService.CompileAndEmit(IGeneratedRazorTemplate razorTemplate)
at RazorLight.Compilation.RazorTemplateCompiler.CompileAndEmitAsync(RazorLightProjectItem projectItem)
at RazorLight.Compilation.RazorTemplateCompiler.OnCacheMissAsync(String templateKey)
at RazorLight.Compilation.RazorTemplateCompiler.OnCacheMissAsync(String templateKey)
at RazorLight.EngineHandler.CompileTemplateAsync(String key)
at RazorLight.EngineHandler.CompileRenderAsync[T](String key, T model, ExpandoObject viewBag)
at Notifications.Jobs.JobBase`1.BuildTemplate(String template, Object model) in Source\Notifications\Jobs\JobBase.cs:line 285
at Notifications.Jobs.Leave.NewLeave.BuildMessage() in Source\Notifications\Jobs\Leave\NewLeave.cs:line 28
at Notifications.Jobs.Leave.NewLeave.Execute(IJobExecutionContext context) in Notifications\Jobs\Leave\NewLeave.cs:line 41
at Quartz.Core.JobRunShell.Run(CancellationToken cancellationToken)
--- End of inner exception stack trace --- [See nested exception: RazorLight.Compilation.TemplateCompilationException: Failed to compile generated Razor template:
- (3:29) The type or namespace name 'Razor' does not exist in the namespace 'RazorLight' (are you missing an assembly reference?)
- (1:6) The type or namespace name 'Project' could not be found (are you missing a using directive or an assembly reference?)
- (25:37) The type or namespace name 'TemplatePage<>' could not be found (are you missing a using directive or an assembly reference?)
- (25:50) The type or namespace name 'LeaveNotification' could not be found (are you missing a using directive or an assembly reference?)
- (28:66) 'GeneratedTemplate.ExecuteAsync()': no suitable method found to override
- (4:4) The name 'Layout' does not exist in the current context
- (5:30) The name 'Model' does not exist in the current context
- (6:8) The name 'Model' does not exist in the current context
- (6:55) The name 'Model' does not exist in the current context
- (6:98) The name 'Model' does not exist in the current context
- (8:38) The name 'Model' does not exist in the current context
- (43:12) The name 'WriteLiteral' does not exist in the current context
- (12:38) The name 'Write' does not exist in the current context
- (12:44) The name 'Model' does not exist in the current context
- (51:12) The name 'WriteLiteral' does not exist in the current context
- (12:99) The name 'Write' does not exist in the current context
- (59:12) The name 'WriteLiteral' does not exist in the current context
- (60:12) The name 'BeginWriteAttribute' does not exist in the current context
- (15:0) The name 'WriteAttributeValue' does not exist in the current context
- (15:67) The name 'ViewBag' does not exist in the current context
- (15:84) The name 'Model' does not exist in the current context
- (68:12) The name 'EndWriteAttribute' does not exist in the current context
- (69:12) The name 'WriteLiteral' does not exist in the current context
- (15:131) The name 'Write' does not exist in the current context
- (15:175) The name 'ViewBag' does not exist in the current context
- (15:193) The name 'Model' does not exist in the current context
- (77:12) The name 'WriteLiteral' does not exist in the current context
See CompilationErrors for detailed information
at RazorLight.Compilation.RoslynCompilationService.CompileAndEmit(IGeneratedRazorTemplate razorTemplate)
at RazorLight.Compilation.RazorTemplateCompiler.CompileAndEmitAsync(RazorLightProjectItem projectItem)
at RazorLight.Compilation.RazorTemplateCompiler.OnCacheMissAsync(String templateKey)
at RazorLight.Compilation.RazorTemplateCompiler.OnCacheMissAsync(String templateKey)
at RazorLight.EngineHandler.CompileTemplateAsync(String key)
at RazorLight.EngineHandler.CompileRenderAsync[T](String key, T model, ExpandoObject viewBag)
at Notifications.Jobs.JobBase`1.BuildTemplate(String template, Object model) in Source\Notifications\Jobs\JobBase.cs:line 285
at Notifications.Jobs.Leave.NewLeave.BuildMessage() in Source\Notifications\Jobs\Leave\NewLeave.cs:line 28
at Notifications.Jobs.Leave.NewLeave.Execute(IJobExecutionContext context) in Source\Notifications\Jobs\Leave\NewLeave.cs:line 41
at Quartz.Core.JobRunShell.Run(CancellationToken cancellationToken)]
Issue Analytics
- State:
- Created a year ago
- Comments:9
Top GitHub Comments
“Proprietary templating system” was just a convenient shorthand for “closed source code base which makes use of templating so I can’t just cut & paste my code to show you the problem”. I am not simply wrapping RazorLight to re-sell.
For more context, it’s a web server framework with object remoting over WebSockets, templating, and the rest of the kitchen sink, developed internally many years ago, which all of our applications depend on. If we want to make progress towards .NET 6, we need to port this framework so as to avoid rewriting all of our applications to use something else.
@czielin My project needs a custom template page base class because it adds some methods and properties for pages to use (e.g. dependency injection, HTTP context). That may be a bad way of doing things, but as an existing framework, other projects depend on the feature set it provides, so I can’t just change it.
Ultimately, my goal is to get applications using Blazor where appropriate, but to get there they first need to work on .NET 6 with the existing frameworks.
I had to alter my TemplateBase class (as referenced in the template above) to inherit from RazorLight’s TemplatePage.