Microsoft.Iot.IotCoreAppDeployment.WebbHelper.PollInstallStateAsync C# (CSharp) Method

PollInstallStateAsync() public method

public PollInstallStateAsync ( string target, UserInfo credentials ) : Task
target string
credentials UserInfo
return Task
        public async Task<bool> PollInstallStateAsync(string target, UserInfo credentials)
        {
            IPAddress ipAddress = IPAddress.Parse(target);
            RestHelper restHelper = new RestHelper(ipAddress, credentials);

            var URL = AppxApiUrl + "state";

            var result = HttpStatusCode.BadRequest;

            CancellationToken? cts;

            while (result != HttpStatusCode.NotFound && result != HttpStatusCode.OK)
            {
                EnterWebBCall(out cts);

                try
                {
                    using (var response = await restHelper.SendRequestAsync(URL, HttpMethod.Get, string.Empty, cts))
                    {
                        result = response.StatusCode;
                        if (response.StatusCode == HttpStatusCode.NoContent)
                        {
                            await Task.Delay(QueryInterval);
                        }
                        else
                        {
                            var state = RestHelper.ProcessJsonResponse(response, typeof(DeploymentState)) as DeploymentState;

                            if (state != null)
                            {
                                if (state.IsSuccess)
                                {
                                    return true;
                                }
                                else
                                {
                                    // This throws a COMException
                                    Marshal.ThrowExceptionForHR(state.HResult);
                                }
                            }
                        }
                    }
                }
                catch (COMException ex)
                {
                    System.Console.WriteLine("Error: app did not deploy: {0}", ex.Message);
                    Debug.WriteLine(ex.Message);
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.Message);
                }
            }

            return false;
        }

Usage Example

        private HttpStatusCode DeployAppx(string outputAppx, string outputCer, ReadOnlyCollection<FileStreamInfo> dependencies, string dependencyFolder, string identityName)
        {
            OutputMessage(string.Format(CultureInfo.InvariantCulture, Resource.DeploymentWorker_StartDeploy, targetName));

            // Create list of all APPX and CER files for deployment
            var files = new List<FileInfo>()
                    {
                        new FileInfo(outputAppx),
                        new FileInfo(outputCer),
                    };
            foreach (var dependency in dependencies)
            {
                files.Add(new FileInfo(dependencyFolder + @"\" + dependency.AppxRelativePath));
            }

            // Call WEBB Rest APIs to deploy
            var packageFullName = string.Format(CultureInfo.InvariantCulture, packageFullNameFormat, identityName, targetType.ToString());
            using (var webbHelper = new WebbHelper())
            {
                OutputMessage(Resource.DeploymentWorker_DeployAppx_starting_to_deploy_certificate_APPX_and_dependencies);
                // Attempt to uninstall existing package if found
                var uninstallTask = webbHelper.UninstallAppAsync(packageFullName, targetName, credentials);
                if (uninstallTask.Result == HttpStatusCode.OK)
                {
                    // result == OK means the package was uninstalled.
                    OutputMessage(string.Format(CultureInfo.InvariantCulture, Resource.DeploymentWorker_PreviousDeployUninstalled, packageFullName));
                }
                else if (uninstallTask.Result == HttpStatusCode.Unauthorized)
                {
                    // result == Unauthorized means the credentials were not accepted.
                    return uninstallTask.Result;
                }
                else
                {
                    // result != OK could mean that the package wasn't already installed
                    //           or it could mean that there was a problem with the uninstall
                    //           request.
                    OutputMessage(string.Format(CultureInfo.InvariantCulture, Resource.DeploymentWorker_PreviousDeployNotUninstalled, packageFullName));
                }
                // Deploy new APPX, cert, and dependency files
                var deployTask = webbHelper.DeployAppAsync(files, targetName, credentials);
                if (deployTask.Result == HttpStatusCode.Accepted)
                {
                    var result = webbHelper.PollInstallStateAsync(targetName, credentials).Result;
                    OutputMessage(string.Format(CultureInfo.InvariantCulture, Resource.DeploymentWorker_DeployFinished, packageFullName));

                    OutputMessage("\r\n\r\n***");
                    OutputMessage(string.Format(CultureInfo.InvariantCulture, "*** PackageFullName = {0}", packageFullName));
                    OutputMessage("***\r\n\r\n");

                    if (!result)
                    {
                        return HttpStatusCode.BadRequest;
                    }
                }
                else
                {
                    OutputMessage(string.Format(CultureInfo.InvariantCulture, Resource.DeploymentWorker_DeployFailed, packageFullName));
                }
                return deployTask.Result;
            }
        }
All Usage Examples Of Microsoft.Iot.IotCoreAppDeployment.WebbHelper::PollInstallStateAsync