public override void Uninstall(IDictionary state)
 {
     base.Uninstall(state);
     try {
         PolicyManager.Unregister();
     }
     catch {
     }
 }
 public override void Rollback(IDictionary state)
 {
     base.Rollback(state);
     if (state.Contains(RegisteredWithWMI))
     {
         try {
             PolicyManager.Unregister();
         }
         catch {
         }
     }
 }
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        static void Main(string[] args)
        {
            SipStringManager.CreateResourceManager("EnhancedAllowList");

            AppEventLog appEventLog = new AppEventLog(Config.ServiceName, "Application");

            Config.AppEventLog = appEventLog;

#if VSDEBUG
            //
            // Registration/Unregistration code for stand alone exe.
            // Service process reg/unreg will be done at install/uninstall time.
            //

            if (args.Length > 0)
            {
                string firstArg = args[0].ToLower();
                if (firstArg == "/register")
                {
                    PolicyManager.Register();
                }
                else if (firstArg == "/unregister")
                {
                    PolicyManager.Unregister();
                }
                else
                {
                    Console.WriteLine("Unknown argument - {0}", firstArg);
                }

                return;
            }

            ConsoleApp app = new ConsoleApp();

            app.Start();

            MessageBox.Show("Hit OK to exit ... ", "EAL Server");

            app.Stop();
#else
            ServiceBase[] ServicesToRun;

            ServicesToRun = new ServiceBase[] {
                new ServiceMain()
            };

            ServiceBase.Run(ServicesToRun);
#endif
        }
 /// <summary>
 /// Carry out custom installer actions.
 /// </summary>
 /// <remarks>
 /// We need to create WMI configuration only. EventLog will be done by InstallUtil.exe
 /// </remarks>
 public override void Install(IDictionary state)
 {
     base.Install(state);
     PolicyManager.Register();
     state.Add(RegisteredWithWMI, true);
 }
        /// <summary>
        /// This routine implements application startup. This is to be executed
        /// usually in a thread pool thread (so that the main thread can wait for stop
        /// events - from SCM or from console).
        /// </summary>
        private void RealStartService(object unused)
        {
            int i, maxRetries = 10;

            try {
                for (i = 0; i < maxRetries; i++)
                {
                    lock (serviceLock) {
                        if (cancelStart)
                        {
                            Config.AppEventLog.LogInfo(
                                SipStringManager.GetString("ServiceStartupAbort"));
                            return;
                        }
                    }

                    try {
                        ServerAgent.WaitForServerAvailable(10);
                        break;
                    }
                    catch {
                        if ((i % 2) == 0)
                        {
                            Config.AppEventLog.LogError(
                                SipStringManager.GetString("ServiceStartPending"));
                        }
                        continue;
                    }
                }

                if (i == maxRetries)
                {
                    Config.AppEventLog.LogError(
                        SipStringManager.GetString("ServerUnreachable"));
                    Environment.Exit(1);
                }

                lock (serviceLock) {
                    if (cancelStart)
                    {
                        Config.AppEventLog.LogInfo(
                            SipStringManager.GetString("ServiceStartupAbort"));
                        return;
                    }

                    policyManager = new PolicyManager();

                    try {
                        //
                        // Read settings.
                        //

                        NameValueCollection properties;
                        Config config = null;

                        try {
                            properties = ConfigurationManager.AppSettings;

                            config = new Config(properties);
                        }
                        catch (Exception e) {
                            Config.AppEventLog.LogError(
                                String.Format(SipStringManager.GetString("AppConfigFileParseError"), e.GetType().ToString(), e.Message));
                            Environment.Exit(1);
                        }

                        //
                        // See if a working directory has been specified.
                        //

                        if (config.WorkingDirectory != null)
                        {
                            try {
                                Environment.CurrentDirectory = config.WorkingDirectory;
                            }
                            catch {
                                Config.AppEventLog.LogError(
                                    String.Format(SipStringManager.GetString("InvalidWorkingDirectory"), config.WorkingDirectory));
                                Environment.Exit(1);
                            }
                        }

                        policyManager.Start(config);
                    }
                    catch (Exception e) {
                        Config.AppEventLog.LogError(
                            String.Format(SipStringManager.GetString("AppException"),
                                          e.Message, e.GetType().ToString(), e.StackTrace));

                        Console.WriteLine("Got exception: " + e.Message);
                        if (e.InnerException != null)
                        {
                            Console.WriteLine("Inner exception: " + e.InnerException.Message);
                        }

                        Environment.Exit(1);
                    }

                    Config.AppEventLog.LogInfo(SipStringManager.GetString("ServiceStarted"));
                }
            }
            finally {
                lock (serviceLock) {
                    startPending = false;
                }
            }
        }