优雅地处理.NET Core中的损坏状态异常 [英] Gracefully handling corrupted state exceptions in .NET Core

查看:578
本文介绍了优雅地处理.NET Core中的损坏状态异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我基本上有一个重复的问题,但针对.NET Core。

I have basically a duplicate question but for .NET Core.

我有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

。但是没有任何效果,该应用程序始终会在不打印异常的情况下严重崩溃。

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

您可以尝试

You can try a few more options, specifically FailFastOnCorruptedStateException combined with legacyCorruptedStateExceptionsPolicy.

这篇关于优雅地处理.NET Core中的损坏状态异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆