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!
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.
In general every service must do following two simple things
SERVICE_CONTROL_START
, SERVICE_CONTROL_STOP
and so on if should return in a short interval. Using SetServiceStatus
function service can prolong this interval for example with calling SetServiceStatus
with incremented dwCheckPoint
value. (In .NET use can use ServiceBase.RequestAdditionalTime instead)SERVICE_CONTROL_INTERROGATE
control code just with return. This control code are used from the service manager to detect whether the service still living.If your program don't follow one of the rules you receive the error "The service is not responding to the control function."
If you write a program in .NET you don't need to do directly the two things which I described before. The ServiceBase
class do there for you. Nevertheless you can easy break this rules if create a thread running with priority higher as normal or if you do some too long work inside an OnXXX handle (OnStop
, OnStart
, OnPowerEvent
etc) without calling of ServiceBase.RequestAdditionalTime. Some other tricks with additional threads can also make problems.
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.
I see that there is a code block
// Wait for new line
Console.WriteLine("Press ENTER to exit..."); Console.ReadLine();
as stated by @Jon since this is a service. when service starts it waits for a stipulated time within which it should respond.
there is a statement "Console.ReadLine()
" you service will wait until key is pressed. since this is a service will keep waiting at this point
User contributions licensed under CC BY-SA 3.0