85b362e8cd
- add the standalone HrynCo.Common solution and projects - include the shared common library source and tests - add package metadata and a repo gitignore
40 lines
1.2 KiB
C#
40 lines
1.2 KiB
C#
namespace HrynCo.Common.HealthChecks;
|
|
|
|
using System.ComponentModel.DataAnnotations;
|
|
using HrynCo.Common.HealthChecks.Interfaces;
|
|
using Microsoft.Extensions.Diagnostics.HealthChecks;
|
|
|
|
public abstract class BaseConfigurationCheck<TOptions> : IConfigurationCheck
|
|
where TOptions : class
|
|
{
|
|
private readonly string _name;
|
|
private readonly TOptions _options;
|
|
|
|
protected BaseConfigurationCheck(TOptions options, string name)
|
|
{
|
|
_options = options;
|
|
_name = name;
|
|
}
|
|
|
|
public Task<HealthCheckResult> CheckConfigurationAsync(CancellationToken cancellationToken)
|
|
{
|
|
var context = new ValidationContext(_options);
|
|
var results = new List<ValidationResult>();
|
|
|
|
bool isValid = Validator.TryValidateObject(
|
|
_options,
|
|
context,
|
|
results,
|
|
true
|
|
);
|
|
|
|
if (!isValid)
|
|
{
|
|
string errors = string.Join("; ", results.Select(r => r.ErrorMessage));
|
|
return Task.FromResult(HealthCheckResult.Unhealthy($"{_name} configuration invalid: {errors}"));
|
|
}
|
|
|
|
return Task.FromResult(HealthCheckResult.Healthy($"{_name} configuration is valid."));
|
|
}
|
|
}
|