Example #1
0
        public MainForm()
        {
            InitializeComponent();
            this.Text   = Properties.Settings.Default.ServiceConsoleTitle;
            niMain.Text = this.Text;

            IServiceBase[] services = GetServicesFromMef();

            //加载/初始化配置
            if (File.Exists(ConfigFile))
            {
                var jsonStr = File.ReadAllText(ConfigFile);
                appConfig = JsonConvert.DeserializeObject <AppConfig>(jsonStr);
            }
            else
            {
                appConfig = new AppConfig();
            }

            if (appConfig.OneKeyStart == null)
            {
                appConfig.OneKeyStart = new Dictionary <string, ServiceItemConfig>();
            }

            if (appConfig.ServicesConfig == null)
            {
                appConfig.ServicesConfig = new Dictionary <string, string>();
            }

            //添加服务到界面
            pDown.SuspendLayout();
            var x = 3;
            var y = 38;

            Color defaultColor   = Color.FromArgb(255, 255, 202);
            Color alternateColor = Color.FromArgb(255, 233, 240);

            int i = 0;

            foreach (var service in services)
            {
                Control ui = null;

                if (service is IScheduledService)
                {
                    ui = new ScheduledServiceUI();
                }
                else if (service is IHostedService)
                {
                    if (service is WindowsServiceBase)
                    {
                        ui = new WindowsServiceUI();
                    }
                    else
                    {
                        ui = new HostedServiceUI();
                    }
                }


                if (ui == null)
                {
                    continue;
                }

                IBindedServiceUI bindedServiceUI = ui as IBindedServiceUI;
                IConfigurableUI  configurableUI  = ui as IConfigurableUI;
                IControllableUI  controllableUI  = ui as IControllableUI;


                ui.Left      = x;
                ui.Top       = y;
                ui.Width     = pDown.Width - x;
                ui.Anchor    = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
                ui.BackColor = i % 2 == 0 ? defaultColor : alternateColor;
                bindedServiceUI.BindedService = service;

                //加载服务配置到界面
                var    serviceName = service.ServiceName;
                string jsonString;
                if (configurableUI != null && appConfig.ServicesConfig.TryGetValue(serviceName, out jsonString))
                {
                    configurableUI.LoadConfig(jsonString);
                }

                pDown.Controls.Add(ui);
                y += ui.Height;

                bindedServiceUIs.Add(serviceName, bindedServiceUI);
                if (controllableUI != null)
                {
                    controllableUIs.Add(serviceName, controllableUI);
                }

                i++;
            }

            pDown.ResumeLayout();

            //初始化一键启动配置
            var oneKeyStart = appConfig.OneKeyStart;

            //移除多余配置
            var keys = oneKeyStart.Keys.ToArray();

            foreach (var key in keys)
            {
                if (!controllableUIs.ContainsKey(key))
                {
                    oneKeyStart.Remove(key);
                }
            }

            //添加应有配置
            foreach (var keyValue in controllableUIs)
            {
                ServiceItemConfig serviceItemConfig;
                if (!oneKeyStart.TryGetValue(keyValue.Key, out serviceItemConfig))
                {
                    serviceItemConfig = new ServiceItemConfig();
                    oneKeyStart.Add(keyValue.Key, serviceItemConfig);
                }
                serviceItemConfig.ServiceName = bindedServiceUIs[keyValue.Key].DisplayName;
            }

            updateInstallButton();

            if (containWindowsService && !containNotInstall) //有windows服务并且都已安装到系统中
            {
                refreshServicesStatus();
            }

            MainForm.Instance = this;

            if (!string.IsNullOrEmpty(Properties.Settings.Default.AutoUpdateUri))
            {
                updater = Updater.CreateUpdaterInstance(Properties.Settings.Default.AutoUpdateUri, "update_c.xml");
                tAutoUpdater.Interval = Properties.Settings.Default.AutoUpdateInterval * 1000;
                tAutoUpdater.Start();
            }
        }
Example #2
0
        public MainWindowViewModel(IDialogs dialogs)
        {
            _dialogs                 = dialogs;
            StopAllCommand           = new DelegateCommand(stopAll);
            CopyMessageCommand       = new DelegateCommand(copyMessage);
            DoOneKeyStartCommand     = new DelegateCommand(doOneKeyStart);
            ConfigOneKeyStartCommand = new DelegateCommand(configOneKeyStart);
            InstallCommand           = new DelegateCommand(install);
            RefreshStatusCommand     = new DelegateCommand(refreshStatus);
            ShowVersionCommand       = new DelegateCommand(() => _dialogs.ShowVersionsDialog());

            Title = Properties.Settings.Default.ServiceConsoleTitle;

            _niMain.Icon         = new System.Drawing.Icon("server.ico");
            _niMain.Visible      = true;
            _niMain.DoubleClick += _niMain_DoubleClick;
            _niMain.Text         = Title;
            _niMain.ContextMenu  = _contextMenu;
            _contextMenu.MenuItems.Add("退出").Click += (sender, e) =>
            {
                if (!_serviceUIs.All(ui => ui.Value is WindowsServiceUIViewModel || ui.Value.IsStopped))
                {
                    ShowError("某些驻留在控制台的服务还未停止!请先关闭这些服务,之后才能正常退出!");
                    return;
                }

                CanClose = true;
                Window.Close();
            };


            IServiceBase[] services = GetServicesFromMef();

            //加载/初始化配置
            if (File.Exists(ConfigFile))
            {
                var jsonStr = File.ReadAllText(ConfigFile);
                _appConfig = JsonConvert.DeserializeObject <AppConfig>(jsonStr);
            }
            else
            {
                _appConfig = new AppConfig();
            }

            if (_appConfig.OneKeyStart == null)
            {
                _appConfig.OneKeyStart = new Dictionary <string, ServiceItemConfig>();
            }

            if (_appConfig.ServicesConfig == null)
            {
                _appConfig.ServicesConfig = new Dictionary <string, string>();
            }


            List <ServiceViewModelBase> serviceViewModels = new List <ServiceViewModelBase>();

            foreach (var service in services)
            {
                ServiceViewModelBase ui = null;

                if (service is IScheduledService)
                {
                    ui = new ScheduledServiceUIViewModel(dialogs);
                }
                else if (service is IHostedService)
                {
                    if (service is WindowsServiceBase)
                    {
                        ui = new WindowsServiceUIViewModel();
                    }
                    else
                    {
                        ui = new HostedServiceUIViewModel();
                    }
                }

                if (ui == null)
                {
                    continue;
                }

                ui.BindedService = service;

                IConfigurableUI configurableUI = ui as IConfigurableUI;

                //加载服务配置到界面
                var serviceName = service.ServiceName;
                if (configurableUI != null && _appConfig.ServicesConfig.TryGetValue(serviceName, out string jsonString))
                {
                    configurableUI.LoadConfig(jsonString);
                }

                _serviceUIs.Add(serviceName, ui);

                serviceViewModels.Add(ui);
            }

            ServiceViewModels = new ObservableCollection <ServiceViewModelBase>(serviceViewModels);


            //初始化一键启动配置
            var oneKeyStart = _appConfig.OneKeyStart;

            //移除多余配置
            var keys = oneKeyStart.Keys.ToArray();

            foreach (var key in keys)
            {
                if (!_serviceUIs.ContainsKey(key))
                {
                    oneKeyStart.Remove(key);
                }
            }

            //添加应有配置
            foreach (var keyValue in _serviceUIs)
            {
                if (!oneKeyStart.TryGetValue(keyValue.Key, out ServiceItemConfig serviceItemConfig))
                {
                    serviceItemConfig = new ServiceItemConfig();
                    oneKeyStart.Add(keyValue.Key, serviceItemConfig);
                }
                serviceItemConfig.ServiceName = _serviceUIs[keyValue.Key].DisplayName;
            }

            updateInstallButton();

            if (_containWindowsService && !_containNotInstall) //有windows服务并且都已安装到系统中
            {
                refreshServicesStatus();
            }

            //配置自动升级
            if (!string.IsNullOrEmpty(Properties.Settings.Default.AutoUpdateUri))
            {
                configAutoUpdate();
            }
        }