diff --git a/src/NuGet.Core/NuGet.Packaging/LocalPackageArchiveDownloader.cs b/src/NuGet.Core/NuGet.Packaging/LocalPackageArchiveDownloader.cs index 521181952bf..53d2ba2032e 100644 --- a/src/NuGet.Core/NuGet.Packaging/LocalPackageArchiveDownloader.cs +++ b/src/NuGet.Core/NuGet.Packaging/LocalPackageArchiveDownloader.cs @@ -176,8 +176,7 @@ public async Task CopyNupkgFileToAsync(string destinationFilePath, Cancell FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite | FileShare.Delete, - bufferSize: 4096, - useAsync: false)) + bufferSize: 4096)) { // This value comes from NuGet.Protocol.StreamExtensions.CopyToAsync(...). // While 8K may or may not be the optimal buffer size for copy performance, @@ -285,8 +284,7 @@ private FileStream GetSourceStream() FileMode.Open, FileAccess.Read, FileShare.Read, - bufferSize: 4096, - useAsync: true); + bufferSize: 4096); } private void ThrowIfDisposed() diff --git a/src/NuGet.Core/NuGet.Packaging/PackageExtractor.cs b/src/NuGet.Core/NuGet.Packaging/PackageExtractor.cs index 675a70a1704..64235d00bb6 100644 --- a/src/NuGet.Core/NuGet.Packaging/PackageExtractor.cs +++ b/src/NuGet.Core/NuGet.Packaging/PackageExtractor.cs @@ -458,12 +458,11 @@ public static async Task InstallFromSourceAsync( { // Extract the nupkg using (var nupkgStream = new FileStream( - targetTempNupkg, - FileMode.Create, - FileAccess.ReadWrite, - FileShare.ReadWrite | FileShare.Delete, - bufferSize: 4096, - useAsync: true)) + targetTempNupkg, + FileMode.Create, + FileAccess.ReadWrite, + FileShare.ReadWrite | FileShare.Delete, + bufferSize: 4096)) { await copyToAsync(nupkgStream); nupkgStream.Seek(0, SeekOrigin.Begin); diff --git a/src/NuGet.Core/NuGet.Protocol/HttpSource/HttpCacheUtility.cs b/src/NuGet.Core/NuGet.Protocol/HttpSource/HttpCacheUtility.cs index 838e50dee8d..7b3d7e0093a 100644 --- a/src/NuGet.Core/NuGet.Protocol/HttpSource/HttpCacheUtility.cs +++ b/src/NuGet.Core/NuGet.Protocol/HttpSource/HttpCacheUtility.cs @@ -3,7 +3,6 @@ using System; using System.IO; -using System.Linq; using System.Net.Http; using System.Threading; using System.Threading.Tasks; @@ -67,44 +66,24 @@ public static async Task CreateCacheFileAsync( // The update of a cached file is divided into two steps: // 1) Delete the old file. // 2) Create a new file with the same name. - - // Some FileStream operations on Windows are synchronous even though it may not seem so. - // The HTTP stack rewrite in .NET Core 2.1 introduced circumstances whereby these - // synchronous FileStream calls will keep an IO completion thread busy and block other - // HTTP requests from completing. The immediate solution is to perform write and read - // operations on separate streams, but only on .NET Core where the problem exists. - // See https://github.com/dotnet/corefx/issues/31914 for details. - const int writeBufferSize = -#if IS_CORECLR - 1; // This disables write buffering. -#else - BufferSize; -#endif - using (var fileStream = new FileStream( result.NewFile, FileMode.Create, - FileAccess.Write, + FileAccess.ReadWrite, FileShare.None, - writeBufferSize, - useAsync: true)) + BufferSize)) { using (var networkStream = await response.Content.ReadAsStreamAsync()) { await networkStream.CopyToAsync(fileStream, BufferSize, cancellationToken); } - } - using (var fileStream = new FileStream( - result.NewFile, - FileMode.Open, - FileAccess.Read, - FileShare.None, - BufferSize, - useAsync: true)) - { // Validate the content before putting it into the cache. - ensureValidContents?.Invoke(fileStream); + if (ensureValidContents != null) + { + fileStream.Seek(0, SeekOrigin.Begin); + ensureValidContents.Invoke(fileStream); + } } if (File.Exists(result.CacheFile)) @@ -141,8 +120,7 @@ public static async Task CreateCacheFileAsync( FileMode.Open, FileAccess.Read, FileShare.Read | FileShare.Delete, - BufferSize, - useAsync: true); + BufferSize); } } -} +} \ No newline at end of file diff --git a/src/NuGet.Core/NuGet.Protocol/Plugins/PluginCacheEntry.cs b/src/NuGet.Core/NuGet.Protocol/Plugins/PluginCacheEntry.cs index 32c6c9c18d4..b0bd8fba8ee 100644 --- a/src/NuGet.Core/NuGet.Protocol/Plugins/PluginCacheEntry.cs +++ b/src/NuGet.Core/NuGet.Protocol/Plugins/PluginCacheEntry.cs @@ -87,8 +87,7 @@ public async Task UpdateCacheFileAsync() FileMode.Create, FileAccess.ReadWrite, FileShare.None, - CachingUtility.BufferSize, - useAsync: true)) + CachingUtility.BufferSize)) { var json = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(OperationClaims, Formatting.Indented)); await fileStream.WriteAsync(json, 0, json.Length); @@ -116,4 +115,4 @@ public async Task UpdateCacheFileAsync() } } } -} +} \ No newline at end of file diff --git a/src/NuGet.Core/NuGet.Protocol/RemotePackageArchiveDownloader.cs b/src/NuGet.Core/NuGet.Protocol/RemotePackageArchiveDownloader.cs index bea7ab70f09..700ae4cd72c 100644 --- a/src/NuGet.Core/NuGet.Protocol/RemotePackageArchiveDownloader.cs +++ b/src/NuGet.Core/NuGet.Protocol/RemotePackageArchiveDownloader.cs @@ -172,8 +172,7 @@ public async Task CopyNupkgFileToAsync(string destinationFilePath, Cancell FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite | FileShare.Delete, - bufferSize: 4096, - useAsync: true)) + bufferSize: 4096)) { var result = await _resource.CopyNupkgToStreamAsync( _packageIdentity.Id, @@ -286,8 +285,7 @@ private FileStream GetDestinationStream() FileMode.Open, FileAccess.Read, FileShare.Read, - bufferSize: 4096, - useAsync: true); + bufferSize: 4096); } private void ThrowIfDisposed() diff --git a/src/NuGet.Core/NuGet.Protocol/Utility/CachingUtility.cs b/src/NuGet.Core/NuGet.Protocol/Utility/CachingUtility.cs index 48bec9ba37a..2ae0d05e17a 100644 --- a/src/NuGet.Core/NuGet.Protocol/Utility/CachingUtility.cs +++ b/src/NuGet.Core/NuGet.Protocol/Utility/CachingUtility.cs @@ -45,8 +45,7 @@ public static Stream ReadCacheFile(TimeSpan maxAge, string cacheFile) FileMode.Open, FileAccess.Read, FileShare.Read | FileShare.Delete, - BufferSize, - useAsync: true); + BufferSize); return stream; } @@ -63,7 +62,7 @@ public static bool IsFileAlreadyOpen(string filePath) { stream = new FileStream(filePath, FileMode.Open, FileAccess.ReadWrite, FileShare.None); } - catch(FileNotFoundException) + catch (FileNotFoundException) { return false; } @@ -92,4 +91,4 @@ public static string RemoveInvalidFileNameChars(string value) .Replace("__", "_"); } } -} +} \ No newline at end of file diff --git a/src/NuGet.Core/NuGet.Protocol/Utility/FindPackagesByIdNupkgDownloader.cs b/src/NuGet.Core/NuGet.Protocol/Utility/FindPackagesByIdNupkgDownloader.cs index 38a1740ab1b..5e398322307 100644 --- a/src/NuGet.Core/NuGet.Protocol/Utility/FindPackagesByIdNupkgDownloader.cs +++ b/src/NuGet.Core/NuGet.Protocol/Utility/FindPackagesByIdNupkgDownloader.cs @@ -55,7 +55,7 @@ public async Task GetNuspecReaderFromNupkgAsync( CancellationToken token) { NuspecReader reader = null; - + lock (_nuspecReadersLock) { if (_nuspecReaders.TryGetValue(url, out reader)) @@ -76,7 +76,7 @@ await ProcessNupkgStreamAsync( cacheContext, logger, token); - + if (reader == null) { // The package was not found on the feed. This typically means @@ -245,7 +245,7 @@ private async Task ProcessStreamAndGetCacheEntryAsync( logger, token); } - + private async Task ProcessHttpSourceResultAsync( PackageIdentity identity, string url, @@ -336,8 +336,7 @@ private async Task ProcessCacheEntryAsync( FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete, - StreamExtensions.BufferSize, - useAsync: true)); + StreamExtensions.BufferSize)); }, token)) { @@ -354,9 +353,9 @@ public CacheEntry(string cacheFile, bool alreadyProcessed) CacheFile = cacheFile; AlreadyProcessed = alreadyProcessed; } - + public string CacheFile { get; } public bool AlreadyProcessed { get; } } } -} +} \ No newline at end of file diff --git a/src/NuGet.Core/NuGet.Protocol/Utility/GetDownloadResultUtility.cs b/src/NuGet.Core/NuGet.Protocol/Utility/GetDownloadResultUtility.cs index 9ae3c841fc9..166c2525a7b 100644 --- a/src/NuGet.Core/NuGet.Protocol/Utility/GetDownloadResultUtility.cs +++ b/src/NuGet.Core/NuGet.Protocol/Utility/GetDownloadResultUtility.cs @@ -202,7 +202,7 @@ private static async Task DirectDownloadAsync( FileAccess.ReadWrite, FileShare.Read, BufferSize, - FileOptions.Asynchronous | FileOptions.DeleteOnClose); + FileOptions.DeleteOnClose); await packageStream.CopyToAsync(fileStream, BufferSize, token); @@ -218,4 +218,4 @@ private static async Task DirectDownloadAsync( } } } -} +} \ No newline at end of file