Click here to Skip to main content
15,892,005 members
Articles / Programming Languages / C#

A ServiceInstaller Extension That Enables Recovery and Autostart Configuration

Rate me:
Please Sign up or sign in to vote.
4.95/5 (48 votes)
2 May 2004CPOL12 min read 231.9K   3.1K   95  
An extension assembly that allows configuring the "advanced" service configuration options for recovery actions.
/////////////////////////////////////////////////////////////////////////////
//
// Library Name		: Verifide.ServiceUtils.dll
// Description		: Extension to System.ServiceProcess.ServiceInstallerEx
//					  to enable configuration of advanced options
// Date				: 1/14/04
//
//	Copyright (C) 2004 Narendra (Neil) Baliga
//
//	This library is free software; you can redistribute it and/or
//	modify it as you wish. It is distributed in the hope that it will 
//	be useful,but WITHOUT ANY WARRANTY; without even the implied 
//	warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
//
/////////////////////////////////////////////////////////////////////////////

using System;
using System.Collections;
using System.ComponentModel;
using System.Configuration.Install;

using Verifide.ServiceUtils;

namespace TestService
{
	/// <summary>
	/// Summary description for ProjectInstaller.
	/// </summary>
	[RunInstaller(true)]
	public class ProjectInstaller : System.Configuration.Install.Installer
	{
		private System.ServiceProcess.ServiceProcessInstaller serviceProcessInstaller1;

		// Change this to use the Verifide.ServiceUtils.ServiceInstallerEx class
		// private System.ServiceProcess.ServiceInstaller serviceInstaller1;

		private Verifide.ServiceUtils.ServiceInstallerEx serviceInstaller1;

		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public ProjectInstaller()
		{
			// This call is required by the Designer.
			InitializeComponent();

			// TODO: Add any initialization after the InitializeComponent call

			// NOTE: Setting these properties here does not make them immediately effective.  
			// The ServiceInstallerEx configures the service AFTER the base installer is done 
			// doing its job. That is when the Committed event is fired from the base installer

			// Set a description
			this.serviceInstaller1.Description = "Look Smeagol! Its the Precious!";

			// The fail run command is used to spawn another process when this service fails
			// it should include the entire command line as would be passed to Win32::CreateProcess()
			this.serviceInstaller1.FailRunCommand = "SomeCommand.exe";

			// The fail count reset time resets the failure count after N seconds of no failures
			// on the service.  This value is set in seconds, though note that the SCM GUI only
			// displays it in increments of days.
			this.serviceInstaller1.FailCountResetTime = 60*60*24*4;

			// The fail reboot message is used when a reboot action is specified and works in 
			// conjunction with the RecoverAction.Reboot type.

			this.serviceInstaller1.FailRebootMsg = "Whitney Houston! We have a problem" ;

			// Set some failure actions : Isn't this easy??
			// Do note that if you specify less than three actions, the remaining actions will take on
			// the value of the last action.  For example, if you only set one action to RunCommand,
			// failure 2 and failure 3 will also take on the default action of RunCommand. This is 
			// a "feature" of the ChangeServiceConfig2() method; Use RecoverAction.None to disable
			// unwanted actions.

			this.serviceInstaller1.FailureActions.Add( new FailureAction( RecoverAction.Restart, 60000)  );
			this.serviceInstaller1.FailureActions.Add( new FailureAction( RecoverAction.RunCommand, 2000 ) );
			this.serviceInstaller1.FailureActions.Add( new FailureAction( RecoverAction.Reboot, 3000 ) );

			// Configure the service to start right after it is installed.  We do not want the user to
			// have to reboot their machine or to have some other process start it.  Do be careful because
			// if this service is dependent upon other services, they must be installed PRIOR to this one
			// for the service to be started properly

			this.serviceInstaller1.StartOnInstall = true;

		}

		/// <summary> 
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Component Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.serviceProcessInstaller1 = new System.ServiceProcess.ServiceProcessInstaller();
			this.serviceInstaller1 = new Verifide.ServiceUtils.ServiceInstallerEx();
			// 
			// serviceProcessInstaller1
			// 
			this.serviceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.LocalSystem;
			this.serviceProcessInstaller1.Password = null;
			this.serviceProcessInstaller1.Username = null;
			// 
			// serviceInstaller1
			// 
			this.serviceInstaller1.ServiceName = "TestService";
			this.serviceInstaller1.AfterInstall += new System.Configuration.Install.InstallEventHandler(this.serviceInstaller1_AfterInstall);
			// 
			// ProjectInstaller
			// 
			this.Installers.AddRange(new System.Configuration.Install.Installer[] {
																					  this.serviceProcessInstaller1,
																					  this.serviceInstaller1});

		}
		#endregion

		private void serviceInstaller1_AfterInstall(object sender, System.Configuration.Install.InstallEventArgs e) {
		
		}

	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
President Verifide Technologies, Inc.
United States United States
Neil Baliga is the founder of Verifide Technologies, Inc. (www.verifide.com), an initiative for automated test systems for product verification used in manufacturing. He strongly believes that the value in software is in its simplicity. His experience includes UNIX, Win32 API, TCP/IP multithreaded servers, C#, C++ et. al, and Radio Frequency (RF) measurement science. He came across .NET in 2001 and has been in love with it ever since. He is an avid LA Lakers and Denver Broncos fan and loves to hang out with his dog 'Reboot'. He is extremely lucky to have the love and support of his beautiful wife Jyothi.

Comments and Discussions