The UtilityLib library provides a set of helper classes to facilitate the implementation of applications by providing standardized implementations in various contexts. An application class is derived from the corresponding base class using the default implementation (constructor, properties, methods). Other helper classes are for default exit codes, data status, property validation, json converters, and custom attributes.
The following interfaces and classes are provided:
- IGateway
- IHttpClientSettings
- IPingSettings
- ITimedServiceSettings
- IUdpClientSettings
- BaseClass
- BaseCommand
- BaseController
- BaseGateway
- BaseProgram
- CommandLineHost
- TimedService
- ValidatedCommand
- NumberConverter
- TimeSpanConverter
- SpecialDoubleConverter
- UIHealthReport
- UIResponseWriter
- DataStatus
- ExitCodes
- StatusCheck
- PingCheck
- JsonExtensions
- PropertyExtensions
- ValidationExtensions
- WebHostBuilderExtensions
- HttpClientSettings
- PingSettings
- TimedServiceSettings
- UdpClientSettings
- AbsoluteUriAttribute
- IPAddressAttribute
- IPEndPointAttribute
- OptionValidationAttribute
The BaseClass class provides a standard logger data member and an optional application settings member using dependency injection.
protected readonly ILogger<BaseClass> _logger;
and
protected readonly ILogger<BaseClass<AppSettings>> _logger;
protected readonly AppSettings _settings;
The BaseCommand class for console application commands provides logger, settings, and environment data members. Standard --help, --version and loglevel options simplify a command implementation allowing synchronous or async execution:
int OnExecute()
and
async Task<int> OnExecuteAsync(CancellationToken cancellationToken)~~~
The loglevel command provides mapping of various loglevels (Microsoft.Extensions.Logging, Serilog):
- trace, verbose
- debug
- info, information
- warning
- error
- fatal, critical
The BaseCommand class is derived from the ValidatedCommand class which provides a custom validation routine:
public virtual bool CheckOptions()
The validation integration is done using the OptionValidationAttribute and the ValidationExtension class.
The BaseController class for an API controller provides logger and settings data members. Some additional methods returning StatusCodeResult and ObjectResult are available:
Method Name | Status Code |
---|---|
MethodNotAllowed | 405 |
NotAcceptable | 406 |
RequestTimeout | 408 |
Gone | 410 |
ExpectationFailed | 417 |
ImaTeapot | 418 |
Locked | 423 |
NotImplemented | 501 |
BadGateway | 502 |
Unavailable | 503 |
GatewayTimeout | 504 |
The BaseGateway class implements some common features of a gateway.
public bool IsStartupOk { get; }
public bool IsLocked { get; }
public DataStatus Status { get; set; }
public bool Startup();
public bool CheckAccess();
public void Lock();
public async Task LockAsync();
public void Unlock();
Locking primitives are implemented using a SemaphoreSlim. Derived classes should override the Startup(), CheckAccess() methods, and the IsStartupOk property. Note that the timestamp of the Status property is automatically updated.
The BaseProgram class provides a standardized main routine, and host setup using the CommandLineHost helper class and adds singleton service for the application settings using an JSON appsettings.json configuration file. The static JSON serilizer options are initialized adding default converters. The CultureInfo is set to "en-US" and a Stopwatch timer is used to display elapsed time.
public static async Task<int> RunCommandLineApplicationAsync(IHostBuilder builder, string[] args)
public static IHostBuilder CreateDefaultBuilder()
The CommandLineHost class provides a standard implementation for a console application default host builder. This is similar to the standard generic host setup, but adds Serilog support.
Helper classes providing a basic background service for repetitive execution. Three virtual methods are available for overriding in a derived class:
protected virtual async Task DoStart()
protected virtual async Task DoWork()
protected virtual async Task DoStop()
This helper class is used by the BaseCommand class.
This converter is used to convert strings containing numbers.
This converter is used to convert TimeSpans (note: should use ISO8601).
This converter allows special values (NaN, Infinity, -Infinity).
Helper class to provide a custom data type for health report output. See https://github.com/Xabaril/AspNetCore.Diagnostics.HealthChecks.
Helper class to provide JSON formatted health reports. See https://github.com/Xabaril/AspNetCore.Diagnostics.HealthChecks.
Class to provide OPC UA compatible result codes.
public long Timestamp { get; set; }
public uint Code { get; set; }
public string Name { get; set; }
public string Explanation { get; set; }
public bool IsGood { get; }
public bool IsNotGood { get; }
public bool IsUncertain { get; }
public bool IsNotUncertain { get; }
public bool IsBad { get; }
public bool IsNotBad { get; }
The ExitCodes class provides standard exit codes for a console application.
Exit Code Name | Code |
---|---|
SuccessfullyCompleted | 0 |
IncorrectFunction | -1 |
FileNotFound | 2 |
PathNotFound | 3 |
CantOpenFile | 4 |
AccessDenied | 5 |
InvalidData | 13 |
ProgramNotRecognized | 9009 |
Exit Code Name | Code |
---|---|
OperationCanceled | 0xC000013A |
UnhandledException | 0xE0434F4D |
Exit Code Name | Code |
---|---|
OperationCanceled | 130 - SIGINT |
UnhandledException | 134 - SIGABRT |
The last two exit codes are OS platform dependent.
A HealthCheck implementation interpreting the DataStatus property of a gateway class.
A HealthCheck to determine whether a remote computer is accessible over the network.
The JsonExtensions class provides helper extension methods dealing with JSON serializer options.
public static JsonSerializerOptions DefaultSerializerOptions
public static void AddDefaultOptions(this JsonSerializerOptions options)
Extension class to provide property helper methods for an object.
public static bool IsProperty(this Type type, string name)
public static object? GetPropertyValue(this object obj, string name)
public static void SetPropertyValue(this object obj, string name, object value)
Extension class to provide a validate an object based on its DataAnnotations, possibly throwing an exception if the object is not valid.
public static ICollection<ValidationResult> Validate<T>(this T obj)
public static T ValidateAndThrow<T>(this T obj)
The WebHostBuilderExtensions class extends the standard implementation for a web application providing Serilog logger support, and an AppSettings singleton.
public static IWebHostBuilder ConfigureBaseHost<TSettings>(this IWebHostBuilder builder)
where TSettings : class, new()
The HttpClientSettings are used to configure HttpClients.
public class HttpClientSettings : IHttpClientSettings
{
[AbsoluteUri]
public string BaseAddress { get; set; } = "http://localhost";
[Range(0, Int32.MaxValue)]
public int Timeout { get; set; } = 100;
}
The PingSettings are used to configure the execution parameter of the Ping health check. The Host property is used to define the remote host for the Ping. The Roundtrip property is used to limit the allowed Ping roundtrip time (degrading) to the specified number of milliseconds. The DontFragment and Ttl are ping options used to control how Ping data packets are transmitted.
public class PingSettings : IPingSettings
{
public string Host { get; set; } = "localhost";
[Range(0, Int32.MaxValue)]
public int Timeout { get; set; } = 100;
public bool DontFragment { get; set; }
[Range(1, Int32.MaxValue)]
public int Ttl { get; set; } = 128;
[Range(1, Int32.MaxValue)]
public int Roundtrip { get; set; } = 100;
}
The TimedServiceSettings are used to configure the execution parameter of the TimedService. The Delay property is used to delay the DoStart() method by the specified number of milliseconds. The Period property is the time between executions of the DoWork() method.
public class TimedServiceSettings
{
[Range(0, Int32.MaxValue)]
public int Delay { get; set; }
[Range(0, Int32.MaxValue)]
public int Period { get; set; } = 1000;
}
The UdpClientSettings are used to configure UdpClients.
public class UdpClientSettings : IUdpClientSettings
{
[IPEndPoint]
public string EndPoint { get; set; } = string.Empty;
[Range(0, 65535)]
public int Port { get; set; }
[Range(0, double.MaxValue)]
public double Timeout { get; set; } = 1.0;
}
- IPAddress: Validates using IPAddress.TryParse().
- IPEndPoint: Validates using IPEndPoint.TryParse().
- AbsoluteUri: Validates using Uri.TryCreate().
- OptionValidation: Validates using CheckOptions().