azure-sdk-for-net icon indicating copy to clipboard operation
azure-sdk-for-net copied to clipboard

[QUERY] Documentation Request for Send and Receive a ServiceBusMessage for a POCO

Open granadacoder opened this issue 3 years ago • 4 comments
trafficstars

Library name and version

Azure.Messaging.ServiceBus Version="7.7.0"

Query/Question

Would you please add an example in the documentation that is a simple POCO (and not only a string). If everything has to be json'fied as a string please explicitly say so.

Existing example:

string connectionString = "<connection_string>";
string queueName = "<queue_name>";
// since ServiceBusClient implements IAsyncDisposable we create it with "await using"
await using var client = new ServiceBusClient(connectionString);

// create the sender
ServiceBusSender sender = client.CreateSender(queueName);

// create a message that we can send. UTF-8 encoding is used when providing a string.
ServiceBusMessage message = new ServiceBusMessage("Hello world!");

// send the message
await sender.SendMessageAsync(message);

Add a basic POCO

public class MyPoco
{
  public string MyFirstString {get;set;} = "Howdy";
  public boolean MyFirstBoolean {get;set;} = true;
  public int MyFirstInt {get;set;} = 333;
  public DateTimeOffset MyFirstDateTimeOffset {get;set;} = DateTimeOffset.Now;

}

and the "Send"

MyPoco mp = new MyPoco();

ServiceBusMessage message = new ServiceBusMessage(mp);

???

And reciprocate the "Receive" example

// Or receive using the receiver
var receiver = new MessageReceiver(connectionString, queueName);
var receivedMessage = await receiver.ReceiveAsync();
MyPoco receivedMyPoco = receivedMessage.Body (???) ; 
await receiver.CompleteAsync(receivedMessage);

If the only option is to json'fy everything to a string, please explicitly say so.

The "old" libraries had generic's support for POCO's.

Old library reference : https://docs.microsoft.com/en-us/dotnet/api/microsoft.servicebus.messaging.brokeredmessage.getbody?view=azure-dotnet#microsoft-servicebus-messaging-brokeredmessage-getbody-1

public T GetBody<T> ();

and the constructor accepting a "T".

https://docs.microsoft.com/en-us/dotnet/api/microsoft.servicebus.messaging.brokeredmessage.-ctor?view=azure-dotnet#microsoft-servicebus-messaging-brokeredmessage-ctor(system-object)

Thank you.

Environment

DotNet-Core on Mac-Catalina.

granadacoder avatar May 09 '22 12:05 granadacoder

Thank you for your feedback. Tagging and routing to the team member best able to assist.

jsquire avatar May 09 '22 12:05 jsquire

Any advice (as a comment on this issue) on this "how do do POCO" question ... in the short term?

granadacoder avatar May 13 '22 17:05 granadacoder

Sure, take a look at https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Message/MessageLiveTests.cs#L267-L280. This shows how to serialize and deserialize a POCO to JSON.

Instead of using a JsonObjectSerializer, you can also just directly pass your POCO into the BinaryData constructor. This will result in the same JSON:

var message = new ServiceBusMessage(new BinaryData(myPoco));

JoshLove-msft avatar May 13 '22 17:05 JoshLove-msft

Thanks.

granadacoder avatar May 13 '22 18:05 granadacoder

Hi @granadacoder, we deeply appreciate your input into this project. Regrettably, this issue has remained unresolved for over 2 years and inactive for 30 days, leading us to the decision to close it. We've implemented this policy to maintain the relevance of our issue queue and facilitate easier navigation for new contributors. If you still believe this topic requires attention, please feel free to create a new issue, referencing this one. Thank you for your understanding and ongoing support.

github-actions[bot] avatar May 08 '24 18:05 github-actions[bot]