2

I am developing an application targetting mobile devices, so I have to consider bad network connectivity. In one use case, I need to reduce the timeout for a request, because if no network is available, that's okay, and I'd fall back to default data immediately, without having the user wait for the HTTP response.

I found that HttpMixin.MakeWebRequest() has a timeout parameter (with default=null) but DownloadUrl() never makes use of it, so the forementioned function always waits for up to 15 seconds:

request.Timeout(timeout ?? TimeSpan.FromSeconds(15), BlobCache.TaskpoolScheduler).Retry(retries);

So actually I do not have the option to use a different timeout, or am I missing something?

Thanks for considering a helpful response.

Uwe Post
  • 518
  • 4
  • 10
  • I saw what you are talking about and am not sure why it is there but, it looks like a timeout related to building the request and not a timeout for the request itself. – Terrance May 02 '17 at 14:50

1 Answers1

2

So after looking at the signature for DownloadUrl in

HttpMixin.cs

I saw what you are talking about and am not sure why it is there but, it looks like the timeout is related to building the request and not a timeout for the request itself.

That being said, in order to set a timeout with a download, you have a couple options that should work.

Via TPL aka Async Await

var timeout = 1000; var task = BlobCache.LocalMachine.DownloadUrl("http://stackoverflow.com").FirstAsync().ToTask(); if (await Task.WhenAny(task, Task.Delay(timeout)) == task) { // task completed within timeout //Do Stuff with your byte data here //var result = task.Result; } else { // timeout logic }

Via Rx Observables

var obs = BlobCache.LocalMachine .DownloadUrl("http://stackoverflow.com") .Timeout(TimeSpan.FromSeconds(5)) .Retry(retryCount: 2); var result = obs.Subscribe((byteData) => { //Do Stuff with your byte data here Debug.WriteLine("Byte Data Length " + byteData.Length); }, (ex) => { Debug.WriteLine("Handle your exceptions here." + ex.Message); });

Terrance
  • 11,764
  • 4
  • 54
  • 80
  • The (option) parameter absoluteExpiration to DownloadURL is passed to Insert(), so I assume that's the time to live for the cached content once downloaded. My concern is the parameter timeout in MakeWebRequest which is used as Timeout for the AsyncSubject. This parameter cannot be changed from outside. I happened to duplicate that code, lowering the default timeout value, which also is a bad solution. Either way - there is no way that the timeout parameter gets a non default value, which would be bad design, so propably I'm missing something. – Uwe Post May 04 '17 at 11:24