soundaranbu / Razor.Templating.Core

Razor Templating Engine to render Razor Views(.cshtml files) to String in Console, Web, Service, Desktop workloads in .NET Core 3+

Home Page:https://soundaranbu.medium.com/render-razor-view-cshtml-to-string-in-net-core-7d125f32c79

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

System.InvalidOperationException issue in 1.6

VictorDevmetro opened this issue · comments

/* CS*/
return await RazorTemplateEngine.RenderAsync($"~/Views/{rendererActionName}.cshtml", viewModel, viewData);

/CSHTML/
@model ForgotPasswordEmail

This cimple code genereate exception "System.InvalidOperationException: The model item passed into the ViewDataDictionary is of type 'EmailTemplates.ViewModels.ForgotPasswordEmail', but this ViewDataDictionary instance requires a model item of type 'EmailTemplates.ViewModels.ForgotPasswordEmail'."

PS: works refecly in prev version (1.5)

Hi @VictorDevmetro, What is your .NET version?

Ok .. If possible, Can you provide a sample project to reproduce the issue?

Hi @VictorDevmetro, I couldn't reproduce this issue when I use it in applications(you can try out the sample applications here & let me know ). But however when I run the test cases in Visual Studio, I encounter this issue. But the same test cases ran in .NET CLI using dotnet test command doesn't throw this error. Pretty interesting bug. I'll look into it. In the meanwhile keep using v1.5.0.
Thanks!

Update: I updated the test project form MSTest to xUnit .. now there's no such error in both Visual Studio & .NET CLI

If you can provide me a sample project, I'll be able to look at it.

Thanks :)

@soundaranbu Sorry for delay.
I've created small solution to reproduce a bug.
https://github.com/VictorDevmetro/RazorTemplatingIssue1.6/tree/master

Thanks, I'll take a look.

Hi @VictorDevmetro , When we remove RazorTemplateEngine.Initialize(); from your Program.cs, the error is gone. The library creates the cache itself when it's run for the first time. So you can safely remove it.

I'll investigate why it's throwing error in your use case in v1.6.0

Update: Problem is specific to worker service when RazorTemplateEngine.Initialize(); is called in bootstrap code in v1.6.0. It's advised to remove this line until a fix is released.
Please note, removing this line doesn't make any big difference. The core feature will work as before.

Thanks!

Thx. It fixed my problem.

Issue fixed in v1.7.0 in .NET 6