/// <summary> /// Initializes a new instance of the <see cref="TcpHealthListener"/> class. /// </summary> /// <param name="healthListener">The registered <see cref="TcpHealthListener"/> instance.</param> /// <param name="options">The registered options to configure the <see cref="TcpHealthListener"/>.</param> /// <param name="logger">The logger to write diagnostic trace messages during accepting or rejecting TCP connections.</param> /// <exception cref="ArgumentNullException">Thrown when the <paramref name="healthListener"/>, <paramref name="options"/>, or <paramref name="logger"/> is <c>null</c>.</exception> /// <exception cref="ArgumentException">Thrown when the <paramref name="options"/> doesn't have a filled-out value.</exception> public TcpHealthCheckPublisher(TcpHealthListener healthListener, TcpHealthListenerOptions options, ILogger <TcpHealthCheckPublisher> logger) { Guard.NotNull(healthListener, nameof(healthListener), "Requires a TCP health listener to accept or reject TCP connections"); Guard.NotNull(options, nameof(options), "Requires a set of registered options to determine if the TCP connections should be accepted or rejected based on the health report"); Guard.NotNull(logger, nameof(logger), "Requires a logger instance to write diagnostic trace messages when TCP connections are accepted or rejected"); _healthListener = healthListener; _options = options; _logger = logger; }
/// <summary> /// Adds TCP health probing /// </summary> /// <remarks> /// In order for the TCP health probes to work, ASP.NET Core Health Checks must be configured. You can configure /// it here or do it yourself as well. /// </remarks> /// <param name="services">The collection of services to use in the application</param> /// <param name="tcpConfigurationKey">The configuration key that defines TCP health port on which the health report is exposed.</param> /// <param name="configureHealthChecks">The capability to configure the required health checks to expose, if required</param> /// <param name="configureTcpListenerOptions">The capability to configure additional options how the <see cref="TcpHealthListener"/> works, if required</param> /// <param name="configureHealthCheckPublisherOptions"> /// The capability to configure additional options regarding how fast or slow changes in the health report should affect the TCP probe's availability, /// when the <see cref="TcpHealthListenerOptions.RejectTcpConnectionWhenUnhealthy"/> is set to <c>true</c>. /// </param> /// <returns>Collection of services to use in the application</returns> /// <exception cref="ArgumentNullException">Thrown when the <paramref name="services"/> is <c>null</c>.</exception> /// <exception cref="ArgumentException">Thrown when the <paramref name="tcpConfigurationKey"/> is blank.</exception> public static IServiceCollection AddTcpHealthProbes( this IServiceCollection services, string tcpConfigurationKey, Action <IHealthChecksBuilder> configureHealthChecks = null, Action <TcpHealthListenerOptions> configureTcpListenerOptions = null, Action <HealthCheckPublisherOptions> configureHealthCheckPublisherOptions = null) { Guard.NotNull(services, nameof(services), "Requires a set of services to add the TCP health probe to"); Guard.NotNullOrWhitespace(tcpConfigurationKey, nameof(tcpConfigurationKey), "Requires a non-blank configuration key to retrieve the TCP port"); IHealthChecksBuilder healthCheckBuilder = services.AddHealthChecks(); configureHealthChecks?.Invoke(healthCheckBuilder); var listenerOptions = new TcpHealthListenerOptions { TcpPortConfigurationKey = tcpConfigurationKey }; configureTcpListenerOptions?.Invoke(listenerOptions); services.AddSingleton(listenerOptions); if (listenerOptions.RejectTcpConnectionWhenUnhealthy) { services.Configure <HealthCheckPublisherOptions>(options => { configureHealthCheckPublisherOptions?.Invoke(options); }); services.AddSingleton <IHealthCheckPublisher, TcpHealthCheckPublisher>(); } services.AddSingleton <TcpHealthListener>(); services.AddHostedService(serviceProvider => serviceProvider.GetRequiredService <TcpHealthListener>()); return(services); }
/// <summary> /// Initializes a new instance of the <see cref="TcpHealthListener"/> class. /// </summary> /// <param name="healthListener">The registered <see cref="TcpHealthListener"/> instance.</param> /// <param name="options">The registered options to configure the <see cref="TcpHealthListener"/>.</param> /// <exception cref="ArgumentNullException">Thrown when the <paramref name="healthListener"/>, or <paramref name="options"/> is <c>null</c>.</exception> /// <exception cref="ArgumentException">Thrown when the <paramref name="options"/> doesn't have a filled-out value.</exception> public TcpHealthCheckPublisher(TcpHealthListener healthListener, TcpHealthListenerOptions options) : this(healthListener, options, NullLogger <TcpHealthCheckPublisher> .Instance) { }