Skip to content

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

@garryxiao

Description

@garryxiao

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions