public async Task RemovePackageAsync(string packageId, string packageVersion)
        {
            if (packageId == null)
            {
                throw new ArgumentNullException("packageId");
            }
            if (packageVersion == null)
            {
                throw new ArgumentNullException("packageVersion");
            }

            await BlogFilesContainer.GetBlockBlobReference(GetPackageKey(packageId, packageVersion)).DeleteAsync();

            var cachedPackage = Cache[packageId] as IWebPackage;

            if (cachedPackage != null && cachedPackage.Version == SemanticVersion.Parse(packageVersion))
            {
                Cache.Remove(packageId);

                // Load the latest package to Cache
                var packages = BlogFilesContainer.ListBlobs().OfType <CloudBlockBlob>().Select(SafeReadZipPackage).Where(package => package != null).Where(p => p.Id == packageId);
                if (packages.Any())
                {
                    var latestPackage = packages.MaxBy(p => p.Version);
                    if (latestPackage != null)
                    {
                        AddToCache(latestPackage);
                    }
                }
            }
        }
        public IWebPackage GetPackage(string id, string version)
        {
            if (Cache[id] != null && ((IWebPackage)Cache[id]).Version == new SemanticVersion(version))
            {
                return(Cache[id] as IWebPackage);
            }

            return(SafeReadZipPackage(BlogFilesContainer.GetBlockBlobReference(GetPackageKey(id, version))));
        }
        public async Task AddPackageAsync(IPackage package)
        {
            if (package == null)
            {
                throw new ArgumentNullException("package");
            }

            if (package is ZipPackage)
            {
                (package as ZipPackage).Published = DateTimeOffset.Now;
            }

            var blockBlobReference = BlogFilesContainer.GetBlockBlobReference(GetPackageKey(package));
            await blockBlobReference.UploadFromStreamAsync(package.GetStream());

            AddToCache(new WebZipPackage(package.GetStream(), blockBlobReference.Uri));
        }
        public AzureBlobPackageRepository(string containerName, string connectionString)
        {
            _packagesBlob     = containerName;
            _connectionString = connectionString;

            _blogFilesContainer = new Lazy <CloudBlobContainer>(
                () =>
            {
                var container = CloudStorageAccount.Parse(_connectionString)
                                .CreateCloudBlobClient()
                                .GetContainerReference(_packagesBlob);

                if (container.CreateIfNotExists())
                {
                    var permissions          = container.GetPermissions();
                    permissions.PublicAccess = BlobContainerPublicAccessType.Container;

                    container.SetPermissions(permissions);
                }

                return(container);
            });


            _lazyMemoryCache = new Lazy <MemoryCache>(
                () =>
            {
                var cache    = MemoryCache.Default;
                var packages = BlogFilesContainer.ListBlobs().OfType <CloudBlockBlob>().Select(SafeReadZipPackage).Where(package => package != null);

                foreach (var package in packages)
                {
                    AddToCache(package, cache);
                }

                return(cache);
            });
        }