private void TryCleanupExistingDownloadWebClient() { if (this.downloadWebClient == null) { return; } try { lock (this) { if (this.downloadWebClient != null) { this.downloadWebClient.DownloadFileCompleted -= OnDownloadCompleted; this.downloadWebClient.DownloadProgressChanged -= OnDownloadProgressChanged; this.downloadWebClient.OpenReadCompleted -= OnOpenReadCompleted; this.downloadWebClient.CancelAsync(); this.downloadWebClient.Dispose(); this.downloadWebClient = null; } } } catch (Exception e) { this.logger.Warn("Error while cleaning up web client : {0}", e.Message); } }
private void TriggerWebClientDownloadFileAsync() { this.logger.Debug("Falling back to legacy DownloadFileAsync."); try { this.isFallback = true; var destinationDirectory = Path.GetDirectoryName(this.localFileName); if (destinationDirectory != null && !Directory.Exists(destinationDirectory)) { Directory.CreateDirectory(destinationDirectory); } TryCleanupExistingDownloadWebClient(); this.downloadWebClient = CreateWebClient(); this.downloadWebClient.DownloadFileAsync(this.fileSource, this.localFileName); this.logger.Debug("Download async started. Source: {0} Destination: {1}", this.fileSource, this.localFileName); } catch (Exception ex) { this.logger.Warn("Failed to download Source:{0}, Destination:{1}, Error:{2}.", this.fileSource, this.localFileName, ex.Message); if (!AttemptDownload()) { InvokeDownloadCompleted(CompletedState.Failed, this.localFileName, ex); } } }
private DownloadWebClient CreateWebClient() { var webClient = new DownloadWebClient(); webClient.DownloadFileCompleted += OnDownloadCompleted; webClient.DownloadProgressChanged += OnDownloadProgressChanged; webClient.OpenReadCompleted += OnOpenReadCompleted; return(webClient); }
private void Download(Uri source, string fileDestination, long totalBytesToReceive) { try { long seekPosition; FileHelpers.TryGetFileSize(fileDestination, out seekPosition); TryCleanupExistingDownloadWebClient(); this.downloadWebClient = CreateWebClient(); this.downloadWebClient.OpenReadAsync(source, seekPosition); this.logger.Debug("Download started. Source: {0} Destination: {1} Size: {2}", source, fileDestination, totalBytesToReceive); } catch (Exception e) { this.logger.Debug("Download failed: {0}", e.Message); if (!AttemptDownload()) { InvokeDownloadCompleted(CompletedState.Failed, this.localFileName, e); } } }