diff --git a/sdk/core/Azure.Core.Experimental/api/Azure.Core.Experimental.netstandard2.0.cs b/sdk/core/Azure.Core.Experimental/api/Azure.Core.Experimental.netstandard2.0.cs index a0214dd6de326..5c2fbc37edcf5 100644 --- a/sdk/core/Azure.Core.Experimental/api/Azure.Core.Experimental.netstandard2.0.cs +++ b/sdk/core/Azure.Core.Experimental/api/Azure.Core.Experimental.netstandard2.0.cs @@ -5,24 +5,27 @@ public readonly partial struct BinaryData { private readonly object _dummy; private readonly int _dummyPrimitive; - public BinaryData(System.ReadOnlyMemory data) { throw null; } + public BinaryData(System.ReadOnlySpan data) { throw null; } public BinaryData(string data) { throw null; } - public BinaryData(string data, System.Text.Encoding encoding) { throw null; } - public System.ReadOnlyMemory AsBytes() { throw null; } + public System.ReadOnlyMemory Bytes { get { throw null; } } public System.Threading.Tasks.ValueTask DeserializeAsync(Azure.Core.ObjectSerializer serializer, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } + public System.Threading.Tasks.ValueTask DeserializeAsync(System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } public T Deserialize(Azure.Core.ObjectSerializer serializer, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } + public T Deserialize(System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)] public override bool Equals(object? obj) { throw null; } - public static System.Threading.Tasks.Task FromSerializableAsync(T data, Azure.Core.ObjectSerializer serializer, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } - public static Azure.Core.BinaryData FromSerializable(T data, Azure.Core.ObjectSerializer serializer, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } + public static Azure.Core.BinaryData FromMemory(System.ReadOnlyMemory data) { throw null; } public static Azure.Core.BinaryData FromStream(System.IO.Stream stream) { throw null; } public static System.Threading.Tasks.Task FromStreamAsync(System.IO.Stream stream, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)] public override int GetHashCode() { throw null; } public static implicit operator System.ReadOnlyMemory (Azure.Core.BinaryData data) { throw null; } + public static System.Threading.Tasks.Task SerializeAsync(T data, Azure.Core.ObjectSerializer serializer, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } + public static System.Threading.Tasks.Task SerializeAsync(T data, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } + public static Azure.Core.BinaryData Serialize(T data, Azure.Core.ObjectSerializer serializer, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } + public static Azure.Core.BinaryData Serialize(T data, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } public System.IO.Stream ToStream() { throw null; } public override string ToString() { throw null; } - public string ToString(System.Text.Encoding encoding) { throw null; } } public partial class DynamicJson : System.Dynamic.IDynamicMetaObjectProvider { diff --git a/sdk/core/Azure.Core.Experimental/src/Primitives/BinaryData.cs b/sdk/core/Azure.Core.Experimental/src/Primitives/BinaryData.cs index a60fae48ccd4f..44e9bc1cf20db 100644 --- a/sdk/core/Azure.Core.Experimental/src/Primitives/BinaryData.cs +++ b/sdk/core/Azure.Core.Experimental/src/Primitives/BinaryData.cs @@ -13,50 +13,73 @@ namespace Azure.Core { /// - /// Abstraction for a payload of binary data. + /// A lightweight abstraction for a payload of bytes. This type integrates with + /// to allow for serializing and deserializing payloads. + /// + /// The ownership model of the underlying bytes varies depending on how the instance is constructed: + /// + /// If created using the static factory method, , + /// the passed in bytes will be wrapped, rather than copied. This is useful in scenarios where performance + /// is critical and/or ownership of the bytes is controlled completely by the consumer, thereby allowing the + /// enforcement of whatever ownership model is needed. + /// + /// If created using the constructor, will + /// maintain its own copy of the underlying bytes. This usage is geared more towards scenarios where the ownership + /// of the bytes might be ambiguous to users of the consuming code. By making a copy of the bytes, the payload is + /// guaranteed to be immutable. + /// + /// For all other constructors and static factory methods, BinaryData will assume ownership of the underlying bytes. /// public readonly struct BinaryData { private const int CopyToBufferSize = 81920; + private static readonly UTF8Encoding s_encoding = new UTF8Encoding(false); + /// /// The backing store for the instance. /// - internal ReadOnlyMemory Data { get; } + public ReadOnlyMemory Bytes { get; } /// - /// Creates a binary data instance from bytes. + /// Creates a binary data instance by making a copy + /// of the passed in bytes. /// /// Byte data. - public BinaryData(ReadOnlyMemory data) + public BinaryData(ReadOnlySpan data) { - Data = data; + Bytes = data.ToArray(); } /// - /// Creates a binary data instance from a string - /// using UTF-8 encoding. + /// Creates a binary data instance by wrapping the + /// passed in bytes. + /// + /// Byte data. + private BinaryData(ReadOnlyMemory data) + { + Bytes = data; + } + /// + /// Creates a binary data instance from a string by converting + /// the string to bytes using UTF-8 encoding. /// /// The string data. /// A instance. + /// The byte order mark is not included as part of the encoding process. public BinaryData(string data) - : this(data, Encoding.UTF8) { + Bytes = s_encoding.GetBytes(data); } /// - /// Creates a binary data instance from a string - /// using the specified encoding. + /// Creates a binary data instance by wrapping the passed in + /// . /// - /// The string data. - /// The encoding to use when converting - /// the data to bytes. + /// /// A instance. - public BinaryData(string data, Encoding encoding) - { - Argument.AssertNotNull(encoding, nameof(encoding)); - Data = encoding.GetBytes(data); - } + public static BinaryData FromMemory(ReadOnlyMemory data) => + new BinaryData(data); /// /// Creates a binary data instance from the specified stream. @@ -64,7 +87,7 @@ public BinaryData(string data, Encoding encoding) /// Stream containing the data. /// A instance. public static BinaryData FromStream(Stream stream) => - CreateAsync(stream, false).EnsureCompleted(); + FromStreamAsync(stream, false).EnsureCompleted(); /// /// Creates a binary data instance from the specified stream. @@ -76,9 +99,9 @@ public static BinaryData FromStream(Stream stream) => public static async Task FromStreamAsync( Stream stream, CancellationToken cancellationToken = default) => - await CreateAsync(stream, true, cancellationToken).ConfigureAwait(false); + await FromStreamAsync(stream, true, cancellationToken).ConfigureAwait(false); - private static async Task CreateAsync( + private static async Task FromStreamAsync( Stream stream, bool async, CancellationToken cancellationToken = default) @@ -100,13 +123,39 @@ private static async Task CreateAsync( { stream.CopyTo(memoryStream); } - return new BinaryData(memoryStream.ToArray()); + return new BinaryData((ReadOnlyMemory) memoryStream.ToArray()); } } /// /// Creates a BinaryData instance from the specified data using - /// the specified type. + /// the . + /// + /// The type of the data. + /// The data to use. + /// The to use during serialization. + /// A instance. + public static BinaryData Serialize( + T data, + CancellationToken cancellationToken = default) => + Serialize(data, new JsonObjectSerializer(), cancellationToken); + + /// + /// Creates a BinaryData instance from the specified data + /// using the . + /// + /// The type of the data. + /// The data to use. + /// The to use during serialization. + /// A instance. + public static async Task SerializeAsync( + T data, + CancellationToken cancellationToken = default) => + await SerializeInternalAsync(data, new JsonObjectSerializer(), true, cancellationToken).ConfigureAwait(false); + + /// + /// Creates a BinaryData instance from the specified data using + /// the provided . /// /// The type of the data. /// The data to use. @@ -114,15 +163,15 @@ private static async Task CreateAsync( /// the data. /// The to use during serialization. /// A instance. - public static BinaryData FromSerializable( + public static BinaryData Serialize( T data, ObjectSerializer serializer, CancellationToken cancellationToken = default) => - FromSerializableAsync(data, serializer, false, cancellationToken).EnsureCompleted(); + SerializeInternalAsync(data, serializer, false, cancellationToken).EnsureCompleted(); /// /// Creates a BinaryData instance from the specified data using - /// the specified type. + /// the provided . /// /// The type of the data. /// The data to use. @@ -130,13 +179,13 @@ public static BinaryData FromSerializable( /// the data. /// The to use during serialization. /// A instance. - public static async Task FromSerializableAsync( + public static async Task SerializeAsync( T data, ObjectSerializer serializer, CancellationToken cancellationToken = default) => - await FromSerializableAsync(data, serializer, true, cancellationToken).ConfigureAwait(false); + await SerializeInternalAsync(data, serializer, true, cancellationToken).ConfigureAwait(false); - private static async Task FromSerializableAsync( + private static async Task SerializeInternalAsync( T data, ObjectSerializer serializer, bool async, @@ -152,43 +201,24 @@ private static async Task FromSerializableAsync( { serializer.Serialize(memoryStream, data, typeof(T), cancellationToken); } - return new BinaryData(memoryStream.ToArray()); + return new BinaryData((ReadOnlyMemory) memoryStream.ToArray()); } /// /// Converts the BinaryData to a string using UTF-8. /// /// The string representation of the data. - public override string ToString() => - ToString(Encoding.UTF8); - - /// - /// Converts the BinaryData to a string using the specified - /// encoding. - /// - /// The encoding to use when decoding - /// the bytes. - /// The string representation of the data. - public string ToString( - Encoding encoding) + public override string ToString() { - Argument.AssertNotNull(encoding, nameof(encoding)); if (MemoryMarshal.TryGetArray( - Data, + Bytes, out ArraySegment data)) { - return encoding.GetString(data.Array, data.Offset, data.Count); + return s_encoding.GetString(data.Array, data.Offset, data.Count); } - return encoding.GetString(Data.ToArray()); + return s_encoding.GetString(Bytes.ToArray()); } - /// - /// Converts the BinaryData to bytes. - /// - /// The data as bytes. - public ReadOnlyMemory AsBytes() => - Data; - /// /// Converts the BinaryData to a stream. /// @@ -196,16 +226,17 @@ public ReadOnlyMemory AsBytes() => public Stream ToStream() { if (MemoryMarshal.TryGetArray( - Data, + Bytes, out ArraySegment data)) { return new MemoryStream(data.Array, data.Offset, data.Count); } - return new MemoryStream(Data.ToArray()); + return new MemoryStream(Bytes.ToArray()); } /// - /// Converts the BinaryData to the specified type. + /// Converts the BinaryData to the specified type using + /// the provided . /// /// The type that the data should be /// converted to. @@ -214,10 +245,35 @@ public Stream ToStream() /// The to use during deserialization. ///The data converted to the specified type. public T Deserialize(ObjectSerializer serializer, CancellationToken cancellationToken = default) => - DeserializeAsync(serializer, false, cancellationToken).EnsureCompleted(); + DeserializeInternalAsync(serializer, false, cancellationToken).EnsureCompleted(); /// - /// Converts the BinaryData to the specified type. + /// Converts the BinaryData to the specified type using the + /// . + /// + /// The type that the data should be + /// converted to. + /// The to use during deserialization. + ///The data cast to the specified type. If the cast cannot + ///be performed, an will be + ///thrown. + public async ValueTask DeserializeAsync(CancellationToken cancellationToken = default) => + await DeserializeInternalAsync(new JsonObjectSerializer(), true, cancellationToken).ConfigureAwait(false); + + /// + /// Converts the BinaryData to the specified type using the + /// . + /// + /// The type that the data should be + /// converted to. + /// The to use during deserialization. + ///The data converted to the specified type. + public T Deserialize(CancellationToken cancellationToken = default) => + DeserializeInternalAsync(new JsonObjectSerializer(), false, cancellationToken).EnsureCompleted(); + + /// + /// Converts the BinaryData to the specified type using the + /// provided . /// /// The type that the data should be /// converted to. @@ -227,12 +283,10 @@ public T Deserialize(ObjectSerializer serializer, CancellationToken cancellat ///The data cast to the specified type. If the cast cannot ///be performed, an will be ///thrown. - /// TODO - add cancellation token support - /// once ObjectSerializer.DeserializeAsync adds it. public async ValueTask DeserializeAsync(ObjectSerializer serializer, CancellationToken cancellationToken = default) => - await DeserializeAsync(serializer, true, cancellationToken).ConfigureAwait(false); + await DeserializeInternalAsync(serializer, true, cancellationToken).ConfigureAwait(false); - private async ValueTask DeserializeAsync( + private async ValueTask DeserializeInternalAsync( ObjectSerializer serializer, bool async, CancellationToken cancellationToken) @@ -258,21 +312,26 @@ private async ValueTask DeserializeAsync( /// public static implicit operator ReadOnlyMemory( BinaryData data) => - data.AsBytes(); + data.Bytes; - /// + /// + /// Two BinaryData objects are equal if the memory regions point to the same array and have the same length. + /// The method does not check to see if the contents are equal. + /// + /// The BinaryData to compare. + /// true if the current instance and other are equal; otherwise, false. [EditorBrowsable(EditorBrowsableState.Never)] public override bool Equals(object? obj) { if (obj is BinaryData data) { - return data.Data.Equals(Data); + return data.Bytes.Equals(Bytes); } return false; } /// [EditorBrowsable(EditorBrowsableState.Never)] public override int GetHashCode() => - Data.GetHashCode(); + Bytes.GetHashCode(); } } diff --git a/sdk/core/Azure.Core.Experimental/tests/BinaryDataTests.cs b/sdk/core/Azure.Core.Experimental/tests/BinaryDataTests.cs index f4b471dcc0361..87f805826f855 100644 --- a/sdk/core/Azure.Core.Experimental/tests/BinaryDataTests.cs +++ b/sdk/core/Azure.Core.Experimental/tests/BinaryDataTests.cs @@ -4,9 +4,9 @@ using System; using System.Collections.Generic; using System.IO; +using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; -using Azure.Core; using Moq; using NUnit.Framework; @@ -17,9 +17,12 @@ public class BinaryDataTests [Test] public void CanCreateBinaryDataFromBytes() { - var payload = Encoding.UTF8.GetBytes("some data"); - var data = new BinaryData(payload); - Assert.AreEqual(payload, data.AsBytes().ToArray()); + byte[] payload = Encoding.UTF8.GetBytes("some data"); + BinaryData data = BinaryData.FromMemory(payload); + Assert.AreEqual(payload, data.Bytes.ToArray()); + + MemoryMarshal.TryGetArray(payload, out var array); + Assert.AreSame(payload, array.Array); // using implicit conversion ReadOnlyMemory bytes = data; @@ -27,21 +30,27 @@ public void CanCreateBinaryDataFromBytes() } [Test] - public void CanCreateBinaryDataFromString() + public void CanCreateBinaryDataUsingSpanCtor() { - var payload = "some data"; - var data = new BinaryData(payload); - Assert.AreEqual(payload, data.ToString()); + byte[] payload = Encoding.UTF8.GetBytes("some data"); + BinaryData data = new BinaryData(payload); + + Assert.AreNotSame(payload, data.Bytes); + Assert.AreNotEqual(payload, data.Bytes); + + Assert.AreEqual(payload, data.Bytes.ToArray()); + + // using implicit conversion + ReadOnlyMemory bytes = data; + Assert.AreEqual(payload, bytes.ToArray()); } [Test] - public void ToStringThrowsOnNullEncoding() + public void CanCreateBinaryDataFromString() { var payload = "some data"; var data = new BinaryData(payload); - Assert.That( - () => data.ToString(null), - Throws.InstanceOf()); + Assert.AreEqual(payload, data.ToString()); } [Test] @@ -50,7 +59,7 @@ public void ToStringRespectsArraySegmentBoundaries() var payload = "pre payload post"; var bytes = Encoding.UTF8.GetBytes(payload); var segment = new ArraySegment(bytes, 4, 7); - var data = new BinaryData(segment); + var data = BinaryData.FromMemory(segment); Assert.AreEqual("payload", data.ToString()); } @@ -60,7 +69,7 @@ public async Task ToStreamRespectsArraySegmentBoundaries() var payload = "pre payload post"; var bytes = Encoding.UTF8.GetBytes(payload); var segment = new ArraySegment(bytes, 4, 7); - var data = new BinaryData(segment); + var data = BinaryData.FromMemory(segment); var stream = data.ToStream(); var sr = new StreamReader(stream); Assert.AreEqual("payload", await sr.ReadToEndAsync()); @@ -73,12 +82,12 @@ public async Task CanCreateBinaryDataFromStream() var buffer = Encoding.UTF8.GetBytes("some data"); var payload = new MemoryStream(buffer); var data = BinaryData.FromStream(payload); - Assert.AreEqual(buffer, data.AsBytes().ToArray()); + Assert.AreEqual(buffer, data.Bytes.ToArray()); Assert.AreEqual(payload, data.ToStream()); payload.Position = 0; data = await BinaryData.FromStreamAsync(payload); - Assert.AreEqual(buffer, data.AsBytes().ToArray()); + Assert.AreEqual(buffer, data.Bytes.ToArray()); Assert.AreEqual(payload, data.ToStream()); } @@ -98,8 +107,12 @@ public async Task CanCreateBinaryDataFromCustomType() { var payload = new TestModel { A = "value", B = 5, C = true}; var serializer = new JsonObjectSerializer(); - await AssertData(BinaryData.FromSerializable(payload, serializer)); - await AssertData(await BinaryData.FromSerializableAsync(payload, serializer)); + + await AssertData(BinaryData.Serialize(payload, serializer)); + await AssertData(await BinaryData.SerializeAsync(payload, serializer)); + + await AssertData(BinaryData.Serialize(payload)); + await AssertData(await BinaryData.SerializeAsync(payload)); async Task AssertData(BinaryData data) { @@ -109,6 +122,13 @@ async Task AssertData(BinaryData data) Assert.AreEqual(payload.A, (await data.DeserializeAsync(serializer)).A); Assert.AreEqual(payload.B, (await data.DeserializeAsync(serializer)).B); Assert.AreEqual(payload.C, (await data.DeserializeAsync(serializer)).C); + + Assert.AreEqual(payload.A, data.Deserialize().A); + Assert.AreEqual(payload.B, data.Deserialize().B); + Assert.AreEqual(payload.C, data.Deserialize().C); + Assert.AreEqual(payload.A, (await data.DeserializeAsync()).A); + Assert.AreEqual(payload.B, (await data.DeserializeAsync()).B); + Assert.AreEqual(payload.C, (await data.DeserializeAsync()).C); } } @@ -117,10 +137,10 @@ public void FromSerializableThrowsOnNullSerializer() { var payload = new TestModel { A = "value", B = 5, C = true }; Assert.That( - () => BinaryData.FromSerializable(payload, null), + () => BinaryData.Serialize(payload, null), Throws.InstanceOf()); Assert.That( - async () => await BinaryData.FromSerializableAsync(payload, null), + async () => await BinaryData.SerializeAsync(payload, null), Throws.InstanceOf()); } @@ -140,17 +160,16 @@ public void CreateThrowsOnNullStream() public async Task DeserializeThrowsExceptionOnIncompatibleType() { var payload = new TestModel { A = "value", B = 5, C = true }; - var serializer = new JsonObjectSerializer(); - AssertData(BinaryData.FromSerializable(payload, serializer)); - AssertData(await BinaryData.FromSerializableAsync(payload, serializer)); + AssertData(BinaryData.Serialize(payload)); + AssertData(await BinaryData.SerializeAsync(payload)); void AssertData(BinaryData data) { Assert.That( - () => data.Deserialize(serializer), + () => data.Deserialize(), Throws.InstanceOf()); Assert.That( - async () => await data.DeserializeAsync(serializer), + async () => await data.DeserializeAsync(), Throws.InstanceOf()); } } @@ -160,7 +179,7 @@ public void DeserializeThrowsOnNullSerializer() { var payload = new TestModel { A = "value", B = 5, C = true }; var serializer = new JsonObjectSerializer(); - var data = BinaryData.FromSerializable(payload, serializer); + var data = BinaryData.Serialize(payload, serializer); Assert.That( () => data.Deserialize(null), Throws.InstanceOf()); @@ -173,11 +192,11 @@ public void DeserializeThrowsOnNullSerializer() public void EqualsRespectsReferenceEquality() { var payload = Encoding.UTF8.GetBytes("some data"); - var a = new BinaryData(payload); - var b = new BinaryData(payload); + var a = BinaryData.FromMemory(payload); + var b = BinaryData.FromMemory(payload); Assert.AreEqual(a, b); - var c = new BinaryData(Encoding.UTF8.GetBytes("some data")); + var c = BinaryData.FromMemory(Encoding.UTF8.GetBytes("some data")); Assert.AreNotEqual(a, c); Assert.AreNotEqual(a, "string data"); @@ -187,8 +206,8 @@ public void EqualsRespectsReferenceEquality() public void GetHashCodeWorks() { var payload = Encoding.UTF8.GetBytes("some data"); - var a = new BinaryData(payload); - var b = new BinaryData(payload); + var a = BinaryData.FromMemory(payload); + var b = BinaryData.FromMemory(payload); var set = new HashSet { a @@ -196,7 +215,7 @@ public void GetHashCodeWorks() // hashcodes of a and b should match since instances use same memory. Assert.IsTrue(set.Contains(b)); - var c = new BinaryData(Encoding.UTF8.GetBytes("some data")); + var c = BinaryData.FromMemory(Encoding.UTF8.GetBytes("some data")); // c should have a different hash code Assert.IsFalse(set.Contains(c)); set.Add(c); diff --git a/sdk/servicebus/Azure.Messaging.ServiceBus/api/Azure.Messaging.ServiceBus.netstandard2.0.cs b/sdk/servicebus/Azure.Messaging.ServiceBus/api/Azure.Messaging.ServiceBus.netstandard2.0.cs index 561c02ef39072..8d38d61962bdf 100644 --- a/sdk/servicebus/Azure.Messaging.ServiceBus/api/Azure.Messaging.ServiceBus.netstandard2.0.cs +++ b/sdk/servicebus/Azure.Messaging.ServiceBus/api/Azure.Messaging.ServiceBus.netstandard2.0.cs @@ -152,7 +152,6 @@ public ServiceBusMessage(Azure.Core.BinaryData body) { } public ServiceBusMessage(Azure.Messaging.ServiceBus.ServiceBusReceivedMessage receivedMessage) { } public ServiceBusMessage(System.ReadOnlyMemory body) { } public ServiceBusMessage(string body) { } - public ServiceBusMessage(string body, System.Text.Encoding encoding) { } public Azure.Core.BinaryData Body { get { throw null; } set { } } public string ContentType { get { throw null; } set { } } public string CorrelationId { get { throw null; } set { } } diff --git a/sdk/servicebus/Azure.Messaging.ServiceBus/src/Amqp/AmqpMessageConverter.cs b/sdk/servicebus/Azure.Messaging.ServiceBus/src/Amqp/AmqpMessageConverter.cs index 392b8f5fe044f..d579dc9b33c1e 100644 --- a/sdk/servicebus/Azure.Messaging.ServiceBus/src/Amqp/AmqpMessageConverter.cs +++ b/sdk/servicebus/Azure.Messaging.ServiceBus/src/Amqp/AmqpMessageConverter.cs @@ -154,7 +154,7 @@ private static ArraySegment ReadStreamToArraySegment(Stream stream) public static AmqpMessage SBMessageToAmqpMessage(SBMessage sbMessage) { - ReadOnlyMemory bodyBytes = sbMessage.Body.AsBytes(); + ReadOnlyMemory bodyBytes = sbMessage.Body; var body = new ArraySegment((bodyBytes.IsEmpty) ? Array.Empty() : bodyBytes.ToArray()); var amqpMessage = AmqpMessage.Create(new Data { Value = body }); amqpMessage.Properties.MessageId = sbMessage.MessageId; diff --git a/sdk/servicebus/Azure.Messaging.ServiceBus/src/Azure.Messaging.ServiceBus.csproj b/sdk/servicebus/Azure.Messaging.ServiceBus/src/Azure.Messaging.ServiceBus.csproj index 8b3f0d7fbdf83..df436a7427348 100644 --- a/sdk/servicebus/Azure.Messaging.ServiceBus/src/Azure.Messaging.ServiceBus.csproj +++ b/sdk/servicebus/Azure.Messaging.ServiceBus/src/Azure.Messaging.ServiceBus.csproj @@ -12,7 +12,6 @@ - @@ -33,6 +32,9 @@ + + + True diff --git a/sdk/servicebus/Azure.Messaging.ServiceBus/src/Diagnostics/EntityScopeFactory.cs b/sdk/servicebus/Azure.Messaging.ServiceBus/src/Diagnostics/EntityScopeFactory.cs index e2a28ffded41c..b23398d19ecf4 100644 --- a/sdk/servicebus/Azure.Messaging.ServiceBus/src/Diagnostics/EntityScopeFactory.cs +++ b/sdk/servicebus/Azure.Messaging.ServiceBus/src/Diagnostics/EntityScopeFactory.cs @@ -2,6 +2,7 @@ // Licensed under the MIT License. using System.Collections.Generic; +using System.Threading.Tasks; using Azure.Core.Pipeline; namespace Azure.Messaging.ServiceBus.Diagnostics diff --git a/sdk/servicebus/Azure.Messaging.ServiceBus/src/Primitives/ServiceBusMessage.cs b/sdk/servicebus/Azure.Messaging.ServiceBus/src/Primitives/ServiceBusMessage.cs index a8cce4199cfd4..1b055ff7f3975 100644 --- a/sdk/servicebus/Azure.Messaging.ServiceBus/src/Primitives/ServiceBusMessage.cs +++ b/sdk/servicebus/Azure.Messaging.ServiceBus/src/Primitives/ServiceBusMessage.cs @@ -36,20 +36,9 @@ public ServiceBusMessage() /// Creates a new message from the specified string, using UTF-8 encoding. /// /// The payload of the message as a string. - public ServiceBusMessage(string body) : - this(body, Encoding.UTF8) + public ServiceBusMessage(string body) { - } - - /// - /// Creates a new message from the specified string, using the specified encoding. - /// - /// The payload of the message as a string. - /// The encoding to use for the body. - public ServiceBusMessage(string body, Encoding encoding) - { - Argument.AssertNotNull(encoding, nameof(encoding)); - Body = new BinaryData(body, encoding); + Body = new BinaryData(body); Properties = new Dictionary(); } @@ -59,7 +48,7 @@ public ServiceBusMessage(string body, Encoding encoding) /// The payload of the message in bytes. public ServiceBusMessage(ReadOnlyMemory body) { - Body = new BinaryData(body); + Body = BinaryData.FromMemory(body); Properties = new Dictionary(); } diff --git a/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Amqp/AmqpConverterTests.cs b/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Amqp/AmqpConverterTests.cs index 849351aaaa4df..106d97ae72572 100644 --- a/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Amqp/AmqpConverterTests.cs +++ b/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Amqp/AmqpConverterTests.cs @@ -63,7 +63,7 @@ public void ConvertSBMessageToAmqpMessageAndBack() var convertedSbMessage = AmqpMessageConverter.AmqpMessageToSBMessage(amqpMessage); Assert.AreEqual("SomeUserProperty", convertedSbMessage.Properties["UserProperty"]); - Assert.AreEqual(messageBody, convertedSbMessage.Body.AsBytes().ToArray()); + Assert.AreEqual(messageBody, convertedSbMessage.Body.Bytes.ToArray()); Assert.AreEqual(messageId, convertedSbMessage.MessageId); Assert.AreEqual(partitionKey, convertedSbMessage.PartitionKey); Assert.AreEqual(viaPartitionKey, convertedSbMessage.ViaPartitionKey); diff --git a/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Message/MessageLiveTests.cs b/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Message/MessageLiveTests.cs index e536911b545d7..413fd603d0c15 100644 --- a/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Message/MessageLiveTests.cs +++ b/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Message/MessageLiveTests.cs @@ -84,7 +84,7 @@ public async Task CanSendMessageWithMaxSize() var receiver = client.CreateReceiver(scope.QueueName); var receivedMaxSizeMessage = await receiver.ReceiveMessageAsync(); await receiver.CompleteMessageAsync(receivedMaxSizeMessage.LockToken); - Assert.AreEqual(maxPayload, receivedMaxSizeMessage.Body.AsBytes().ToArray()); + Assert.AreEqual(maxPayload, receivedMaxSizeMessage.Body.Bytes.ToArray()); } } @@ -124,7 +124,7 @@ public async Task CreateFromReceivedMessageCopiesProperties() void AssertMessagesEqual(ServiceBusMessage sentMessage, ServiceBusReceivedMessage received) { - Assert.IsTrue(received.Body.AsBytes().ToArray().SequenceEqual(sentMessage.Body.AsBytes().ToArray())); + Assert.IsTrue(received.Body.Bytes.ToArray().SequenceEqual(sentMessage.Body.Bytes.ToArray())); Assert.AreEqual(received.ContentType, sentMessage.ContentType); Assert.AreEqual(received.CorrelationId, sentMessage.CorrelationId); Assert.AreEqual(received.Label, sentMessage.Label); @@ -158,7 +158,7 @@ public async Task SendJsonBodyMessage() B = 5, C = false }; - var body = BinaryData.FromSerializable(testBody, serializer); + var body = BinaryData.Serialize(testBody, serializer); var msg = new ServiceBusMessage(body); await sender.SendMessageAsync(msg); diff --git a/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Message/MessageTests.cs b/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Message/MessageTests.cs index 0e45e40c94d96..4e70b695f41af 100644 --- a/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Message/MessageTests.cs +++ b/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Message/MessageTests.cs @@ -46,11 +46,6 @@ public void SetMessageBodyToString() var messageBody = "some message"; var message = new ServiceBusMessage(messageBody); Assert.AreEqual(message.Body.ToString(), messageBody); - Assert.AreEqual(message.Body.ToString(), messageBody); - - message = new ServiceBusMessage(messageBody, Encoding.UTF32); - Assert.AreEqual(message.Body.ToString(Encoding.UTF32), messageBody); - Assert.AreNotEqual(message.Body.ToString(), messageBody); } } } diff --git a/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Samples/Sample03_SendReceiveSessions.cs b/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Samples/Sample03_SendReceiveSessions.cs index 3e276e66c4a29..ae33f6faf5eed 100644 --- a/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Samples/Sample03_SendReceiveSessions.cs +++ b/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Samples/Sample03_SendReceiveSessions.cs @@ -52,7 +52,7 @@ public async Task SendAndReceiveSessionMessage() byte[] state = await receiver.GetSessionStateAsync(); #endregion - Assert.AreEqual(Encoding.UTF8.GetBytes("Hello world!"), receivedMessage.Body.AsBytes().ToArray()); + Assert.AreEqual(Encoding.UTF8.GetBytes("Hello world!"), receivedMessage.Body.Bytes.ToArray()); Assert.AreEqual("mySessionId", receivedMessage.SessionId); Assert.AreEqual(Encoding.UTF8.GetBytes("some state"), state); } @@ -101,7 +101,7 @@ public async Task ReceiveFromSpecificSession() Console.WriteLine(receivedMessage.SessionId); #endregion - Assert.AreEqual(Encoding.UTF8.GetBytes("Second"), receivedMessage.Body.AsBytes().ToArray()); + Assert.AreEqual(Encoding.UTF8.GetBytes("Second"), receivedMessage.Body.Bytes.ToArray()); Assert.AreEqual("Session2", receivedMessage.SessionId); } }