82 lines
3.1 KiB
C#
82 lines
3.1 KiB
C#
using Swan.Logging;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Reflection;
|
|
using System.Threading;
|
|
|
|
using Swan.Services;
|
|
|
|
namespace Swan {
|
|
/// <summary>
|
|
/// Extension methods.
|
|
/// </summary>
|
|
public static class WindowsServicesExtensions {
|
|
/// <summary>
|
|
/// Runs a service in console mode.
|
|
/// </summary>
|
|
/// <param name="this">The service to run.</param>
|
|
/// <param name="loggerSource">The logger source.</param>
|
|
/// <exception cref="ArgumentNullException">this.</exception>
|
|
[Obsolete("This extension method will be removed in version 3.0")]
|
|
public static void RunInConsoleMode(this ServiceBase @this, String loggerSource = null) {
|
|
if(@this == null) {
|
|
throw new ArgumentNullException(nameof(@this));
|
|
}
|
|
|
|
RunInConsoleMode(new[] { @this }, loggerSource);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Runs a set of services in console mode.
|
|
/// </summary>
|
|
/// <param name="this">The services to run.</param>
|
|
/// <param name="loggerSource">The logger source.</param>
|
|
/// <exception cref="ArgumentNullException">this.</exception>
|
|
/// <exception cref="InvalidOperationException">The ServiceBase class isn't available.</exception>
|
|
[Obsolete("This extension method will be removed in version 3.0")]
|
|
public static void RunInConsoleMode(this ServiceBase[] @this, String loggerSource = null) {
|
|
if(@this == null) {
|
|
throw new ArgumentNullException(nameof(@this));
|
|
}
|
|
|
|
const String onStartMethodName = "OnStart";
|
|
const String onStopMethodName = "OnStop";
|
|
|
|
MethodInfo onStartMethod = typeof(ServiceBase).GetMethod(onStartMethodName, BindingFlags.Instance | BindingFlags.NonPublic);
|
|
MethodInfo onStopMethod = typeof(ServiceBase).GetMethod(onStopMethodName, BindingFlags.Instance | BindingFlags.NonPublic);
|
|
|
|
if(onStartMethod == null || onStopMethod == null) {
|
|
throw new InvalidOperationException("The ServiceBase class isn't available.");
|
|
}
|
|
|
|
List<Thread> serviceThreads = new List<Thread>();
|
|
"Starting services . . .".Info(loggerSource ?? SwanRuntime.EntryAssemblyName.Name);
|
|
|
|
foreach(ServiceBase service in @this) {
|
|
Thread thread = new Thread(() => {
|
|
_ = onStartMethod.Invoke(service, new Object[] { Array.Empty<String>() });
|
|
$"Started service '{service.GetType().Name}'".Info(loggerSource ?? service.GetType().Name);
|
|
});
|
|
|
|
serviceThreads.Add(thread);
|
|
thread.Start();
|
|
}
|
|
|
|
"Press any key to stop all services.".Info(loggerSource ?? SwanRuntime.EntryAssemblyName.Name);
|
|
_ = Terminal.ReadKey(true, true);
|
|
"Stopping services . . .".Info(SwanRuntime.EntryAssemblyName.Name);
|
|
|
|
foreach(ServiceBase service in @this) {
|
|
_ = onStopMethod.Invoke(service, null);
|
|
$"Stopped service '{service.GetType().Name}'".Info(loggerSource ?? service.GetType().Name);
|
|
}
|
|
|
|
foreach(Thread thread in serviceThreads) {
|
|
thread.Join();
|
|
}
|
|
|
|
"Stopped all services.".Info(loggerSource ?? SwanRuntime.EntryAssemblyName.Name);
|
|
}
|
|
}
|
|
}
|