Eric
Eric

Reputation: 1210

c# HttpClient just stops without any indication why

I am trying to fetch data from our API instance. The code I have is pretty much copied MS website with teaks found on SO. The code that I am using is this:

string peopleEndpoint = $"{apiConf.ApiEndpoint}/{apiConf.dbspec}/_table/mdm.PEOPLE_SV";
string json = null;
try
{
    using (var httpClient = new HttpClient())
    {
        httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
        httpClient.DefaultRequestHeaders.Add("X-Api-Key", apiConf.ApiKey);
        HttpResponseMessage response = await httpClient.GetAsync(peopleEndpoint);
        json = await response.Content.ReadAsStringAsync();
    }
}
catch (Exception ex)
{
    Debug.WriteLine(ex.ToString());
    throw;
}

The configuration values are good... I can copy them and paste them into a cURL command and get the data. Same with the headers.

Now when I debug, execution stops after executing this line:

HttpResponseMessage response = await httpClient.GetAsync(peopleEndpoint);

And, in the output window, all I see is:

The program '[10172] dotnet.exe' has exited with code 0 (0x0).
The program '[10172] dotnet.exe: Program Trace' has exited with code 0 (0x0).

How can I figure out what is causing this failure?

The framework is noted as:

<TargetFramework>netcoreapp2.1</TargetFramework>

Upvotes: 6

Views: 2090

Answers (1)

Matti Price
Matti Price

Reputation: 3551

Is the code async all the way to the top or is there a non async method that calls this code somewhere that may not be waiting until it is complete. That's kind of what this smells like to me.

Upvotes: 7

Related Questions