Подтвердить что ты не робот

.Net DownloadFileTaskAsync надежный код WPF

Код WPF ниже висит навсегда, когда сетевое соединение теряется в течение 3 или более минут. Когда соединение восстанавливается, он не бросает и не продолжает загрузку или таймауты. Если сетевое соединение теряется в течение более короткого периода, скажем, полминуты, оно срабатывает после восстановления соединения. Как я могу сделать его более надежным, чтобы выжить в сети?

using System;
using System.Net;
using System.Net.NetworkInformation;
using System.Windows;

namespace WebClientAsync
{

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            NetworkChange.NetworkAvailabilityChanged +=
                (sender, e) => Dispatcher.Invoke(delegate()
                    {
                        this.Title = "Network is " + (e.IsAvailable ? " available" : "down");
                    });
        }

        const string SRC = "http://ovh.net/files/10Mio.dat";
        const string TARGET = @"d:\stuff\10Mio.dat";

        private async void btnDownload_Click(object sender, RoutedEventArgs e)
        {
            btnDownload.IsEnabled = false;
            btnDownload.Content = "Downloading " + SRC;
            try {
                using (var wcl = new WebClient())
                {
                    wcl.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
                    await wcl.DownloadFileTaskAsync(new Uri(SRC), TARGET);
                    btnDownload.Content = "Downloaded";
                }
            }
            catch (Exception ex)
            {
                btnDownload.Content = ex.Message + Environment.NewLine
                    + ((ex.InnerException != null) ? ex.InnerException.Message : String.Empty);
            }
            btnDownload.IsEnabled = true;
        }
    }
}

ОБНОВЛЕНИЕ

Текущее решение основано на перезапуске Timer в DownloadProgressChangedEventHandler, поэтому таймер срабатывает, только если в течение таймаута не происходит событий DownloadProgressChanged. Похож на уродливый взлом, все еще ищущее лучшее решение.

using System;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;

namespace WebClientAsync
{

    public partial class MainWindow : Window
    {

        const string SRC = "http://ovh.net/files/10Mio.dat";
        const string TARGET = @"d:\stuff\10Mio.dat";
        // Time needed to restore network connection
        const int TIMEOUT = 30 * 1000;

        public MainWindow()
        {
            InitializeComponent();
        }

        private async void btnDownload_Click(object sender, RoutedEventArgs e)
        {
            btnDownload.IsEnabled = false;
            btnDownload.Content = "Downloading " + SRC;
            CancellationTokenSource cts = new CancellationTokenSource();
            CancellationToken token = cts.Token;
            Timer timer = new Timer((o) =>
                {
                    // Force async cancellation
                    cts.Cancel();
                }
                , null //state
                , TIMEOUT
                , Timeout.Infinite // once
            );
            DownloadProgressChangedEventHandler handler = (sa, ea) =>
                {
                    // Restart timer
                    if (ea.BytesReceived < ea.TotalBytesToReceive && timer != null)
                    {
                        timer.Change(TIMEOUT, Timeout.Infinite);
                    }

                };
            btnDownload.Content = await DownloadFileTA(token, handler);
            // Note ProgressCallback will fire once again after awaited.
            timer.Dispose();
            btnDownload.IsEnabled = true;
        }

        private async Task<string> DownloadFileTA(CancellationToken token, DownloadProgressChangedEventHandler handler)
        {
            string res = null;
            WebClient wcl = new WebClient();
            wcl.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
            wcl.DownloadProgressChanged += handler;
            try
            {
                using (token.Register(() => wcl.CancelAsync()))
                {
                    await wcl.DownloadFileTaskAsync(new Uri(SRC), TARGET);
                }
                res = "Downloaded";
            }
            catch (Exception ex)
            {
                res = ex.Message + Environment.NewLine
                    + ((ex.InnerException != null) ? ex.InnerException.Message : String.Empty);
            }
            wcl.Dispose();
            return res;
        }
    }
}
4b9b3361

Ответ 1

Вам необходимо реализовать правильный тайм-аут для этой загрузки. Но вам не нужно использовать таймер, просто используйте Task.Delay и Task.WaitAny. Например:

static async Task DownloadFile(string url, string output, TimeSpan timeout) {            
    using (var wcl = new WebClient())
    {
        wcl.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;                                                
        var download = wcl.DownloadFileTaskAsync(url, output);
        // await two tasks - download and delay, whichever completes first
        await Task.WhenAny(Task.Delay(timeout), download);
        var exception = download.Exception; // need to observe exception, if any
        bool cancelled = !download.IsCompleted && exception == null;

        // download is not completed yet, nor it is failed - cancel
        if (cancelled) {
            wcl.CancelAsync();
        }

        if (cancelled || exception != null) {
            // delete partially downloaded file if any (note - need to do with retry, might not work with a first try, because CancelAsync is not immediate)
            int fails = 0;
            while (true) {
                try {
                    File.Delete(output);
                    break;
                }
                catch {
                    fails++;
                    if (fails >= 10)
                        break;

                    await Task.Delay(1000);
                }
            }
        }
        if (exception != null) {
            throw new Exception("Failed to download file", exception);
        }
        if (cancelled) {
            throw new Exception($"Failed to download file (timeout reached: {timeout})");
        }
    }
}

Использование:

const string SRC = "http://ovh.net/files/10Mio.dat";
const string TARGET = @"d:\stuff\10Mio.dat";
// Time needed to restore network connection
TimeSpam TIMEOUT = TimeSpan.FromSeconds(30);
DownloadFile(SRC,TARGET, TIMEOUT); // might want to await this to handle exceptions

Обновление в ответ на комментарий. Если вы хотите, чтобы тайм-аут основывался на полученных данных, а не на все время работы, это также возможно с помощью Task.Delay. Например:

static async Task DownloadFile(string url, string output, TimeSpan timeout)
{
    using (var wcl = new WebClient())
    {
        wcl.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
        DateTime? lastReceived = null;
        wcl.DownloadProgressChanged += (o, e) =>
        {
            lastReceived = DateTime.Now;
        };
        var download = wcl.DownloadFileTaskAsync(url, output);
        // await two tasks - download and delay, whichever completes first
        // do that until download fails, completes, or timeout expires
        while (lastReceived == null || DateTime.Now - lastReceived < timeout) {
            await Task.WhenAny(Task.Delay(1000), download); // you can replace 1 second with more reasonable value
            if (download.IsCompleted || download.IsCanceled || download.Exception != null)
                break;
        }
        var exception = download.Exception; // need to observe exception, if any
        bool cancelled = !download.IsCompleted && exception == null;

        // download is not completed yet, nor it is failed - cancel
        if (cancelled)
        {
            wcl.CancelAsync();
        }

        if (cancelled || exception != null)
        {
            // delete partially downloaded file if any (note - need to do with retry, might not work with a first try, because CancelAsync is not immediate)
            int fails = 0;
            while (true)
            {
                try
                {
                    File.Delete(output);
                    break;
                }
                catch
                {
                    fails++;
                    if (fails >= 10)
                        break;

                    await Task.Delay(1000);
                }
            }
        }
        if (exception != null)
        {
            throw new Exception("Failed to download file", exception);
        }
        if (cancelled)
        {
            throw new Exception($"Failed to download file (timeout reached: {timeout})");
        }
    }
}

Ответ 2

Лично, если бы я должен был создать надежное решение для загрузки, я бы добавил монитор сетевого подключения, потому что это то, чего мы действительно ожидаем. Для простоты чего-то подобного будет достаточно.

online = true;

NetworkChange.NetworkAvailabilityChanged += NetworkChange_NetworkAvailabilityChanged;
_isNetworkOnline = NetworkInterface.GetIsNetworkAvailable();

void NetworkChange_NetworkAvailabilityChanged(object sender, NetworkAvailabilityEventArgs e)
{
    online  = e.IsAvailable;
}

Затем вы можете проверить доступность сети и подождать, когда это необходимо, прежде чем пытаться загрузить или продвинуться... Я определенно соглашусь с тем, что простое решение ping работает лучше, чем это время от времени, основываясь на опыте.

В зависимости от размера загружаемого файла, скорость сети также может помочь, так что вы можете решить, как вырезать в случае прерывистых подключений. Взгляните на этот проект для идей.