快速失败与Web客户端

快速失败与Web客户端

本文介绍了快速失败与Web客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用,我会定期从服务下载数据,具有以下特点:

Using a modified WebClient, I download data periodically from a service with the following characteristics:


  • 数据下载(〜1GB)可能需要大约20分钟

  • 有时,服务决定完全不返回任何数据(请求挂起),或者需要几分钟到几小时返回第一个字节。

我想在事件快速失败的服务没有在合理的时间(可配置)额度内返回任何数据,但允许足够的时间即取得进展成功下载。

I would like to fail fast in the event that the service does not return any data within a reasonable (configurable) amount of time, yet allow plenty of time for a download that is making progress to succeed.

看来, 属性的时,而 ReadWriteTimeout 控制可用的总时间,读取一次数据数据传输开始。

It seems that the WebRequest.Timeout property controls the total time for the request to complete, while ReadWriteTimeout controls the total time available to read data once the data transfer begins.

我缺少的,将控制的最长时间建立连接并返回第一个字节之间等待的属性?如果没有这样的属性,我怎么能解决这个问题?

Am I missing a property that would control the maximum amount of time to wait between establishing the connection and the first byte returning? If there is no such property, how can I approach the problem?

推荐答案

我不知道任何额外的超时属性即会实现你正在寻找的结果。浮现在脑海的第一个念头是附加处理程序 DownloadProgressChanged 将更新一个标志,表示已收到数据(并不总是准确的,虽然)。

I am not aware of any additional timeout property that will achieve the result you are looking for. The first thought that comes to mind is attaching a handler to DownloadProgressChanged that will update a flag to indicate data has been received (not always accurate though).

使用定时的EventWaitHandle 然后你可以块(或异步处理,如果你喜欢)的时间周期短,评估任何数据是否已经收到。下面的代码是不是完全充实的例子,但它可能会如何实现的想法。

Using a Timer or EventWaitHandle you could then block (or handle async if you prefer) for a short period of time and evaluate whether any data has been received. The code below is not a fully fleshed out example, but an idea of how it may be implemented.

using (var manualResetEvent = new ManualResetEvent(false))
using (var client = new WebClient())
{
    client.DownloadProgressChanged += (sender, e) => manualResetEvent.Set();
    client.DownloadDataAsync(new Uri("https://github.com/downloads/cbaxter/Harvester/Harvester.msi"));

    if (!manualResetEvent.WaitOne(5000))
        client.CancelAsync();
}

在上面的例子中, manualResetEvent.WaitOne 将返回真正如果 DownloadProgressChanged 被调用。你可能会想检查 e.BytesReceived> 0 ,只设置非零值,但我认为你的想法?

In the above example, the manualResetEvent.WaitOne will return true if DownloadProgressChanged was invoked. You will likely want to check e.BytesReceived > 0 and only set for non-zero values, but I think you get the idea?

这篇关于快速失败与Web客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 06:52