在.NET 4.6的AppDomain中抛出AccessViolationException(或其他损坏的状态异常)时,如何防止整个应用程序(进程)崩溃?我的目标是在其AppDomain中包装一个第三方库(GdPicture.NET条形码读取器),该库根据IIS故障转储间歇性地使IIS工作进程崩溃。换句话说,我想在自己的应用程序域中隔离第3方库调用,以使它在自身内部失败而无需关闭整个网站。
在下面的示例代码中,如果我不使用HandleProcessCorruptedStateExceptions属性,则无法捕获被抛出在另一个AppDomain(在ClassLibrary1.dll中)中的AccessViolationException(包装在TargetInvocationException中)。如果我装饰了HandleProcessCorruptedStateExceptions(仅适用于第二个AppDomain调用),那么主进程继续运行是否安全还是会变得不稳定?
具有两个项目(控制台应用程序和类库)的Visual Studio解决方案。
using System;
using System.Reflection;
using System.Runtime.ExceptionServices;
using System.Security;
namespace StackOverflowQuestion
{
class Program
{
static void Main(string[] args)
{
new Tester().RunTest();
}
}
class Tester
{
public void RunTest()
{
var myTestAppDomain = AppDomain.CreateDomain("My Test AppDomain");
var loader = (Loader)myTestAppDomain.CreateInstanceAndUnwrap(typeof(Loader).Assembly.FullName, typeof(Loader).FullName);
TestHandleProcessCorruptedStateExceptions(loader, myTestAppDomain);
// how can I make sure process doesn't crash when AppDomain "My Test AppDomain" crashes
Console.WriteLine("This won't be written to console unless [HandleProcessCorruptedStateExceptions] attribute is present...");
// keep console window open
Console.ReadKey();
}
// [HandleProcessCorruptedStateExceptions, SecurityCritical]
private void TestHandleProcessCorruptedStateExceptions(Loader loader, AppDomain appDomain)
{
Console.WriteLine("Loading 3rd party lib ClassLibrary1.dll...");
loader.LoadAssembly(@"..\..\..\ClassLibrary1\bin\debug\ClassLibrary1.dll");
try
{
// executing static method in dummy 3rd party dll in a different app domain (named "My Test AppDomain")
loader.ExecuteStaticMethod("ClassLibrary1.Class1", "DoStuff", DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToLongTimeString());
}
catch (TargetInvocationException)
{
AppDomain.Unload(appDomain);
Console.WriteLine("DoStuff failed. This won't be written to console unless [HandleProcessCorruptedStateExceptions] attribute is present...");
}
}
}
class Loader : MarshalByRefObject
{
private Assembly _assembly;
public void LoadAssembly(string path)
{
_assembly = Assembly.Load(AssemblyName.GetAssemblyName(path));
}
public object ExecuteStaticMethod(string typeName, string methodName, params object[] parameters)
{
var type = _assembly.GetType(typeName);
// assume there is no overloads for simplicity
var method = type.GetMethod(methodName, BindingFlags.Static | BindingFlags.Public);
return method.Invoke(null, parameters);
}
}
}
这是ClassLibrary1项目中Class1.cs中的代码:
using System;
using System.Runtime.InteropServices;
namespace ClassLibrary1
{
public class Class1
{
public static void DoStuff(string msg)
{
Console.WriteLine("Throwing AccessViolationException now...");
// throw an AccessViolationException which cannot be caught in a try/catch block
FakeAccessViolationException();
Console.WriteLine("Class1.DoStuff: " + msg);
}
private static void FakeAccessViolationException()
{
var ptr = new IntPtr(42);
Marshal.StructureToPtr(42, ptr, true);
}
}
}