public void StartAsync(Action <bool> callback)
 {
     TaskUtils.StartLongRunning(() =>
     {
         _warningHandler.Info("Service starting: " + _settings.GetAddress());
         var binding = new BasicHttpBinding(BasicHttpSecurityMode.Transport);
         binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;
         binding.CloseTimeout        =
             binding.OpenTimeout     =
                 binding.SendTimeout = TimeSpan.FromMinutes(ServerTimoutMinutes);
         var address = new Uri(_settings.GetAddress());
         var service = new LazuriteService(_settings.SecretKey);
         _host       = new WebServiceHost(service, address);
         _host.AddServiceEndpoint(typeof(IServer), binding, address);
         _host.Credentials.UserNameAuthentication.UserNamePasswordValidationMode  = UserNamePasswordValidationMode.Custom;
         _host.Credentials.UserNameAuthentication.CustomUserNamePasswordValidator = new LoginValidator();
         _host.Credentials.ServiceCertificate.SetCertificate(
             StoreLocation.LocalMachine,
             StoreName.My,
             X509FindType.FindByThumbprint,
             _settings.CertificateHash);
         _host.Open();
         _warningHandler.Info("Service started: " + _settings.GetAddress());
         callback?.Invoke(true);
         Started = true;
         StatusChanged?.Invoke(this, new EventsArgs <LazuriteServer>(this));
     },
                                (exception) => {
         _warningHandler.Error("Ошибка запуска сервиса: " + _settings.GetAddress(), exception);
         callback?.Invoke(false);
         Started = false;
         StatusChanged?.Invoke(this, new EventsArgs <LazuriteServer>(this));
     });
 }
        public CertificateSelectView(ServerSettings settings)
        {
            InitializeComponent();
            _settings = settings;
            Refresh();

            btInstallNewCert.Click += (o, e) =>
            {
                var openFileDialog = new OpenFileDialog();
                openFileDialog.Filter = "Файл сертификата (*.pfx,*.cer,*.p7b)|*.pfx;*.cer;*.p7b";
                if (openFileDialog.ShowDialog() == DialogResult.OK)
                {
                    EnterPasswordView.Show("Введите пароль сертификата...", (pass) =>
                    {
                        try
                        {
                            var certHash = SecurityHelper.AddCertificateInWindows(openFileDialog.FileName, pass, (msg) => _warningHandler.Info(msg));
                            Refresh();
                            certListView.GetItems().Where(x => ((ServerHelper.CertificateInfo)((ItemView)x).Tag).Hash.Equals(certHash)).All(x => x.Selected = true);
                        }
                        catch (Exception exception)
                        {
                            _warningHandler.ErrorFormat(exception, "Ошибка при добавлении сертификата [{0}]", openFileDialog.FileName);
                        }
                    }, null, "Если файл сертификата не имеет пароль, то нажмите \"Применить\"");
                }
            };

            certListView.SelectionChanged += (o, e) => btApply.IsEnabled = true;
            btApply.Click += (o, e) =>
            {
                var selectedCert = (ServerHelper.CertificateInfo)((ItemView)certListView.SelectedItem).Tag;
                _settings.CertificateHash = selectedCert.Hash;
                Selected?.Invoke(settings);
            };
        }