-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add interop sample * File header * PR FB
- Loading branch information
1 parent
0fda97c
commit 416b57d
Showing
5 changed files
with
134 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
sdk/servicebus/Azure.Messaging.ServiceBus/samples/Sample08_Interop.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
## 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. | ||
|
||
### 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)); | ||
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}); | ||
|
||
// 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<TestModel>(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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
sdk/servicebus/Azure.Messaging.ServiceBus/tests/Samples/Sample08_Interop.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// 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; | ||
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)); | ||
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}); | ||
|
||
// 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<TestModel>(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; } | ||
} | ||
} | ||
} |