Пример #1
0
        /// <summary>
        /// Mains the specified arguments.
        /// </summary>
        /// <param name="args">The arguments.</param>
        static void Main(string[] args)
        {
            XmlConfigurator.Configure();
            try
            {
                Log.InfoFormat("##########   Starting service '{0}', V '{1}'   ##########",
                               AssemblyHelper.AssemblyTitle,
                               AssemblyHelper.AssemblyVersion);

                // Add the event handler for handling unhandled  exceptions to the event.
                AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;

                // Start Service Updater
                IUpdater      selfupdater = null;
                ServiceHosted service     = new ServiceHosted();

                IUpdateManager updateManager = null;
                try
                {
                    Log.Info("Updater Initialisation");
                    updateManager = new UpdateManager(_urlNugetRepositories, AssemblyHelper.AssemblyTitle);
                    selfupdater   = new RepeatedTimeUpdater(updateManager).SetCheckUpdatePeriod(TimeSpan.FromMinutes(30));
                    selfupdater.Start();
                }
                catch (Exception exx)
                {
                    Log.WarnFormat("'{0}' is not installed via Squirrel. Install program first.", AssemblyHelper.AssemblyTitle);
                    Log.Warn(exx);
                }

                // Start TopShelf
                var x = new SquirreledHost(service, AssemblyHelper.CurrentAssembly, selfupdater, true, RunAS.LocalSystem);

                // If RunAS.RunSpecificUser set login / password
                //x.SetCredentials("", "");

                x.ConfigureAndRun(HostConfig =>
                {
                    HostConfig.Service <ServiceHosted>(s =>
                    {
                        s.ConstructUsing(name => new ServiceHosted());
                        s.WhenStarted(tc => tc.Start());
                        s.WhenStopped(tc => tc.Stop());
                        s.WhenPaused(tc => { });
                        s.WhenContinued(tc => { });
                    });
                    HostConfig.EnableServiceRecovery(rc => rc.RestartService(1));
                    HostConfig.EnableSessionChanged();
                    HostConfig.UseLog4Net();
                    HostConfig.StartAutomatically();
                });
            }
            catch (Exception ex)
            {
                Log.Error(ex);
            }
            finally
            {
                Log.InfoFormat("##########   Stoppping service '{0}', V '{1}'   ##########",
                               AssemblyHelper.AssemblyTitle,
                               AssemblyHelper.AssemblyVersion);
            }
        }