views:

401

answers:

5

I have a .Net service that has begun to refuse to start - the system log shows that it "was successfully sent a start control" and then five seconds later the "service entered the stop state". I have the Main() function of my service class wrapped in a try/catch block -- but when this situation occurs, no errors appear in my events log from my service.

I would like to know what the problem is. If there is some exception that is not being caught - where would it be caught? Below is the code in which I run the service.

using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceProcess;
using System.Text;

namespace MyServerService
{
    static class service
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        static void Main()
        {
            try
                {
#if DEBUG

                    System.Diagnostics.Debugger.Launch();

#endif

                    ServiceBase[] ServicesToRun;
                    ServicesToRun = new ServiceBase[] 
          { 
           new MyService() 
          };
                    ServiceBase.Run(ServicesToRun);
            } catch (Exception ex)
            {
                System.Diagnostics.EventLog.WriteEntry("MyServerService", "Main() Error: " + ex.Message + ex.InnerException, System.Diagnostics.EventLogEntryType.Error);
            }
        }
    }
}
+1  A: 

The Run method is not going to throw here. Rather, you need to wrap the code in the overload of the OnStart method on the MyService class in a try/catch block, as that is where the exception is occuring.

casperOne
I am going to try that. If there was an exception though (even uncaught), wouldn't I get some other kind of notification other than that the service went to the stop state?
pc1oad1etter
If there's an exception during start up then it should be logged in the Windows event log. If it simply times out you'll get nothing though.
Stu Mackellar
+1  A: 

What does your ServiceBase.OnStart implementation do? If it doesn't return within a set period of time then the Service Control Manager will stop the service again. Ideally you should create a new thread in your OnStart method to any required processing and return ASAP.

Stu Mackellar
Such a good gotcha. It took me 3 days to find that I should kick off another thread in the OnStart and return.
hometoast
stu - i am creating a new thread -- the service has been running testing/dev environments for months without this problem. one of a few production machines started behaving this way, though. (it worked on that machine for a while, too). odd.
pc1oad1etter
Is there anything else in the OnStart method that would stop it from returning in a reasonable time?
Stu Mackellar
A: 

I would check you all the components the service relies on are installed correctly.

Use ildasm.exe or an equivalent tool to check what it needs and see if it's available.

Bravax
A: 

You can add a handler to AppDomain.Current.UnhandledException and put some logging code into the exception handler. That will allow you to catch whatever exception it is that is causing the service to stop.

In VB.NET, it will look something like this

Sub Main(ByVal ParamArray parameters As String())
    AddHandler AppDomain.CurrentDomain.UnhandledException, AddressOf AppDomainExceptionHandler

...

End Sub

Private Sub AppDomainExceptionHandler(ByVal sender As Object, ByVal e As UnhandledExceptionEventArgs)

...

End Sub

not sure what it will look like in C# since my C#-fu is not great

Matthew Brubaker
A: 

In this situation, I was writing the to the Windows Application log -- in the production environment, the log was full. I'm unsure of what the debugging technique should be to 'catch' this class of a problem - but that was the problem for us.

pc1oad1etter