public static MetaDataService.DeployResult WaitDeployResult(string id, SalesforceContext ctx, CancellationToken cancellationToken)
        {
            Task <MetaDataService.DeployResult> task = Task <MetaDataService.DeployResult> .Factory.StartNew(() => ctx.PollingResult.PollForResultWrapper(3, 180, 3, () =>
                                                                                                                                                          ctx.CheckDeployResult(id), res => res != null, cancellationToken), cancellationToken);

            ctx.WaitAllTaskskWithHandlingAggregateException(new[] { task }, cancellationToken);

            return(task.Result);
        }
        public static List <SalesforceFileProxy> DownloadAllFilesSynchronously(PackageEntity package, SalesforceContext ctx, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            if (!ctx.IsLoggedIn)
            {
                throw new InvalidOperationException("Service should be logged into Salesforce");
            }

            List <SalesforceFileProxy> listFileProxies = new List <SalesforceFileProxy>();

            RetrieveRequest[] retrieveRequestArray = ConvertPackageToRequests(package);

            List <AsyncResult> listResults = new List <AsyncResult>();

            foreach (var retrieveRequest in retrieveRequestArray)
            {
                RetrieveRequest request = retrieveRequest;
                cancellationToken.ThrowIfCancellationRequested();
                AsyncResult asyncResult = ctx.SessionExpirationWrapper(() => ctx.MetadataServiceAdapter.Retrieve(request));
                listResults.Add(asyncResult);
            }

            cancellationToken.ThrowIfCancellationRequested();
            ICollection <Task <RetrieveResult> > collection = new Collection <Task <RetrieveResult> >();

            foreach (var asyncResult in listResults)
            {
                cancellationToken.ThrowIfCancellationRequested();
                Task <RetrieveResult> task = Task <RetrieveResult> .Factory.StartNew(() => ctx.PollingResult.PollForResultWrapper(3, 180, 3, () =>
                                                                                                                                  ctx.CheckRetrieveResult(asyncResult.id), res => res != null, cancellationToken), cancellationToken);

                collection.Add(task);
            }

            ctx.WaitAllTaskskWithHandlingAggregateException(collection.ToArray(), cancellationToken);
            foreach (Task <RetrieveResult> task in collection)
            {
                cancellationToken.ThrowIfCancellationRequested();
                RetrieveResult result = task.Result;

                string fileName = String.Format("{0}\\{1}.zip", ctx.OutputLocation, Guid.NewGuid().ToString());

                bool rawZip = Boolean.Parse(ConfigurationManager.AppSettings["salesforcemigrations:dumprawzip"]);

                if (rawZip)
                {
                    var rawZipFile = new SalesforceFileProxy
                    {
                        FullName   = result.id,
                        FileName   = String.Format("unpackaged/zips/{0}.zip", result.id),
                        Type       = "zip",
                        BinaryBody = result.zipFile
                    };

                    listFileProxies.Add(rawZipFile);
                }

                List <SalesforceFileProxy> files = UnzipPackageFilesHelper.UnzipPackageFilesRecursive(result.zipFile);

                UpdateFileProperties(files, result.fileProperties);
                listFileProxies.AddRange(files);
            }
            return(listFileProxies);
        }