[BUG] New ServiceBusMessage Body empty if BinaryData created with FromObjectAsJson method #46006
Labels
Client
This issue points to a problem in the data-plane of the library.
customer-reported
Issues that are reported by GitHub users external to the Azure organization.
needs-author-feedback
Workflow: More information is needed from author to address the issue.
question
The issue doesn't require a change to the product in order to be resolved. Most issues start as that
Service Bus
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 toFromObjectAsJson
.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 withMyRequest
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.
The text was updated successfully, but these errors were encountered: