/// <summary> /// Processes the synchronisation. /// </summary> /// <param name="stream">The stream.</param> /// <param name="content">The content.</param> /// <param name="transportContext">The transport context.</param> private void ProcessSync(Stream stream, HttpContent content, TransportContext transportContext) { try { using (stream) using (var client = new WebClient()) { // Ensure TLS 1.2 is enabled. ServicePointManager.SecurityProtocol = ServicePointManager.SecurityProtocol | SecurityProtocolType.Tls12; int updatedRessources = 0; client.Headers[HttpRequestHeader.Authorization] = $"Bearer {JwtHelper.GetAuthorizationToken()}"; client.Headers[HttpRequestHeader.Cookie] = "x-ms-routing-name=self"; var webServiceUri = new Uri(Settings.ServerToSync); var files = GetFileList(client, webServiceUri); WriteLine(stream, $"Files to sync: {files.Count()}"); foreach (var file in files) { string temp = null; try { var local = new FileInfo(HostingEnvironment.MapPath(file.Name)); if (!local.Exists || DateHelper.ToEpoch(local.LastWriteTimeUtc) != file.LastModified || local.Length != file.Size) { temp = Path.GetTempFileName(); local.Directory.Create(); client.Headers[HttpRequestHeader.Authorization] = $"Bearer {JwtHelper.EncodeUrl(file.Name)}"; client.DownloadFile(new Uri(webServiceUri, "Download?x-ms-routing-name=self"), temp); File.SetLastWriteTimeUtc(temp, DateHelper.FromEpoch(file.LastModified)); File.Copy(temp, local.FullName, true); updatedRessources++; WriteLine(stream, $"Sync: {file.Name}"); } } finally { if (temp != null) { File.Delete(temp); } } } WriteLine(stream, $"Success - Updated: {updatedRessources} files."); } } catch (Exception exception) { WriteLine(stream, $"Error: {exception}"); } }