I believe there is a bug in the way Nuget is handling network timeouts when retrieving packages over the network. I have a patchy network connection and whenever Nuget timesout, subsequent calls to Nuget result in corrupted packages in the local cache.
I believe I have isolated the issue to src/Core/Packages/DataServicePackage.cs and have attached a patch. However I am unable to get the development environment working so cannot test.
Log when Local timeout occurs:
```
Nuget path is D:\nuget.exe
GET http://build-nuget.test.xxx.com/api/v2/package/Custom.Package/3.56.162
System.Net.WebException: The operation has timed out.
at System.Net.ConnectStream.Read(Byte[] buffer, Int32 offset, Int32 size)
at NuGet.HttpClient.DownloadData(Stream targetStream)
at NuGet.PackageDownloader.DownloadPackage(IHttpClient downloadClient, IPackageMetadata package, Stream targetStream)
at NuGet.PackageDownloader.DownloadPackage(Uri uri, IPackageMetadata package, Stream targetStream)
at NuGet.DataServicePackage.EnsurePackage(IPackageCacheRepository cacheRepository)
at NuGet.DataServicePackage.get_FrameworkAssemblies()
at NuGet.PackageExtensions.HasProjectContent(IPackage package)
at NuGet.PackageWalker.GetPackageTarget(IPackage package)
at NuGet.PackageWalker.GetPackageInfo(IPackage package)
at NuGet.PackageWalker.ProcessPackageTarget(IPackage package)
at NuGet.PackageWalker.Walk(IPackage package)
at NuGet.InstallWalker.ResolveOperations(IPackage package)
at NuGet.PackageManager.Execute(IPackage package, IPackageOperationResolver resolver)
at NuGet.PackageManager.InstallPackage(IPackage package, FrameworkName targetFramework, Boolean ignoreDependencies, Boolean allowPrereleaseVersions)
at NuGet.PackageManager.InstallPackage(IPackage package, Boolean ignoreDependencies, Boolean allowPrereleaseVersions)
at NuGet.PackageManager.InstallPackage(String packageId, SemanticVersion version, Boolean ignoreDependencies, Boolean allowPrereleaseVersions)
at NuGet.Commands.InstallCommand.InstallPackage(IFileSystem fileSystem, String packageId, SemanticVersion version)
at NuGet.Commands.InstallCommand.ExecuteCommand()
at NuGet.Commands.Command.Execute()
at NuGet.Program.Main(String[] args)
```
Log on subsequent requests to Nuget
```
Nuget path is D:\nuget.exe
GET http://build-nuget.test.xxx.com/api/v2/package/Custom.Package/3.56.162
System.IO.InvalidDataException: Unable to read package from path 'Custom.Package.3.56.162.nupkg'. ---> System.IO.FileFormatException: File contains corrupted data.
at MS.Internal.IO.Zip.ZipIOEndOfCentralDirectoryBlock.FindPosition(Stream archiveStream)
at MS.Internal.IO.Zip.ZipIOEndOfCentralDirectoryBlock.SeekableLoad(ZipIOBlockManager blockManager)
at MS.Internal.IO.Zip.ZipArchive..ctor(Stream archiveStream, FileMode mode, FileAccess access, Boolean streaming, Boolean ownStream)
at MS.Internal.IO.Zip.ZipArchive.OpenOnStream(Stream stream, FileMode mode, FileAccess access, Boolean streaming)
at System.IO.Packaging.ZipPackage..ctor(Stream s, FileMode mode, FileAccess access, Boolean streaming)
at System.IO.Packaging.Package.Open(Stream stream, FileMode packageMode, FileAccess packageAccess, Boolean streaming)
at NuGet.OptimizedZipPackage.EnsureManifest()
at NuGet.OptimizedZipPackage..ctor(IFileSystem fileSystem, String packagePath)
at NuGet.LocalPackageRepository.OpenPackage(String path)
--- End of inner exception stack trace ---
at NuGet.LocalPackageRepository.OpenPackage(String path)
at NuGet.LocalPackageRepository.GetPackage(Func`2 openPackage, String path)
at NuGet.LocalPackageRepository.<>c__DisplayClass10.<FindPackagesById>b__c(String path)
at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at NuGet.PackageRepositoryExtensions.FindPackagesById(IPackageRepository repository, String packageId)
at NuGet.AggregateRepository.<>c__DisplayClass35.<>c__DisplayClass37.<FindPackagesById>b__34(Object state)
at System.Threading.Tasks.Task`1.InvokeFuture(Object futureAsObj)
at System.Threading.Tasks.Task.InnerInvoke()
at System.Threading.Tasks.Task.Execute()
```
I believe I have isolated the issue to src/Core/Packages/DataServicePackage.cs and have attached a patch. However I am unable to get the development environment working so cannot test.
Log when Local timeout occurs:
```
Nuget path is D:\nuget.exe
GET http://build-nuget.test.xxx.com/api/v2/package/Custom.Package/3.56.162
System.Net.WebException: The operation has timed out.
at System.Net.ConnectStream.Read(Byte[] buffer, Int32 offset, Int32 size)
at NuGet.HttpClient.DownloadData(Stream targetStream)
at NuGet.PackageDownloader.DownloadPackage(IHttpClient downloadClient, IPackageMetadata package, Stream targetStream)
at NuGet.PackageDownloader.DownloadPackage(Uri uri, IPackageMetadata package, Stream targetStream)
at NuGet.DataServicePackage.EnsurePackage(IPackageCacheRepository cacheRepository)
at NuGet.DataServicePackage.get_FrameworkAssemblies()
at NuGet.PackageExtensions.HasProjectContent(IPackage package)
at NuGet.PackageWalker.GetPackageTarget(IPackage package)
at NuGet.PackageWalker.GetPackageInfo(IPackage package)
at NuGet.PackageWalker.ProcessPackageTarget(IPackage package)
at NuGet.PackageWalker.Walk(IPackage package)
at NuGet.InstallWalker.ResolveOperations(IPackage package)
at NuGet.PackageManager.Execute(IPackage package, IPackageOperationResolver resolver)
at NuGet.PackageManager.InstallPackage(IPackage package, FrameworkName targetFramework, Boolean ignoreDependencies, Boolean allowPrereleaseVersions)
at NuGet.PackageManager.InstallPackage(IPackage package, Boolean ignoreDependencies, Boolean allowPrereleaseVersions)
at NuGet.PackageManager.InstallPackage(String packageId, SemanticVersion version, Boolean ignoreDependencies, Boolean allowPrereleaseVersions)
at NuGet.Commands.InstallCommand.InstallPackage(IFileSystem fileSystem, String packageId, SemanticVersion version)
at NuGet.Commands.InstallCommand.ExecuteCommand()
at NuGet.Commands.Command.Execute()
at NuGet.Program.Main(String[] args)
```
Log on subsequent requests to Nuget
```
Nuget path is D:\nuget.exe
GET http://build-nuget.test.xxx.com/api/v2/package/Custom.Package/3.56.162
System.IO.InvalidDataException: Unable to read package from path 'Custom.Package.3.56.162.nupkg'. ---> System.IO.FileFormatException: File contains corrupted data.
at MS.Internal.IO.Zip.ZipIOEndOfCentralDirectoryBlock.FindPosition(Stream archiveStream)
at MS.Internal.IO.Zip.ZipIOEndOfCentralDirectoryBlock.SeekableLoad(ZipIOBlockManager blockManager)
at MS.Internal.IO.Zip.ZipArchive..ctor(Stream archiveStream, FileMode mode, FileAccess access, Boolean streaming, Boolean ownStream)
at MS.Internal.IO.Zip.ZipArchive.OpenOnStream(Stream stream, FileMode mode, FileAccess access, Boolean streaming)
at System.IO.Packaging.ZipPackage..ctor(Stream s, FileMode mode, FileAccess access, Boolean streaming)
at System.IO.Packaging.Package.Open(Stream stream, FileMode packageMode, FileAccess packageAccess, Boolean streaming)
at NuGet.OptimizedZipPackage.EnsureManifest()
at NuGet.OptimizedZipPackage..ctor(IFileSystem fileSystem, String packagePath)
at NuGet.LocalPackageRepository.OpenPackage(String path)
--- End of inner exception stack trace ---
at NuGet.LocalPackageRepository.OpenPackage(String path)
at NuGet.LocalPackageRepository.GetPackage(Func`2 openPackage, String path)
at NuGet.LocalPackageRepository.<>c__DisplayClass10.<FindPackagesById>b__c(String path)
at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at NuGet.PackageRepositoryExtensions.FindPackagesById(IPackageRepository repository, String packageId)
at NuGet.AggregateRepository.<>c__DisplayClass35.<>c__DisplayClass37.<FindPackagesById>b__34(Object state)
at System.Threading.Tasks.Task`1.InvokeFuture(Object futureAsObj)
at System.Threading.Tasks.Task.InnerInvoke()
at System.Threading.Tasks.Task.Execute()
```