I get this InnerException with below Exception Tree;
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;
And I get the same error on other re-launches;
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.