在不同的AppDomain对象实例中发生的异常会使整个应用程序崩溃 [英] Exception occurring in different AppDomain object instance crashes the entire application

查看:49
本文介绍了在不同的AppDomain对象实例中发生的异常会使整个应用程序崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在应用程序主(控制台应用程序.NET 4.6.1)中,我在不同的AppDomain中创建一个新对象。

In the application main (console application .NET 4.6.1) , I am creating a new object in different AppDomain.

如果在执行此对象的方法时出现异常,则整个应用程序崩溃。

If there is an exception while executing a method of this object, the entire application crashes.

下面是示例代码。

namespace TestAppDomain
{
  class Program
  {
    static void Main(string[] args)
    {
      AppDomainSetup domaininfo = new AppDomainSetup();
      domaininfo.ApplicationBase = Directory.GetParent
                                   (Assembly.GetExecutingAssembly().Location).FullName;
      domaininfo.ConfigurationFile = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;
      Evidence evidence = AppDomain.CurrentDomain.Evidence;
      AppDomain appDomain = AppDomain.CreateDomain
                            ("TestAppDomain",
                             evidence,
                             domaininfo);
      var currentAssemblyPath = Assembly.GetExecutingAssembly().Location;
      var parentDir = Directory.GetParent(currentAssemblyPath).FullName;
      var proxy = (TestClass)appDomain.CreateInstanceFromAndUnwrap
                  (Assembly.GetExecutingAssembly().Location,
                   "TestAppDomain.TestClass");
      proxy.Process();
      Console.WriteLine("Main is still running");
    }
  }
  public class TestClass : MarshalByRefObject
  {
    public void Process()
    {
      throw new Exception();
    }
  }
}

这是预期的行为还是应用程序域或对象的创建方式有问题?

Is this the expected behavior or is there something wrong in the way the app domain or the object is created?

我希望应用程序不会崩溃,因为异常发生在不同的应用程序域中(不在运行main()的应用程序域中)

I expect the application not to crash as the exception is occurring in a different app domain (not in the app domain where main() is running)

即使在不同的app域对象中发生异常,如何使main()继续运行?

How do I make the main() to keep running even though an exception occurs in different app domain object?

推荐答案

我认为这是预期的行为。

让我们考虑
流程函数不是 void 但返回一些值。如果异常没有传播到执行
'r = proxy.Process()'的调用域,则不清楚将获得什么结果。

Let us consider Process function that is not void but returns some value. If exceptions were not propagated to calling domain executing ‘r = proxy.Process()’, then it is not clear what result will be obtained.

为了继续使用主域名,请尝试
try-catch

尝试


{

  proxy.Process();

}

catch(例外情况)

{

< span style ="margin:0px">  
//处理异常

   。 。 。

}

try
{
  proxy.Process();
}
catch( Exception exc )
{
   // process exception
   . . .
}

如果你需要一种不会影响主要的准隔离环境代码,然后可以考虑一个
线程,它有一个正确的 try-catch 来拦截异常。

If you need a kind of quasi-isolated environment that does not influence the main code, then maybe consider a Thread, which has a proper try-catch that intercepts the exceptions.


这篇关于在不同的AppDomain对象实例中发生的异常会使整个应用程序崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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