/// <summary> /// The Virtual Radar Server main thread when running as a service. /// </summary> /// <param name="state"></param> private void ServiceThread(object state) { try { var args = (string[])state; ProgramLifetime.InitialiseUnhandledExceptionHandling(); ProgramLifetime.PrepassCommandLineArgs(args); ProgramLifetime.Headless = true; ApplicationInformation.SetHeadless(ProgramLifetime.Headless); InitialiseClassFactory(); ProgramLifetime.InitialiseManagers(); ProgramLifetime.LoadPlugins(); ProgramLifetime.RegisterPlugins(); ProgramLifetime.SingleInstanceStart(args); } catch (ThreadAbortException) { ; } catch (Exception ex) { try { var log = Factory.ResolveSingleton <ILog>(); log.WriteLine("Caught exception in ServiceThread: {0}", ex); } catch { ; } Stop(); } }
static void Main(string[] args) { ProgramLifetime.InitialiseUnhandledExceptionHandling(); ProgramLifetime.PrepassCommandLineArgs(args); ApplicationInformation.SetHeadless(ProgramLifetime.Headless); if (!ProgramLifetime.Headless) { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); } InitialiseClassFactory(); if (ProgramLifetime.Headless) { VirtualRadar.Interop.Console.ShowConsole(); } if (args.Contains("-showConfigFolder")) { ShowConfigurationFolder(); } ProgramLifetime.InitialiseManagers(); ProgramLifetime.LoadPlugins(); ProgramLifetime.RegisterPlugins(); ProgramLifetime.SingleInstanceStart(args); // Calling Environment.Exit rather than falling off the end of Main will ensure that all threads get shut down Environment.Exit(0); }
/// <summary> /// Initialises the database plugins. /// </summary> /// <remarks><para> /// In the field the utility will be running in the same folder as VirtualRadar.exe and will have /// access to all of VRS's plugins. The LoadPlugins call will load every plugin into the process. /// However we only want the database plugins to do any work, so the function does not call the normal /// RegisterImplementations on the plugin manager to get all of them hooked into the system. Rather it /// picks out the database plugins and just calls RegisterImplementations on those. The other plugins /// remain loaded but impotent. /// </para><para> /// The other thing to be aware of is that the utility's version number will be used by the VRS library /// code that loads plugins. If plugins unexpectedly fail to load then make sure that the utility's /// version number falls within the range of version numbers that the plugin's manifest is asking for. /// </para> /// </remarks> private static void LoadDatabasePlugins() { var pluginManager = Factory.ResolveSingleton <IPluginManager>(); ProgramLifetime.LoadPlugins(); foreach (var plugin in pluginManager.LoadedPlugins) { switch (plugin.Id) { case "VirtualRadarServer.Plugin.SqlServer": plugin.RegisterImplementations(Factory.Singleton); break; } } }
/// <summary> /// Called when Windows wants to stop the service. /// </summary> protected override void OnStop() { if (_ServiceThread != null) { IConsole console = null; try { console = Factory.ResolveSingleton <IConsole>(); } catch { console = null; } console?.WriteLine("Stopping service at {0:yyyy-MM-dd HH:mm:ss.fff} (UTC)", DateTime.UtcNow); ProgramLifetime.MainView?.CloseView(); ProgramLifetime.WaitForShutdown(ShutdownTimeoutMilliseconds); console?.WriteLine("Service stopped at {0:yyyy-MM-dd HH:mm:ss.fff} (UTC)", DateTime.UtcNow); } }
/// <summary> /// Initialises the database plugins. /// </summary> /// <remarks><para> /// In the field the utility will be running in the same folder as VirtualRadar.exe and will have /// access to all of VRS's plugins. The LoadPlugins call will load every plugin into the process. /// However we only want the database plugins to do any work, so the function does not call the normal /// RegisterImplementations on the plugin manager to get all of them hooked into the system. Rather it /// picks out the database plugins and just calls RegisterImplementations on those. The other plugins /// remain loaded but impotent. /// </para><para> /// The other thing to be aware of is that the utility's version number will be used by the VRS library /// code that loads plugins. If plugins unexpectedly fail to load then make sure that the utility's /// version number falls within the range of version numbers that the plugin's manifest is asking for. /// </para> /// </remarks> private static void LoadDatabasePlugins() { var pluginManager = Factory.ResolveSingleton <IPluginManager>(); ProgramLifetime.LoadPlugins(); var countLoaded = 0; foreach (var plugin in pluginManager.LoadedPlugins) { switch (plugin.Id) { case "VirtualRadarServer.Plugin.SqlServer": plugin.RegisterImplementations(Factory.Singleton); Console.WriteLine($"Plugin {plugin.Id} loaded"); ++countLoaded; break; } } Console.WriteLine($"{countLoaded:N0} plugin(s) loaded"); Console.WriteLine(); }
/// <summary> /// See interface docs. /// </summary> /// <param name="ex"></param> public void ShowUnhandledException(Exception ex) { ProgramLifetime.ShowException(ex); }
static void Main(string[] args) { var exitCode = 0; var verbose = (args ?? new string[0]).Any(r => String.Equals(r, "-verbose", StringComparison.OrdinalIgnoreCase)); try { ProgramLifetime.Headless = true; ProgramLifetime.InitialiseUnhandledExceptionHandling(); ProgramLifetime.ConfigureSecurityProtocols(); Factory.Register <IApplicationInformation, ApplicationInformation>(); VirtualRadar.SQLiteWrapper.Implementations.Register(Factory.Singleton); VirtualRadar.Headless.Implementations.Register(Factory.Singleton); VirtualRadar.Library.Implementations.Register(Factory.Singleton); VirtualRadar.Database.Implementations.Register(Factory.Singleton); ProgramLifetime.InitialiseManagers(); var appInfo = Factory.Resolve <IApplicationInformation>(); Console.WriteLine($"{appInfo.ApplicationName}, version {appInfo.ShortVersion}, built {appInfo.BuildDate} UTC"); Console.WriteLine(appInfo.Copyright); Console.WriteLine(); LoadDatabasePlugins(); CommandRunner commandRunner = null; var options = OptionsParser.Parse(args); switch (options.Command) { case Command.ApplySchema: commandRunner = new CommandRunner_ApplySchema(); break; case Command.Import: commandRunner = new CommandRunner_Import(); break; default: OptionsParser.Usage("Missing command"); break; } commandRunner.Options = options; if (!commandRunner.Run()) { exitCode = 1; } } catch (Exception ex) { if (!verbose) { Console.WriteLine($"Caught exception: {ex.Message}"); } else { Console.WriteLine($"Caught exception: {ex.ToString()}"); Console.WriteLine(); } try { var log = Factory.ResolveSingleton <ILog>(); log.WriteLine($"Caught exception in BaseStationImport: {ex.ToString()}"); Console.WriteLine("Full details have been recorded in the log"); } catch (Exception iEx) { Console.WriteLine($"The exception could not be logged: {iEx.Message}"); } exitCode = 2; } Environment.Exit(exitCode); }