Solved

Service Control Manager on 2008 Server stops my C# service

Posted on 2011-03-09
5
383 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Restoring deleted objects in Active Directory has been a standard feature in Active Directory for many years, yet some admins may not know what is available.
This article explains how to install and use the NTBackup utility that comes with Windows Server.
This tutorial will walk an individual through configuring a drive on a Windows Server 2008 to perform shadow copies in order to quickly recover deleted files and folders. Click on Start and then select Computer to view the available drives on the se…
This tutorial will walk an individual through setting the global and backup job media overwrite and protection periods in Backup Exec 2012. Log onto the Backup Exec Central Administration Server. Examine the services. If all or most of them are stop…

786 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