示例#1
0
        protected override void OnCustomCommand(int command)
        {
            EventLog.Source = "Deployment Update Windows Service";
            try
            {
                if (command == 0)
                {
                    this.EventLog.WriteEntry("Service stopping ...");
                    NotificationDAO.Stop();
                    host.Close();
                    host = null;
                    NotificationDAO.UpdateRegistered  -= new EventHandler <UpdateNotificationEventArgs>(NotificationDAO_UpdateRegistered);
                    NotificationDAO.WarningRegistered -= new EventHandler <UnhandledExceptionEventArgs>(NotificationDAO_WarningRegistered);
                    NotificationDAO.ErrorRegistered   -= new EventHandler <UnhandledExceptionEventArgs>(NotificationDAO_ErrorRegistered);
                }
                else if (command == 1)
                {
                    this.EventLog.WriteEntry("Service starting ...");
                    NotificationDAO.UpdateRegistered  += new EventHandler <UpdateNotificationEventArgs>(NotificationDAO_UpdateRegistered);
                    NotificationDAO.WarningRegistered += new EventHandler <UnhandledExceptionEventArgs>(NotificationDAO_WarningRegistered);
                    NotificationDAO.ErrorRegistered   += new EventHandler <UnhandledExceptionEventArgs>(NotificationDAO_ErrorRegistered);

                    if (host != null)
                    {
                        host.Close();
                    }
                    host = new ServiceHost(typeof(ConfigurationService));
                    host.Open();

                    EventLog.WriteEntry(String.Format("WCF ConfigurationService hosted at {0} ({1})", host.BaseAddresses.FirstOrDefault().AbsoluteUri, host.State));
                    NotificationDAO.Start();
                }
                else if (command == 2)
                {
                    this.EventLog.WriteEntry("Service restarting ...");
                    NotificationDAO.Stop();
                    NotificationDAO.Start();
                }
            }
            catch (Exception ex)
            {
                this.EventLog.WriteEntry(String.Format("Following error was encountered {0}{1}{0}{2}{0}{3}", Environment.NewLine, ex.Message, ex.InnerException != null?ex.InnerException.Message:String.Empty, ex.StackTrace), System.Diagnostics.EventLogEntryType.Error);
            }
        }