-2

The problem is, when I put await before the HttpClient.GetStreamAsync() it does not return anything, Visual Studio just hangs itself, not returning anything for as long as I decide to stop the debugging. When I try to use .GetAwaiter().GetResult() it prompts the error which I showed on this question which yielded no solutions unfortunatelly. What is resulting with these errors? TLS issue is not it for sure. Network team made sure no request of mines to the api that I'm trying to get results from is blocked and the other side confirmed they also do not block our requests. Which can be confirmed because Postman requests work just fine with the api. I used HttpClient and WebRequest which prompted the same error. Awaiting does not even result with an error, it just goes idle...

Why am I having this issue? How can I overcome it? Any help is appreciated.

(code and solutions that I tried can be found in this question of mine.

oividiosCaeremos
  • 364
  • 1
  • 2
  • 15
  • 1
    All of the errors point to a problem with the *server*. You can't fix that on the client side. You can only add a timeout to your operations so you don't wait forever. BTW blocking an asynchronous operation is a bad idea. So is weakening TLS encryption by allowing obsolete algorithms – Panagiotis Kanavos Feb 15 '21 at 13:07
  • @PanagiotisKanavos, I have created a .Net Framework 4.7.2 project and a .Net Core 3.1 project which work just fine. No trouble at all. But a project which works on .Net Framework 4.7.2, somehow doesn't work as expected. This issue **can not** be a server issue. – oividiosCaeremos Feb 15 '21 at 13:10
  • 1
    Creating a new HttpClient instance is bad too, this class is thread-safe and meant to be reused. Otherwise, you end up exhausting all available sockets which *can* lead to problems. Check [You're using HttpClient wrong and its destabilizing your software](https://www.aspnetmonsters.com/2016/08/2016-08-27-httpclientwrong/). – Panagiotis Kanavos Feb 15 '21 at 13:10
  • 1
    You don't provide enough information so people can only guess what's wrong. Especially in *this* question. HttpClient isn't broken. If there was a stream, `GetStreamAsync` would open it. So either the *server* blocked, or the client code somehow ended up blocking itself. HttpClient isn't broken. The code you posted in the original question though has serious bugs - blocking async calls, weakening security, creating new HttpClient instances and probably exhausting the available sockets – Panagiotis Kanavos Feb 15 '21 at 13:12
  • I did convert the code to use one instance of `HttpClient` but it didn't change a thing. Can you please remove your vote on closing this question by the way? I am having an issue which I don't know why. Other solutions failed to be an answer to mine etc. If I knew what to provide for this problem I would share it of course. But I have no clue what's causing this. I use the same snippet from the question before in a new project and it works fine. Exhaustion, security weakenings are not my problem for now, until I get a successful result. – oividiosCaeremos Feb 15 '21 at 13:15
  • Post your actual code instead of describing it or saying it didn't work. There's no code in this question, and the other one has serious bugs. HttpClient works, otherwise thousands of developers would have noticed. Post code that *doesn't* block, *doesn't* create a new instance each time and *doesn't* try to modify the DNS cache – Panagiotis Kanavos Feb 15 '21 at 13:19
  • To see what's going on use a debugging proxy like Fiddler. If you see in Fiddler that the server never responds, there's nothing you can do on the client side. – Panagiotis Kanavos Feb 15 '21 at 13:20
  • @PanagiotisKanavos **the same snippet works on a newly created project** I know HttpClient works, but for some reason it doesn't work for me. Everything on .NET environment works but there are thousands of people searching solutions to the problems they are having. The very same snippet **works** **without** **ANY** **problem** on a newly created project. I change nothing in between these two projects network-wise. The code you refer to as **bugs** were just copy/paste from other solutions, which I tried and didn't work. I put them in the question to let people know what **did not** work for. – oividiosCaeremos Feb 15 '21 at 13:25
  • Did you use Fiddler to see what's actually going on? – Panagiotis Kanavos Feb 15 '21 at 13:34
  • can't download it to my work computer :/ But can server be blocking requests from specifically a visual studio project? Same desktop, same .net version and same snippet but another project doesn't have the issue. I know it doesn't sound right. But it is what it is. Now I'm going to try to use the same web.config on the new project, and give it a go... Hopefully something comes out of it.. – oividiosCaeremos Feb 15 '21 at 13:39
  • If you are calling your code from UI thread, `GetAwaiter().GetResult()` can result in deadlock. Did you try making this code pure async? – Alexey Rumyantsev Feb 15 '21 at 14:02
  • @AlexeyRumyantsev yes I did convert it to async after seeing suggestions that it should not be used like that. But while using the `await` it goes into this eternal wait mode, in which VS doesn't return anything. It does not allow other requests as well until I restart the debugging. I also created a new .Net Framework project on 4.7.2 with `Web API` structure and got the same issue. Tried it on my personal computer and no issue there. So it is a network-side issue. But how come a .Net Core 3.1 app and Postman doesn't show an issue but .Net Framework 4.7.2 Web API shows, still a mystery. – oividiosCaeremos Feb 15 '21 at 14:07
  • 1
    I suppose `Postman` can use your corporate proxy settings, while .Net application tries to establish it directly, maybe. It looks like problem with your environment, rather that code. – Alexey Rumyantsev Feb 15 '21 at 14:13
  • @AlexeyRumyantsev thank you for your kind approach. I did talk to network team and they did some changes before but I'll talk to them again about the issue, to hopefully find an answer. Thanks again. – oividiosCaeremos Feb 15 '21 at 14:15

1 Answers1

1

The short answer to your question is "Your server is not returning all the data and you did ask the method to wait after all". But that is not helpful.

You might want to be a timeout on the HttpClient get async method. The code below is a contrived example of how to do so. Here is what the code is doing:

  1. A CancellationToken source is created that will signal the CancellationToken as cancelled after five hundred milliseconds.
  2. I get the default landing page for the Google search page. I'm using just a Geta sync instead of a GetstreamAsync because I don't can't think of a source of a stream response off the top of my head that I wouldn't have to code myself. It is the same principal though. On my network, the 500 milliseconds timeout is enough time, so I get a message of "Response code = OK".
  3. If I change the five hundred milliseconds to ten milliseconds the response does not complete before time runs out and I get a message of "A task was canceled."

So, when you say "await" it really does mean "wait here until the method I am calling completes unless I cancel the await earlier than that. Also remember there can be all kinds of CancellationTokenSources depending on what you need. This is just an example.

using System;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;

namespace cancel
{
    class Program
    {
        static async Task Main(string[] args)
        {
            CancellationTokenSource cancellationSource = new CancellationTokenSource(new TimeSpan(0,0,0,0,5000)); //Succeeds
            //CancellationTokenSource cancellationSource = new CancellationTokenSource(new TimeSpan(0,0,0,0,10)); //Fails
            CancellationToken cancellationToken = cancellationSource.Token;

            var client = new HttpClient();
            try
            {
                var result = await client.GetAsync("https://www.google.com",cancellationToken);
                Console.WriteLine($"Response code = {result.StatusCode}");
            }
            catch(Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

        }
    }
}
GlennSills
  • 2,955
  • 18
  • 25
  • thank you for the suggestion for limiting the await time. This surely will help me a lot. Do you, by any chance, have any idea how a .Net Framework 4.7.2 Web API project fails to fetch something using `HttpClient` but on the same machine which uses the same network, a .Net Core 3.1 console application fetches the same query just fine? Also a regular .Net Framework 4.7.2 console app is just fine as the .Net Core 3.1. Can `ApiController` which is inherited on the `Web API` project cause this issue? – oividiosCaeremos Feb 15 '21 at 14:11
  • I would need more information, but I can tell you that there is very little difference in how .Net Framework HttpClient works and .NET Core HttpClient works. If I had to guess I would say there is a bug in the 4.7.2 code. – GlennSills Feb 15 '21 at 14:17
  • I'll talk to the network team to see if there are any errors the network might cause. They said they fixed it and since I get OK results using other projects/tools from the same query, they simply say that it's not about the network :/ I hope they're wrong because I don't have anything on how the code might have bug or bugs. Thanks for your help :) – oividiosCaeremos Feb 15 '21 at 14:21