Skip to content
This repository has been archived by the owner on Jun 29, 2020. It is now read-only.

Changed usage of HttpClient #63

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 12 additions & 14 deletions src/Microsoft.Extensions.HealthChecks/Internal/UrlChecker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ namespace Microsoft.Extensions.HealthChecks.Internal
{
public class UrlChecker
{
private readonly HttpClient _client;
private readonly Func<HttpResponseMessage, ValueTask<IHealthCheckResult>> _checkFunc;
private readonly string _url;

Expand All @@ -21,24 +22,24 @@ public UrlChecker(Func<HttpResponseMessage, ValueTask<IHealthCheckResult>> check

_checkFunc = checkFunc;
_url = url;
_client = CreateHttpClient();
}

public CheckStatus PartiallyHealthyStatus { get; set; } = CheckStatus.Warning;

protected virtual HttpClient GetHttpClient() => new HttpClient();

public async Task<IHealthCheckResult> CheckAsync()
{
using (var httpClient = CreateHttpClient())
try
{
try
{
var response = await httpClient.GetAsync(_url).ConfigureAwait(false);
return await _checkFunc(response);
}
catch (Exception ex)
{
var data = new Dictionary<string, object> { { "url", _url } };
return HealthCheckResult.Unhealthy($"Exception during check: {ex.GetType().FullName}", data);
}
var response = await _client.GetAsync(_url).ConfigureAwait(false);
return await _checkFunc(response);
}
catch (Exception ex)
{
var data = new Dictionary<string, object> { { "url", _url } };
return HealthCheckResult.Unhealthy($"Exception during check: {ex.GetType().FullName}", data);
}
}

Expand All @@ -61,8 +62,5 @@ public static async ValueTask<IHealthCheckResult> DefaultUrlCheck(HttpResponseMe
};
return HealthCheckResult.FromStatus(status, $"status code {response.StatusCode} ({(int)response.StatusCode})", data);
}

protected virtual HttpClient GetHttpClient()
=> new HttpClient();
}
}