/// <summary> /// Processes an Async Status until it is completed or runs into an error. /// </summary> /// <param name="status">The async status to process.</param> /// <param name="remainingTimeCallback">A callback function in which the estimated remaining time in seconds is sent.</param> /// <returns></returns> private static async Task <AsyncStatus> ProcessAsyncStatus(AsyncStatus status, Action <int> remainingTimeCallback) { var statusUrl = new Uri(status.CallbackUrl); if (status.CallbackInSeconds > 0 || !status.IsCompleted || string.IsNullOrEmpty(status.ResultUrl)) { remainingTimeCallback?.Invoke(status.CallbackInSeconds); //Wait remaining seconds. await Task.Delay(TimeSpan.FromSeconds(status.CallbackInSeconds)).ConfigureAwait(false); status = await ServiceHelper.MonitorAsyncStatus(statusUrl, 0, remainingTimeCallback).ConfigureAwait(false); } if (status != null) { if (status.IsCompleted && !string.IsNullOrEmpty(status.ResultUrl)) { return(status); } else if (!status.IsAccepted) { throw new Exception("The request was not accepted. " + (!string.IsNullOrEmpty(status.ErrorMessage) ? status.ErrorMessage : "")); } else if (!string.IsNullOrEmpty(status.ErrorMessage)) { throw new Exception(status.ErrorMessage); } } return(status); }