0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

ファイル送信 リトライ付き

Posted at
        private static bool RetryInProgress { get; set; } = false;

        public static async Task UploadFileWithRetryAsync()
        {
            Console.WriteLine($"Start UploadFileWithRetryAsync");

            if (RetryInProgress)
            {
                Console.WriteLine($"Retrying");

                return;
            }

            RetryInProgress = true;

            for (int i = 0; i < 7; i++)
            {
                if (i != 0)
                {
                    int waitTimeInSeconds = (int)Math.Pow(2, i - 1);

                    Console.WriteLine($"Waiting for {waitTimeInSeconds} seconds...");

                    await Task.Delay(TimeSpan.FromSeconds(waitTimeInSeconds));
                }

                try
                {
                    Console.WriteLine($"Trying {i + 1}");

                    using (var stream = new FileStream(LocalFilePath, FileMode.Open))
                    {
                        await UploadFileAsync(stream, RemoteFileName);

                        break;
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"Error: {ex.Message}");
                }
            }

            RetryInProgress = false;

            Console.WriteLine($"End UploadFileWithRetryAsync");
        }
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?