6

I have the following code

static void Main(string[] args)
{
        string url = "http://www.google.com";
        Console.WriteLine(GetUrl(url).Result); // throws TaskCanceledException
        Console.WriteLine(GetUrl2(url).Result);
    }

    public static Task<string> GetUrl(string url)
    {
        using (var client = new HttpClient())
        {
            return client.GetStringAsync(url);
        }
    }

    public static Task<string> GetUrl2(string url)
    {
        using (var client = new WebClient())
        {
            return client.DownloadStringTaskAsync(url);
        }
    }

I'm trying to get the string of an url, the problem is GetUrl method (uses HttpClient's GetStringAsync) throws an TaskCacelledException, but GetUrl2 method (uses WebClient's DownloadStringTaskAsync) runs correctly. Is this caused due to using statement? What am I missing?

Edit. In this example I'm calling Result on the task because this is a console application, I know that it is best to await the result in a event handler for example.

1 Answer 1

9

Is this caused due to using statement?

Yes. In both code examples, you're disposing the underlying client before the operation completes. Both code examples should be changed as such:

public static async Task<string> GetUrlAsync(string url)
{
    using (var client = new HttpClient())
    {
        return await client.GetStringAsync(url);
    }
}

public static async Task<string> GetUrl2Async(string url)
{
    using (var client = new WebClient())
    {
        return await client.DownloadStringTaskAsync(url);
    }
}

The behavior of asynchronous downloads when their underlying clients are disposed is undocumented. It's best not to dispose the clients until your code is done using them.

Sign up to request clarification or add additional context in comments.

2 Comments

So can't I count on the "correctness" of the WebClient example?
@chais: I wouldn't. Just use await and then you know it will work correctly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.