コード例 #1
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();
            }
        }