/// <summary> /// Add a health check for network TCP connection. /// </summary> /// <param name="builder">The <see cref="IHealthChecksBuilder"/>.</param> /// <param name="setup">The action to configure TCP connection parameters.</param> /// <param name="name">The health check name. Optional. If <c>null</c> the type name 'tcp' will be used for the name.</param> /// <param name="failureStatus"> /// The <see cref="HealthStatus"/> that should be reported when the health check fails. Optional. If <c>null</c> then /// the default status of <see cref="HealthStatus.Unhealthy"/> will be reported. /// </param> /// <param name="tags">A list of tags that can be used to filter sets of health checks. Optional.</param> /// <returns>The <see cref="IHealthChecksBuilder"/>.</returns></param> public static IHealthChecksBuilder AddTcpHealthCheck(this IHealthChecksBuilder builder, Action <TcpHealthCheckOptions> setup, string name = default, HealthStatus?failureStatus = default, IEnumerable <string> tags = default) { var options = new TcpHealthCheckOptions(); setup?.Invoke(options); return(builder.Add(new HealthCheckRegistration( name ?? TCP_NAME, sp => new TcpHealthCheck(options), failureStatus, tags))); }
public async Task respect_configured_timeout_and_throw_operation_cancelled_exception() { var options = new TcpHealthCheckOptions(); options.AddHost("invalid", 5555); var tcpHealthCheck = new TcpHealthCheck(options); var result = await tcpHealthCheck.CheckHealthAsync(new HealthCheckContext { Registration = new HealthCheckRegistration("tcp", instance: tcpHealthCheck, failureStatus: HealthStatus.Degraded, null, timeout: null) }, new CancellationTokenSource(TimeSpan.FromSeconds(2)).Token); result.Exception.Should().BeOfType <OperationCanceledException>(); }