private async Task <ApplicationTypeContext> GetApplicationTypeContextAsync(TimeoutHelper timeoutHelper)
        {
            StoreLayoutSpecification storeLayoutSpecification = StoreLayoutSpecification.Create();

            // Read the the ApplicationManifest from the store
            string applicationManifestFile = storeLayoutSpecification.GetApplicationManifestFile(this.ApplicationTypeName, this.ApplicationTypeVersion);
            ApplicationManifestType applicationManifestType = await this.ImageStoreWrapper.GetFromStoreAsync <ApplicationManifestType>(applicationManifestFile, timeoutHelper.GetRemainingTime());

            ApplicationTypeContext applicationTypeContext = new ApplicationTypeContext(applicationManifestType, string.Empty);

            // Read the Collection of ServiceManifests associated with the ApplicationManifest from the store
            List <Task <ServiceManifest> > taskList = new List <Task <ServiceManifest> >();
            TimeSpan remainingTime = timeoutHelper.GetRemainingTime();

            foreach (ApplicationManifestTypeServiceManifestImport serviceManifestImport in applicationManifestType.ServiceManifestImport)
            {
                taskList.Add(this.GetServiceManifestAsync(serviceManifestImport, storeLayoutSpecification, remainingTime));
            }

            await Task.WhenAll(taskList);

            taskList.ForEach(task => applicationTypeContext.ServiceManifests.Add(task.Result));

            return(applicationTypeContext);
        }
        private void GetImageStoreContentByAppVersionImpl(
            List <ImageStoreFile> sourceFiles,
            List <ImageStoreFolder> sourceFolders,
            List <ImageStoreFile> targetFiles,
            List <ImageStoreFolder> targetFolders,
            string version,
            IImageStore imageStore,
            StoreLayoutSpecification storeLayoutSpecification,
            IClusterConnection clusterConnection,
            TimeoutHelper helper)
        {
            Action <string, string, string, string, bool> updateStoreInfo = (string path, string serviceName, string serviceVersion, string appVersion, bool isFile) =>
            {
                if (isFile)
                {
                    var existingFile = (from file in targetFiles
                                        where string.Compare(file.StoreRelativePath, path, StringComparison.OrdinalIgnoreCase) == 0
                                        select file).FirstOrDefault();

                    if (existingFile != null)
                    {
                        existingFile.VersionInfo.ConfigVersionInfo(serviceName, serviceVersion, appVersion);
                    }
                    else
                    {
                        existingFile = (from file in sourceFiles
                                        where string.Compare(file.StoreRelativePath, path, StringComparison.OrdinalIgnoreCase) == 0
                                        select file).FirstOrDefault();

                        if (existingFile != null)
                        {
                            existingFile.VersionInfo.ConfigVersionInfo(serviceName, serviceVersion, appVersion);
                            targetFiles.Add(existingFile);
                        }
                        else
                        {
                            throw new ArgumentException(StringResources.Error_ImageStoreRelativePathNotExist);
                        }
                    }
                }
                else
                {
                    var existingFolder = (from folder in targetFolders
                                          where string.Compare(folder.StoreRelativePath, path, StringComparison.OrdinalIgnoreCase) == 0
                                          select folder).FirstOrDefault();

                    if (existingFolder != null)
                    {
                        existingFolder.VersionInfo.ConfigVersionInfo(serviceName, serviceVersion, appVersion);
                    }
                    else
                    {
                        existingFolder = (from folder in sourceFolders
                                          where string.Compare(folder.StoreRelativePath, path, StringComparison.OrdinalIgnoreCase) == 0
                                          select folder).FirstOrDefault();

                        if (existingFolder != null)
                        {
                            existingFolder.VersionInfo.ConfigVersionInfo(serviceName, serviceVersion, appVersion);
                            targetFolders.Add(existingFolder);
                        }
                        else
                        {
                            throw new ArgumentException(StringResources.Error_ImageStoreRelativePathNotExist);
                        }
                    }
                }
            };

            if (helper != null)
            {
                helper.ThrowIfExpired();
            }

            var    foldersPath             = new Dictionary <string, Tuple <string, string> >();
            string applicationManifestPath = storeLayoutSpecification.GetApplicationManifestFile(this.ApplicationTypeName, version);

            updateStoreInfo(applicationManifestPath, string.Empty, string.Empty, version, true);
            var serviceTypeList = clusterConnection.GetServiceTypeListAsync(this.ApplicationTypeName, version, null, this.GetTimeout(), this.GetCancellationToken()).Result;

            foreach (var serviceType in serviceTypeList)
            {
                if (helper != null)
                {
                    helper.ThrowIfExpired();
                }

                string serviceManifestPath = storeLayoutSpecification.GetServiceManifestFile(this.ApplicationTypeName, serviceType.ServiceManifestName, serviceType.ServiceManifestVersion);
                updateStoreInfo(serviceManifestPath, serviceType.ServiceManifestName, serviceType.ServiceManifestVersion, version, true);
                if (helper != null)
                {
                    helper.ThrowIfExpired();
                }

                string serviceManifestChecksumPath = storeLayoutSpecification.GetServiceManifestChecksumFile(this.ApplicationTypeName, serviceType.ServiceManifestName, serviceType.ServiceManifestVersion);
                updateStoreInfo(serviceManifestChecksumPath, serviceType.ServiceManifestName, serviceType.ServiceManifestVersion, version, true);
                string localPath = System.IO.Path.Combine(System.IO.Path.GetTempPath(), serviceManifestPath.Substring(serviceManifestPath.LastIndexOf(@"\") + 1));
                try
                {
                    if (helper != null)
                    {
                        helper.ThrowIfExpired();
                        imageStore.DownloadContent(serviceManifestPath, localPath, helper.GetRemainingTime(), CopyFlag.AtomicCopy);
                    }
                    else
                    {
                        imageStore.DownloadContent(serviceManifestPath, localPath, this.GetTimeout(), CopyFlag.AtomicCopy);
                    }

                    foreach (var packagePath in this.GetPackagesPath(localPath, storeLayoutSpecification))
                    {
                        updateStoreInfo(packagePath, serviceType.ServiceManifestName, serviceType.ServiceManifestVersion, version, false);
                        updateStoreInfo(string.Format("{0}.{1}", packagePath, ChecksumFileExtension), serviceType.ServiceManifestName, serviceType.ServiceManifestVersion, version, true);
                    }
                }
                catch (Exception exception)
                {
                    this.ThrowTerminatingError(exception, Constants.GetImageStoreContentErrorId, null);
                }
                finally
                {
                    File.Delete(localPath);
                }
            }
        }