Exemplo n.º 1
0
        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.
                _usingMachineCache = cacheRepository.InvokeOnPackage(packageMetadata.Id, packageMetadata.Version,
                                                                     (stream) => Downloader.DownloadPackage(DownloadUrl, this, stream)
                                                                     );

                if (_usingMachineCache)
                {
                    _package = cacheRepository.FindPackage(packageMetadata.Id, packageMetadata.Version);
                    Debug.Assert(_package != null);
                }
                else
                {
                    // this can happen when access to the %LocalAppData% directory is blocked, e.g. on Windows Azure Web Site build
                    using (var targetStream = new MemoryStream())
                    {
                        Downloader.DownloadPackage(DownloadUrl, this, targetStream);
                        targetStream.Seek(0, SeekOrigin.Begin);
                        _package = new ZipPackage(targetStream);
                    }
                }

                OldHash = PackageHash;
            }
        }
Exemplo n.º 2
0
        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;
            }
        }
Exemplo n.º 3
0
        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;
            if (_package == null ||
                    (_package is OptimizedZipPackage && !((OptimizedZipPackage)_package).IsValid) ||
                    !String.Equals(OldHash, PackageHash, StringComparison.OrdinalIgnoreCase) ||
                    (_usingMachineCache && !cacheRepository.Exists(Id, packageMetadata.Version)))
            {
                IPackage newPackage = null;
                bool inMemOnly = false;
                bool isValid = false;

                // If the package exists in the cache and has the correct hash then use it. Otherwise download it.
                if (TryGetPackage(cacheRepository, packageMetadata, out newPackage) && MatchPackageHash(newPackage))
                {
                    isValid = true;
                }
                else
                {
                    // We either do not have a package available locally or they are invalid. Download the package from the server.
                    if (cacheRepository.InvokeOnPackage(packageMetadata.Id, packageMetadata.Version,
                        (stream) => Downloader.DownloadPackage(DownloadUrl, this, stream)))
                    {
                        newPackage = cacheRepository.FindPackage(packageMetadata.Id, packageMetadata.Version);
                        Debug.Assert(newPackage != null);
                    }
                    else
                    {
                        // this can happen when access to the %LocalAppData% directory is blocked, e.g. on Windows Azure Web Site build
                        using (var targetStream = new MemoryStream())
                        {
                            Downloader.DownloadPackage(DownloadUrl, this, targetStream);
                            targetStream.Seek(0, SeekOrigin.Begin);
                            newPackage = new ZipPackage(targetStream);
                        }

                        inMemOnly = true;
                    }

                    // Because of CDN caching, the hash returned in odata feed
                    // can be out of sync with the hash of the file itself.
                    // So for now, we cannot call MatchPackageHash(newPackage) to 
                    // validate that the file downloaded has the right hash.
                    isValid = true;
                }

                // apply the changes if the package hash was valid
                if (isValid)
                {
                    _package = newPackage;

                    // Make a note that the backing store for the ZipPackage is the machine cache.
                    _usingMachineCache = !inMemOnly;

                    OldHash = PackageHash;
                }
                else
                {
                    // ensure package must end with a valid package, since we cannot load one we must throw.
                    throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture,
                        NuGetResources.Error_InvalidPackage, Version, Id));
                }
            }
        }
Exemplo n.º 4
0
        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;
            if (_package == null ||
                    (_package is OptimizedZipPackage && !((OptimizedZipPackage)_package).IsValid) ||
                    !String.Equals(OldHash, PackageHash, StringComparison.OrdinalIgnoreCase) ||
                    (_usingMachineCache && !cacheRepository.Exists(Id, packageMetadata.Version)))
            {
                IPackage newPackage = null;
                bool inMemOnly = false;
                bool isValid = false;

                // If the package exists in the cache and has the correct hash then use it. Otherwise download it.
                if (TryGetPackage(cacheRepository, packageMetadata, out newPackage) && MatchPackageHash(newPackage))
                {
                    isValid = true;
                }
                else
                {
                    // We either do not have a package available locally or they are invalid. Download the package from the server.
                    if (cacheRepository.InvokeOnPackage(packageMetadata.Id, packageMetadata.Version,
                        (stream) => Downloader.DownloadPackage(DownloadUrl, this, stream)))
                    {
                        newPackage = cacheRepository.FindPackage(packageMetadata.Id, packageMetadata.Version);
                        Debug.Assert(newPackage != null);
                    }
                    else
                    {
                        // this can happen when access to the %LocalAppData% directory is blocked, e.g. on Windows Azure Web Site build
                        using (var targetStream = new MemoryStream())
                        {
                            Downloader.DownloadPackage(DownloadUrl, this, targetStream);
                            targetStream.Seek(0, SeekOrigin.Begin);
                            newPackage = new ZipPackage(targetStream);
                        }

                        inMemOnly = true;
                    }

                    // Because of CDN caching, the hash returned in odata feed
                    // can be out of sync with the hash of the file itself.
                    // So for now, we cannot call MatchPackageHash(newPackage) to
                    // validate that the file downloaded has the right hash.
                    isValid = true;
                }

                // apply the changes if the package hash was valid
                if (isValid)
                {
                    _package = newPackage;

                    // Make a note that the backing store for the ZipPackage is the machine cache.
                    _usingMachineCache = !inMemOnly;

                    OldHash = PackageHash;
                }
                else
                {
                    // ensure package must end with a valid package, since we cannot load one we must throw.
                    throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture,
                        NuGetResources.Error_InvalidPackage, Version, Id));
                }
            }
        }
Exemplo n.º 5
0
        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;
            }
        }
Exemplo n.º 6
0
        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;
            }
        }
Exemplo n.º 7
0
        internal void EnsurePackage(IPackageCacheRepository cacheRepository)
        {
            IPackageMetadata packageMetadata = this;

            if ((((this._package == null) || ((this._package is OptimizedZipPackage) && !((OptimizedZipPackage)this._package).IsValid)) || !string.Equals(this.OldHash, this.PackageHash, StringComparison.OrdinalIgnoreCase)) || (this._usingMachineCache && !cacheRepository.Exists(this.Id, packageMetadata.Version)))
            {
                IPackage package = null;
                bool     flag    = false;
                bool     flag2   = false;
                if (TryGetPackage(cacheRepository, packageMetadata, out package) && this.MatchPackageHash(package))
                {
                    flag2 = true;
                }
                else
                {
                    if (cacheRepository.InvokeOnPackage(packageMetadata.Id, packageMetadata.Version, stream => this.Downloader.DownloadPackage(this.DownloadUrl, this, stream)))
                    {
                        package = cacheRepository.FindPackage(packageMetadata.Id, packageMetadata.Version);
                    }
                    else
                    {
                        using (MemoryStream stream = new MemoryStream())
                        {
                            this.Downloader.DownloadPackage(this.DownloadUrl, this, stream);
                            stream.Seek(0L, SeekOrigin.Begin);
                            package = new ZipPackage(stream);
                        }
                        flag = true;
                    }
                    flag2 = true;
                }
                if (!flag2)
                {
                    object[] args = new object[] { this.Version, this.Id };
                    throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, NuGetResources.Error_InvalidPackage, args));
                }
                this._package           = package;
                this.Id                 = this._package.Id;
                this.Version            = this._package.Version.ToString();
                this._usingMachineCache = !flag;
                this.OldHash            = this.PackageHash;
            }
        }