internal void EnsurePackage(IPackageCacheRepository cacheRepository) { // OData caches instances of DataServicePackage while updating their property values. As a result, // the ZipPackage that we downloaded may no longer be valid (as indicated by a newer hash). // When using MachineCache, once we've verified that the hashes match (which happens the first time around), // we'll simply verify the file exists between successive calls. IPackageMetadata packageMetadata = this; bool refreshPackage = _package == null || (_package is OptimizedZipPackage && !((OptimizedZipPackage)_package).IsValid) || !String.Equals(OldHash, PackageHash, StringComparison.OrdinalIgnoreCase) || (_usingMachineCache && !cacheRepository.Exists(Id, packageMetadata.Version)); if (refreshPackage && TryGetPackage(cacheRepository, packageMetadata, out _package) && _package.GetHash(HashProvider).Equals(PackageHash, StringComparison.OrdinalIgnoreCase)) { OldHash = PackageHash; // Reset the flag so that we no longer need to download the package since it exists and is valid. refreshPackage = false; // Make a note that the backing store for the ZipPackage is the machine cache. _usingMachineCache = true; } if (refreshPackage) { using (Stream targetStream = cacheRepository.CreatePackageStream(packageMetadata.Id, packageMetadata.Version)) { // We either do not have a package available locally or they are invalid. Download the package from the server. Downloader.DownloadPackage(DownloadUrl, this, targetStream); } _package = cacheRepository.FindPackage(packageMetadata.Id, packageMetadata.Version); // Make a note that we are using an in-memory instance of the package. _usingMachineCache = false; OldHash = PackageHash; } }
internal void EnsurePackage(IPackageCacheRepository cacheRepository) { // OData caches instances of DataServicePackage while updating their property values. As a result, // the ZipPackage that we downloaded may no longer be valid (as indicated by a newer hash). // When using MachineCache, once we've verified that the hashes match (which happens the first time around), // we'll simply verify the file exists between successive calls. IPackageMetadata packageMetadata = this; bool refreshPackage = _package == null || (_package is OptimizedZipPackage && !((OptimizedZipPackage)_package).IsValid) || !String.Equals(OldHash, PackageHash, StringComparison.OrdinalIgnoreCase) || (_usingMachineCache && !cacheRepository.Exists(Id, packageMetadata.Version)); if (refreshPackage && TryGetPackage(cacheRepository, packageMetadata, out _package) && _package.GetHash(HashProvider).Equals(PackageHash, StringComparison.OrdinalIgnoreCase)) { OldHash = PackageHash; // Reset the flag so that we no longer need to download the package since it exists and is valid. refreshPackage = false; // Make a note that the backing store for the ZipPackage is the machine cache. _usingMachineCache = true; } if (refreshPackage) { // We either do not have a package available locally or they are invalid. Download the package from the server. Stream targetStream = null; try { targetStream = cacheRepository.CreatePackageStream(packageMetadata.Id, packageMetadata.Version); // this can happen when access to the %LocalAppData% directory is blocked, e.g. on Windows Azure Web Site build if (targetStream != null) { _usingMachineCache = true; } else { // if we can't store the package into machine cache, store it in memory targetStream = new MemoryStream(); _usingMachineCache = false; } // download package into the stream Downloader.DownloadPackage(DownloadUrl, this, targetStream); } finally { if (targetStream != null && _usingMachineCache) { targetStream.Dispose(); } } if (_usingMachineCache) { _package = cacheRepository.FindPackage(packageMetadata.Id, packageMetadata.Version); Debug.Assert(_package != null); } else { targetStream.Seek(0, SeekOrigin.Begin); _package = new ZipPackage(targetStream); targetStream.Dispose(); } OldHash = PackageHash; } }