Azure / azure-sdk-for-net

This repository is for active development of the Azure SDK for .NET. For consumers of the SDK we recommend visiting our public developer docs at https://learn.microsoft.com/dotnet/azure/ or our versioned developer docs at https://azure.github.io/azure-sdk-for-net.
MIT License
5.26k stars 4.6k forks source link

[BUG] New ServiceBusMessage Body empty if BinaryData created with FromObjectAsJson method #46006

Open ivandamyanov opened 3 days ago

ivandamyanov commented 3 days ago

Library name and version

Azure.Messaging.ServiceBus 7.18.1

Describe the bug

I have a type called MyRequest with a public property TransactionId. I'm trying to create a ServiceBusMessage instance in the following way: var message = new ServiceBusMessage(BinaryData.FromObjectAsJson(MyRequest)) { MessageId = messageId, Subject = "mySubject", }; After inspecting the message.Body property - it is empty even though TransactionId is already a valid Guid in the MyRequest instance passed to FromObjectAsJson.

As a workaround, I used Newtonsoft JsonConvert and UTF8.GetBytes like this and it works fine - the body is not empty: var serializedMessageBody = Newtonsoft.Json.JsonConvert.SerializeObject(MyRequest); var bytes = Encoding.UTF8.GetBytes(serializedMessageBody); return new ServiceBusMessage(new BinaryData(bytes)) { MessageId = messageId, Subject = messageLabel };

Any idea what is the issue with BinaryData not filling in the Body property properly depending on the serializer? By the way, this seems to depend on the class being serialized because in another piece of code I have with AnotherRequest, FromObjectAsJson is used and then the Body is filled correctly.

Expected behavior

The Body should be filled in both cases

Actual behavior

The Body of ServiceBusMessage is empty

Reproduction Steps

I think using classes like the ones explained below will allow a reproduction of the issue:

The different between this piece of code with AnotherRequest and the one above with MyRequest is that:

AnotherRequest has these attributes (and it works with FromObjectAsJson): [System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Tools.ServiceModel.Svcutil", "2.1.0")] [System.Diagnostics.DebuggerStepThroughAttribute()] [System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://example.namespace")]

and the public properties have: [System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified, Order = 0)]

while the MyRequest class has these

[System.Diagnostics.DebuggerStepThroughAttribute()] [System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Tools.ServiceModel.Svcutil", "2.1.0")] [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)] [System.ServiceModel.MessageContractAttribute(WrapperName="OnConfirmationRequest", WrapperNamespace="http://example.namespace", IsWrapped=true)] and the properties:

[System.ServiceModel.MessageBodyMemberAttribute(Namespace="http://SecureApp/2017/SecureAppResponse", Order=0)]

Environment

Hosting in a ServiceFabric cluster on a Windows OS. The code is in a .NET 6 project.

github-actions[bot] commented 3 days ago

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

jsquire commented 2 days ago

Hi @ivandamyanov. Thanks for reaching out and we regret that you're experiencing difficulties. Service Bus does not inspect nor alter the bytes passed to it for the body. If you're not seeing the body populated when using BinaryData but are when using Newtonsoft, it seems likely there's something in your structure that System.Text.Json handles differently than Newtonsoft.

Unfortunately, there's not enough context to speculate further. Can you share a small, stand-alone sample that reproduces the behavior that you're asking about?

github-actions[bot] commented 2 days ago

Hi @ivandamyanov. Thank you for opening this issue and giving us the opportunity to assist. To help our team better understand your issue and the details of your scenario please provide a response to the question asked above or the information requested above. This will help us more accurately address your issue.