diff --git a/src/Relayer.AzureServiceBus/ServiceBusBinding.cs b/src/Relayer.AzureServiceBus/ServiceBusBinding.cs index 88c9154f7..48b68765c 100644 --- a/src/Relayer.AzureServiceBus/ServiceBusBinding.cs +++ b/src/Relayer.AzureServiceBus/ServiceBusBinding.cs @@ -25,9 +25,17 @@ public static ReadOnlySequence GetBody(ProcessMessageEventArgs message) public static int? GetDeliveryCount(ProcessMessageEventArgs message) => message.Message.DeliveryCount; /// - /// Non-text application properties remain available through the native message. + /// + /// 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. + /// 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) diff --git a/tests/Relayer.Tests/ServiceBusBindingEmulatorTests.cs b/tests/Relayer.Tests/ServiceBusBindingEmulatorTests.cs index cf4ff2127..e9d5951b9 100644 --- a/tests/Relayer.Tests/ServiceBusBindingEmulatorTests.cs +++ b/tests/Relayer.Tests/ServiceBusBindingEmulatorTests.cs @@ -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"; @@ -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); }; diff --git a/tests/Relayer.Tests/ServiceBusBindingTests.cs b/tests/Relayer.Tests/ServiceBusBindingTests.cs index 99ac5e365..ad6c98faf 100644 --- a/tests/Relayer.Tests/ServiceBusBindingTests.cs +++ b/tests/Relayer.Tests/ServiceBusBindingTests.cs @@ -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 { ["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().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); @@ -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(); } } diff --git a/website/docs/transports/service-bus-receive.md b/website/docs/transports/service-bus-receive.md index fa98c47f7..c1c47c15f 100644 --- a/website/docs/transports/service-bus-receive.md +++ b/website/docs/transports/service-bus-receive.md @@ -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>().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.