Skip to content

Commit 932f7d8

Browse files
Avoid unnecessary allocations (Azure#16539)
* Avoid unnecessary allocations * Update exceptions and add live test * fix test
1 parent f1af612 commit 932f7d8

File tree

4 files changed

+31
-15
lines changed

4 files changed

+31
-15
lines changed

sdk/servicebus/Azure.Messaging.ServiceBus/src/Amqp/AmqpMessageExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ public static BinaryData GetBody(this AmqpAnnotatedMessage message)
134134
{
135135
return dataBody.ConvertAndFlattenData();
136136
}
137-
throw new NotSupportedException($"{message.Body.GetType()} is not a supported message body type.");
137+
throw new NotSupportedException($"{message.Body.BodyType} is not a supported message body type.");
138138
}
139139
}
140140
}

sdk/servicebus/Azure.Messaging.ServiceBus/src/Primitives/ServiceBusMessage.cs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System;
55
using System.Collections.Generic;
66
using System.Globalization;
7+
using System.Text;
78
using Azure.Core;
89
using Azure.Core.Amqp;
910
using Azure.Messaging.ServiceBus.Amqp;
@@ -33,27 +34,26 @@ public ServiceBusMessage() :
3334
/// </summary>
3435
/// <param name="body">The payload of the message as a string.</param>
3536
public ServiceBusMessage(string body) :
36-
this(new BinaryData(body))
37+
this(Encoding.UTF8.GetBytes(body))
3738
{
3839
}
3940

4041
/// <summary>
4142
/// Creates a new message from the specified payload.
4243
/// </summary>
4344
/// <param name="body">The payload of the message in bytes.</param>
44-
public ServiceBusMessage(ReadOnlyMemory<byte> body) :
45-
this(BinaryData.FromBytes(body))
45+
public ServiceBusMessage(ReadOnlyMemory<byte> body)
4646
{
47+
AmqpMessageBody amqpBody = new AmqpMessageBody(new ReadOnlyMemory<byte>[] { body });
48+
AmqpMessage = new AmqpAnnotatedMessage(amqpBody);
4749
}
4850

4951
/// <summary>
5052
/// Creates a new message from the specified <see cref="BinaryData"/> instance.
5153
/// </summary>
5254
/// <param name="body">The payload of the message.</param>
53-
public ServiceBusMessage(BinaryData body)
55+
public ServiceBusMessage(BinaryData body) : this (body?.ToMemory() ?? default)
5456
{
55-
AmqpMessageBody amqpBody = new AmqpMessageBody(new ReadOnlyMemory<byte>[] { body ?? new BinaryData(Array.Empty<byte>()) });
56-
AmqpMessage = new AmqpAnnotatedMessage(amqpBody);
5757
}
5858

5959
/// <summary>
@@ -63,7 +63,12 @@ public ServiceBusMessage(BinaryData body)
6363
public ServiceBusMessage(ServiceBusReceivedMessage receivedMessage)
6464
{
6565
Argument.AssertNotNull(receivedMessage, nameof(receivedMessage));
66-
AmqpMessageBody body = new AmqpMessageBody(new ReadOnlyMemory<byte>[] { receivedMessage.Body });
66+
if (!receivedMessage.AmqpMessage.Body.TryGetData(out IEnumerable<ReadOnlyMemory<byte>> dataBody))
67+
{
68+
throw new NotSupportedException($"{receivedMessage.AmqpMessage.Body.BodyType} is not a supported message body type.");
69+
}
70+
71+
AmqpMessageBody body = new AmqpMessageBody(dataBody);
6772
AmqpMessage = new AmqpAnnotatedMessage(body);
6873

6974
// copy properties

sdk/servicebus/Azure.Messaging.ServiceBus/src/Primitives/ServiceBusReceivedMessage.cs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,7 @@ internal ServiceBusReceivedMessage(): this(body: default)
6161
/// <summary>
6262
/// Gets the body of the message.
6363
/// </summary>
64-
public BinaryData Body
65-
{
66-
get
67-
{
68-
return AmqpMessage.GetBody();
69-
}
70-
}
64+
public BinaryData Body => AmqpMessage.GetBody();
7165

7266
/// <summary>
7367
/// Gets the MessageId to identify the message.

sdk/servicebus/Azure.Messaging.ServiceBus/tests/Message/MessageLiveTests.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,23 @@ public async Task CanSendMessageWithMaxSize()
9090
}
9191
}
9292

93+
[Test]
94+
public async Task CanSendNullBodyMessage()
95+
{
96+
await using (var scope = await ServiceBusScope.CreateWithQueue(enablePartitioning: false, enableSession: false))
97+
{
98+
var client = new ServiceBusClient(TestEnvironment.ServiceBusConnectionString);
99+
100+
var maxSizeMessage = new ServiceBusMessage((BinaryData)null);
101+
102+
await client.CreateSender(scope.QueueName).SendMessageAsync(maxSizeMessage);
103+
var receiver = client.CreateReceiver(scope.QueueName);
104+
var receivedMessage = await receiver.ReceiveMessageAsync();
105+
Assert.IsNotNull(receivedMessage);
106+
await receiver.CompleteMessageAsync(receivedMessage.LockToken);
107+
}
108+
}
109+
93110
[Test]
94111
public async Task CreateFromReceivedMessageCopiesProperties()
95112
{

0 commit comments

Comments
 (0)