1

I have this code which I borrowed from this site (https://psycodedeveloper.wordpress.com/2013/04/02/how-to-download-a-file-with-httpclient-in-c/):

 public static Task DownloadAsync(string requestUri, string filename)
 {
     if (requestUri == null)
            throw new ArgumentNullException("requestUri");

     return DownloadAsync(new Uri(requestUri), filename);
 }

 public static async Task DownloadAsync(Uri requestUri, string filename)
 {
     if (filename == null)
            throw new ArgumentNullException("filename");

     using (var httpClient = new HttpClient())
     {
         using (var request = new HttpRequestMessage(HttpMethod.Get, requestUri))
         {
             using (Stream contentStream = await (await httpClient.SendAsync(request)).Content.ReadAsStreamAsync(),
                    stream = new FileStream(filename, FileMode.Create, FileAccess.Write, FileShare.None, 10000, true))
             {
                    await contentStream.CopyToAsync(stream);
             }
         }
     }
}

and I am calling it like this:

 DownloadAsync(@"http://www.stafforini.com/docs/Covey%20-%20The%207%20habits%20of%20highly%20effective%20people.pdf", @"D:\Temp\test.pdf").Wait();

but it doesn't download any file and it actually never finish downloading.

What is the problem with this code?

edit1

The answer is deadlock, but I did not know that it has deadlock, so thanks for letting me know that my code has deadlock which I now understand and can fix it.

It is not the duplicate of the other question as I did not know why it was not work, f I knew, that answer would helped me

Community
  • 1
  • 1
mans
  • 17,104
  • 45
  • 172
  • 321
  • 2
    Why dont you have async all the way up the chain? Why are you using HttpClient the [wrong way](https://aspnetmonsters.com/2016/08/2016-08-27-httpclientwrong/)? – maccettura Jan 31 '18 at 16:48
  • Probably an async deadlock because the calling context is waiting on it to finish, and it is waiting on the calling context to signal completion. There's lots of articles and answers on this site about async deadlocks. I'd help more but I'm in bed on my smartphone :) – ProgrammingLlama Jan 31 '18 at 16:48
  • 1
    Looks like an async deadlock https://msdn.microsoft.com/en-us/magazine/jj991977.aspx – DavidG Jan 31 '18 at 16:49
  • [Don't block on async code](https://blog.stephencleary.com/2012/07/dont-block-on-async-code.html) – EJoshuaS - Stand with Ukraine Jan 31 '18 at 16:50

2 Answers2

3

This code will deadlock in any environment with a synchronization context (which is most environments other than a console application) because it creates a circular wait.

If you're working in such an environment, you should use async/await "all the way down."

See Don't Block on Async Code by Stephen Cleary for more details.

1

This code only stores the file which was downloaded from the URL. You have to save the data into a file from your FileStream.

A short and simple example is John Skeet's answer on this link: How do I save a stream to a file in C#?

NagyDani
  • 93
  • 9
  • OP's download method works fine - I have tested it. The problem is that he's calling it in a way that causes an async deadlock. > "This code only stores the file which was downloaded from the URL." - Yes, this is what OP intends. A method to download a file. > You have to save the data into a file from your FileStream. - OP is doing that. His download method works. – ProgrammingLlama Feb 01 '18 at 03:08