Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/Relayer.AzureServiceBus/ServiceBusBinding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,17 @@ public static ReadOnlySequence<byte> GetBody(ProcessMessageEventArgs message)
public static int? GetDeliveryCount(ProcessMessageEventArgs message) => message.Message.DeliveryCount;

/// <inheritdoc />
/// <remarks>Non-text application properties remain available through the native message.</remarks>
/// <remarks>
/// The logical content-type header uses the native AMQP ContentType when present, with application properties as fallback.
/// Non-text application properties remain available through the native message.
/// </remarks>
public static bool TryGetHeader(ProcessMessageEventArgs message, string name, out WireString value)
{
if (name == "content-type" && message.Message.ContentType is { } contentType)
{
value = new(contentType);
return true;
}
if (message.Message.GetRawAmqpMessage().ApplicationProperties.TryGetValue(name, out var native))
{
switch (native)
Expand Down
10 changes: 9 additions & 1 deletion tests/Relayer.Tests/ServiceBusBindingEmulatorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ public async Task Native_settlement_round_trips_against_service_bus_emulator()

foreach (var operation in new[] { "complete", "abandon", "defer", "dead_letter" })
{
var outgoing = new ServiceBusMessage(new BinaryData(new byte[] { 42 })) { MessageId = operation };
var outgoing = new ServiceBusMessage(new BinaryData(new byte[] { 42 }))
{
MessageId = operation,
ContentType = "application/cbor",
};
outgoing.ApplicationProperties["cloudEvents:id"] = operation;
outgoing.ApplicationProperties["cloudEvents:type"] = "servicebus.payload;v=1";
outgoing.ApplicationProperties["traceparent"] = "00-0123456789abcdef0123456789abcdef-0123456789abcdef-01";
Expand All @@ -56,6 +60,10 @@ public async Task Native_settlement_round_trips_against_service_bus_emulator()
{
await Assert.That(context.MessageId).IsEqualTo(operation);
await Assert.That(context.Attempt).IsEqualTo(1);
await Assert.That(context.ServiceBus().Message.ContentType).IsEqualTo(outgoing.ContentType);
await Assert.That(context.ServiceBus().Message.ApplicationProperties.ContainsKey("content-type")).IsFalse();
await Assert.That(context.Headers.TryGetValue("content-type", out var contentType)).IsTrue();
await Assert.That(contentType.Text).IsEqualTo(outgoing.ContentType);
await Assert.That(ServiceBusBinding.GetBody(context.ServiceBus().Callback).First.Span[0]).IsEqualTo((byte)42);
await Settle(context.ServiceBus(), operation, properties);
};
Expand Down
47 changes: 46 additions & 1 deletion tests/Relayer.Tests/ServiceBusBindingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,53 @@ public async Task Binding_borrows_native_body_and_reads_only_requested_text_or_b
}

[Test]
public async Task Binding_does_not_allocate_after_warmup()
[Arguments("application/cbor", null)]
[Arguments("application/cbor", "application/json")]
[Arguments("", "application/json")]
[Arguments(null, "application/json")]
[Arguments(null, null)]
public async Task Binding_reads_native_content_type_before_application_property(string? native, string? fallback)
{
var properties = new Dictionary<string, object> { ["other"] = "unchanged" };
if (fallback is not null) properties["content-type"] = fallback;
var message = ServiceBusModelFactory.ServiceBusReceivedMessage(contentType: native, properties: properties);
var args = new ProcessMessageEventArgs(message, new RecordingReceiver(), "content-type", default);

var found = ServiceBusBinding.TryGetHeader(args, "content-type", out var value);
var expected = native ?? fallback;
await Assert.That(found).IsEqualTo(expected is not null);
await Assert.That(value.Text).IsSameReferenceAs(expected);
await Assert.That(ServiceBusBinding.TryGetHeader(args, "other", out value)).IsTrue();
await Assert.That(value.Text).IsEqualTo("unchanged");
await Assert.That(message.ContentType).IsEqualTo(native);
await Assert.That(message.ApplicationProperties.ContainsKey("content-type")).IsEqualTo(fallback is not null);
}

[Test]
public async Task Native_content_type_is_visible_through_context_headers()
{
var args = Args(new RecordingReceiver());
args.Message.GetRawAmqpMessage().Properties.ContentType = "application/cbor";
await using var provider = Provider();
var handled = false;
provider.GetRequiredService<Control>().Handle = async context =>
{
await Assert.That(context.Headers.TryGetValue("content-type", out var value)).IsTrue();
await Assert.That(value.Text).IsEqualTo("application/cbor");
handled = true;
};
await Pipeline(provider).ProcessAsync(args);
await Assert.That(handled).IsTrue();
}

[Test]
[Arguments(true)]
[Arguments(false)]
public async Task Binding_does_not_allocate_after_warmup(bool nativeContentType)
{
var args = Args(new RecordingReceiver());
if (nativeContentType) args.Message.GetRawAmqpMessage().Properties.ContentType = "application/cbor";
else args.Message.GetRawAmqpMessage().ApplicationProperties["content-type"] = "application/cbor";
for (var i = 0; i < 1_000; i++) Read(args);
var before = GC.GetAllocatedBytesForCurrentThread();
for (var i = 0; i < 10_000; i++) Read(args);
Expand All @@ -64,6 +108,7 @@ static void Read(ProcessMessageEventArgs args)
_ = ServiceBusBinding.GetBody(args);
_ = ServiceBusBinding.GetDeliveryCount(args);
if (!ServiceBusBinding.TryGetHeader(args, "cloudEvents:type", out _)) throw new InvalidOperationException();
if (!ServiceBusBinding.TryGetHeader(args, "content-type", out _)) throw new InvalidOperationException();
}
}

Expand Down
2 changes: 1 addition & 1 deletion website/docs/transports/service-bus-receive.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,6 @@ await native.DeferAsync();

`Callback` exposes `ProcessMessageEventArgs` for other native operations, including lock renewal. If you settle through `Callback` yourself, set `context.Features.GetRequired<NativeMessage<ProcessMessageEventArgs>>().Disposition` to `Disposition.SettledByHandler` after success. The view, callback, message, body and header storage are borrowed for the current processing call; never retain them after it returns. This binding is for ordinary processor callbacks; session processor callbacks are separate planned work.

`ServiceBusBinding` reads only the requested AMQP application property. Text stays text; binary values stay borrowed UTF-8 memory. Missing properties and other native value types return `false`; access numeric and other typed values through `native.Message.ApplicationProperties`. CloudEvents attributes use the `cloudEvents:` prefix; creation context remains in `traceparent` and `tracestate`. No SDK tracing header is substituted for the creation context.
`ServiceBusBinding` reads only the requested header. The logical `content-type` header comes from the native `ServiceBusReceivedMessage.ContentType` property, as required by the CloudEvents AMQP binding. When the native property is absent, the binding falls back to an application property named `content-type`; when both exist, the native property wins. Other headers come from AMQP application properties. Text stays text; binary values stay borrowed UTF-8 memory. Missing properties and other native value types return `false`; access numeric and other typed values through `native.Message.ApplicationProperties`. CloudEvents attributes use the `cloudEvents:` prefix; creation context remains in `traceparent` and `tracestate`. No SDK tracing header is substituted for the creation context.

For an indexed single AMQP data section, the binding borrows the original body memory and creates no allocation. Other body layouts use the SDK's `Body` conversion, preserving native behavior; that conversion may allocate and join multiple sections. Header reads use the raw AMQP property dictionary to avoid the SDK's per-access read-only dictionary wrapper.
Loading