0

I get this InnerException with below Exception Tree;

enter image description here

My Code is;

public T GetQueryResultsFromQuery<T>(string query)
{
    try
    {
        HttpClient client = new HttpClient();

        System.Net.ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls | SecurityProtocolType.Ssl3;

        var results = client.GetAsync(query).GetAwaiter().GetResult(); // error happens here

        return _jsonHelper.DeserializeObject<T>(results.Content.ReadAsStringAsync().GetAwaiter().GetResult());
    }
    catch(System.Exception ex)
    {
        throw ex;
    }
}

And an example query string;

https://api.foobar.com/foo?startDate=0001-01-01%2000%3A00%3A00&endDate=2020-02-17%2000%3A00%3A00

As one can see, I tried the solution ideas which were given on the similar questions but it doesn't work.

Same "query" works like a charm on Postman.

Is there any other solution ideas that might help my situation?

Edit

.NET Framework version is 4.7.2 and I just created a console app in which I don't have this issue whatsoever. Not sure what is really causing the problem on this project. I'd appreciate a help.

Edit 2

So, I have found an answer(?) which was to flush the cache. But even if it works on one startup, it fails if I relaunch the application. What is wrong here, I'm not sure.

The newest code part;

using (var httpClient = new System.Net.Http.HttpClient())
{
    System.Net.ServicePointManager.SecurityProtocol |= SecurityProtocolType.Ssl3 |
    SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;

    ServicePointManager.DnsRefreshTimeout = 0;

    httpClient.DefaultRequestHeaders.ConnectionClose = true;

    var getter = httpClient.GetAsync("https://api.foobar.com/foo");
    FlushCache();
    var vuhu = getter.GetAwaiter().GetResult();

    return _jsonHelper.DeserializeObject<T>(vuhu.Content.ReadAsStringAsync().GetAwaiter().GetResult());
}

I get a succesfull result on some re-launches of the project;

enter image description here

And I get the same error on other re-launches;

enter image description here

Nothing code-wise changes between the relaunches, so what is the issue here?

Edit 3 The FlushCahce Function (from this answer);

[DllImport("dnsapi.dll", EntryPoint = "DnsFlushResolverCache")]1
static extern UInt32 DnsFlushResolverCache();

[DllImport("dnsapi.dll", EntryPoint = "DnsFlushResolverCacheEntry_A")]
public static extern int DnsFlushResolverCacheEntry(string hostName);

public static void FlushCache()
{
    DnsFlushResolverCache();
}

public static void FlushCache(string hostName)
{
    DnsFlushResolverCacheEntry(hostName);
}

Last Update:

We could not find what was causing this behaviour, proxy wasn't it and the client-side wasn't it either. Instead, we added a new .Net Core 3.1 project into our system, making the calls to there and from there to the client. Really weird how it just does work sometimes and not for other times. And solution idea can be commented or entered as answers, I'll check them if they work.

oividiosCaeremos
  • 364
  • 1
  • 2
  • 15
  • Any information regarding the api? (issues with tls configuration?) Did you check the SSL/TLS versions server and client-side? – kapsiR Feb 08 '21 at 07:44
  • @kapsiR I have not. But as I have stated on the first edit, on the console app that I created, I always get successful results. Did an endless while loop and it never failed even once. But on this project, it fails on the first few tries after the launch. If it was some kind of a server issue, the problem should've happened on the console app too every now and then. – oividiosCaeremos Feb 08 '21 at 07:48
  • + even the `jsonplaceholder` api gives the same error. So client-side problem is not the case for sure. – oividiosCaeremos Feb 08 '21 at 07:49
  • What exactly does FlushCache do? – Lasse V. Karlsen Feb 08 '21 at 07:52
  • Also, you say it works in the console app, what kind of app is it not working in? – Lasse V. Karlsen Feb 08 '21 at 07:52
  • @LasseV.Karlsen I edited the question with the function. The console app is a `.net core 3.1` app. And the project I have the issue is `.Net Framework 4.7.2` – oividiosCaeremos Feb 08 '21 at 07:55
  • But what kind of application? Is it a desktop winforms/wpf app? A windows service? A web application? – Lasse V. Karlsen Feb 08 '21 at 07:57
  • What TLS/SSL version does the api use? `ServicePointManager` does not affect `HttpClient` in .NET Core – kapsiR Feb 08 '21 at 07:59
  • @kapsiR not sure what they're using. Tried deleting that line, the result neither went worse or better. Still the first few requests throws exceptions and the latter are just fine. – oividiosCaeremos Feb 08 '21 at 08:02
  • 2
    My guess: Issue with the SSL/TLS transport or an issue with the encoding. Additional hint to prevent socket exhaustion: Don't use a new `HttpClient` for every request. – kapsiR Feb 08 '21 at 08:13
  • Thank you for the hint :) Hope the problem is solved soon, I'll try to contact the client side and my team-leader to find out about those SSL/TLS thingies :D I'm just a newbie trying to adjust to the wild so didn't even know there could be issues like these before this integration. Thanks for your time, the issue stays open for the probability of someone coming up with a solution that doesn't concern SSL/TLS. – oividiosCaeremos Feb 08 '21 at 08:18
  • Take a look at this issue, maybe you can get a hint from there https://github.com/dotnet/runtime/issues/43682, also this one https://stackoverflow.com/questions/64283848/very-weird-ssl-error-in-net-the-specified-data-could-not-be-decrypted-only-for/64455087#64455087 – Andrey Belykh Feb 08 '21 at 16:40
  • 1
    There are several problems with this code - blocking async calls, weakening security for no reason, and creating new HttpClient instances that can lead to socket exhaustion. Which could appear as blocking. This can't be fixed by reducing timeouts, it's not the *timeouts* that caused the exhaustion, it's the incorrect use of HttpClient – Panagiotis Kanavos Feb 15 '21 at 13:14
  • 1
    Check [You're using HttpClient wrong and it's destabilizing your software](https://www.aspnetmonsters.com/2016/08/2016-08-27-httpclientwrong/). Use a single `HttpClient` instance and make all methods asynchronous instead of blocking – Panagiotis Kanavos Feb 15 '21 at 13:16
  • thank you for your suggestions, but my problem, for now, aren't those which you mentioned. – oividiosCaeremos Feb 15 '21 at 13:17

0 Answers0