RazorEngineCore icon indicating copy to clipboard operation
RazorEngineCore copied to clipboard

Previously worked code shows System.InvalidCastException after upgrade to .NET 10

Open garryxiao opened this issue 1 month ago • 0 comments

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".

garryxiao avatar Dec 07 '25 14:12 garryxiao