Solved

Service Control Manager on 2008 Server stops my C# service

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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

Citrix XenApp, Internet Explorer 11 set to Enterprise Mode and using central hosted sites.xml file.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In this video, we discuss why the need for additional vertical screen space has become more important in recent years, namely, due to the transition in the marketplace of 4x3 computer screens to 16x9 and 16x10 screens (so-called widescreen format). …
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

730 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