.NET Core 2.0中捕获AccessViolationException的方法(兼容.NET Framework4.0)
Great question—dealing with cross-framework exception handling for corrupted state errors can be tricky, especially since .NET Core changed how these are handled compared to the old .NET Framework. Let’s break down what’s going on and how to get this working in .NET Core 2.0.
First, the quick context: The [HandleProcessCorruptedStateExceptions] attribute is specific to .NET Framework. In .NET Core, the runtime uses a different mechanism to control whether corrupted state exceptions (CSEs like AccessViolationException) can be caught. Even setting the old .NET Framework environment variable won’t help here—we need to use Core-specific tools.
1. Use the AppContext Switch (Recommended for Code-Level Control)
The most reliable way to enable CSE handling in .NET Core 2.0 is to set an AppContext switch before executing the code that might throw the exception. This lets you control the behavior directly in your app, no external environment variables needed.
Add this line early in your method (or during app startup, if the risky code runs later):
AppContext.SetSwitch("System.Runtime.ExceptionServices.HandleProcessCorruptedStateExceptions", true);
Then wrap your Pkcs11Interop calls in a targeted try-catch block:
try { // Your Pkcs11Interop driver calls go here } catch (AccessViolationException ex) { // Log every detail of the exception for debugging (stack trace, driver info, etc.) // IMPORTANT: Do NOT try to continue normal execution! The process state is corrupted. // Gracefully shut down the app or the affected component to avoid crashes or security issues }
2. Verify Environment Variable Setup (If You Prefer External Control)
If you’d rather control this via environment variables (for example, for testing or deployment scenarios), note that .NET Core uses a different variable name than .NET Framework:
- .NET Framework:
COR_ENABLE_CORRUPTED_STATE_EXCEPTIONS=1 - .NET Core 2.0+:
COMPlus_EnableCorruptedStateExceptions=1
Make sure you’re setting the correct variable, and that it’s applied before your app starts—environment variables set after launch won’t take effect.
3. Keep Your Code Cross-Framework Compatible
Since your app targets both .NET Framework 4.0 and .NET Core 2.0, use conditional compilation to keep the code clean and framework-appropriate:
#if NETFRAMEWORK using System.Runtime.ExceptionServices; #endif public void RunPkcs11Operations() { #if NETCOREAPP // Enable CSE handling for .NET Core AppContext.SetSwitch("System.Runtime.ExceptionServices.HandleProcessCorruptedStateExceptions", true); #endif #if NETFRAMEWORK [HandleProcessCorruptedStateExceptions] // Only apply this attribute to .NET Framework code #endif try { // Your shared Pkcs11Interop logic here } catch (AccessViolationException ex) { // Handle exception consistently across frameworks: log, shut down, etc. } }
Critical Reminder
Once you catch an AccessViolationException, the process’s memory is in an unstable state. Resuming normal operations can lead to unpredictable crashes, data corruption, or security vulnerabilities. Always prioritize logging as much information as possible and gracefully terminating the app or affected component.
内容的提问来源于stack exchange,提问作者Prashanth

