【发布时间】:2011-02-22 12:38:52
【问题描述】:
我的印象是 AppDomain 是相互隔离的。似乎在 StackOverException 的情况下,情况并非如此。
为了演示这个问题,我创建了一个简单的控制台应用程序,其唯一目的是生成一个新的 AppDomain,我在其中加载了一个非常简单的程序集并调用其中一个方法。此方法恰好抛出 StackOverflowException。这会导致我的控制台应用程序毫不客气地终止。
我想要的行为是让“子”AppDomain 在此类异常上崩溃并烧毁,但让我的控制台应用程序在“父”AppDomain 中运行,毫发无损。
这可能吗?
更新:这是一些代码。两个异常处理程序均未命中。
class Program
{
static void Main(string[] args)
{
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
// create app domain
var domain = AppDomain.CreateDomain("MyDomain");
// create a component
var component = (MyComponent)domain.CreateInstanceAndUnwrap(
"AppDomainMonitor.Component",
typeof(MyComponent).FullName);
// create a thread from a method on this component
var thread = new Thread(component.CauseStackOverflow);
// start the thread
thread.Start();
Console.ReadKey();
}
static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
// never hit
}
}
public class MyComponent : MarshalByRefObject
{
public void CauseStackOverflow()
{
try
{
Infinite();
}
catch (Exception ex)
{
// never hit
}
}
void Infinite()
{
Infinite();
}
}
【问题讨论】:
-
能看到代码示例吗? :) 我过去设置了类似的东西,它经受住了异常(尽管我不记得尝试过 StackOverflowException)。我很想看看是否有一些古怪的东西导致程序集被加载到您的默认应用程序域中。干杯!
标签: .net appdomain stack-overflow