• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 429
  • Last Modified:

Service Control Manager on 2008 Server stops my C# service

 
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
Borsec
Asked:
Borsec
  • 3
  • 2
1 Solution
 
Todd GerbertIT ConsultantCommented:
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
 
Todd GerbertIT ConsultantCommented:
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
 
BorsecAuthor Commented:
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
 
Todd GerbertIT ConsultantCommented:
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
 
BorsecAuthor Commented:
Well, then I have to search deeper in my service.
0

Featured Post

Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now