K
K
kosyak_472021-06-20 17:57:33
C++ / C#
kosyak_47, 2021-06-20 17:57:33

Why is a large piece of code ignored after executing httpClient.GetStreamAsync()?

Good afternoon. I practically wrote the entire code for adding a product to the Vkontakte group using the API and ran into a new problem. I have a fragment in my code that is responsible for downloading an image via a direct link:

...
HttpClient httpClient = new HttpClient();

Stream stream = await httpClient.GetStreamAsync(dataExcelTabel[i].photo2);
FileStream file = File.OpenWrite(PathForSavePhotos + "/" + dataExcelTabel[i].codeProduct + "." + splitFileName[splitFileName.Length - 1]);

dataExcelTabel[i].pathPhotoSave = file.Name;

await stream.CopyToAsync(file);

//Закрываем потоки
stream.Close();
file.Close();
...


In general, when executing the line
Stream stream = await httpClient.GetStreamAsync(dataExcelTabel[i].photo2);
the compiler simply ignores the huge piece of code and jumps to the next method without executing the current one. Even in turn-by-turn mode flies by. What am I missing? Tell me please

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey Bereznikov, 2021-06-22
@gdt

I would recommend wrapping your piece of code in a try/catch (Exception e) first, put a breakpoint on the next line after the problematic method, and in the catch body. Either there or there should work, so you narrow down the circle of suspects :)

V
Vasily Bannikov, 2021-06-28
@vabka

in the code, there is a complete exit from the UploadPhotos () method, although I still have a decent part of the code there that should be executed. The jump occurs in both turn-based and normal mode.

1. This is a feature of async-await. Jumping while debugging is normal.
2. Don't use async void
3. Try wrapping everything inside the method with awaits in a try-catch to make sure the exit isn't due to exceptions. At the same time output error messages to the console.
4.
File.OpenWrite(PathForSavePhotos + "/" + dataExcelTabel[i].codeProduct + "." + splitFileName[splitFileName.Length - 1]);

Looks very suspicious. In fact, it may crash due to crooked path separators. Use Path.Combine
5. .Close();Use using
PS instead: no need to duplicate questions

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question