locked
Catching exceptions in Windows Service RRS feed

  • Question

  • Hi,

    I want to catch exceptions that can happen everywhere in Windows Service application code, predicted or unpredicted ones (try-catch is not usefull for me).

    I tried to do this by adding an event handler to the event: AppDomain.CurrentDomain.UnhandledException,

    but this catches only exceptions happend in another thread fired by the main thread not in OnStart and OnStop methods.

    If exception happens here (OnStart/OnStop), its not handled by my handler but I'm only getting this:

    Error 1053: The service did not respond to the start or control request in a timely fashion

    System.Timers.Timer() swallows exceptions.

     

    I need to solve this in .NET Framework 2.0.

    Thanks.

    Friday, October 21, 2011 11:39 AM

Answers

  • 1. What's wrong with try/catch?

    2. It's better for the service to crash, you get a crash dump of the issue, and then have the service restart; than to have an exception put the service in an unknown zombie state.

    • Proposed as answer by Paul Zhou Tuesday, October 25, 2011 7:59 AM
    • Marked as answer by Paul Zhou Monday, October 31, 2011 6:04 AM
    Friday, October 21, 2011 3:59 PM

All replies

  • Hi,

    Did you done any coding for that??. If so post your code here. Also for logging the errors that are catched in the exception, try to write the exception details to file, then read the contents from file like LogFiles.

    Regards.

    A.Murugan


    If it solved your problem,Please click "Mark As Answer" on that post and "Mark as Helpful". Happy Programming!
    Friday, October 21, 2011 12:06 PM
  • This is the Main method from the windows service where I add events and handlers:

     

    [STAThread]

            static void Main()

            {

    Application.ThreadException += new ThreadExceptionEventHandler(threadExceptionHandlerFunction);

    try

      {

    Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);

    }

    catch (InvalidOperationException) { } 

    AppDomain.CurrentDomain.UnhandledException += new

      UnhandledExceptionEventHandler(unhandledExceptionHandlerFunction); 

    ServiceBase[] ServicesToRun;

    ServicesToRun = new ServiceBase[] { new MyNewService() };

    ServiceBase.Run(ServicesToRun);

    }

     

     

    I have problem to catch the exception...

    If I catch it with try-catch threadExceptionHandlerFunction and unhandledExceptionHandlerFunction handle it just fine.

    I must not use try-catch.

     

    Friday, October 21, 2011 12:56 PM
  • 1. What's wrong with try/catch?

    2. It's better for the service to crash, you get a crash dump of the issue, and then have the service restart; than to have an exception put the service in an unknown zombie state.

    • Proposed as answer by Paul Zhou Tuesday, October 25, 2011 7:59 AM
    • Marked as answer by Paul Zhou Monday, October 31, 2011 6:04 AM
    Friday, October 21, 2011 3:59 PM