Skip to main content

Azure Service Bus

Relayer.AzureServiceBus implements named queue/topic sending and native queue/subscription processors on Azure.Messaging.ServiceBus. Native session processors preserve Service Bus session ordering and state. Receive primitives remain available for applications that own their processor lifecycle.

Register sending​

using Azure.Messaging.ServiceBus.Administration;
using Relayer;
using Relayer.AzureServiceBus;

services.AddRelayer(relayer =>
{
relayer.AddMessageTypesFromThisAssembly();
relayer.ServiceBus("orders", bus =>
{
bus.Options.Configure(options =>
{
options.ConnectionString = connectionString;
options.Queues.Add(new CreateQueueOptions("orders"));
});
bus.Send<OrderPlaced>("orders");
});
});

await sender.SendAsync(new OrderPlaced(customerId));

Give messages a stable [Message] identity. AddMessageTypesFromThisAssembly() registers their generated JSON metadata. Start the host before sending. Service Bus routes default to the AMQP cloudEvents: header binding; creation context uses traceparent and tracestate. Named options support the Options pattern. Set ClientOptions to the native ServiceBusClientOptions; Relayer passes it unchanged to the SDK. Relayer owns connection-string clients and disposes them with the provider. Alternatively, set Client to an existing ServiceBusClient, including a client constructed with TokenCredential. Relayer disposes its cached senders and leaves that client open. Configure exactly one of ConnectionString or Client; do not mutate captured native options after startup.

Declare destinations with native CreateQueueOptions or CreateTopicOptions in Queues or Topics. Every send destination needs exactly one declaration. Send destinations match native queue and topic names case-insensitively. Queue consumer sources also match native queue names case-insensitively. Declare each native name once, even if routes or queue consumers use different casing. Native CreateSubscriptionOptions in Subscriptions declare subscriptions of a declared topic. Every consumer needs a matching native queue or topic/subscription declaration.

Add these declarations in bus.Options.Configure by constructing the native options, as in the example above. The standard .NET configuration binder cannot construct the entries in Queues, Topics, or Subscriptions through bus.Options.Bind. You can still bind supported settings such as AutoStartConsumers.

On the same named Service Bus transport, each message type can route to a queue or topic only once, even if the routes use different casing. Startup rejects duplicates across fixed routes and the destinations listed for selector routes. A selector must return a destination exactly as listed.

With TopologyMode.None, routes and consumers already name their entities, so these declarations are optional. A consumer implies its queue, or with Subscription set, its topic and subscription; SessionProcessorOptions implies RequiresSession = true. A send destination is implied as a queue, or as a topic when its name is in TrustedSessionEntities; the native sender is the same for both. An explicit declaration always wins, so declare an entity to override these defaults. Implied entities are not added to your options. The infrastructure export renders only explicit declarations, so it fails when a route or consumer names an undeclared entity rather than emitting incomplete infrastructure; declare every entity to export it:

builder.ServiceBus("bus", bus =>
{
bus.TopologyMode = TopologyMode.None; // topology owned by IaC
bus.Options.Configure(options => options.ConnectionString = connectionString);
bus.Send<OrderPlaced>("orders");
bus.Consume("orders", consumer =>
{
consumer.Handler<OrderPlacedHandler>();
consumer.Options.Subscription = "billing";
});
});

For single and batch sends, the serializer's content-type header maps to native ServiceBusMessage.ContentType, as required by the CloudEvents AMQP binary binding. It is not copied to application properties. The serializer's media type takes precedence over a native callback's ContentType; when no content-type header is present, a callback may set the native property.

Hosted Service Bus consumers read the cloudEvents: prefix. Use that prefix for CloudEvents routes that send a message type to its hosted queue or topic subscription on the same named transport. Startup rejects a custom SendRouteOptions.HeaderNames prefix for those routes in every topology mode. Routes to external consumers can still use a custom prefix.

Native Subject (sys.Label) carries the CloudEvents type, for example orders.placed;v=1 (stable name and version), on direct sends, batches and outbox dispatch. Topic subscriptions can then select message types with a native correlation filter such as new CorrelationRuleFilter { Subject = "orders.placed;v=1" } instead of an application-property SQL filter.

The type also stays in the cloudEvents:type application property. A native callback that sets Subject takes precedence. Raw-envelope routes have no type and leave Subject unset. Set options.SubjectFromMessageType = false to keep Subject empty unless a callback sets it.

Topology and permissions​

Validate is the default. Send destinations use an empty native message batch to open an authenticated send link, then dispose the batch and sender. No probe message is sent. Queues with consumers and declared subscriptions use native peek operations without locking, consuming or settling messages. A queue used for both sending and receiving gets both checks. Receive-only topic consumers validate their subscriptions without opening a topic send link. Send destinations need Send rights, and receive checks need Listen rights. Validate never constructs or calls the administration client, even when one is supplied.

This verifies endpoint accessibility and existence. It cannot compare entity settings or distinguish a queue from a topic through a send link. Service Bus requires Manage rights to read entity descriptions. Native entity options specify what Provision should create and compare. Unless explicitly trusted with TrustedSessionEntities (below), Validate reports declared RequiresSession = true as unresolved and fails startup. It never infers session support from a name. Manage permission is never an automatic fallback.

Use bus.TopologyMode = TopologyMode.Provision explicitly to create missing entities through the native administration APIs and verify their properties. Existing queues, topics and subscriptions are never updated or deleted. Remaining differences name the entity, property, expected value and actual value. Relayer handles a concurrent create by reading and comparing the winning entity. Other SDK failures and cancellation propagate.

Provision compares a queue or topic's duplicate detection window when either the declaration or the broker has duplicate detection enabled. It always compares the enablement setting. For queues, topics, and subscriptions, the SDK omits DefaultMessageTimeToLive when its value is TimeSpan.MaxValue. Provision lets the broker choose the TTL in that case. Set an explicit duration to check it for drift.

Provision uses AdministrationClient when supplied. With an owned connection-string client it otherwise creates a native administration client using that connection string. With a caller-owned messaging client, supply an administration client explicitly; Relayer cannot extract credentials from the messaging client. The Service Bus emulator has a separate management endpoint: use its HTTP connection string for the administration client. Native administration clients are caller-owned and the SDK does not require disposal.

TopologyMode.None skips broker topology checks and writes, and implies undeclared entities as described above. Native scheduling and batching remain available. Portable OrderingKey requires either Provision with verified session declarations or explicit trust in Validate and None. Consumers support native immediate abandon and dead-lettering. No transaction support or namespace-dependent message-size limit is advertised.

Register receiving​

Add a consumer to the same named transport declaration:

bus.Consume("orders", consumer => consumer.Handler<OrderPlacedHandler>());

Relayer composes generated handlers, default JSON serialization, the core failure pipeline, process telemetry and native settlement before starting the processor. The default native ServiceBusProcessorOptions uses PeekLock, one concurrent callback and AutoCompleteMessages = false. Successful handling completes the message only after durable work and asynchronous scope disposal. Poison messages go to the native dead-letter queue without stopping healthy messages.

Pass native options through consumer.Options.ProcessorOptions. Set AutoCompleteMessages = false on replacement options. ReceiveAndDelete, auto-complete, undeclared entities (in Validate and Provision) and batch handlers fail at startup. The portable concurrency declaration must match the native concurrency; Relayer never rewrites native options:

bus.Consume("orders", consumer =>
{
consumer.Options.ProcessorOptions = new ServiceBusProcessorOptions
{
AutoCompleteMessages = false,
MaxConcurrentCalls = 8,
PrefetchCount = 32,
};
consumer.Handler<OrderPlacedHandler>().Concurrency(8);
});

For a topic subscription, consume the topic name and set consumer.Options.Subscription to the declared subscription name. Startup and infrastructure export match both names case-insensitively. Declare each subscription once per topic. In TopologyMode.None, a matching declaration still supplies the native settings for validation.

Native processors own delivery, prefetch, lock renewal and reconnect. Shutdown stops new receives and drains callbacks through settlement; the host's shutdown cancellation token bounds the drain. A stopped processor can restart on the same host services.

The SDK cancels its callback token as soon as processor shutdown starts. The composed pipeline uses context.CancellationToken for durable work and settlement; Relayer cancels it only when the host's drain deadline expires or the adapter is disposed. The original callback and its native token remain unchanged. Use the context token when draining application work.

Start consumers later​

Processors start with the host by default. Set AutoStartConsumers = false to bring the host up first, for example while other infrastructure creates the queues and subscriptions, and start the processors yourself through the transport's keyed IConsumerControl:

using Microsoft.Extensions.DependencyInjection;
using Relayer;
using Relayer.Transports;

bus.TopologyMode = TopologyMode.None; // the entities may not exist yet when the host starts
bus.Options.Configure(options => options.AutoStartConsumers = false);

// After the host starts and the entities exist.
var consumers = app.Services.GetRequiredKeyedService<IConsumerControl>("orders");
await consumers.StartAsync(cancellationToken);

The host still composes and validates every consumer at startup, and sending works while the processors are stopped. StartAsync fails until the host signals ApplicationStarted and after the host stops. StopAsync stops new receives and drains in-flight work like host shutdown, so the same control pauses and resumes consumption, for example during a maintenance window; both calls do nothing when the processors are already in that state. Host shutdown stops processors that the control started. While the processors are stopped, consumer health reports them as unavailable, not idle. Leave TopologyMode at Validate when the entities exist before startup; it checks them before the host starts.

If any processor fails to start, Relayer attempts to stop all processors on that transport and waits for those attempts before returning the original startup error. Relayer logs any rollback failures when logging is configured.

Configure the common FailureOptions with consumer.Failures(policy). In-process retry delays and immediate durable redelivery use the core policy and native abandon. Service Bus has no delayed abandon operation: positive RedeliveryDelays fail at startup rather than broadcasting a retry copy to every topic subscription. Delayed redelivery requires a separately designed durable executor; it is not implemented by this adapter.

Publish byte payloads as AMQP Data bodies. Relayer treats AMQP Value and Sequence bodies as malformed envelopes before dispatching a handler. By default, the failure policy dead-letters the original native message on the first processing attempt. The dead-letter description names the unsupported AMQP body layout.

When you declare a native queue or subscription, set its MaxDeliveryCount to at least one plus the number of entries in RedeliveryDelays. The first delivery uses one attempt, and each immediate abandon needs another delivery. For example, RedeliveryDelays = [TimeSpan.Zero] requires MaxDeliveryCount of at least 2. In-process RetryDelays run on the same delivery and use no additional broker attempts. Startup checks the declared limit in every topology mode. With TopologyMode.None and an implied entity, the limit is unknown, so configure the broker to allow enough attempts. Lock expiry and other abandons can use more of the broker's budget.

Inside a handler, context.ServiceBus() exposes the unchanged received message and native callback. CompleteAsync, AbandonAsync, DeferAsync and DeadLetterAsync call the SDK, use the context cancellation token and suppress automatic settlement only after success. Native defer retains the message for later retrieval by sequence number. Persist that sequence number before deferring. See manual native settlement.

Session processors​

Declare RequiresSession = true on the native queue or subscription and supply SessionProcessorOptions to select ServiceBusSessionProcessor:

bus.Consume("customer-orders", consumer =>
{
consumer.Options.SessionProcessorOptions = new ServiceBusSessionProcessorOptions
{
AutoCompleteMessages = false,
MaxConcurrentSessions = 8,
MaxConcurrentCallsPerSession = 1,
};
consumer.Handler<OrderPlacedHandler>().Concurrency(8);
});

The common concurrency must equal MaxConcurrentSessions * MaxConcurrentCallsPerSession. Use one callback per session when the handler depends on ordered completion. Relayer passes these native options unchanged and adds no ordering layer. context.ServiceBus().SessionCallback exposes the original ProcessSessionMessageEventArgs, including native session state and session lock operations. Callback is for non-session deliveries; using it on a session delivery fails with an actionable error. The common native settlement helpers work for both processor kinds.

Receive telemetry​

Each callback has a process span. Each actual completion, abandon, defer or dead-letter operation has a child settle span, including manual helper settlement. Spans use messaging.system = servicebus, subscription name, native delivery count, enqueued time and disposition status. The SDK's messaging ActivitySource is experimental and requires its own opt-in. Set AZURE_EXPERIMENTAL_ENABLE_ACTIVITY_SOURCE=true before process startup and subscribe to Azure.Messaging.ServiceBus.* to add native spans beneath the active Relayer settlement operation. Relayer does not mutate that process switch. See the Azure SDK tracing sample. Original traceparent and tracestate application properties remain creation context.

Health​

Install Relayer.AzureServiceBus.HealthChecks and register the exact, case-sensitive transport key before host startup:

using Relayer.AzureServiceBus;
using Relayer.HealthChecks;

services.AddRelayerServiceBusHealthProviders("orders");
services.AddRelayerHealthChecks();

The companion registers ITransportHealthProbe and IConsumerHealthProvider under that key. Connectivity uses native read-only operations and never sends or consumes a probe message. Consumer snapshots belong to each exact declaration. An idle consumer has no active callback; an active consumer reports its oldest active worker's progress time, so a busy worker cannot hide a stalled sibling. Stopped or unavailable receivers do not report a false healthy idle state. Configure the common health check stall threshold for your workload.

The Testcontainers suite uses Floci Azure 0.18.0 with an Apache Artemis sidecar. It covers queues, topic subscriptions, session ordering and manual settlement, native retry/dead-lettering, scheduled sends, creation context, drain/restart, bounded outage sends and offline shutdown, read-only health and connection recovery. Immediate native redelivery passes; positive delayed redelivery is an explicit capability skip. Emulator results do not establish production throughput, Azure identity/RBAC behavior, transaction support or service tier quotas.

Emulator entities created through the administration endpoint do not survive an emulator container restart. Recovery tests declare their owned endpoints in the emulator's startup configuration so the same client and processors reconnect to the same configured endpoint names. A separate test proves an administration-created queue is absent after restart, reapplies Provision, and verifies send/receive/complete on the recreated queue. Applications reapply topology explicitly; native reconnect does not itself rerun provisioning. This test setup does not add topology persistence to the adapter or imply that a managed namespace loses entities on restart.

Sessions, scheduling and native callbacks​

For native ordered queue sends, declare CreateQueueOptions("orders") { RequiresSession = true }, choose Provision (or explicitly trust the entity below) and configure route.OrderingKey(order => order.CustomerId). For a topic, either declare its session-enabled subscriptions (every declared subscription must require sessions before the topic advertises ordering), or trust the topic itself as shown below. Relayer maps the resolved ordering key to native SessionId. Broker sessions and the receiver determine ordering; Relayer adds no ordering layer.

For externally provisioned topology and a runtime identity without Manage rights, explicitly trust each native session declaration:

bus.Options.Configure(options =>
{
options.Queues.Add(new CreateQueueOptions("orders") { RequiresSession = true });
options.TrustedSessionEntities.Add("orders");
});
bus.Send<Order>("orders", route => route.OrderingKey(order => order.CustomerId));

This works with the default TopologyMode.Validate and with None. TrustedSessionEntities matches native names case-insensitively. The queue advertises ordering by key, so ordered outbox sends retain their key and receive lanes through the existing outbox integration. Validate still opens its Send link without an administration call. For a topic, set RequiresSession = true on every declared subscription and trust each full path, for example options.TrustedSessionEntities.Add("events/Subscriptions/audit"). A send-only application does not peek trusted subscriptions; a declared consumer still needs Listen rights and validates its receive link. Undeclared subscriptions cannot be discovered with Send rights, so the application owns the completeness of these declarations.

A producer usually does not own its topic's subscriptions: consumers add their own, often from other deployments. Trust the topic name instead of listing each subscription:

bus.Options.Configure(options =>
{
options.Topics.Add(new CreateTopicOptions("orders"));
options.TrustedSessionEntities.Add("orders");
});
bus.Send<Order>("orders", route => route.OrderingKey(order => order.CustomerId));

This asserts that every subscription on the topic, declared or not, requires sessions, so the topic advertises ordering by key and new consumers need no producer change. A declared subscription on a trusted topic must also set RequiresSession = true; startup rejects one that does not. Provision lists the topic's subscriptions with its administration client and reports each one that does not require sessions.

Trust is an assertion, not broker configuration or verification. Names must match session-enabled native queue or subscription declarations, or a declared topic. Provision always compares the actual RequiresSession setting and reports drift, even for trusted entities. Without trust, Validate/None retain the unresolved-ordering error. A broker rejects a message without SessionId sent to a session-enabled queue or subscription; a message with SessionId is accepted by a non-session entity, so a false assertion can silently lose ordering. Topic fan-out follows each subscription's native behavior. Keep infrastructure definitions and declarations aligned; send-only startup cannot detect this mismatch.

SendOptions.DeliverAt becomes native ScheduledEnqueueTime. Native callbacks run once for each message before native admission:

await sender.SendAsync(order, options => options.ServiceBus(message =>
{
message.Subject = "priority";
}));

Callbacks receive the SDK's own ServiceBusMessage and may set session IDs, partition keys and other native properties. Do not retain the borrowed native message. Original CloudEvents and creation-context application properties take precedence over callback changes; a callback's Subject takes precedence over the message type.

Per-send callbacks cannot be persisted in the outbox. To set native properties on outbox sends too, declare a callback on the route. It is code on the route, not data on the row, so outbox dispatch applies it to stored rows as well as to direct sends and batches:

bus.Send<OrderPlaced>("orders", route => route.ServiceBus(static (outgoing, message) =>
{
message.TimeToLive = TimeSpan.FromHours(1);
message.ReplyTo = "order-replies";
}));

The route callback receives the serialized send (Destination, Body, Headers, OrderingKey) and the native ServiceBusMessage, not the message object, so dispatch never deserializes a stored row. To derive a native property from message content, carry the value in a header. The callback runs before a per-send callback, which can override it, and before the CloudEvents application properties and ContentType are written, which take precedence. Use a static lambda, do not retain either argument, and declare one callback per route.

Dispatch identifies the route by its CloudEvents type header, so the route must use the CloudEvents envelope; a route callback on a Raw-envelope route fails at startup, as does one declared on a route of another transport.

The CloudEvents id (cloudEvents:id, or the id header of a route's custom HeaderNames) also becomes native MessageId on every send, including outbox redispatch, scheduled copies and dead-letter copies. On a queue or topic with RequiresDuplicateDetection = true, the broker drops a resend of the same message within its duplicate-detection window, such as an outbox row dispatched again after a crash before it was marked sent. A native callback that sets MessageId keeps its value.

Serialized transport batches use CreateMessageBatchAsync, TryAddMessage and SendMessagesAsync. Relayer splits batches at the native byte limit, destination changes, and native session/partition key changes, preserving input order. A message too large for an empty native batch fails with MessageSizeExceeded; use individual sending when a namespace permits single messages larger than its batch limit. All calls pass cancellation to the SDK. Batches are not atomic: cancellation or failure can follow successful earlier batches.

Relayer emits send spans with messaging.system = servicebus; enabling the Azure SDK's activity source adds native spans. The original traceparent and tracestate application properties remain creation context.

Performance validation​

ServiceBusSendBenchmarks compares acknowledged single sends and 32-message batches against the raw SDK, including native message and header preparation, with MemoryDiagnoser. Its DefaultJson category also compares the real ISender.SendAsync path with direct SDK sending: both serialize the same model using the registered default JSON serializer and write a fresh CloudEvents envelope with changing IDs and timestamps. The raw baseline writes text directly into native application properties; Relayer also includes routing, its reusable UTF-8 envelope storage and send pipeline. The Single and Batch categories use pre-serialized bodies and isolate the transport adapter.

Set RELAYER_SERVICEBUS_CONNECTION_STRING to a test namespace with Manage rights. The benchmark creates and deletes a fresh non-session queue per case, so earlier cases do not change the next case's queue depth. Hosted namespaces use the SDK's default queue size. For the emulator, also set RELAYER_SERVICEBUS_ADMIN_CONNECTION_STRING to its HTTP management connection string, including UseDevelopmentEmulator=true; this selects the emulator's 100 MB queue size:

dotnet run -c Release --project benchmarks/Relayer.Benchmarks -- --filter "*ServiceBusSendBenchmarks*"

Native message containers and batch lease lists are pooled. Serialized body memory is borrowed without a body copy, and batch leases remain live through native send completion and batch disposal. Text application properties and session IDs require SDK strings. Relayer materializes a string for every nonempty UTF-8 WireString value before calling the SDK. The default send envelope stores its ID, type, source, timestamp and specification version as UTF-8, so all five conversions allocate even after warming the message pool. Tracing can add further conversions. D52 classifies strings required by the native SDK as native-client cost when the raw-client comparison allocates at least as much. Relayer must still add zero pipeline allocations beyond native-required objects and application lifetime costs.

The registered serializer's content type is already a string, so the normal content-type mapping reuses that string. Explicit UTF-8 content types require conversion. The upstream API request tracks borrowed UTF-8 text support: passing bytes as application-property values currently produces AMQP binary values, which would change the CloudEvents wire types. Relayer keeps the supported string conversion and native AMQP encoder until a compatible native API exists; it does not cache changing IDs or mutate immutable strings.

ServiceBusSendAllocationTests asserts that warmed preparation adds 0 B over equivalent raw SDK message preparation (borrowed body, native content type and application properties), with changing IDs/timestamps, stored creation context and alternating registered serializer content types. It also checks that reusing caller buffers after lease release cannot change native snapshots and that clearing a pooled message detaches the borrowed body. This allocation comparison pins the native-required conversions tracked in #295 under D52.

ServiceBusHeaderBenchmarks isolates these conversions from SDK/network and serializer allocations. It writes the actual envelope with changing IDs and timestamps on every invocation, for both a single route and alternating routes, with and without the stored trace creation context, and compares direct native property assignment with the adapter. The raw baseline performs the same string conversions; D52 uses that comparison to attribute native-required objects while retaining the pipeline budget. The DefaultJson acknowledged comparison can measure the full pipeline's overhead, but adding a benchmark does not establish the 5% throughput budget: that requires measurements against the same native client and environment. Receive benchmarks and transport conformance tests also cover native processors.

ServiceBusReceiveBenchmarks compares raw SDK processor callbacks with the composed Relayer receive pipeline, using the same source-generated default JSON and native completion. Each iteration seeds 128 messages outside the timed region, then measures processor start and handler progress for both cases. Both signal progress before native completion; iteration cleanup drains the final settlement and stops the processor outside timing. This excludes at most the last settlement from each measured workload. Both clients use the same three-second native try timeout. It uses MemoryDiagnoser and the same namespace environment variables as sending. Run --filter "*ServiceBusReceiveBenchmarks*"; use an isolated test namespace. Startup, the final settlement boundary and emulator latency limit its use as a steady-state comparison; these numbers do not establish the production budget.

Receive allocations​

The native processor callback must return a single-use Task. When processing suspends, Relayer's ValueTask.AsTask() bridge allocates 80 B per delivery in the measured .NET 10 arm64 configuration; a raw asynchronous SDK callback allocates 128 B. D52 counts this required object as native-client cost. Health tracking uses a pooled frame and adds no allocation.

Default scoped handlers also allocate their Microsoft DI scope, service cache and handler instance (measured at 336 B + 24 B). These are application lifetime costs under D52. Singleton-only consumers open a delivery scope only when context.Services is read, so handlers that do not need that scope avoid its allocation.

The retained measurements show Relayer adds 288 B per delivery over the raw SDK callback for a scoped handler, 305 B with eight concurrent workers, and -48 B for a singleton handler. These totals include the native-required Task and application lifetime costs above; they are diagnostic measurements, not universal runtime sizes. ServiceBusReceiveBenchmarks pins the raw-client comparison, and FailureConsumerScopeBenchmarks and the consume-pipeline allocation tests pin scope behavior.

Inspect and redrive dead letters​

Each named Service Bus transport registers a keyed IDeadLetterManager. It lists configured consumer sources, not every entity in the namespace. Queue IDs are queue names; subscription IDs are topic/Subscriptions/subscription. Redrive sends to the queue or original topic.

using Microsoft.Extensions.DependencyInjection;
using Relayer;

var manager = provider.GetRequiredKeyedService<IDeadLetterManager>("orders");
var sources = manager.List();
var page = await manager.PeekAsync("orders", maxMessages: 20, cancellationToken: token);
var next = await manager.PeekAsync("orders", 20, page.NextCursor, token);

var redriven = await manager.RedriveAsync("orders",
message => message.MessageId == selectedMessageId,
maxMessages: 20, cancellationToken: token);
var removed = await manager.PurgeAsync("orders", maxMessages: 20, cancellationToken: token);

Peek is non-destructive. For an AMQP Data body, HasByteBody is true and Body contains an owned snapshot, including when the payload has zero bytes. For AMQP Value and Sequence bodies, HasByteBody is false and Body is empty. ID-based filters work for all three layouts. Features.GetRequired<ServiceBusReceivedMessage>() exposes native metadata, including the sequence number and dead-letter reason. Use GetRawAmqpMessage().Body on that native message to inspect a Value or Sequence body.

Peek cursors are opaque continuations for one source. Stop when a page is empty. Concurrent activity can change the results between pages.

Redrive and purge receive one native PeekLock batch, bounded to 1–100 deliveries (default 20); the broker can return fewer. Redrive filters that batch, not the entire queue. Unmatched deliveries are abandoned unchanged. Repeat maintenance calls deliberately; do not loop until a filtered call returns zero and assume that the source has no other matches. Deferred DLQ messages require the native deferred-receive API and are not included in these maintenance batches. Session-enabled entities use the same native dead-letter subqueue access.

Redrive uses the SDK's received-message copy constructor, preserving the AMQP body layout, message ID, session and partition keys, application properties and creation trace context. The SDK omits the broker's dead-letter reason and error description from the copy. It clears broker lifetime timestamps so the original TTL applies to the new enqueue. The send must succeed before the original is completed. A failed send or filter leaves the original in the DLQ. On failure or cancellation the manager attempts to abandon the failing delivery and all unprocessed deliveries without using the cancelled token. Locks that cannot be released can remain until expiry. If the send succeeds and completion fails, repeating can duplicate the message. Keep handlers idempotent. Native duplicate detection can suppress a re-send with the same message ID within its configured window; choose the namespace/entity policy accordingly. Redriving a subscription sends to its topic, so every matching subscription can receive the copy.

Purge permanently completes received DLQ messages without sending a copy. Concurrent consumers or administrators can change results, and cancellation or failure can leave partial progress. Exceptions propagate; these APIs do not promise atomic maintenance or an exact message count. Use an application timeout appropriate for the entity's native lock duration, and avoid long-running filters. Relayer does not run a parallel lock-renewal loop. The manager shares the transport client and never disposes a caller-owned client.

If peek, redrive or purge fails, the manager returns the original error even if a native link fails to close. The Service Bus SDK reports each close failure through its Azure-Messaging-ServiceBus event source (ClientCloseExceptionCore). If the operation succeeds and a link fails to close, the manager returns the first close error.

Optional HTTP management​

Install Relayer.AspNetCore in the ASP.NET Core application. These shared endpoints work with the named IDeadLetterManager registered by either Service Bus or Kafka. Map the transport's registered name and apply the application's authorization policy to the returned group:

using Relayer.AspNetCore;

builder.Services.AddAuthentication(/* application authentication scheme */);
builder.Services.AddAuthorization(options =>
options.AddPolicy("dead-letter-operators", policy =>
policy.RequireAuthenticatedUser().RequireRole("MessagingOperator")));
// Register the named Service Bus transport "bus" as above.
var app = builder.Build();
app.UseAuthentication();
app.UseAuthorization();
app.MapRelayerDeadLetters("bus", "/ops/dead-letters")
.RequireAuthorization("dead-letter-operators");

Authentication remains application-owned. Mapping alone does not install authentication or authorization; protect this surface because it exposes message bodies and destructive operations. The manager name is checked when routes are mapped, so register the transport before building the app.

Method and relative pathInputResult
GET /NoneConfigured { id, destination } sources
GET /messagesQuery: source, optional maxMessages and opaque cursor{ messages: [{ messageId, body }], nextCursor }
POST /redriveJSON: source, optional maxMessages and messageIds{ affectedMessages }
POST /purgeJSON: source, optional maxMessages{ affectedMessages }

body contains base64-encoded bytes when available, including an empty string for a zero-byte body. Service Bus AMQP Value and Sequence bodies appear as null. The response contains only the portable snapshot fields. Supply source IDs as query or JSON values, including subscription IDs such as events/Subscriptions/audit. URL-encode query values. All operations default to 20 messages and accept 1–100. Unknown JSON properties are rejected to catch misspelled filters. Invalid input and manager argument validation return 400 with an { error } explanation; POST requests require JSON (415 otherwise). Request cancellation reaches the manager; broker failures propagate to application exception handling.

For example, redrive selected IDs from one bounded scan:

POST /ops/dead-letters/redrive
Content-Type: application/json

{"source":"orders","maxMessages":100,"messageIds":["order-42","order-43"]}

The non-empty messageIds array accepts up to 100 IDs and matches them exactly (case-sensitive). Omit it to redrive all messages encountered in that scan. The scan examines at most maxMessages deliveries, not that many matches; unmatched messages remain in the DLQ. Purge permanently removes up to the bound and does not accept filters. Concurrent operations may see different messages. Redrive remains at-least-once, can partially succeed before failure or cancellation, and topic redrive can fan out to every subscription as described above. Inspect the source before retrying a failed maintenance request.

These endpoints use source-generated JSON and support Native AOT without adding ASP.NET dependencies to core. For Kafka, pass the Kafka transport's name to MapRelayerDeadLetters and use a configured dead-letter topic, such as orders.dead-letter, as source. The same routes, request limits and application-owned authorization apply; maintenance retains the Kafka manager's broker-specific semantics. Kafka redrive advances its dedicated group's offsets without deleting originals; an unmatched record pauses its partition for that call. Purge deletes partition prefixes for all consumer groups, rather than selected records. HTTP inspection exposes only message IDs and body bytes: it cannot distinguish a Kafka tombstone from an empty body or expose KafkaDeadLetterFeature.Record. Use the manager directly when inspection needs that native metadata; redrive still preserves tombstones.

Consumer failure circuit​

Set consumer.Options.CircuitBreaker to ConsumerCircuitOptions to stop queue or session processing when its failure rate crosses a threshold. Native redelivery retains failed deliveries while open, and one half-open probe controls recovery. See consumer circuit breaking for configuration, settlement and native delivery-limit behavior.

Abandoning a failed delivery and allowing a rejected delivery's lock to expire both consume native delivery attempts. During a prolonged outage, Service Bus can dead-letter messages at MaxDeliveryCount even while the circuit is open. Size BreakDuration together with the queue or subscription's MaxDeliveryCount and expected outage duration; circuit retention does not override broker limits or message expiry.

If a native circuit stop or recovery start fails, consumption stops and the failure is logged and retained for shutdown. With AddRelayerServiceBusHealthProviders and AddRelayerHealthChecks registered, consumer health reports the failure as unhealthy. Recreate the application host to resume consumption.

API naming​

The transport's client options are ServiceBusTransportOptions, matching relayer.ServiceBus(...) and ServiceBusConsumerOptions. The receive binding is ServiceBusMessageBinding (or ServiceBusSessionMessageBinding for sessions). These replace the former AzureServiceBusTransportOptions ServiceBusBinding and ServiceBusSessionBinding names; update explicit type references when upgrading.

Send pool retention​

Each Service Bus endpoint retains at most 256 idle message containers and 256 batch lease lists. Concurrent sends above either bound remain supported and allocate temporary overflow state on each burst. Borrowed body memory is cleared after native completion, including batch disposal, before retention or collection. The bound limits object count, not bytes: retained state can keep storage sized for its largest batch. There is no retention option.

TransportPoolRetentionBenchmarks measures warmed pool reuse at concurrency 1 and 256 and repeated 512-operation bursts with MemoryDiagnoser. Measurements isolate pooling from broker I/O and do not establish whole-path throughput parity.