public void TurnApplicationOffline(bool softTurnOff, bool recompileCompositeGenerated)
        {
            Verify.IsTrue(this.IsApplicationOnline, "The application is already offline");

            Log.LogVerbose("ApplicationOnlineHandlerFacade", string.Format("Turning off the application ({0})", softTurnOff ? "Soft" : "Hard"));


            _recompileCompositeGenerated = recompileCompositeGenerated;

            _shutdownGuard = new ShutdownGuard();

            try
            {
                if (softTurnOff == false)
                {
                    ApplicationOnlineHandlerPluginFacade.TurnApplicationOffline();
                    Verify.IsFalse(ApplicationOnlineHandlerPluginFacade.IsApplicationOnline(), "Plugin failed to turn the application offline");
                }
                else
                {
                    ConsoleMessageQueueFacade.Enqueue(new LockSystemConsoleMessageQueueItem(), "");
                }
            }
            catch (Exception)
            {
                _shutdownGuard.Dispose();
                _shutdownGuard = null;

                throw;
            }

            _isApplicationOnline = false;
            _wasLastTurnOffSoft  = softTurnOff;
        }
        public void TurnApplicationOffline(bool softTurnOff, bool recompileCompositeGenerated)
        {
            Verify.IsTrue(this.IsApplicationOnline, "The application is already offline");

            Log.LogVerbose("ApplicationOnlineHandlerFacade", string.Format("Turning off the application ({0})", softTurnOff ? "Soft" : "Hard"));


            _recompileCompositeGenerated = recompileCompositeGenerated;

            _shutdownGuard = new ShutdownGuard();

            try
            {
                if (softTurnOff == false)
                {
                    ApplicationOnlineHandlerPluginFacade.TurnApplicationOffline();
                    Verify.IsFalse(ApplicationOnlineHandlerPluginFacade.IsApplicationOnline(), "Plugin failed to turn the application offline");
                }
                else
                {
                    ConsoleMessageQueueFacade.Enqueue(new LockSystemConsoleMessageQueueItem(), "");
                }
            }
            catch(Exception)
            {
                _shutdownGuard.Dispose();
                _shutdownGuard = null;

                throw;
            }

            _isApplicationOnline = false;
            _wasLastTurnOffSoft = softTurnOff;
        }
        public void TurnApplicationOnline()
        {
            Verify.IsFalse(this.IsApplicationOnline, "The application is already online");

            Log.LogVerbose("ApplicationOnlineHandlerFacade", "Turning on the application");

            if (_recompileCompositeGenerated)
            {
                try
                {
                    CodeGenerationManager.GenerateCompositeGeneratedAssembly();
                }
                catch (Exception ex)
                {
                    Log.LogError(LogTitle, "Failed to recompile Composite.Generated.dll");
                    Log.LogError(LogTitle, ex);
                }
            }

            try
            {
                if (_wasLastTurnOffSoft == false)
                {
                    ApplicationOnlineHandlerPluginFacade.TurnApplicationOnline();
                    Verify.IsTrue(ApplicationOnlineHandlerPluginFacade.IsApplicationOnline(), "Plugin failed to turn the application online");
                }
            }
            finally
            {
                // Adding a sleep, so delayed notification from FileWatcher will not kill a newly spawned AppDomain
                Thread.Sleep(250);

                _shutdownGuard.Dispose();
                _shutdownGuard = null;

                if (HostingEnvironment.IsHosted)
                {
                    HostingEnvironment.InitiateShutdown();
                }
            }

            _isApplicationOnline = true;
        }