diff --git a/src/HealthChecks.AzureServiceBus/AzureEventHubHealthCheck.cs b/src/HealthChecks.AzureServiceBus/AzureEventHubHealthCheck.cs index 7aa98df35b..4b33fd2663 100644 --- a/src/HealthChecks.AzureServiceBus/AzureEventHubHealthCheck.cs +++ b/src/HealthChecks.AzureServiceBus/AzureEventHubHealthCheck.cs @@ -47,7 +47,7 @@ public async Task CheckHealthAsync(HealthCheckContext context { try { - var client = await ClientCache.GetOrAddAsyncDisposableAsync(ConnectionKey, _ => CreateClient()).ConfigureAwait(false); + var client = await ClientCache.GetOrAddAsyncDisposableAsync(ConnectionKey, _ => CreateClient(context)).ConfigureAwait(false); _ = await client.GetEventHubPropertiesAsync(cancellationToken).ConfigureAwait(false); @@ -59,15 +59,17 @@ public async Task CheckHealthAsync(HealthCheckContext context } } - private EventHubProducerClient CreateClient() + private EventHubProducerClient CreateClient(HealthCheckContext context) { + var clientOptions = CreateClientOptions(context); + if (_options.ConnectionString is not null) - return new EventHubProducerClient(GetFullConnectionString()); + return new EventHubProducerClient(GetFullConnectionString(), clientOptions); if (_options.Connection is not null) - return new EventHubProducerClient(_options.Connection); + return new EventHubProducerClient(_options.Connection, clientOptions); - return new EventHubProducerClient(_options.FullyQualifiedNamespace, _options.EventHubName, _options.Credential); + return new EventHubProducerClient(_options.FullyQualifiedNamespace, _options.EventHubName, _options.Credential, clientOptions); } private string GetFullConnectionString() @@ -78,4 +80,12 @@ private string GetFullConnectionString() connectionString = $"{connectionString};{ENTITY_PATH_SEGMENT}{_options.EventHubName}"; return connectionString; } + + private static EventHubProducerClientOptions CreateClientOptions(HealthCheckContext context) => new() + { + ConnectionOptions = new() + { + ConnectionIdleTimeout = context.Registration.Timeout, + } + }; }