1

I am trialling the FirstChangeException event handler for the service layer of my WCF. The aim is to capture the exception from any method and throw it as a new FaultException so it can pass back to the client.

For example below is a test server class

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;
    }
}

However, on exiting the function on the return line because the previous call was from the throwing the new exception, I get the following error.

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.

I must be doing something stupid. How can I achieve my aim of a catch all exception handler?

Valamas
  • 24,169
  • 25
  • 107
  • 177
  • 1
    The WCF aspect seems to be irrelevant btw. I can reproduce the problem in the Console https://gist.github.com/ByteBlast/55f44cb48f752e9106d6 – User 12345678 Jul 02 '14 at 05:02
  • Have you tried to not wrap the FirstChanceException up in the exception you throw? – Mare Infinitus Jul 02 '14 at 05:07
  • Based on the information you get from the documentation on that (http://msdn.microsoft.com/de-de/library/system.appdomain.firstchanceexception(v=vs.110).aspx) I guess it's not a good idea to throw inside this handler at all. IMHO it's not even a good design to try to wrap every exception and give them back to the caller. – Random Dev Jul 02 '14 at 05:08
  • @MareInfinitus: Tried throwing the part ex.InnerException which is what I think you were asking. No joy. – Valamas Jul 02 '14 at 05:10

1 Answers1

2

You are using the FirstChanceException Event here, which is just a notification, not a place to handle exceptions.

What you probably want is both of

Application.ThreadException
AppDomain.CurrentDomain.UnhandledException

There are already lots of questions on that topic.

Just have a look here on ThreadException

Also investigate on

Application.SetUnhandledExceptionMode

Exception handling in WCF is explained here:

handling exceptions in WCF right

WCF exceptionn handling

Community
  • 1
  • 1
Mare Infinitus
  • 8,024
  • 8
  • 64
  • 113
  • `Application` appears to be in the windows form namespace. So far, UnhandledException event is not firing like the firstchance was. Still looking around and testing... – Valamas Jul 02 '14 at 05:27
  • 1
    Probably that post is helpful: http://johannblais.blogspot.de/2009/02/handling-exceptions-right-way-in-wcf.html – Mare Infinitus Jul 02 '14 at 07:13
  • And, more current, this post here: http://www.codeproject.com/Articles/636843/WCF-Exception-Handling It is written with more explanation – Mare Infinitus Jul 02 '14 at 07:21
  • you are awesome. Both articles were very helpful with the johann being the best. – Valamas Jul 02 '14 at 22:37