Windows Service terminated unexpectedly

12,371

Solution 1

Put a call to System.Diagnostics.Debugger.Break() in your OnStart() callback. When your service starts up, you'll be prompted to start the Visual Studio debugger. Once the debugger opens, you can debug from there.

Solution 2

here you can find How to: Debug Windows Service Applications

Share:
12,371

Related videos on Youtube

JD.
Author by

JD.

Updated on April 21, 2022

Comments

  • JD.
    JD. about 2 years

    I have a windows service which has a number of threads that do some work. All has been going well in testing, until once, where I saw "windows service terminated unexpectedly" in the event viewer.

    How do I go about trying to debug where this is happening? I have exceptions being caught under normal circumstances but not in this case.

    I do not know where to start.

    JD.

    • Wim Coenen
      Wim Coenen almost 15 years
      Do you use unmanaged libraries?
  • JD.
    JD. almost 15 years
    The problem I have is the windows service dies and I cannot reproduce it. How will debugging it help? Somewhere an unhandled exception is being thrown. I have try/excepts in my code but clearly the exception is not being caught.
  • Matt Davis
    Matt Davis almost 15 years
    If the service is at least making it into your OnStart() callback, then put the call to Debugger.Break() in as I said. As soon as the debugger pops up and stops at your breakpoint, go to the Debug menu and select the Exceptions option. Click the checkbox under the Thrown column for the Common Language Runtime Exceptions. When you continue the debugging session, the debugger will break where the exception is <b>thrown</b> as opposed to where it is caught.
  • Matt Davis
    Matt Davis almost 15 years
    You can even put the Debugger.Break() call in the entry point function for your service, which is even earlier in the startup process (obviously) than the OnStart() callback. Do this in case you never make it to your OnStart() callback.
  • JD.
    JD. over 14 years
    Thanks, did not know about that way of debugging. That has helped. I now have a clue where the unhandled exception is occurring.