Solved

Service Control Manager on 2008 Server stops my C# service

Posted on 2011-03-09
5
377 Views
Last Modified: 2012-05-11
 
However few moments later the SCM ends the service with following message written in the event log:
 
Product: Windows Operating System
Event ID: 7034
Source: Service Control Manager
Version: 5.2
Symbolic Name: EVENT_SERVICE_CRASH_NO_ACTION
Message: The %1 service terminated unexpectedly. It has done this %2 time(s).
 
I found following explanation on a Microsoft page (http://www.microsoft.com/technet/support/ee/transform.aspx?ProdName=Windows+Operating+System&ProdVer=5.2&EvtID=7034&EvtSrc=Service+Control+Manager&LCID=1033)
 
"The specified service could not continue. This service is configured to report the number of failures and, after a specific number of failures are reported, the Service Control Manager will perform the recovery action configured for the specified service."
 
Is there a way to suppress in a programmatic manner this behavior?
0
Comment
Question by:Borsec
  • 3
  • 2
5 Comments
 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 35083165
Probably, but that sort of thing would probably be done as part of the service installation.  That is to say, it may not be possible to "suspend" failure reporting and the recovery action in your service's OnStart, for example; and even if you could I would generally recommend against it.

I would suggest that this setting is better left to the server's administrator, so he can set or unset it as he sees fit.

Probably the better course of action would be to fix the service so it doesn't crash.
0
 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 35083602
Here's some sample code that uses the Windows APIs OpenSCManager, CloseServiceHandle, OpenService and ChangeServiceConfig2 to remove recovery actions from a service. Note, however, that as I stated above this shouldn't be run from your service; recovery actions will be read from the service database before your service starts, so running this code in your service would have no effect (at least not until the service is re-started). Also note that this isn't going to keep the service from crashing - it'll just keep Windows from trying to re-start it when it does crash.

using System;
using System.Runtime.InteropServices;

class Program
{
	static void Main(string[] args)
	{
		// Get handle to the service control manager
		IntPtr hSCManager = Win32Services.OpenSCManager(null, null, Win32Services.ServiceAccessRights.AllAccess);

		// Get a handle to the service
		IntPtr hService = Win32Services.OpenService(hSCManager, "service name", Win32Services.ServiceAccessRights.AllAccess);

		// Set the ServiceFailureActions struct such that recovery actions for the service will be deleted
		Win32Services.ServiceFailureActions sfa;
		sfa.dwResetPeriod = 0;
		sfa.lpCommand = IntPtr.Zero; // leave lpCommand and lpRebootMsg set to IntPtr.Zero to leave them unchanged
		sfa.lpRebootMsg = IntPtr.Zero;
		sfa.cActions = 0; // In order to delete recovery actions, cActions must be zero and lpsaActions must be non-zero
		sfa.lpsaActions = new IntPtr(1);

		// Set recovery actions for service based on the ServiceFailureActions struct declared above (i.e. delete'em)
		Win32Services.ConfigFailureActions(hService, Win32Services.ConfigInfoLevel.FailureActions, ref sfa);

		// Close the handle to the service
		Win32Services.CloseServiceHandle(hService);

		// Close the handle to the SCM
		Win32Services.CloseServiceHandle(hSCManager);
	}
}

public class Win32Services
{
	[DllImport("advapi32.dll", SetLastError = true)]
	public static extern IntPtr OpenSCManager(string lpMachineName, string lpDatabaseName, ServiceAccessRights dwDesiredAccess);

	[DllImport("advapi32.dll", SetLastError = true)]
	public static extern bool CloseServiceHandle(IntPtr hSCObject);

	[DllImport("advapi32.dll", SetLastError = true)]
	public static extern IntPtr OpenService(IntPtr hSCManager, [In] string lpServiceName, ServiceAccessRights dwDesiredAccess);

	[DllImport("advapi32.dll", SetLastError = true, EntryPoint = "ChangeServiceConfig2")]
	public static extern bool ConfigFailureActions(IntPtr hService, ConfigInfoLevel dwInfoLevel, ref ServiceFailureActions lpInfo);

	[Flags]
	public enum ServiceAccessRights : uint
	{
		AllAccess = 0xf003f,
		CreateService = 0x0002,
		Connect = 0x0001,
		EnumerateService = 0x0004,
		Lock = 0x0008,
		ModifyBootConfig = 0x0020,
		QueryLockStatus = 0x0010
	}

	public enum ConfigInfoLevel : uint
	{
		FailureActions = 2
	}

	[StructLayout(LayoutKind.Sequential)]
	public struct ServiceFailureActions
	{
		public uint dwResetPeriod;
		public IntPtr lpRebootMsg;
		public IntPtr lpCommand;
		public uint cActions;
		public IntPtr lpsaActions;
	}
}

Open in new window

0
 

Author Comment

by:Borsec
ID: 35083783
Thanks for your reply tgerbert.

As far I can tell the service does not crash. The exceptions are properly handled and in the case of an FTP timeout the service should continue to run and try next time according to a schedule to upload the file. I do not want windows to decide for me when to stop my service.
0
 
LVL 33

Accepted Solution

by:
Todd Gerbert earned 500 total points
ID: 35084020
Windows isn't stopping your service.  The event you've cited is logged when a service unexpectedly terminates (i.e. the process ends without reporting it's status as "stopped").  Windows might be trying to restart your service (if that action has been set on the service's Recovery tab), but it is not stopping it.
0
 

Author Closing Comment

by:Borsec
ID: 35093664
Well, then I have to search deeper in my service.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

OfficeMate Freezes on login or does not load after login credentials are input.
The recent Microsoft changes on update philosophy for Windows pre-10 and their impact on existing WSUS implementations.
Windows 8 came with a dramatically different user interface known as Metro. Notably missing from that interface was a Start button and Start Menu. Microsoft responded to negative user feedback of the Metro interface, bringing back the Start button a…
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now