RazorEngineCore
RazorEngineCore copied to clipboard
Previously worked code shows System.InvalidCastException after upgrade to .NET 10
System.InvalidCastException: Unable to cast object of type 'TemplateNamespace.Template' to type 'com.etsoo.Web.RazorHtmlSafeTemplate'. com.etsoo.Web.RazorHtmlSafeTemplate is extended from RazorEngineTemplateBase.
` // Get or add the compiled version if (!templateCache.TryGetValue(key, out var compiledTemplate)) { // Engine var razorEngine = new RazorEngine();
// Compile
compiledTemplate = await razorEngine.CompileAsync<RazorHtmlSafeTemplate>(template, builder =>
{
builder.AddAssemblyReference(typeof(M));
if (types != null)
{
foreach (var assembly in types)
{
builder.AddAssemblyReference(assembly);
}
}
}, cancellationToken);
templateCache.TryAdd(key, compiledTemplate);
}
// Run and return the result
return await compiledTemplate.RunAsync(template => template.Model = model);
`
Debug shows it happens inside the "RunAsync".