Search code examples
asp.netjsonhttprequestioexceptionhealth-check

HttpClient request gives HttpRequestException


I developed an healthcheck endpoint for my web application. It prints the following JSON:

{
  "Status": "Healthy",
  "HealthChecks": [
  {
    "Status": "Healthy",
    "Component": "Persistence Server gRPC Status Health Check",
    "Description": "Persistence Server gRPC status is healthy"
  }]
}

This is the health model class:

internal class HealthModel
{
    public string Status { get; set; } = string.Empty;
    public List<HealthCheck> HealthChecks { get; set; } = new List<HealthCheck>();
}

This is HealthCheck class:

internal class HealthCheck
{
    public string Status { get; set; } = string.Empty;
    public string Component { get; set; } = string.Empty;
    public string Description { get; set; } = string.Empty;
}

I have 2 health-check endpoints that uses the exact same model (HealthModel). Those 2 endpoints are running over 2 different ports. For example;

  • application1: https://localhost:7001/h-persistence
  • application2: https://localhost:7101/h-other-persistence

I created a very simple tool with WPF to access those JSON endpoints and consume them.

This is the tool code:

HttpClient httpClient = new HttpClient();
HealthModel? model = await httpClient.GetFromJsonAsync<HealthModel>(url);
return model;

The problem is my tool can connect to https://localhost:7001/h-persistence and consume and print the json content. however, I get HttpRequestException (System.IO.IOException: The response ended prematurely.) when i try to connect the 2nd application.

I compare how i initialize two application and they are identical. I use the same HealthCheck model classes to read their contents but I can read app one's content successfully but app two throws the exception above.

I searched the error already and looked at the solutions in the existing questions on Stackoverflow but no success yet.

Here is what I have tried so far:

  • I entered url into Postman. Received error.
  • I entered another json endpoint url to my HttpClient. My HttpClient works fine.
  • I installed the application on another computer. I defined a rule in Windows Firewall that allows my application to send and receive packets. I got the same error.
  • I tried another port number. I got the same error.
  • I checked if anything else is using the port that I use. I didn't find anything.
  • I can see the content of the json very well, if i visit the URL with an internet browser.

Postman Error:

Error: read ECONNRESET
Request Headers
User-Agent: PostmanRuntime/7.31.1
Accept: */*
Postman-Token: c9da74a8-73f1-451f-b145-708dc98d5b72
Host: localhost:7041
Accept-Encoding: gzip, deflate, br
Connection: keep-alive

Here is the error I get:

enter image description here


Solution

  • The information you're showing indicates that there is an error happening on the server, not the client.

    Check 1) the IIS logs to see if it lists an error code, 2) your NT Event logs ("Application" log) for any unhandled exceptions, 3) any (server-side) error handling & logging?