chore: add hrynco common library solution
- add the standalone HrynCo.Common solution and projects - include the shared common library source and tests - add package metadata and a repo gitignore
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
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."));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user