更新VS后使用dotnet watch修改Layout.cshtml触发HTTP 500错误求助
ASP.NET中修改Layout.cshtml后dotnet watch触发HTTP 500错误(Visual Studio更新后出现)
问题描述
我用ASP.NET开发网站,一直通过dotnet watch实时查看开发进度,此前一切正常。但今日更新Visual Studio后,修改代码并保存以预览网站变化时,页面出现HTTP ERROR 500错误。目前已回滚到VS旧版本继续开发,寻求问题原因及解决方案。
补充:已定位到问题仅在修改Layout.cshtml时触发,其他页面修改无异常,但担心网站部署后出现同类问题。
错误日志(中文翻译)
fail: Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware[1] 执行请求时发生未处理的异常。 fail: Microsoft.AspNetCore.Server.Kestrel[13] 连接 ID "0HMK58AJSOF5Q",请求 ID "0HMK58AJSOF5Q:00000007":应用程序抛出未处理的异常。 System.AggregateException: 写入日志记录器时发生错误。(无法从程序集 'PortaBlu, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' 加载类型 'PortaBlu.Pages.Pages_Index+<ExecuteAsync>d__34#1'。) (无法从程序集 'PortaBlu, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' 加载类型 'PortaBlu.Pages.Pages_Index+<ExecuteAsync>d__34#1'。) ---> System.TypeLoadException: 无法从程序集 'PortaBlu, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' 加载类型 'PortaBlu.Pages.Pages_Index+<ExecuteAsync>d__34#1'。 在 System.Reflection.CustomAttribute._CreateCaObject(RuntimeModule pModule, RuntimeType type, IRuntimeMethodInfo pCtor, Byte** ppBlob, Byte* pEndBlob, Int32* pcNamedArgs) 在 System.Reflection.CustomAttribute.AddCustomAttributes(ListBuilder`1& attributes, RuntimeModule decoratedModule, Int32 decoratedMetadataToken, RuntimeType attributeFilterType, Boolean mustBeInheritable, ListBuilder`1 derivedAttributes) 在 System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeModule decoratedModule, Int32 decoratedMetadataToken, Int32 pcaCount, RuntimeType attributeFilterType) 在 System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeMethodInfo method, RuntimeType caType, Boolean inherit) 在 System.Reflection.RuntimeMethodInfo.GetCustomAttributes(Type attributeType, Boolean inherit) 在 System.Attribute.GetCustomAttributes(MemberInfo element, Type attributeType, Boolean inherit) 在 System.Reflection.CustomAttributeExtensions.GetCustomAttributes[T](MemberInfo element, Boolean inherit) 在 System.Diagnostics.StackTrace.TryResolveStateMachineMethod(MethodBase& method, Type& declaringType) 在 System.Diagnostics.StackTrace.ToString(TraceFormat traceFormat, StringBuilder sb) 在 System.Diagnostics.StackTrace.ToString(TraceFormat traceFormat) 在 System.Exception.get_StackTrace() 在 System.Exception.ToString() 在 Microsoft.Extensions.Logging.Console.SimpleConsoleFormatter.CreateDefaultLogMessage[TState](TextWriter textWriter, LogEntry`1& logEntry, String message, IExternalScopeProvider scopeProvider) 在 Microsoft.Extensions.Logging.Console.SimpleConsoleFormatter.Write[TState](LogEntry`1& logEntry, IExternalScopeProvider scopeProvider, TextWriter textWriter) 在 Microsoft.Extensions.Logging.Console.ConsoleLogger.Log[TState](LogLevel logLevel, EventId eventId, TState state, Exception exception, Func`3 formatter) 在 Microsoft.Extensions.Logging.Logger.<Log>g__LoggerLog|12_0[TState](LogLevel logLevel, EventId eventId, ILogger logger, Exception exception, Func`3 formatter, List`1& exceptions, TState& state) --- 内部异常堆栈跟踪结束 --- 在 Microsoft.Extensions.Logging.Logger.ThrowLoggingError(List`1 exceptions) 在 Microsoft.Extensions.Logging.Logger.Log[TState](LogLevel logLevel, EventId eventId, TState state, Exception exception, Func`3 formatter) 在 Microsoft.Extensions.Logging.Logger`1.Microsoft.Extensions.Logging.ILogger.Log[TState](LogLevel logLevel, EventId eventId, TState state, Exception exception, Func`3 formatter) 在 Microsoft.Extensions.Logging.LoggerMessage.<>c__DisplayClass8_0.<Define>g__Log|0(ILogger logger, Exception exception) 在 Microsoft.AspNetCore.Diagnostics.DiagnosticsLoggerExtensions.UnhandledException(ILogger logger, Exception exception) 在 Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context) 在 Microsoft.AspNetCore.Watch.BrowserRefresh.BrowserRefreshMiddleware.InvokeAsync(HttpContext context) 在 Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.ProcessRequests[TContext](IHttpApplication`1 application) ---> (内部异常 #1) System.TypeLoadException: 无法从程序集 'PortaBlu, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' 加载类型 'PortaBlu.Pages.Pages_Index+<ExecuteAsync>d__34#1'。 在 System.Reflection.CustomAttribute._CreateCaObject(RuntimeModule pModule, RuntimeType type, IRuntimeMethodInfo pCtor, Byte** ppBlob, Byte* pEndBlob, Int32* pcNamedArgs) 在 System.Reflection.CustomAttribute.AddCustomAttributes(ListBuilder`1& attributes, RuntimeModule decoratedModule, Int32 decoratedMetadataToken, RuntimeType attributeFilterType, Boolean mustBeInheritable, ListBuilder`1 derivedAttributes) 在 System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeModule decoratedModule, Int32 decoratedMetadataToken, Int32 pcaCount, RuntimeType attributeFilterType) 在 System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeMethodInfo method, RuntimeType caType, Boolean inherit) 在 System.Reflection.RuntimeMethodInfo.GetCustomAttributes(Type attributeType, Boolean inherit) 在 System.Attribute.GetCustomAttributes(MemberInfo element, Type attributeType, Boolean inherit) 在 System.Reflection.CustomAttributeExtensions.GetCustomAttributes[T](MemberInfo element, Boolean inherit) 在 System.Diagnostics.StackTrace.TryResolveStateMachineMethod(MethodBase& method, Type& declaringType) 在 System.Diagnostics.StackTrace.ToString(TraceFormat traceFormat, StringBuilder sb) 在 System.Diagnostics.StackTrace.ToString(TraceFormat traceFormat) 在 System.Exception.get_StackTrace() 在 System.Exception.ToString() 在 System.Text.StringBuilder.Append(Object value) 在 Microsoft.Extensions.Logging.EventLog.EventLogLogger.Log[TState](LogLevel logLevel, EventId eventId, TState state, Exception exception, Func`3 formatter) 在 Microsoft.Extensions.Logging.Logger.<Log>g__LoggerLog|12_0[TState](LogLevel logLevel, EventId eventId, ILogger logger, Exception exception, Func`3 formatter, List`1& exceptions, TState& state)<---
原因分析
这是Visual Studio更新后,dotnet watch的热重载机制与Razor布局页的编译逻辑冲突导致的问题:修改Layout.cshtml会触发全站视图的重新编译,但旧程序集未被正确卸载,导致运行时尝试加载已失效的异步状态机类型(Pages_Index+<ExecuteAsync>d__34#1),最终抛出TypeLoadException。部署环境下通常不会触发热重载,所以大概率不会出现同类问题,但仍需确保编译环境的一致性。
解决方案
临时应急方案
- 修改
Layout.cshtml后,手动终止dotnet watch进程,重新启动,绕过热重载的冲突。 - 清理项目的编译输出目录,重新构建:
Remove-Item -Recurse .\bin, .\obj dotnet build dotnet watch run
长期修复方案
- 检查Visual Studio的更新通道,安装最新的小版本补丁,微软通常会快速修复这类热重载的兼容性问题。
- 确保项目使用的.NET SDK版本与Visual Studio版本匹配(例如VS 2022对应.NET 6+),避免跨版本兼容问题。
- 在开发环境禁用Razor视图预编译,修改项目文件(
.csproj):<PropertyGroup> <!-- 开发环境禁用预编译,发布时启用 --> <RazorCompileOnBuild>false</RazorCompileOnBuild> <RazorCompileOnPublish>true</RazorCompileOnPublish> </PropertyGroup>
内容的提问来源于stack exchange,提问作者Daniel Stephens
相关产品推荐
相关产品推荐

