Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Service Control Manager on 2008 Server stops my C# service

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

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

New Windows 7 Installations take days for Windows-Updates to show up and install. This can easily be fixed. I have finally decided to write an article because this seems to get asked several times a day lately. This Article and the Links apply to…
It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
This tutorial will walk an individual through the steps necessary to configure their installation of BackupExec 2012 to use network shared disk space. Verify that the path to the shared storage is valid and that data can be written to that location:…
This tutorial will show how to configure a new Backup Exec 2012 server and move an existing database to that server with the use of the BEUtility. Install Backup Exec 2012 on the new server and apply all of the latest hotfixes and service packs. The…

809 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