我正在为我的 WCF 的服务层试用 FirstChangeException 事件处理程序。目的是从任何方法捕获异常并将其作为新的 FaultException 抛出,以便它可以传递回客户端。
例如下面是一个测试服务器类
private static bool thrown;
public class EchoService : _BaseService, IEchoService
{
public EchoService()
{
AppDomain.CurrentDomain.FirstChanceException += HandleFirstChanceException;
}
private void HandleFirstChanceException(object sender, FirstChanceExceptionEventArgs e)
{
if (thrown == false)
{
thrown = true;
throw new FaultException<Exception>(e.Exception);
}
}
public DTO_Echo_Response SendEcho(DTO_Echo_Request request)
{
DTO_Echo_Response response = new DTO_Echo_Response();
//SO note: AppError inherits from Exception.
if (request.ThrowTestException) throw new AppError("Throwing test exception");
return response;
}
}
但是,由于先前的调用来自抛出新异常,因此在退出该函数时return
,我收到以下错误。
The runtime has encountered a fatal error. The address of the error was at 0x750916ed, on thread 0x1d5c. The error code is 0x80131506. This error may be a bug in the CLR or in the unsafe or non-verifiable portions of user code. Common sources of this bug include user marshaling errors for COM-interop or PInvoke, which may corrupt the stack.
我一定是在做一些愚蠢的事情。如何实现捕获所有异常处理程序的目标?