You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET 8升级后RazorLight邮件模板生成器异常求助

.NET 8升级后RazorLight邮件生成器抛出TypeLoadException异常

项目升级至.NET 8.0并更新所有NuGet包至最新版本后,基于RazorLight的邮件内容生成器停止工作,抛出如下异常:

An unhandled exception has occurred while executing the request.
System.TypeLoadException: Type '<ExecuteAsync>d__0' from assembly 'hmc151k1.dzl, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' tried to override method 'MoveNext' but does not implement or inherit that method.
   at RazorLight.CompiledTemplates.GeneratedTemplate.ExecuteAsync()
   at RazorLight.TemplateRenderer.RenderPageCoreAsync(ITemplatePage page, PageContext context)
   at RazorLight.TemplateRenderer.RenderPageAsync(ITemplatePage page, PageContext context, Boolean invokeViewStarts)
   at RazorLight.TemplateRenderer.RenderAsync(ITemplatePage page)
   at RazorLight.EngineHandler.RenderTemplateAsync[T](ITemplatePage templatePage, T model, TextWriter textWriter, ExpandoObject viewBag)
   at RazorLight.EngineHandler.RenderTemplateAsync[T](ITemplatePage templatePage, T model, ExpandoObject viewBag)
   at RazorLight.EngineHandler.CompileRenderAsync[T](String key, T model, ExpandoObject viewBag)
   at API.Service.Email.Helpers.RazorParser.ParseAsync[T](String template, T model) in D:\Projects\Projekti\API\api\API.Service\Email\Helpers\RazorParser.cs:line 28
   at API.Service.Email.Helpers.RazorParser.UsingTemplateFromEmbeddedAsync[T](String path, T model) in D:\Projects\Projekti\API\api\API.Service\Email\Helpers\RazorParser.cs:line 20
   at API.Service.Email.EmailContentGenerator.GenerateAsync[T](T emailDataModel) in D:\Projects\Projekti\API\api\API.Service\Email\EmailContentGenerator.cs:line 51
   at API.Service.Email.EmailDispatcher.Dispatch[T](String recipient, String subject, T emailDataModel) in D:\Projects\Projekti\API\api\API.Service\Email\EmailDispatcher.cs:line 22
   at API.Service.Auth.GenerateLoginTokenHandler.Handle(GenerateLoginTokenCommand request, CancellationToken cancellationToken) in D:\Projects\Projekti\API\api\API.Service\Auth\GenerateLoginTokenHandler.cs:line 73
   at API.Controllers.AuthController.GenerateLoginToken(LoginTokenGenerateViewModel loginViewModel) in D:\Projects\Projekti\API\api\API.Controllers\AuthController.cs:line 63
   at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.TaskOfIActionResultExecutor.Execute(ActionContext actionContext, IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeActionMethodAsync>g__Awaited|12_0(ControllerActionInvoker invoker, ValueTask`1 actionResultValueTask)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeNextActionFilterAsync>g__Awaited|10_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed context)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeInnerFilterAsync>g__Awaited|13_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextResourceFilter>g__Awaited|25_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResourceExecutedContextSealed context)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeFilterPipelineAsync>g__Awaited|20_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Logged|17_1(ResourceInvoker invoker)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Logged|17_1(ResourceInvoker invoker)
   at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|7_0(Endpoint endpoint, Task requestTask, ILogger logger)
   at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddlewareImpl.Invoke(HttpContext context)

异常中from assembly与Version=0.0.0.0之间的程序集名称每次报错都会变化。

当前RazorParser.cs的实现代码如下:

public class RazorParser
{
    private readonly Assembly assembly;

    public RazorParser(Assembly assembly)
    {
        this.assembly = assembly;
    }

    public async Task<string> UsingTemplateFromEmbeddedAsync<T>(string path, T model)
    {
        var template = EmbeddedResourceHelper.GetResourceAsString(assembly, GenerateFileAssemblyPath(path, assembly));
        return await ParseAsync(template, model);
    }

    async Task<string> ParseAsync<T>(string template, T model)
    {
        var project = new InMemoryRazorLightProject();
        var engine = new RazorLightEngineBuilder().UseProject(project).Build();

        return await engine.CompileRenderStringAsync<T>(Guid.NewGuid().ToString(), template, model);
    }

    string GenerateFileAssemblyPath(string template, Assembly fileAssembly)
    {
        string assemblyName = fileAssembly.GetName().Name;
        return $"{assemblyName}.Email.{template}.\"cshtml\"";
    }
}

另外,更新包后已将项目从包含Program.cs和Startup.cs改为仅保留Program.cs,确认所有内容迁移完整无遗漏。求问是否有开发者遇到过类似问题并找到了解决方案?


内容的提问来源于stack exchange,提问作者predefined_

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.29 22:32:04