Пример #1
0
        private async void DeleteSite()
        {
            using (var iisManager = new ServerManager())
            {
                // Stop Site
                var stopSiteProgress = new Progress <int>(percent =>
                {
                    this.progressStopSite.Value = percent;
                    this.UpdateTotalProgress();
                });
                try
                {
                    await Task.Run(() => IISController.StopSite(this.site.Id, stopSiteProgress)).ConfigureAwait(true);
                }
                catch (ArgumentException ex)
                {
                    this.Abort(ex.Source, ex.Message);
                }

                // Stop AppPool
                var stopAppPoolProgress = new Progress <int>(percent =>
                {
                    this.progressStopAppPool.Value = percent;
                    this.UpdateTotalProgress();
                });
                try
                {
                    await Task.Run(() => IISController.StopAppPool(this.site.Id, stopAppPoolProgress)).ConfigureAwait(true);
                }
                catch (ArgumentException ex)
                {
                    this.Abort(ex.Source, ex.Message);
                }

                // Delete Database
                var deleteDatabaseProgress = new Progress <int>(percent =>
                {
                    this.progressDeleteDatabae.Value = percent;
                    this.UpdateTotalProgress();
                });
                try
                {
                    var installFolder      = Directory.GetParent(this.sitePath);
                    var databaseController = new DatabaseController(
                        this.site.Name.Split('.')[0],
                        Properties.Settings.Default.DatabaseServerNameRecent,
                        true,
                        string.Empty,
                        string.Empty,
                        installFolder.FullName,
                        true,
                        this.site.Name);
                    await Task.Run(() => databaseController.DeleteDatabase(deleteDatabaseProgress)).ConfigureAwait(true);
                }
                catch (ArgumentException ex)
                {
                    this.Abort(ex.Source, ex.Message);
                    throw;
                }

                // Delete files
                var deleteFilesProgress = new Progress <string>(name =>
                {
                    if (this.progressDeleteFiles.Value < this.progressDeleteFiles.Maximum)
                    {
                        this.progressDeleteFiles.Value++;
                        this.UpdateTotalProgress();
                    }
                });
                try
                {
                    await Task.Run(() => FileSystemController.DeleteDirectory(this.sitePath, deleteFilesProgress, true)).ConfigureAwait(true);
                }
                catch (IOException)
                {
                    // Files mights still be streaming (logs for instance) after the site is stopped and deleted. Let's wait a bit and retry once after waiting 10 seconds.
                    Thread.Sleep(10000);
                    await Task.Run(() => FileSystemController.DeleteDirectory(this.sitePath, deleteFilesProgress, true)).ConfigureAwait(true);
                }

                this.progressDeleteFiles.Value = this.progressDeleteFiles.Maximum;
                this.UpdateTotalProgress();

                // Delete entry from HOSTS file
                var deleteHostEntryProgress = new Progress <int>(percent =>
                {
                    this.progressRemoveHostEntry.Value = percent;
                    this.UpdateTotalProgress();
                });
                await Task.Run(() => FileSystemController.RemoveHostEntry(this.site.Name, deleteHostEntryProgress)).ConfigureAwait(true);

                // Delete Site
                var deleteSiteProgress = new Progress <int>(percent =>
                {
                    this.progressDeletingSite.Value = percent;
                    this.UpdateTotalProgress();
                });
                try
                {
                    await Task.Run(() => IISController.DeleteSite(this.site.Id, deleteSiteProgress)).ConfigureAwait(true);
                }
                catch (ArgumentException ex)
                {
                    this.Abort(ex.Source, ex.Message);
                }

                // Try to delete AppPool if possible
                var deleteAppPoolProgress = new Progress <int>(percent =>
                {
                    this.progressDeleteAppPool.Value = percent;
                    this.UpdateTotalProgress();
                });
                try
                {
                    await Task.Run(() => IISController.DeleteAppPool(this.site.ApplicationDefaults.ApplicationPoolName, deleteAppPoolProgress)).ConfigureAwait(true);
                }
                catch (ArgumentException ex)
                {
                    DialogController.ShowMessage(
                        ex.Source,
                        ex.Message,
                        SystemIcons.Error,
                        DialogController.DialogButtons.OK);
                }
            }

            this.progressTotal.Value = this.progressTotal.Maximum;

            DialogController.ShowMessage(
                "Site Deleted",
                $"{this.site.Name} has been deleted successfully.",
                SystemIcons.Information,
                DialogController.DialogButtons.OK);
            this.DialogResult = DialogResult.OK;
            this.Close();
        }