c# WebClient DownloadFileAsync() 不会抛出错误

c# WebClient DownloadFileAsync() does not throw errors

我正在使用以下代码成功下载文件。因为我希望在应用程序启动时下载它们,所以我不想阻止任何东西 -> 异步。

但是我面临的问题是,即使 URI 路径完全是废话,它也会为我生成一个空文件,而不是像 msdn

中所述那样引发错误

谁能帮帮我?

private void DownloadDocuments()
    {
        using (WebClient myWebClient = new WebClient())
        {
            try
            {
                Log("load1");
                myWebClient.DownloadFileAsync(new Uri(documentsUri + documentActivation), documentspath + "\" + documentActivation);

            }
            catch (WebException)
            {
                Log("Guide Activation Download failed.");
            }
            catch (InvalidOperationException)
            {
                Log("Guide Activation could not be saved.");
            }
        }
        using (WebClient myWebClient = new WebClient())
        { 
            try
            {
                Log("load2");
                myWebClient.DownloadFileAsync(new Uri(documentsUri + documentFloating), documentspath + "\" + documentFloating);
            }
            catch (WebException)
            {
                Log("Guide Floating Download failed.");
            }
            catch (InvalidOperationException)
            {
                Log("Guide Floating could not be saved.");
            }
        }
        using (WebClient myWebClient = new WebClient())
        {
            try
            {
                Log("load3");
                myWebClient.DownloadFileAsync(new Uri(documentsUri + documentSeat), documentspath + "\" + documentSeat);
            }
            catch (WebException)
            {
                Log("Guide Seat Download failed.");
            }
            catch (InvalidOperationException)
            {
                Log("Guide Seat could not be saved.");
            }
        }
    }

WebClient.DownloadFileAsync 不会在 HTTP 请求失败时抛出异常。您需要订阅 DownloadFileCompleted 事件才能收到错误通知。

但是,一旦我们在 C# 中拥有基于任务的 async/await 功能,我不建议乱用事件处理程序回调:WebClient.DownloadFileTaskAsync 很多使用起来更方便

考虑到您对并行处理的评论,您可以这样做:

static async Task DownloadDocumentAsync(Uri uri, string fileName)
{
    using (var webClient = new WebClient())
    {
        try
        {
            await webClient.DownloadFileTaskAsync(uri, fileName);
        }
        catch (WebException ex)
        {
            Log($"Downloading {uri} failed. {ex.Message}");
            throw;
        }
        catch (InvalidOperationException)
        {
            Log($"Saving {uri} to {fileName} failed. File is in use.");
            throw;
        }
    }
}

那么你在应用程序启动时的逻辑:

var baseUri = new Uri(documentsUri);
var downloadTasks = new[]
{
    DownloadDocumentAsync(new Uri(baseUri, documentActivation), Path.Combine(documentspath, documentActivation)),
    DownloadDocumentAsync(new Uri(baseUri, documentFloating), Path.Combine(documentspath, documentFloating)),
    DownloadDocumentAsync(new Uri(baseUri, documentSeat), Path.Combine(documentspath, documentSeat)),
};

try
{
    Task.WaitAll(downloadTasks);
}
catch (AggregateException)
{
    // handle the case when some of the download tasks failed
}

这样下载任务并行执行,但 Task.WaitAll 阻塞,直到所有任务完成。如果您想保持异步,则需要 await Task.WhenAll