如何在.Net Process中隔离非托管代码崩溃

时间:2021-04-18 07:32:03

.Net Console Application depends on COM objects that tend to crash from time to time in very unpredictable way. Application simply closes with no reason at all: no logs, no exceptions (catch block doesn't work at all), no event logs, nothing about the error occured.

.Net控制台应用程序依赖于COM对象,这些对象往往以非常不可预测的方式崩溃。应用程序根本没有任何理由关闭:没有日志,没有异常(catch块根本不起作用),没有事件日志,没有发生错误。

How can I isolate native crashes? Will AppDomain approach save me?

如何隔离原生崩溃? AppDomain方法会救我吗?

Thank you in advance!

先谢谢你!

1 个解决方案

#1


1  

Have you tried RuntimeCompatibilityAttribute.WrapNonExceptionThrows?

你试过RuntimeCompatibilityAttribute.WrapNonExceptionThrows吗?

Alternatively, a catch block without a type should catch non CLS-compliant exceptions, i.e:

或者,没有类型的catch块应该捕获非符合CLS的异常,即:

try
{
    // Code here.
    // Maybe a COM call here.
}
catch(Exception ex)
{
    // Managed exceptions.
}
catch
{
    // non CLS-compliant exceptions.
}

Using a seperate AppDomain will most likely not work depending on why exactly your application is getting killed off in the first place as unhandled exceptions will still kill the entire process and currently your exception is unhandled as you haven't caught it.

使用单独的AppDomain很可能无法正常工作,具体取决于您的应用程序首先被杀死的原因,因为未处理的异常仍将终止整个进程,并且当前您的异常未处理,因为您没有捕获它。

Alternatively you could use the COM objects within a separate process and use inter process communication between the two. This would isolate the problematic code into it's own process which you can then restart / use as needed without your main process being killed off.

或者,您可以在单独的进程中使用COM对象,并使用两者之间的进程间通信。这会将有问题的代码隔离到它自己的进程中,然后您可以根据需要重新启动/使用,而不会使主进程被终止。

#1


1  

Have you tried RuntimeCompatibilityAttribute.WrapNonExceptionThrows?

你试过RuntimeCompatibilityAttribute.WrapNonExceptionThrows吗?

Alternatively, a catch block without a type should catch non CLS-compliant exceptions, i.e:

或者,没有类型的catch块应该捕获非符合CLS的异常,即:

try
{
    // Code here.
    // Maybe a COM call here.
}
catch(Exception ex)
{
    // Managed exceptions.
}
catch
{
    // non CLS-compliant exceptions.
}

Using a seperate AppDomain will most likely not work depending on why exactly your application is getting killed off in the first place as unhandled exceptions will still kill the entire process and currently your exception is unhandled as you haven't caught it.

使用单独的AppDomain很可能无法正常工作,具体取决于您的应用程序首先被杀死的原因,因为未处理的异常仍将终止整个进程,并且当前您的异常未处理,因为您没有捕获它。

Alternatively you could use the COM objects within a separate process and use inter process communication between the two. This would isolate the problematic code into it's own process which you can then restart / use as needed without your main process being killed off.

或者,您可以在单独的进程中使用COM对象,并使用两者之间的进程间通信。这会将有问题的代码隔离到它自己的进程中,然后您可以根据需要重新启动/使用,而不会使主进程被终止。