1

I can“t find a Application.ThreadException event to listen to on my WCF service. I suppose that this is for WinForms so is there a ThreadException event for WCF services? Or will they end up in AppDomain.CurrentDomain.UnhandledException?

Enrico Campidoglio
  • 56,676
  • 12
  • 126
  • 154
Banshee
  • 15,376
  • 38
  • 128
  • 219

3 Answers3

0

The Application.ThreadException event is only used to "to handle otherwise unhandled exceptions that occur in Windows Forms thread" (emphasis added, MSDN), so it isn't of much help in a WCF service.

Your best bet is to indeed use AppDomain.CurrentDomain.UnhandledException. Note however, that you cannot prevent the process from exiting. This event merely allows you to do some logging or error reporting, before "the system default handler reports the exception to the user and terminates the application." (MSDN).

You many also want to implement your own IErrorHandler. Also, checkout this blog entry about some WCF error handling best practices, while your at it.

Christian.K
  • 47,778
  • 10
  • 99
  • 143
0

It looks like you'll need to implement IErrorHandler: http://msdn.microsoft.com/en-us/library/system.servicemodel.dispatcher.ierrorhandler.aspx

Then register your handler with your ServiceHost: http://msdn.microsoft.com/en-us/library/system.servicemodel.dispatcher.channeldispatcher.errorhandlers.aspx

It looks like you can also wire up the IErrorHandlers using a custom attribute as described here: http://msdn.microsoft.com/en-us/library/ms751439.aspx

Robert Dusek
  • 106
  • 5
0

It depends on your client framework. WCF faults get thrown as exceptions on the client side, so handle them like any other global exception handler:

Winforms uses

Application.ThreadException

WPF uses:

Application.DispatcherUnhandledException

As Christian states, these are only for exceptions thrown on the "GUI" thread, but WCF will marshal callbacks on a duplex service to these by default I think.

GazTheDestroyer
  • 20,722
  • 9
  • 70
  • 103