/// <summary> /// The entry point of the SynchroFeed.Listener application. /// </summary> /// <param name="args">The arguments.</param> public static void Main(string[] args) { var logFactory = new NLog.LogFactory(); var logger = logFactory.GetLogger("SynchroFeed.Listener.Program"); logger.Log(NLog.LogLevel.Info, "SynchroFeed.Listener Starting"); logger.Log(NLog.LogLevel.Debug, $"args length: {args.Length}"); foreach (var arg in args) { logger.Log(NLog.LogLevel.Debug, $"arg: {arg}"); } var commandLineApp = new ListenerCommandLine(); Environment.ExitCode = Execute(commandLineApp, logger); }
/// <summary> /// Creates the host builder and initializes the application. /// </summary> /// <param name="commandLineApp">The parsed command line arguments.</param> /// <param name="logger">The NLog logger instance.</param> /// <returns>Returns an instance of IHostBuilder.</returns> private static IHostBuilder CreateHostBuilder(ListenerCommandLine commandLineApp, NLog.Logger logger) { var builder = new HostBuilder() .ConfigureAppConfiguration((hostingContext, config) => { logger.Log(NLog.LogLevel.Debug, $"Using config file: {commandLineApp.GetFullPathConfigFile()}"); var configFilename = commandLineApp.GetFullPathConfigFile(); if (!File.Exists(configFilename)) { logger.Log(NLog.LogLevel.Fatal, $"Configuration file not found {configFilename}. Aborting..."); Environment.Exit(1); } config.SetBasePath(Directory.GetCurrentDirectory()); config.AddJsonFile(configFilename); }) .ConfigureServices((hostContext, services) => { services.AddOptions(); services.AddLogging(); services.AddSingleton(commandLineApp); services.Configure <ApplicationSettings>(hostContext.Configuration.GetSection("FeedSettings")); services.Configure <Settings.AwsSettings>(hostContext.Configuration.GetSection("AwsSettings")); services.AddSingleton(provider => provider.GetService <IOptions <Settings.AwsSettings> >().Value); services.AddSingleton(typeof(ListenerService)); services.AddSynchroFeed(); }) .ConfigureLogging((hostingContext, logging) => { logging.SetMinimumLevel(LogLevel.Trace); logging.AddNLog(); }); return(builder); }
/// <summary> /// The main setup and execution logic for the SynchroFeed.Listener program. /// </summary> /// <param name="commandLineApp">The command line application containing the parsed command line parameters.</param> /// <param name="logger">The NLog logger instance.</param> /// <returns>System.Int32.</returns> private static int Execute(ListenerCommandLine commandLineApp, NLog.Logger logger) { var rc = HostFactory.Run(x => { x.Service <ListenerService>(s => { x.AddCommandLineDefinition("config", f => { commandLineApp.ConfigFile = f; }); x.ApplyCommandLine(); s.ConstructUsing(name => { logger.Log(NLog.LogLevel.Debug, "Calling CreateHostBuilder..."); var host = CreateHostBuilder(commandLineApp, logger) .Build(); logger.Log(NLog.LogLevel.Debug, "Calling CreateHostBuilder...done"); return(host.Services.GetRequiredService <ListenerService>()); }); s.WhenStarted(tc => tc.Start()); s.WhenStopped(tc => { try { tc.Stop(); } finally { NLog.LogManager.Shutdown(); } }); }); x.RunAsLocalSystem(); x.SetDescription("SynchroFeed Listener"); x.SetDisplayName("SynchroFeed Listener"); x.SetServiceName("SynchroFeedListener"); x.AfterInstall(installHostSettings => { using (var scmHandle = NativeMethods.OpenSCManager(null, null, (int)NativeMethods.SCM_ACCESS.SC_MANAGER_ALL_ACCESS)) { using (var serviceHandle = NativeMethods.OpenService(scmHandle, installHostSettings.ServiceName, (int)NativeMethods.SCM_ACCESS.SC_MANAGER_ALL_ACCESS)) { var exePath = Process.GetCurrentProcess().MainModule.FileName + $" -config:{commandLineApp.GetFullPathConfigFile()}"; ChangeServiceConfig(serviceHandle, SERVICE_NO_CHANGE, SERVICE_NO_CHANGE, SERVICE_NO_CHANGE, exePath, null, IntPtr.Zero, null, null, null, null); } } }); }); var exitCode = (int)Convert.ChangeType(rc, rc.GetTypeCode()); //11 return(exitCode); }