Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The service is not responding to the control function (error 2186)

I'm developing a service using .NET on Windows platforms.

It had worked until yesterday... but today it doesn't want to start!!! It seems strange, and I feel I'm missing something...

I've also tried to revert sources to the last working version, but nothing else happens: net start outputs:

The service is not responding to the control function.

What could cause this malfunction?


Probably most of you wants to know more about it. So, let me show you some code:

The service code:

#if DEBUG
class iGeckoService : DebuggableService
#else
class iGeckoService : ServiceBase
#endif
{
    static void Main()
    {
#if DEBUG
        if (Debugger.IsAttached == true) {
            DebuggableService[] services = Services;

            // Create console
            AllocConsole();

            // Emulate ServiceBase.Run
            foreach (DebuggableService service in services)
                service.Start(null);

            // Wait for new line
            Console.WriteLine("Press ENTER to exit..."); Console.ReadLine();

            // Emulate ServiceBase.Run
            foreach (DebuggableService service in services)
                service.Stop();
        } else
            ServiceBase.Run(Services);
#else
        ServiceBase.Run(Services);
#endif
    }

#if DEBUG

    static DebuggableService[] Services
    {
        get {
            return (new DebuggableService[] { new iGeckoService() });
        }
    }

    [DllImport("kernel32")]
    static extern bool AllocConsole();

#else

    static DebuggableService[] Services
    {
        get {
            return (new ServiceBase[] { new iGeckoService() });
        }
    }

#endif

    #endregion

    #region Constructors

    /// <summary>
    /// Default constructor.
    /// </summary>
    public iGeckoService()
    {
        // Base properties
        ServiceName = DefaultServiceName;

        // Service feature - Power events
    }

    #endregion

    protected override void OnStart(string[] args)
    {
        try {
            ...

        } catch (Exception e) {
            sLog.Error("Unable to initialize the service. Request to stop.", e);
        }
    }

    /// <summary>
    /// Stop this service.
    /// </summary>
    protected override void OnStop()
            {
                     ...
            }
  }

  [RunInstaller(true)]
public class iGeckoDaemonInstaller : Installer
{
    /// <summary>
    /// Default constructor.
    /// </summary>
    public iGeckoDaemonInstaller()
    {
        ServiceProcessInstaller spi = new ServiceProcessInstaller();
        spi.Account = ServiceAccount.LocalSystem;

        ServiceInstaller si = new ServiceInstaller();
        si.ServiceName = iGeckoService.DefaultServiceName;
        si.StartType = ServiceStartMode.Automatic;

        Installers.AddRange(new Installer[] {spi, si});
    }
}

class DebuggableService : ServiceBase
{
    public void Start(string[] args) { OnStart(args); }
}

The start script is:

installutil ..\bin\Debug\iGeckoService.exe
net start "Gecko Videowall"

while the stop script is:

net stop "Gecko Videowall"
installutil /u ..\bin\Debug\iGeckoService.exe

However, I think it is a system setting, since the application has worked well until the last day. (Sigh).


Update

When the service was working, I used log4net for logging service activity (I'm unable to attach the debugger to the running service...), and it has always logged.

From now, the log4net log it is never created (even if I enable internal debug option), even if I log at the Main routine!


Another update

It seems that the application is never executed. I've reduced every routine (Main, OnStart, OnStop), and I run an empty service. OnStart routine creates a file on a directory (fully writeable by everyone), but when the service is started, no file is created.


Yet another update

Stimulated by the Rob's comment, I've seen this message on the event viewer:

> Faulting application name: iGeckoService.exe, version: 1.0.0.0, time stamp: 0x4c60de6a
> Faulting module name: ntdll.dll, version: 6.1.7600.16385, time stamp: 0x4a5be02b
> Exception code: 0x80000003
> Fault offset: 0x000000000004f190
> Faulting process id: 0x1258
> Faulting application start time: 0x01cb384a726c7167
> Faulting application path: C:\Users\Luca\Documents\Projects\iGeckoSvn\iGeckoService\bin\Debug\iGeckoService.exe
> Faulting module path: C:\Windows\SYSTEM32\ntdll.dll
> Report Id: b096a237-a43d-11df-afc4-001e8c414537

This is, definitively, the reason on the service shutdown... not question becomes: "How to debug it?" (Thank you Rob, I've never thought about the event viewer until now!) Debugging it running as console application it doesn't show any error, indeed it seems related to the service environment. The only thing that comes to my mind could be some DLL loading failure, since now the service is empty... any idea?

(Thank you all for following me... I'd like to offer you pizza & beer)


Solved!

The service was unable to start since a crash before the Main routine, caused by the installation and the setup of the MS Application Verifier (x64). After having uninstalled that application, everything worked as usual!

Thank you all!

like image 355
Luca Avatar asked Aug 10 '10 06:08

Luca


2 Answers

Usually this happens if you're trying to do too much work in the OnStart call. For example, if you start an endless loop in the same thread, you'll get this error message.

Generally the service should create a new thread in the OnStart call, and then cleanly terminate it in the OnStop call.

Of course that doesn't help if you're using code which was previously working. Have you tried rebooting it since the failure? I seem to remember that if you've already got a service which is borked, it can sometimes be tricky to get back to a working state without rebooting it. You may want to look in your process list and see whether you've got a copy still running, and kill it if so.

like image 68
Jon Skeet Avatar answered Nov 15 '22 12:11

Jon Skeet


For me it just meant that an exception was being thrown. (Platform conflict in Configuration Manager, resulting in "bad image format".) Try running the .exe from the command line and see if you get an error.

like image 30
Dave Cousineau Avatar answered Nov 15 '22 13:11

Dave Cousineau