From 8f2e26a0a2a5879476bf7beb590cd61d5127163c Mon Sep 17 00:00:00 2001 From: JoshLove-msft <54595583+JoshLove-msft@users.noreply.github.com> Date: Tue, 6 Jul 2021 12:54:25 -0700 Subject: [PATCH 1/3] Add interop sample --- .../samples/README.md | 1 + .../samples/Sample08_Interop.md | 52 +++++++++++++ .../tests/Samples/Sample01_HelloWorld.cs | 10 +-- .../tests/Samples/Sample08_Interop.cs | 76 +++++++++++++++++++ 4 files changed, 130 insertions(+), 9 deletions(-) create mode 100644 sdk/servicebus/Azure.Messaging.ServiceBus/samples/Sample08_Interop.md create mode 100644 sdk/servicebus/Azure.Messaging.ServiceBus/tests/Samples/Sample08_Interop.cs diff --git a/sdk/servicebus/Azure.Messaging.ServiceBus/samples/README.md b/sdk/servicebus/Azure.Messaging.ServiceBus/samples/README.md index 91932ba75e7b5..efec3f800806f 100644 --- a/sdk/servicebus/Azure.Messaging.ServiceBus/samples/README.md +++ b/sdk/servicebus/Azure.Messaging.ServiceBus/samples/README.md @@ -18,3 +18,4 @@ description: Samples for the Azure.Messaging.ServiceBus client library - [Using the Session Processor](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/servicebus/Azure.Messaging.ServiceBus/samples/Sample05_SessionProcessor.md) - [Working with Transactions](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/servicebus/Azure.Messaging.ServiceBus/samples/Sample06_Transactions.md) - [CRUD Operations](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/servicebus/Azure.Messaging.ServiceBus/samples/Sample07_CrudOperations.md) +- [Interop](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/servicebus/Azure.Messaging.ServiceBus/samples/Sample08_Interop.md) diff --git a/sdk/servicebus/Azure.Messaging.ServiceBus/samples/Sample08_Interop.md b/sdk/servicebus/Azure.Messaging.ServiceBus/samples/Sample08_Interop.md new file mode 100644 index 0000000000000..7ae5dab73acda --- /dev/null +++ b/sdk/servicebus/Azure.Messaging.ServiceBus/samples/Sample08_Interop.md @@ -0,0 +1,52 @@ +## CRUD operations + +This sample demonstrates how to interoperate with messages that are sent or received using the `WindowsAzure.ServiceBus` library. The `WindowsAzure.ServiceBus` library uses the DataContractSerializer to serialize the BrokeredMessage body. Because of this, when attempting to interoperate with this library, there a few additional steps that are needed. + +### Sending a message using `Azure.Messaging.ServiceBus` that will be received with `WindowsAzure.ServiceBus` + +```C# Snippet:ServiceBusInteropSend +ServiceBusSender sender = client.CreateSender(queueName); +// When constructing the `DataContractSerializer`, We pass in the type for the model, which can be a strongly typed model or some pre-serialized data. +// If you use a strongly typed model here, the model properties will be serialized into XML. Since JSON is more commonly used, we will use it in our example, and +// and specify the type as string, since we will provide a JSON string. +var serializer = new DataContractSerializer(typeof(string)); +var stream = new MemoryStream(); +var writer = XmlDictionaryWriter.CreateBinaryWriter(stream); + +// serialize an instance of our type into a JSON string +string json = JsonSerializer.Serialize(new TestModel {A = "Hello world", B = 5, C = true}); + +// serialize our JSON string into the XML envelope using the DataContractSerializer +serializer.WriteObject(writer, json); +writer.Flush(); + +// construct the ServiceBusMessage using the DataContract serialized JSON +var message = new ServiceBusMessage(stream.ToArray()); + +await sender.SendMessageAsync(message); +``` + +### Receiving a message using `Azure.Messaging.Service` that was sent with `WindowsAzure.ServiceBus` + +```C# Snippet:ServiceBusInteropReceive +ServiceBusReceiver receiver = client.CreateReceiver(queueName); +ServiceBusReceivedMessage received = await receiver.ReceiveMessageAsync(); + +// Similar to the send scenario, we still rely on the DataContractSerializer and we use string as our type because we are expecting a JSON +// message body. +var deserializer = new DataContractSerializer(typeof(string)); +XmlDictionaryReader reader = + XmlDictionaryReader.CreateBinaryReader(received.Body.ToStream(), XmlDictionaryReaderQuotas.Max); + +// deserialize the XML envelope into a string +string receivedJson = (string) deserializer.ReadObject(reader); + +// deserialize the JSON string into TestModel +TestModel output = JsonSerializer.Deserialize(receivedJson); +``` + +## Source + +To see the full example source, see: + +* [Sample08_Interop.cs](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Samples/Sample08_Interop.cs) diff --git a/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Samples/Sample01_HelloWorld.cs b/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Samples/Sample01_HelloWorld.cs index d00eed83a5e71..61d1ac42d25ff 100644 --- a/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Samples/Sample01_HelloWorld.cs +++ b/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Samples/Sample01_HelloWorld.cs @@ -90,14 +90,9 @@ public async Task SendAndReceiveMessageBatch() { await using (var scope = await ServiceBusScope.CreateWithQueue(enablePartitioning: false, enableSession: false)) { - #region Snippet:ServiceBusInitializeSend -#if SNIPPET - string connectionString = ""; - string queueName = ""; -#else string connectionString = TestEnvironment.ServiceBusConnectionString; string queueName = scope.QueueName; -#endif + // since ServiceBusClient implements IAsyncDisposable we create it with "await using" await using var client = new ServiceBusClient(connectionString); @@ -110,8 +105,6 @@ public async Task SendAndReceiveMessageBatch() // send the messages await sender.SendMessagesAsync(messages); #endregion - #endregion - #region Snippet:ServiceBusReceiveBatch // create a receiver that we can use to receive the messages ServiceBusReceiver receiver = client.CreateReceiver(queueName); @@ -124,7 +117,6 @@ public async Task SendAndReceiveMessageBatch() string body = receivedMessage.Body.ToString(); Console.WriteLine(body); } - #endregion var sentMessagesEnum = messages.GetEnumerator(); foreach (ServiceBusReceivedMessage receivedMessage in receivedMessages) diff --git a/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Samples/Sample08_Interop.cs b/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Samples/Sample08_Interop.cs new file mode 100644 index 0000000000000..ad943023fefa7 --- /dev/null +++ b/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Samples/Sample08_Interop.cs @@ -0,0 +1,76 @@ +using System.IO; +using System.Runtime.Serialization; +using System.Text.Json; +using System.Threading.Tasks; +using System.Xml; +using NUnit.Framework; + +namespace Azure.Messaging.ServiceBus.Tests.Samples +{ + public class Sample08_Interop : ServiceBusLiveTestBase + { + [Test] + public async Task TestInterop() + { + await using (var scope = await ServiceBusScope.CreateWithQueue( + enablePartitioning: false, + enableSession: false)) + { + var queueName = scope.QueueName; + await using var client = CreateClient(); + + // Scenario #1 - Sending a message using Azure.Messaging.ServiceBus that will be received with WindowsAzure.ServiceBus + #region Snippet:ServiceBusInteropSend + ServiceBusSender sender = client.CreateSender(queueName); + // When constructing the `DataContractSerializer`, We pass in the type for the model, which can be a strongly typed model or some pre-serialized data. + // If you use a strongly typed model here, the model properties will be serialized into XML. Since JSON is more commonly used, we will use it in our example, and + // and specify the type as string, since we will provide a JSON string. + var serializer = new DataContractSerializer(typeof(string)); + var stream = new MemoryStream(); + var writer = XmlDictionaryWriter.CreateBinaryWriter(stream); + + // serialize an instance of our type into a JSON string + string json = JsonSerializer.Serialize(new TestModel {A = "Hello world", B = 5, C = true}); + + // serialize our JSON string into the XML envelope using the DataContractSerializer + serializer.WriteObject(writer, json); + writer.Flush(); + + // construct the ServiceBusMessage using the DataContract serialized JSON + var message = new ServiceBusMessage(stream.ToArray()); + + await sender.SendMessageAsync(message); + #endregion + + // Scenario #2 - Receiving a message using Azure.Messaging.ServiceBus that was sent with WindowsAzure.ServiceBus + #region Snippet:ServiceBusInteropReceive + ServiceBusReceiver receiver = client.CreateReceiver(queueName); + ServiceBusReceivedMessage received = await receiver.ReceiveMessageAsync(); + + // Similar to the send scenario, we still rely on the DataContractSerializer and we use string as our type because we are expecting a JSON + // message body. + var deserializer = new DataContractSerializer(typeof(string)); + XmlDictionaryReader reader = + XmlDictionaryReader.CreateBinaryReader(received.Body.ToStream(), XmlDictionaryReaderQuotas.Max); + + // deserialize the XML envelope into a string + string receivedJson = (string) deserializer.ReadObject(reader); + + // deserialize the JSON string into TestModel + TestModel output = JsonSerializer.Deserialize(receivedJson); + #endregion + + Assert.AreEqual("Hello world", output.A); + Assert.AreEqual(5, output.B); + Assert.IsTrue(output.C); + } + } + + public class TestModel + { + public string A { get; set; } + public int B { get; set; } + public bool C { get; set; } + } + } +} \ No newline at end of file From 2cae83d283088601c3a32c6f73e8e30af797a39d Mon Sep 17 00:00:00 2001 From: JoshLove-msft <54595583+JoshLove-msft@users.noreply.github.com> Date: Tue, 6 Jul 2021 13:19:17 -0700 Subject: [PATCH 2/3] File header --- .../tests/Samples/Sample08_Interop.cs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Samples/Sample08_Interop.cs b/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Samples/Sample08_Interop.cs index ad943023fefa7..046783a91fe7a 100644 --- a/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Samples/Sample08_Interop.cs +++ b/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Samples/Sample08_Interop.cs @@ -1,4 +1,7 @@ -using System.IO; +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +using System.IO; using System.Runtime.Serialization; using System.Text.Json; using System.Threading.Tasks; From fb4fab6b9a1a38cc2981aa6dbddeadb69ee8d409 Mon Sep 17 00:00:00 2001 From: JoshLove-msft <54595583+JoshLove-msft@users.noreply.github.com> Date: Tue, 6 Jul 2021 13:38:59 -0700 Subject: [PATCH 3/3] PR FB --- .../Azure.Messaging.ServiceBus.sln | 1 + .../samples/Sample08_Interop.md | 8 ++++---- .../tests/Samples/Sample08_Interop.cs | 4 ++-- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/sdk/servicebus/Azure.Messaging.ServiceBus/Azure.Messaging.ServiceBus.sln b/sdk/servicebus/Azure.Messaging.ServiceBus/Azure.Messaging.ServiceBus.sln index a1d3eb9361390..950416722b2a3 100644 --- a/sdk/servicebus/Azure.Messaging.ServiceBus/Azure.Messaging.ServiceBus.sln +++ b/sdk/servicebus/Azure.Messaging.ServiceBus/Azure.Messaging.ServiceBus.sln @@ -27,6 +27,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "samples", "samples", "{8B8C samples\Sample05_SessionProcessor.md = samples\Sample05_SessionProcessor.md samples\Sample06_Transactions.md = samples\Sample06_Transactions.md samples\Sample07_CrudOperations.md = samples\Sample07_CrudOperations.md + samples\Sample08_Interop.md = samples\Sample08_Interop.md EndProjectSection EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Azure.Core", "..\..\core\Azure.Core\src\Azure.Core.csproj", "{9CD1905A-19B9-4F71-8BE6-D25CF49DA5B3}" diff --git a/sdk/servicebus/Azure.Messaging.ServiceBus/samples/Sample08_Interop.md b/sdk/servicebus/Azure.Messaging.ServiceBus/samples/Sample08_Interop.md index 7ae5dab73acda..40618ac22bd9a 100644 --- a/sdk/servicebus/Azure.Messaging.ServiceBus/samples/Sample08_Interop.md +++ b/sdk/servicebus/Azure.Messaging.ServiceBus/samples/Sample08_Interop.md @@ -1,6 +1,6 @@ -## CRUD operations +## Interop with `WindowsAzure.ServiceBus` -This sample demonstrates how to interoperate with messages that are sent or received using the `WindowsAzure.ServiceBus` library. The `WindowsAzure.ServiceBus` library uses the DataContractSerializer to serialize the BrokeredMessage body. Because of this, when attempting to interoperate with this library, there a few additional steps that are needed. +This sample demonstrates how to interoperate with messages that are sent or received using the `WindowsAzure.ServiceBus` library. The `WindowsAzure.ServiceBus` library uses the `DataContractSerializer` to serialize the `BrokeredMessage` body. Because of this, when attempting to interoperate with this library, there a few additional steps that are needed. ### Sending a message using `Azure.Messaging.ServiceBus` that will be received with `WindowsAzure.ServiceBus` @@ -10,8 +10,8 @@ ServiceBusSender sender = client.CreateSender(queueName); // If you use a strongly typed model here, the model properties will be serialized into XML. Since JSON is more commonly used, we will use it in our example, and // and specify the type as string, since we will provide a JSON string. var serializer = new DataContractSerializer(typeof(string)); -var stream = new MemoryStream(); -var writer = XmlDictionaryWriter.CreateBinaryWriter(stream); +using var stream = new MemoryStream(); +XmlDictionaryWriter writer = XmlDictionaryWriter.CreateBinaryWriter(stream); // serialize an instance of our type into a JSON string string json = JsonSerializer.Serialize(new TestModel {A = "Hello world", B = 5, C = true}); diff --git a/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Samples/Sample08_Interop.cs b/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Samples/Sample08_Interop.cs index 046783a91fe7a..90ca4baf72f21 100644 --- a/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Samples/Sample08_Interop.cs +++ b/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Samples/Sample08_Interop.cs @@ -29,8 +29,8 @@ public async Task TestInterop() // If you use a strongly typed model here, the model properties will be serialized into XML. Since JSON is more commonly used, we will use it in our example, and // and specify the type as string, since we will provide a JSON string. var serializer = new DataContractSerializer(typeof(string)); - var stream = new MemoryStream(); - var writer = XmlDictionaryWriter.CreateBinaryWriter(stream); + using var stream = new MemoryStream(); + XmlDictionaryWriter writer = XmlDictionaryWriter.CreateBinaryWriter(stream); // serialize an instance of our type into a JSON string string json = JsonSerializer.Serialize(new TestModel {A = "Hello world", B = 5, C = true});