0

I created an application as a Windows Service which works fine on my workstation running Windows 8 x64, and writes several events in the Application eventlog on OnStart(). I used InstallUtil to install it on a Windows 2012 machine and it stops right after it starts. The only thing I see regarding it in the eventlog is under System:

The Foo service entered the stopped state. 
  • I can't even debug it on that machine without installing VS2012 which I don't want to do because it is in a production environment.
  • The application depends on one DLL which is copied locally.
  • .NET Frameworks 2.0 to 4.5 are installed on both machines.
  • I tried Run as Administrator under Compatibility but it's set to run under LocalService anyways so it should have permission to do everything it's tasked to do...

EDIT 1: why would you downvote the question without even leaving a remark as to why it's bad?

Theveloper
  • 806
  • 4
  • 19
  • 35
  • Have you checked the event viewer to see if there's any information in there? Another option is to add logging to your service and have it write to a text file - you could potentially find out where it's failing through that. – Tim Aug 21 '13 at 23:14
  • Tim, I mentioned in the question that my app writes several events in the OnStart method (none of which get passed to the event log (or event viewer)) please reread. – Theveloper Aug 22 '13 at 00:12
  • Sorry, I missed that the first time. Since none of the events were written, that gives you one place to start looking to see if you can determine why it's not working. Adding logging to a text file where you can log every step (if you don't already) could possibly narrow it down to the exact spot it stops working. Also, double-check your production box to ensure LocalService has rights to write to the Application log. – Tim Aug 22 '13 at 00:58
  • My guess would be it stops working before OnStart is raised, I don't think it's a permission error either. Any other logs that I can check? – Theveloper Aug 22 '13 at 03:07
  • Short of rewriting your service so that it writes each steps completion to a text file, not that I can think of. – Tim Aug 22 '13 at 05:20
  • Please post some code related to your windows service and what it is trying to do after OnStart Event. – Mitul Aug 22 '13 at 14:21
  • @Mitul what's the point of that if it doesn't even get to the OnStart event? – Theveloper Aug 23 '13 at 17:35

1 Answers1

1

Few things I have learned on debugging windows services that might help you in troubleshooting your issue.

  1. Try to log everything for each method inside your windows service to text file as Tim suggested in the comment.
  2. Try to catch UnhandledException before the service exits by adding an event handler to the appdomain. I have my Log function that writes to text file.

    AddHandler AppDomain.CurrentDomain.UnhandledException, AddressOf CurrentDomain_UnhandledException

    Public Sub CurrentDomain_UnhandledException(sender As Object, e As UnhandledExceptionEventArgs)
            'Log and output exception message before the application exits.
            Dim exp = DirectCast(e.ExceptionObject, Exception)
            Dim formatexp = String.Format("Exception: {0}; Message: {1}: InnerException: {2}",
                              exp.ToString(),
                              exp.Message.ToString(),
                              exp.InnerException.ToString())
            Log(formatexp)
            Console.WriteLine(formatexp)
    
            Log("Application exited due to unhandled exception")
            Console.WriteLine("Service exited due to unhandled exception")
            'Exit the batch process gracefully.
            Environment.Exit(-1)
    End Sub
    
  3. Try using WindDbg for debugging Windows Service Crash or Hang issues and you can download it from Microsoft Download site.

  4. Check if service account that is trying to access any resource on the server has access to them.

  5. If you want to see real time execution of your code then log execution to a SignalR Host. This might be overkill but I once had multiple threads executing different processes and I was able to debug an issue using SignalR.

Note: If you can post some sample code then it would be helpful to us in helping you out.

Mitul
  • 9,734
  • 4
  • 43
  • 60