.NET 7程序在非英文Win10下出现无限递归崩溃问题求助
.NET 7程序在非英文Windows 10上的无限递归崩溃解决方案
问题背景
使用.NET 7(VS 2022)开发的程序在非英文Windows 10(x64)系统上多数运行正常,但偶尔会触发事件查看器错误并弹窗退出,核心问题为.NET系统资源查找时出现无限递归。
错误日志
Application: XXX.EXE CoreCLR Version: 7.0.1323.51816 .NET Version: 7.0.13 Description: The application requested process termination through System.Environment.FailFast. Message: Encountered infinite recursion while looking up resource 'Arg_NullReferenceException' in System.Private.CoreLib. Verify the installation of .NET is complete and does not need repairing, and that the state of the process has not become corrupted. Stack: at System.Environment.FailFast(System.String) at System.SR.InternalGetResourceString(System.String) at System.SR.GetResourceString(System.String) at System.NullReferenceException..ctor() at System.Globalization.CultureInfo.get_CurrentUICulture() at System.Resources.ResourceManager.GetString(System.String, System.Globalization.CultureInfo) at System.SR.InternalGetResourceString(System.String) at System.SR.GetResourceString(System.String) at System.NullReferenceException..ctor() at xxx (related to my code)
解决方案
修复/更新.NET 7运行时
目标机器的.NET 7运行时可能损坏或版本存在已知bug:- 打开命令提示符执行
dotnet --info,确认已安装的.NET 7版本为7.0.13或更高补丁版; - 使用官方.NET修复工具扫描并修复运行时损坏;
- 若修复无效,直接下载安装.NET 7的最新补丁版本(.NET 7支持至2024年5月,后续补丁已修复部分文化相关递归问题)。
- 打开命令提示符执行
显式设置程序UI文化
问题根源是CultureInfo.CurrentUICulture获取异常引发资源查找死循环,在程序启动阶段显式设置UI文化:
在Program.cs入口处添加代码:using System.Globalization; // 可设置为目标系统文化(如"zh-CN")或中性文化"en-US" CultureInfo.DefaultThreadCurrentUICulture = new CultureInfo("en-US"); CultureInfo.DefaultThreadCurrentCulture = new CultureInfo("en-US");提前处理代码中的空引用异常
崩溃由用户代码中的空引用触发,后续系统加载异常资源时进入死循环:- 定位
xxx标记代码段中的空引用风险点,添加空值检查(如if (obj == null)); - 在风险代码块添加try-catch,手动处理异常,避免依赖系统默认异常资源:
try { // 原业务逻辑代码 } catch (NullReferenceException ex) { // 自定义日志记录或用户提示逻辑 Logger.Error("空引用异常:" + ex.Message); // 按需选择优雅退出或继续执行 }
- 定位
检查目标系统区域设置
非英文系统的区域设置可能损坏,导致.NET无法加载文化资源:- 打开Windows设置→时间和语言→语言和区域,确认当前区域设置正常;
- 尝试切换区域为英文后再切回原语言,修复区域设置缓存。
内容的提问来源于stack exchange,提问作者Linus
相关产品推荐
相关产品推荐

