Where is WebClient.DownloadStringTaskAsync (Uri, CancellationToken) in VS11

Async CTP has an extension method with signature

WebClient.DownloadStringTaskAsync(Uri,CancellationToken) 

      

Where is it in VS11?
Do I need to install Async CTP to get this method?

+3


source to share


4 answers


In .NET 4.5, you are probably using the new HttpClient class , specifically the GetStringAsync method .



+3


source


This still exists in .Net 4.5 beta, see MSDN , except it is not an extension method anymore.



What can you say about what's WebClient

not included in .Net for Metro style apps. There you should probably use HttpClient

. Another option is to use HttpWebRequest

, which is still present and has been extended with async based methods Task

.

+1


source


It's unfortunate that CancellationToken support is not built in, but here's how you can approximate it using the Register and CancelAsync methods:

var downloadTask = webClient.DownloadStringTaskAsync(source);

string text;
using (cancellationToken.Register(() => webClient.CancelAsync()))
{
    text = await downloadTask;
}

      

+1


source


Both System.Net.WebClient and System.Net.Http.HttpClient have async functionality. This allows you to create an asynchronous function. Although the GetStringAsync function works asynchronously, you can check regularly to see if cancellation is required.

Example: using System.Net.Http; class HttpSonnetFetcher {const string sonnetsShakespeare = @ " http://www.gutenberg.org/cache/epub/1041/pg1041.txt ";

    public async Task<IEnumerable<string>> Fetch(CancellationToken token)
    {
        string bookShakespeareSonnets = null;
        using (var downloader = new HttpClient())
        {
            var downloadTask = downloader.GetStringAsync(sonnetsShakespeare);
            // wait until downloadTask finished, but regularly check if cancellation requested:
            while (!downloadTask.Wait(TimeSpan.FromSeconds(0.2)))
            {
                token.ThrowIfCancellationRequested();
            }
            // if still here: downloadTask completed
            bookShakespeareSonnets = downloadTask.Result;
        }

        // just for fun: find a nice sonnet, remove the beginning, split into lines and return 12 lines
        var indexNiceSonnet = bookShakespeareSonnets.IndexOf("Shall I compare thee to a summer day?");
        return bookShakespeareSonnets.Remove(0, indexNiceSonnet)
            .Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries)
            .Take(12);  
    }
}

      

Usage will be as follows:

private void TestCancellationHttpClient()
{
    try
    {
        var sonnetFetcher = new HttpSonnetFetcher();
        var cancellationTokenSource = new CancellationTokenSource();

        var sonnetTask = Task.Run(() => sonnetFetcher.Fetch(cancellationTokenSource.Token));
        cancellationTokenSource.CancelAfter(TimeSpan.FromSeconds(10));

        // meanwhile do something else, checking regularly if the task finished, or if you have nothing to do, just Task.Wait():
        while (!sonnetTask.Wait(TimeSpan.FromSeconds(0.25)))
        {
            Console.Write('.');
        }
        // if still here: the sonnet is fetched. return value is in sonnetTask.Result
        Console.WriteLine("A nice sonnet by William Shakespeare:");
        foreach (var line in sonnetTask.Result)
        {
            Console.WriteLine(line);
        }
    }

    catch (OperationCanceledException exc)
    {
        Console.WriteLine("Canceled " + exc.Message);
    }
    catch (AggregateException exc)
    {
        Console.WriteLine("Task reports exceptions");
        var x = exc.Flatten();
        foreach (var innerException in x.InnerExceptions)
        {
            Console.WriteLine(innerException.Message);
        }
    }
    catch (Exception exc)
    {
        Console.WriteLine("Exception: " + exc.Message);
    }
}

      

Try this in a simple console program and make sure the sonnet is loaded correctly. Decrease CancelAfter from 10 seconds to, say, 0.1 seconds and make sure the task is canceled.

Nota Bene: Even though the OperationCancelledException event is thrown, this exception is thrown as an internal AggregateException exception. All exceptions thrown in a task are always enclosed in an AggregateException.

0


source







All Articles