Path: redb-route/redb.Route.AzureServiceBus/AzureServiceBusEndpoint.cs
Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route
Page: https://redbase.app/src/dev-2026-12-06/redb-route/redb.Route.AzureServiceBus/AzureServiceBusEndpoint.cs
----
using Azure.Messaging.ServiceBus;
using redb.Route.Abstractions;
using redb.Route.Extensions;
using redb.Route.Core;
namespace redb.Route.AzureServiceBus;
///
/// Azure Service Bus endpoint. Manages shared lifecycle.
/// Queue/topic name comes from .
///
internal sealed class AzureServiceBusEndpoint : EndpointBase
{
private ServiceBusClient? _client;
///
/// Creates the client instead of the connection factory or the URI options when set: a test sets a fake here to
/// drive a processor that fails to start without a namespace.
///
internal Func? ClientSource { get; set; }
private readonly SemaphoreSlim _clientLock = new(1, 1);
internal AzureServiceBusEndpoint(EndpointUri uri, AzureServiceBusComponent component,
AzureServiceBusEndpointOptions options)
: base(uri, component, options)
{
_headerFilter = new EndpointHeaderFilter(this, options.HeaderFilterStrategy);
}
private readonly EndpointHeaderFilter _headerFilter;
///
/// The header filter strategy of this endpoint: the one headerFilterStrategy=#name names, or
/// . A name the registry does not hold fails when the
/// producer or consumer is built.
///
internal IHeaderFilterStrategy HeaderFilterStrategy => _headerFilter.Or(AzureServiceBusHeaderFilterStrategy.Default);
/// Queue or topic name resolved from options or URI path.
internal string EntityName => Options.QueueName ?? Options.TopicName ?? Uri.Path;
/// True when a subscription name is set (topic mode).
internal bool IsTopic => !string.IsNullOrEmpty(Options.SubscriptionName);
///
/// Returns a shared for this endpoint.
/// Thread-safe; ServiceBusClient is designed for long-lived reuse.
///
internal async Task GetOrCreateClientAsync(CancellationToken ct = default)
{
if (_client is not null) return _client;
await _clientLock.WaitAsync(ct).ConfigureAwait(false);
try
{
if (_client is not null) return _client;
if (ClientSource is { } source)
{
_client = source();
return _client;
}
if (!string.IsNullOrWhiteSpace(Options.ConnectionFactory))
{
// A set-but-unknown name fails loud — a typo must never silently
// fall back to URI parameters.
var context = (Component as ComponentBase)?.Context;
var factory = context.GetRequiredFromRegistry(
Options.ConnectionFactory);
_client = factory.Build();
return _client;
}
_client = BuildClientFromOptions();
return _client;
}
finally
{
_clientLock.Release();
}
}
///
public override IProducer CreateProducer()
=> new AzureServiceBusProducer(this, Options);
///
public override IConsumer CreateConsumer(IProcessor processor)
{
if (Options.Batch is { } batch)
return new AzureServiceBusBatchConsumer(this, processor, Options, batch);
if (Options.EnableSessions)
return new AzureServiceBusSessionConsumer(this, processor, Options);
return new AzureServiceBusConsumer(this, processor, Options);
}
///
public override async Task Stop(CancellationToken ct = default)
{
if (_client is not null)
{
await _client.DisposeAsync().ConfigureAwait(false);
_client = null;
}
await base.Stop(ct).ConfigureAwait(false);
}
private ServiceBusClient BuildClientFromOptions()
{
var clientOptions = new ServiceBusClientOptions
{
RetryOptions = new ServiceBusRetryOptions
{
Mode = Options.ParsedRetryMode,
MaxRetries = Options.RetryMaxRetries,
Delay = TimeSpan.FromMilliseconds(Options.RetryDelayMs),
MaxDelay = TimeSpan.FromMilliseconds(Options.RetryMaxDelayMs)
}
};
return new ServiceBusClient(Options.ConnectionString, clientOptions);
}
}