Example #1
0
        protected override void OnStop()
        {
            //

            try
            {
                if (WCFHost != null)
                {
                    WCFHost.Close();
                }
            }
            catch (Exception e)
            {
                logger.Error("WCF close error ", e);
            }

            try
            {
                sqlTaskScheduler.Stop();

                logger.Debug("collection service stopped");
            }
            catch (Exception e)
            {
                logger.Error("stop service error ", e);
            }

            //   sqlTaskScheduler.Stop();
        }
Example #2
0
        private static void UninstallService(ISLogger logger)
        {
            if (!IsInstalled())
            {
                Console.WriteLine("CollectionService already uninstalled! ");
                logger.Debug("CollectionService already uninstalled! ");
                return;
            }



            try
            {
                using (AssemblyInstaller installer = GetInstaller(logger))
                {
                    IDictionary state = new Hashtable();
                    try
                    {
                        installer.Uninstall(state);
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine("Uninstall CollecionService error " + e.Message);
                        logger.Error("Uninstall CollecionService error " + e.Message);
                        logger.Error(e.ToString());
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Uninstall CollecionService error " + e.Message);
                logger.Error("Uninstall CollecionService error " + e.Message);
                logger.Error(e.ToString());
            }
        }
Example #3
0
        private static void InstallService(ISLogger logger)
        {
            if (IsInstalled())
            {
                Console.WriteLine("Try to install collection service but, service already installed! ");
                logger.Debug("Try to install collection service but, service already installed! ");
                return;
            }

            logger.Debug("Install collection service... ");

            AssemblyInstaller installer = GetInstaller(logger);
            IDictionary       state     = new Hashtable();

            try
            {
                installer.Install(state);
                installer.Commit(state);
            }
            catch (Exception e)
            {
                Console.WriteLine("Install CollectionService error " + e.Message);
                logger.Error("Install CollectionService error " + e.Message);
                logger.Error(e.ToString());
            }
            finally
            {
                try { installer.Rollback(state); } catch { };
            }
        }
Example #4
0
        private static void StopService(ISLogger logger)
        {
            if (!IsInstalled())
            {
                logger.Debug("try stop  collection service, but it is not installed! ");
                Console.WriteLine("try stop  collection service, but it is not installed! ");
                return;
            }

            logger.Debug("Stopping  collection service... ");
            Console.WriteLine("Stopping  collection service... ");


            using (ServiceController controller =
                       new ServiceController(name))
            {
                try
                {
                    if (controller.Status != ServiceControllerStatus.Stopped)
                    {
                        controller.Stop();
                        controller.WaitForStatus(ServiceControllerStatus.Stopped,
                                                 TimeSpan.FromSeconds(10));
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine("Stop CollectionService error " + e.Message);
                    logger.Error("Stop CollectionService error " + e.Message);
                    logger.Error(e.ToString());
                }
            }
        }
Example #5
0
        private static void StartService(ISLogger logger)
        {
            if (!IsInstalled())
            {
                logger.Debug("CollectionService already started! ");
                Console.WriteLine("CollectionService already started! ");
                return;
            }

            logger.Debug("Starting  collection service.... ");
            Console.WriteLine("Starting  collection service.... ");

            using (ServiceController controller =
                       new ServiceController(name))
            {
                try
                {
                    if (controller.Status != ServiceControllerStatus.Running)
                    {
                        controller.Start();
                        controller.WaitForStatus(ServiceControllerStatus.Running, TimeSpan.FromSeconds(10));
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine("Start CollectionService() error " + e.Message);
                    logger.Error("Start CollectionService() error " + e.Message);
                    logger.Error(e.ToString());
                }
            }
        }
        public async Task <SqlConnection> OpenConnection(Instance instance)
        {
            if (instance == null)
            {
                logger.Error("ConnectionManager.OpenConnection: get null instance");
                return(null);
            }

            Impersonation       impersonation = null;
            ImpersonationResult impResult     = null;

            connection = new SqlConnection(BuildConnectionString(instance));


            if (instance.Authentication == AuthenticationType.Windows)
            {
                impersonation = new Impersonation();
                impResult     = impersonation.Impersonate(instance.Login, null, instance.Password);//тут

                if (impResult.HasError)
                {
                    logger.Error("Imperosnation error: ConnectionManager.OpenConnection: Instance id =" + instance.Id);
                }
                else
                {
                    logger.Debug("Impersonation oK instance id =" + instance.Id);
                }

                return(null);
            }


            try
            {
                //ConfigureAwait(true); to return to the same context to do undo imersonation for the same thread
                await connection.OpenAsync().ConfigureAwait(true);
            }
            catch (Exception e)
            {
                logger.Error("ConnectionManager.OpenConnection: Error open connection  instance=" + instance.InstanceName, e);
                return(null);
            }
            finally
            {
                if (impResult != null && impersonation != null)
                {
                    if (!impResult.HasError)
                    {
                        impersonation.UndoImpersonation(impResult.User);
                    }
                }
            }

            return(connection);
        }
Example #7
0
 public void SaveStatusOnly(InstanceInfo[] newInfo, ISLogger logger)
 {
     try
     {
         MsSqlMonitorEntities context = new MsSqlMonitorEntities();
         MapBaseInstanceDataToContext(newInfo, context);
         context.SaveChanges();
     }catch (Exception e)
     {
         logger.Error("LocalDb.SaveSatusOnly " + e.Message);
         logger.Error(e.StackTrace);
     }
 }
Example #8
0
 public void start()
 {
     try
     {
         WCFHost = new ServiceHost(typeof(WCFTEST), new Uri[] { new Uri("net.tcp://localhost:9999") });
         WCFHost.AddServiceEndpoint(typeof(IWCFContract), new NetTcpBinding(), "WCFService");
         WCFHost.Open();
     }
     catch (Exception e)
     {
         WCFHost = null;
         logger.Error("WCF open error ", e);
     }
     sqlTaskScheduler.Start();
 }
Example #9
0
        public async void RemoveInstanceForeverAsync(CollectionResult result, ISLogger logger)
        {
            logger.Debug("Removing forever instance id=" + result.InstanceID);

            try
            {
                MsSqlMonitorEntities context  = new MsSqlMonitorEntities();
                Instance             instance = context.Instances.Find(result.InstanceID);

                instance.Assigns.ToList().ForEach(i => context.Assigns.Remove(i));


                instance.Roles.ToList().ForEach(r => { r.RefuseAllLogins(); context.Entry(r).State = EntityState.Deleted; });
                // Delete all users before update.
                instance.Logins.ToList().ForEach(l => context.Entry(l).State = EntityState.Deleted);
                // Delete all roles from databases
                instance.Databases.ToList().ForEach(db => db.Roles.ToList().ForEach(role => { role.RefuseAllUsers(); context.Entry(role).State = EntityState.Deleted; }));
                instance.Databases.ToList().ForEach(db => db.Users.ToList().ForEach(user => { user.RefuseAllRoles(); context.Entry(user).State = EntityState.Deleted; }));
                // Delete all databases
                instance.Databases.ToList().ForEach(db => context.Entry(db).State = EntityState.Deleted);


                context.Entry(instance).State = EntityState.Deleted;

                await context.SaveChangesAsync().ConfigureAwait(false);
            }
            catch (Exception e)
            {
                logger.Error(e.StackTrace);
            }
        }
Example #10
0
 public void SaveInstances(InstanceInfo[] newInfo, ISLogger logger)
 {
     try
     {
         using (MsSqlMonitorEntities context = new MsSqlMonitorEntities())
         {
             MapWholeInstanceDataToContext(context, newInfo);
             context.SaveChanges();
         }
     }
     catch (Exception e)
     {
         logger.Error("LocalDb.SaveSatusOnly " + e.Message);
         logger.Error(e.StackTrace);
     }
 }
Example #11
0
 public async Task SaveStatusOnlyAsync(InstanceInfo[] newInfo, ISLogger logger)
 {
     try
     {
         MsSqlMonitorEntities context = new MsSqlMonitorEntities();
         MapBaseInstanceDataToContext(newInfo, context);
         await context.SaveChangesAsync().ConfigureAwait(false);
     }
     catch (AggregateException e)
     {
         foreach (Exception ex in e.InnerExceptions)
         {
             logger.Error("LocalDb.SaveSatusOnly " + ex.Message);
         }
         logger.Error(e.StackTrace);
     }
 }
Example #12
0
        protected override void OnStart(string[] args)
        {
            IUnityContainer unity = DependencyConfig.Initialize();

            logger            = ServiceLocator.Current.GetInstance <ISLogger>();
            resourceManager   = ServiceLocator.Current.GetInstance <IResourceManager>();
            connManager       = ServiceLocator.Current.GetInstance <IConnectionManager>();
            encryptionManager = ServiceLocator.Current.GetInstance <IEncryptionManager>();

            logger.Debug("collection service started");


            unitOfWork = unity.Resolve <IUnitOfWork>(new ParameterOverride("context", new MsSqlMonitorEntities()));


            try
            {
                WCFHost = new ServiceHost(this, new Uri[] { new Uri(WCFADRESS) });
                var behaviour = WCFHost.Description.Behaviors.Find <ServiceBehaviorAttribute>();
                behaviour.InstanceContextMode = InstanceContextMode.Single;

                WCFHost.AddServiceEndpoint(typeof(IWCFContract), new NetTcpBinding(), WCFSERVICE);
                WCFHost.Open();
            }
            catch (Exception e)
            {
                WCFHost = null;
                logger.Error("WCF open error ", e);
            }


            try
            {
                sqlTaskScheduler = new SQLTaskScheduler(logger, resourceManager, unitOfWork, connManager, encryptionManager);
                sqlTaskScheduler.Start();
            }
            catch (Exception e)
            {
                logger.Error("start service error ", e);
            }

            //
        }
Example #13
0
 public string Decrypt(string key, string encryptedText)
 {
     if (key != null)
     {
         //logger.Debug(($"SecurityManager Decrypt({encryptedText}, {key})"));
         return(encryptor.Decrypt(encryptedText, key, encryptionPassword));
     }
     else
     {
         logger.Error(($"SecurityManager Decrypt({encryptedText}, key==null)"));
         return(encryptedText);
     }
 }