Split into service and UI

This commit is contained in:
2014-05-12 20:38:41 -04:00
parent 043daea51e
commit aa1d28d6a8
40 changed files with 1942 additions and 114 deletions

View File

@@ -0,0 +1,70 @@
using System;
namespace SystemTemperatureService.Framework
{
public static class ConsoleHarness
{
// Run a service from the console given a service implementation
public static void Run(string[] args, IWindowsService service)
{
bool isRunning = true;
// simulate starting the windows service
service.OnStart(args);
// let it run as long as Q is not pressed
while (isRunning)
{
WriteToConsole(ConsoleColor.Yellow, "Enter either [Q]uit, [P]ause, [R]esume : ");
isRunning = HandleConsoleInput(service, Console.ReadLine());
}
// stop and shutdown
service.OnStop();
service.OnShutdown();
}
// Private input handler for console commands.
private static bool HandleConsoleInput(IWindowsService service, string line)
{
bool canContinue = true;
// check input
if (line != null)
{
switch (line.ToUpper())
{
case "Q":
canContinue = false;
break;
case "P":
service.OnPause();
break;
case "R":
service.OnContinue();
break;
default:
WriteToConsole(ConsoleColor.Red, "Did not understand that input, try again.");
break;
}
}
return canContinue;
}
// Helper method to write a message to the console at the given foreground color.
internal static void WriteToConsole(ConsoleColor foregroundColor, string format, params object[] formatArguments)
{
ConsoleColor originalColor = Console.ForegroundColor;
Console.ForegroundColor = foregroundColor;
Console.WriteLine(format, formatArguments);
Console.Out.Flush();
Console.ForegroundColor = originalColor;
}
}
}

View File

@@ -0,0 +1,46 @@
using System;
namespace SystemTemperatureService.Framework
{
/// <summary>
/// The interface that any windows service should implement to be used
/// with the GenericWindowsService executable.
/// </summary>
public interface IWindowsService : IDisposable
{
/// <summary>
/// This method is called when the service gets a request to start.
/// </summary>
/// <param name="args">Any command line arguments</param>
void OnStart(string[] args);
/// <summary>
/// This method is called when the service gets a request to stop.
/// </summary>
void OnStop();
/// <summary>
/// This method is called when a service gets a request to pause,
/// but not stop completely.
/// </summary>
void OnPause();
/// <summary>
/// This method is called when a service gets a request to resume
/// after a pause is issued.
/// </summary>
void OnContinue();
/// <summary>
/// This method is called when the machine the service is running on
/// is being shutdown.
/// </summary>
void OnShutdown();
/// <summary>
/// This method is called when a custom command is issued to the service.
/// </summary>
/// <param name="command">The command identifier to execute.</param >
void OnCustomCommand(int command);
}
}

View File

@@ -0,0 +1,47 @@
using System;
using System.Collections.Generic;
using System.Linq;
namespace SystemTemperatureService.Framework
{
/// <summary>
/// Extension methods for the Type class
/// </summary>
public static class TypeExtensions
{
/// <summary>
/// Loads the configuration from assembly attributes
/// </summary>
/// <typeparam name="T">The type of the custom attribute to find.</typeparam>
/// <param name="typeWithAttributes">The calling assembly to search.</param>
/// <returns>The custom attribute of type T, if found.</returns>
public static T GetAttribute<T>(this Type typeWithAttributes)
where T : Attribute
{
return GetAttributes<T>(typeWithAttributes).FirstOrDefault();
}
/// <summary>
/// Loads the configuration from assembly attributes
/// </summary>
/// <typeparam name="T">The type of the custom attribute to find.</typeparam>
/// <param name="typeWithAttributes">The calling assembly to search.</param>
/// <returns>An enumeration of attributes of type T that were found.</returns>
public static IEnumerable<T> GetAttributes<T>(this Type typeWithAttributes)
where T : Attribute
{
// Try to find the configuration attribute for the default logger if it exists
object[] configAttributes = Attribute.GetCustomAttributes(typeWithAttributes,
typeof(T), false);
// get just the first one
if (configAttributes != null && configAttributes.Length > 0)
{
foreach (T attribute in configAttributes)
{
yield return attribute;
}
}
}
}
}

View File

@@ -0,0 +1,90 @@
using System;
using System.ServiceProcess;
namespace SystemTemperatureService.Framework
{
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = true)]
public class WindowsServiceAttribute : Attribute
{
/// <summary>
/// The name of the service.
/// </summary>
public string Name { get; set; }
/// <summary>
/// The displayable name that shows in service manager (defaults to Name).
/// </summary>
public string DisplayName { get; set; }
/// <summary>
/// A textural description of the service name (defaults to Name).
/// </summary>
public string Description { get; set; }
/// <summary>
/// The user to run the service under (defaults to null). A null or empty
/// UserName field causes the service to run as ServiceAccount.LocalService.
/// </summary>
public string UserName { get; set; }
/// <summary>
/// The password to run the service under (defaults to null). Ignored
/// if the UserName is empty or null, this property is ignored.
/// </summary>
public string Password { get; set; }
/// <summary>
/// Specifies the event log source to set the service's EventLog to. If this is
/// empty or null (the default) no event log source is set. If set, will auto-log
/// start and stop events.
/// </summary>
public string EventLogSource { get; set; }
/// <summary>
/// The method to start the service when the machine reboots (defaults to Manual).
/// </summary>
public ServiceStartMode StartMode { get; set; }
/// <summary>
/// True if service supports pause and continue (defaults to true).
/// </summary>
public bool CanPauseAndContinue { get; set; }
/// <summary>
/// True if service supports shutdown event (defaults to true).
/// </summary>
public bool CanShutdown { get; set; }
/// <summary>
/// True if service supports stop event (defaults to true).
/// </summary>
public bool CanStop { get; set; }
/// <summary>
/// The service account to use if the UserName is not specified.
/// </summary>
public ServiceAccount ServiceAccount { get; set; }
/// <summary>
/// Marks an IWindowsService with configuration and installation attributes.
/// </summary>
/// <param name="name">The name of the windows service.</param>
public WindowsServiceAttribute(string name)
{
// set name and default description and display name to name.
Name = name;
Description = name;
DisplayName = name;
// default all other attributes.
CanStop = true;
CanShutdown = true;
CanPauseAndContinue = true;
StartMode = ServiceStartMode.Manual;
EventLogSource = null;
Password = null;
UserName = null;
ServiceAccount = ServiceAccount.LocalService;
}
}
}

View File

@@ -0,0 +1,37 @@
namespace SystemTemperatureService.Framework
{
public partial class WindowsServiceHarness
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (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()
{
components = new System.ComponentModel.Container();
this.ServiceName = "UsageService";
}
#endregion
}
}

View File

@@ -0,0 +1,129 @@
using System;
using System.ServiceProcess;
namespace SystemTemperatureService.Framework
{
/// <summary>
/// A generic Windows Service that can handle any assembly that
/// implements IWindowsService (including AbstractWindowsService)
/// </summary>
public partial class WindowsServiceHarness : ServiceBase
{
/// <summary>
/// Get the class implementing the windows service
/// </summary>
public IWindowsService ServiceImplementation { get; private set; }
/// <summary>
/// Constructor a generic windows service from the given class
/// </summary>
/// <param name="serviceImplementation">Service implementation.</param>
public WindowsServiceHarness(IWindowsService serviceImplementation)
{
// make sure service passed in is valid
if (serviceImplementation == null)
{
throw new ArgumentNullException("serviceImplementation",
"IWindowsService cannot be null in call to GenericWindowsService");
}
// set instance and backward instance
ServiceImplementation = serviceImplementation;
// configure our service
ConfigureServiceFromAttributes(serviceImplementation);
}
/// <summary>
/// Override service control on continue
/// </summary>
protected override void OnContinue()
{
// perform class specific behavior
ServiceImplementation.OnContinue();
}
/// <summary>
/// Called when service is paused
/// </summary>
protected override void OnPause()
{
// perform class specific behavior
ServiceImplementation.OnPause();
}
/// <summary>
/// Called when a custom command is requested
/// </summary>
/// <param name="command">Id of custom command</param>
protected override void OnCustomCommand(int command)
{
// perform class specific behavior
ServiceImplementation.OnCustomCommand(command);
}
/// <summary>
/// Called when the Operating System is shutting down
/// </summary>
protected override void OnShutdown()
{
// perform class specific behavior
ServiceImplementation.OnShutdown();
}
/// <summary>
/// Called when service is requested to start
/// </summary>
/// <param name="args">The startup arguments array.</param>
protected override void OnStart(string[] args)
{
ServiceImplementation.OnStart(args);
}
/// <summary>
/// Called when service is requested to stop
/// </summary>
protected override void OnStop()
{
ServiceImplementation.OnStop();
}
/// <summary>
/// Set configuration data
/// </summary>
/// <param name="serviceImplementation">The service with configuration settings.</param>
private void ConfigureServiceFromAttributes(IWindowsService serviceImplementation)
{
var attribute = serviceImplementation.GetType().GetAttribute<WindowsServiceAttribute>();
if (attribute != null)
{
// wire up the event log source, if provided
if (!string.IsNullOrWhiteSpace(attribute.EventLogSource))
{
// assign to the base service's EventLog property for auto-log events.
EventLog.Source = attribute.EventLogSource;
}
CanStop = attribute.CanStop;
CanPauseAndContinue = attribute.CanPauseAndContinue;
CanShutdown = attribute.CanShutdown;
// we don't handle: laptop power change event
CanHandlePowerEvent = false;
// we don't handle: Term Services session event
CanHandleSessionChangeEvent = false;
// always auto-event-log
AutoLog = true;
}
else
{
throw new InvalidOperationException(
string.Format("IWindowsService implementer {0} must have a WindowsServiceAttribute.",
serviceImplementation.GetType().FullName));
}
}
}
}

View File

@@ -0,0 +1,36 @@
namespace SystemTemperatureService.Framework
{
public partial class WindowsServiceInstaller
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (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()
{
components = new System.ComponentModel.Container();
}
#endregion
}
}

View File

@@ -0,0 +1,212 @@
using System;
using System.Collections;
using System.ComponentModel;
using System.Configuration.Install;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.ServiceProcess;
namespace SystemTemperatureService.Framework
{
/// <summary>
/// A generic windows service installer
/// </summary>
[RunInstaller(true)]
public partial class WindowsServiceInstaller : Installer
{
/// <summary>
/// Gets or sets the type of the windows service to install.
/// </summary>
public WindowsServiceAttribute Configuration { get; set; }
/// <summary>
/// Creates a blank windows service installer with configuration in ServiceImplementation
/// </summary>
public WindowsServiceInstaller()
: this(typeof(ServiceImplementation))
{
}
/// <summary>
/// Creates a windows service installer using the type specified.
/// </summary>
/// <param name="windowsServiceType">The type of the windows service to install.</param>
public WindowsServiceInstaller(Type windowsServiceType)
{
if (!windowsServiceType.GetInterfaces().Contains(typeof(IWindowsService)))
{
throw new ArgumentException("Type to install must implement IWindowsService.",
"windowsServiceType");
}
var attribute = windowsServiceType.GetAttribute<WindowsServiceAttribute>();
if (attribute == null)
{
throw new ArgumentException("Type to install must be marked with a WindowsServiceAttribute.",
"windowsServiceType");
}
Configuration = attribute;
}
/// <summary>
/// Performs a transacted installation at run-time of the AutoCounterInstaller and any other listed installers.
/// </summary>
/// <typeparam name="T">The IWindowsService implementer to install.</typeparam>
public static void RuntimeInstall<T>()
where T : IWindowsService
{
string path = "/assemblypath=" + Assembly.GetEntryAssembly().Location;
using (var ti = new TransactedInstaller())
{
ti.Installers.Add(new WindowsServiceInstaller(typeof(T)));
ti.Context = new InstallContext(null, new[] { path });
ti.Install(new Hashtable());
}
}
/// <summary>
/// Performs a transacted un-installation at run-time of the AutoCounterInstaller and any other listed installers.
/// </summary>
/// <param name="otherInstallers">The other installers to include in the transaction</param>
/// <typeparam name="T">The IWindowsService implementer to install.</typeparam>
public static void RuntimeUnInstall<T>(params Installer[] otherInstallers)
where T : IWindowsService
{
string path = "/assemblypath=" + Assembly.GetEntryAssembly().Location;
using (var ti = new TransactedInstaller())
{
ti.Installers.Add(new WindowsServiceInstaller(typeof(T)));
ti.Context = new InstallContext(null, new[] { path });
ti.Uninstall(null);
}
}
/// <summary>
/// Installer class, to use run InstallUtil against this .exe
/// </summary>
/// <param name="savedState">The saved state for the installation.</param>
public override void Install(IDictionary savedState)
{
ConsoleHarness.WriteToConsole(ConsoleColor.White, "Installing service {0}.", Configuration.Name);
// install the service
ConfigureInstallers();
base.Install(savedState);
// wire up the event log source, if provided
if (!string.IsNullOrWhiteSpace(Configuration.EventLogSource))
{
// create the source if it doesn't exist
if (!EventLog.SourceExists(Configuration.EventLogSource))
{
EventLog.CreateEventSource(Configuration.EventLogSource, "Application");
}
}
}
/// <summary>
/// Removes the counters, then calls the base uninstall.
/// </summary>
/// <param name="savedState">The saved state for the installation.</param>
public override void Uninstall(IDictionary savedState)
{
ConsoleHarness.WriteToConsole(ConsoleColor.White, "Un-Installing service {0}.", Configuration.Name);
// load the assembly file name and the config
ConfigureInstallers();
base.Uninstall(savedState);
// wire up the event log source, if provided
if (!string.IsNullOrWhiteSpace(Configuration.EventLogSource))
{
// create the source if it doesn't exist
if (EventLog.SourceExists(Configuration.EventLogSource))
{
EventLog.DeleteEventSource(Configuration.EventLogSource);
}
}
}
/// <summary>
/// Rolls back to the state of the counter, and performs the normal rollback.
/// </summary>
/// <param name="savedState">The saved state for the installation.</param>
public override void Rollback(IDictionary savedState)
{
ConsoleHarness.WriteToConsole(ConsoleColor.White, "Rolling back service {0}.", Configuration.Name);
// load the assembly file name and the config
ConfigureInstallers();
base.Rollback(savedState);
}
/// <summary>
/// Method to configure the installers
/// </summary>
private void ConfigureInstallers()
{
// load the assembly file name and the config
Installers.Add(ConfigureProcessInstaller());
Installers.Add(ConfigureServiceInstaller());
}
/// <summary>
/// Helper method to configure a process installer for this windows service
/// </summary>
/// <returns>Process installer for this service</returns>
private ServiceProcessInstaller ConfigureProcessInstaller()
{
var result = new ServiceProcessInstaller();
// if a user name is not provided, will run under local service acct
if (string.IsNullOrEmpty(Configuration.UserName))
{
result.Account = Configuration.ServiceAccount;
result.Username = null;
result.Password = null;
}
else
{
// otherwise, runs under the specified user authority
result.Account = ServiceAccount.User;
result.Username = Configuration.UserName;
result.Password = Configuration.Password;
}
return result;
}
/// <summary>
/// Helper method to configure a service installer for this windows service
/// </summary>
/// <returns>Process installer for this service</returns>
private ServiceInstaller ConfigureServiceInstaller()
{
// create and config a service installer
var result = new ServiceInstaller
{
ServiceName = Configuration.Name,
DisplayName = Configuration.DisplayName,
Description = Configuration.Description,
StartType = Configuration.StartMode,
};
return result;
}
}
}