Azi.Tools.Retry.Do C# (CSharp) Method

Do() public static method

Does func and retries if it failed
public static Do ( int times, Func act ) : bool
times int Maximum times to retry
act Func Func with action and which returns false if retry required. Throw exception if action fail and can not be retried.
return bool
        public static bool Do(int times, Func<bool> act)
        {
            while (times > 0)
            {
                if (act())
                {
                    return true;
                }

                times--;
            }

            return false;
        }

Same methods

Retry::Do ( int times, TimeSpan>.Func retryDelay, Func act ) : Task
Retry::Do ( int times, TimeSpan>.Func retryDelay, Func act, Func exceptionPocessor ) : Task
Retry::Do ( int times, TimeSpan>.Func retryDelay, Func act ) : bool
Retry::Do ( int times, TimeSpan>.Func retryDelay, Func act, bool>.Func exceptionPocessor ) : bool
Retry::Do ( int times, bool>.Func act ) : bool

Usage Example

Beispiel #1
0
        /// <summary>
        /// Uploads file
        /// </summary>
        /// <typeparam name="R">Result type</typeparam>
        /// <param name="method">HTTP method</param>
        /// <param name="url">URL for request</param>
        /// <param name="file">File upload parameters. Input stream must support Length</param>
        /// <returns>Async result object</returns>
        public async Task <R> SendFile <R>(HttpMethod method, string url, SendFileInfo file)
        {
            R result = default(R);
            await Retry.Do(
                RetryTimes,
                RetryDelay,
                async() =>
            {
                var client = await GetHttpClient(url).ConfigureAwait(false);
                try
                {
                    client.Method = method.ToString();
                    client.AllowWriteStreamBuffering = false;

                    string boundry     = Guid.NewGuid().ToString();
                    client.ContentType = $"multipart/form-data; boundary={boundry}";
                    client.SendChunked = false;
                    client.KeepAlive   = true;
                    client.ServicePoint.Expect100Continue = false;
                    client.ServicePoint.SetTcpKeepAlive(true, 100, 100);

                    using (var input = file.StreamOpener())
                    {
                        var pre              = GetMultipartFormPre(file, input.Length, boundry);
                        var post             = GetMultipartFormPost(boundry);
                        client.ContentLength = pre.Length + input.Length + post.Length;
                        using (var output = await client.GetRequestStreamAsync().ConfigureAwait(false))
                        {
                            if (file.CancellationToken != null)
                            {
                                var token = (CancellationToken)file.CancellationToken;
                                await pre.CopyToAsync(output, 81920).ConfigureAwait(false);

                                // await input.CopyToAsync(output, 81920).ConfigureAwait(false);
                                await CopyToStreamAsync(input, output, 81920, token, file.Progress).ConfigureAwait(false);
                                if (token.IsCancellationRequested)
                                {
                                    client.Abort();
                                    throw new OperationCanceledException(token);
                                }
                                await post.CopyToAsync(output, 81920).ConfigureAwait(false);
                            }
                            else
                            {
                                await pre.CopyToAsync(output).ConfigureAwait(false);
                                await input.CopyToAsync(output).ConfigureAwait(false);
                                await post.CopyToAsync(output).ConfigureAwait(false);
                            }
                        }
                    }
                    using (var response = (HttpWebResponse)await client.GetResponseAsync().ConfigureAwait(false))
                    {
                        if (!response.IsSuccessStatusCode())
                        {
                            return(await LogBadResponse(response).ConfigureAwait(false));
                        }

                        result = await response.ReadAsAsync <R>().ConfigureAwait(false);
                    }
                    return(true);
                }
                catch (Exception)
                {
                    client.Abort();
                    throw;
                }
            },
                GeneralExceptionProcessor).ConfigureAwait(false);

            return(result);
        }