优雅地处理 .NET Core 中的损坏状态异常 [英] Gracefully handling corrupted state exceptions in .NET Core
问题描述
我基本上有一个重复的问题,但对于 .NET Core.
I have basically a duplicate question but for .NET Core.
我有核心控制台应用程序:
I have Core console app:
class Program
{
static void DoSomeAccessViolation()
{
// if you have any questions about why this throws,
// the answer is "42", of course
var ptr = new IntPtr(42);
Marshal.StructureToPtr(42, ptr, true);
}
[SecurityCritical]
[HandleProcessCorruptedStateExceptions]
static void Main(string[] args)
{
try
{
DoSomeAccessViolation();
}
catch (Exception ex)
{
Console.Error.WriteLine(ex);
}
}
}
我尝试添加一个 Settings.setting 文件
I've tried to add a Settings.setting file
<SettingsFile xmlns="http://schemas.microsoft.com/VisualStudio/2004/01/settings" CurrentProfile="(Default)" GeneratedClassNamespace="ConsoleApp2" GeneratedClassName="Settings1">
<Profiles />
<Settings>
<Setting Name="legacyCorruptedStateExceptionsPolicy" Type="System.Boolean" Scope="Application">
<Value Profile="(Default)">True</Value>
</Setting>
</Settings>
</SettingsFile>
我尝试根据 文档:
C:>set COMPlus_legacyCorruptedStateExceptionsPolicy=1
C:>dotnet run
或
C:>set COMPlus_legacyCorruptedStateExceptionsPolicy=true
C:>dotnet run
(在 Windows 上).但没有任何效果,应用程序总是在不打印异常的情况下严重崩溃.
(on Windows). But nothing works, the app always crashes hard without printing the exception.
推荐答案
我一直在按时间顺序挖掘这个主题的一些背景...
Some background I've been digging up on the subject, in chronological order...
dotnet/coreclr 问题 #9045:剥离损坏状态异常处理
dotnet/coreclr Issue #9045: Strip corrupted state exceptions handling
dotnet/coreclr PR #10957:不支持属性 HandleProcessCorruptedStateExceptions
dotnet/coreclr PR #10957: Do not honor attribute HandleProcessCorruptedStateExceptions
dotnet/coreclr 问题 #19192:无法在托管代码中捕获任何用户Linux 原生代码抛出的异常
dotnet/coreclr Issue #19192: Unable to catch in managed code any user exception thrown from native Linux code
我目前是 SOL.必须创建各种非托管包装器来捕获外部 CSE.
I'm SOL currently. Have to create an unmanaged wrapper of sorts to catch external CSEs.
你可以试试更多选项,特别是 FailFastOnCorruptedStateException 与 legacyCorruptedStateExceptionsPolicy 相结合.
You can try a few more options, specifically FailFastOnCorruptedStateException combined with legacyCorruptedStateExceptionsPolicy.
这篇关于优雅地处理 .NET Core 中的损坏状态异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!