none
Debugging Windows Service on Test Machine? RRS feed

  • Question

  • I have a real brain buster here... In a previous post I was having some issue getting this Windows Service installed, but that is resolved now.  The problem I have now is that the Service "stops unexpectedly" without any errors or exceptions thrown on our 64bit test server (WinServer2008 R2) - this Windows Service functions perfectly on our 32bit servers (2003/2008).  It starts, runs for about 5 or so seconds, then stops, and the only error is in the System Event Log: "The [service] terminated unexpectedly. It has done this [X] times."
     
    The Service is pretty straight forward, relatively light in code, which is why this is such a pain in the butt to figure out.... not to mention it's doing it on a non-development machine, and I have no clue how to debug it 'over there'.
     
    The Service is a Visual Studio 2008 Windows Service Project, which has a ServiceStarter.vb component (the OnStart and OnStop handlers), and a Worker.vb component. The OnStart handler of ServiceStarter instatiates a new thread, sets the ThreadStart property to the main function of the Worker component (DoWork()), and starts the thread.  The DoWork() method is a loop that calls an internal function, and then does a Thread.Sleep for 10 seconds before looping again. All pretty standard.
     
    However, the Service "terminates unexpectedly" just before the internal method call of DoWork().  I know this because I added a method that writes an entry into the Event Log (basically a version of the old debug.print("Got to here") statement) and called it at each step along the way of the process chain. What I get is this (going top down, in custom event log):
     
    [2/2/2010 12:01 PM]: [Service]: NPInterface Service Started - 2/2/2010 12:01:39 PM
    [2/2/2010 12:01 PM]: [Worker Process]: DoWork() Begun - 2/2/2010 12:01:39 PM
    [2/2/2010 12:01 PM]: [Worker Process]: Entering DoWork() Loop - 2/2/2010 12:01:39 PM
    [2/2/2010 12:01 PM]: [Worker Process]: Executing ExchangeSchedule() - 2/2/2010 12:01:39 PM
     
    followed by this in the System Event log:
     
    "The [Service] service terminated unexpectedly.  It has done this [X] time(s)." [2/2/2010 12:01:56 PM]
     
    Here is the code of the Worker.vb component relavent to the above:
     
        Public Sub DoWork()
            WriteEvent(String.Format("DoWork() Begun - {0}", Now), EventLogEntryType.Information)
     
            ' This loop is set up to run indefinitely
            While Not MustStop
                WriteEvent(String.Format("Entering DoWork() Loop - {0}", Now), EventLogEntryType.Information)
                Try
                    ' Process any information waiting on the Cache system
                    WriteEvent(String.Format("Executing ExchangeSchedule() - {0}", Now), EventLogEntryType.Information)
                    ExchangeSchedule()
                    WriteEvent(String.Format("ExchangeSchedule() Completed - {0}", Now), EventLogEntryType.Information)
     
    And the beginning of the ExchangeSchedule() method:
     
        Private Sub ExchangeSchedule()
            WriteEvent(String.Format("Inside and beginning ExchangeSchedule() method - {0}", Now), EventLogEntryType.Information)
            Try
     
    So, you can see that if ExchangeSchedule() exectued, the very first thing it would do is make another entry. But from the log file, that never happened.  The Service just stops, mid-process, mid-loop, no error, no exceptions, no reason that I can discover. And the fact I cannot reproduce this problem anywhere else is making this a real b!tch to figure out.
     
    Help, anyone??
     
    -- Andrew
     
    Tuesday, February 2, 2010 7:12 PM

Answers

  • Install Debugging Tools for Windows on your test box and either generate a debug dump to analyze offline or attach & debug using windbg. You should also try to log the full exception with stack.

    Paul
    • Marked as answer by eryang Thursday, February 11, 2010 9:07 AM
    Tuesday, February 2, 2010 7:23 PM
  • Are you running dowork on its own thread? you MUST return control to the caller in the OnStart  and constructor of your class if you don't the service manager assumes somethings wrong and kills you.
    • Marked as answer by eryang Thursday, February 11, 2010 9:07 AM
    Tuesday, February 2, 2010 7:26 PM

All replies

  • Install Debugging Tools for Windows on your test box and either generate a debug dump to analyze offline or attach & debug using windbg. You should also try to log the full exception with stack.

    Paul
    • Marked as answer by eryang Thursday, February 11, 2010 9:07 AM
    Tuesday, February 2, 2010 7:23 PM
  • Are you running dowork on its own thread? you MUST return control to the caller in the OnStart  and constructor of your class if you don't the service manager assumes somethings wrong and kills you.
    • Marked as answer by eryang Thursday, February 11, 2010 9:07 AM
    Tuesday, February 2, 2010 7:26 PM