为什么在单独的 AppDomain 上运行的代码会导致我的进程崩溃?
Why this code which runs on seperate AppDomain Crashes my process?
我正在尝试了解如何使用 AppDomains。
需求:
我有一个动态加载 dll 并使用反射调用它的单进程 Web 应用程序。
除了在 "external" 代码和我的基本代码之间创建分隔之外,我还想确保加载的 dll 中的崩溃不会导致进程崩溃。
所以我有这个 "isloated" class:
public sealed class Isolated<T> : IDisposable where T : MarshalByRefObject
{
private AppDomain _domain;
private T _value;
public Isolated()
{
_domain = AppDomain.CreateDomain("Isolated:" + Guid.NewGuid(),
null, AppDomain.CurrentDomain.SetupInformation);
Type type = typeof(T);
_value = (T)_domain.CreateInstanceAndUnwrap(type.Assembly.FullName, type.FullName);
}
public T Value
{
get
{
return _value;
}
}
public void Dispose()
{
if (_domain != null)
{
AppDomain.Unload(_domain);
_domain = null;
}
}
}
我在下面写了这段代码,我的期望是它不会破坏进程,但确实如此。
public class Work : MarshalByRefObject
{
public void DoSomething()
{
Thread thread = new Thread(new ThreadStart(() =>
{
throw new Exception();
}));
thread.IsBackground = true;
thread.Start();
while (true)
{
System.Diagnostics.Trace.WriteLine("Hello from main thread");
}
}
}
protected void Page_Load(object sender, EventArgs e)
{
using (Isolated<Work> isolated = new Isolated<Work>())
{
isolated.Value.DoSomething();
}
}
你能帮我理解我做错了什么吗?
无法阻止进程终止。
根据@Jehof 的建议,
更多信息在这里:http://www.codeproject.com/Articles/635497/AppDomains-Wont-Protect-Host-From-a-Failing-Plugin
我正在尝试了解如何使用 AppDomains。
需求:
我有一个动态加载 dll 并使用反射调用它的单进程 Web 应用程序。
除了在 "external" 代码和我的基本代码之间创建分隔之外,我还想确保加载的 dll 中的崩溃不会导致进程崩溃。
所以我有这个 "isloated" class:
public sealed class Isolated<T> : IDisposable where T : MarshalByRefObject
{
private AppDomain _domain;
private T _value;
public Isolated()
{
_domain = AppDomain.CreateDomain("Isolated:" + Guid.NewGuid(),
null, AppDomain.CurrentDomain.SetupInformation);
Type type = typeof(T);
_value = (T)_domain.CreateInstanceAndUnwrap(type.Assembly.FullName, type.FullName);
}
public T Value
{
get
{
return _value;
}
}
public void Dispose()
{
if (_domain != null)
{
AppDomain.Unload(_domain);
_domain = null;
}
}
}
我在下面写了这段代码,我的期望是它不会破坏进程,但确实如此。
public class Work : MarshalByRefObject
{
public void DoSomething()
{
Thread thread = new Thread(new ThreadStart(() =>
{
throw new Exception();
}));
thread.IsBackground = true;
thread.Start();
while (true)
{
System.Diagnostics.Trace.WriteLine("Hello from main thread");
}
}
}
protected void Page_Load(object sender, EventArgs e)
{
using (Isolated<Work> isolated = new Isolated<Work>())
{
isolated.Value.DoSomething();
}
}
你能帮我理解我做错了什么吗?
无法阻止进程终止。
根据@Jehof 的建议,
更多信息在这里:http://www.codeproject.com/Articles/635497/AppDomains-Wont-Protect-Host-From-a-Failing-Plugin