Azure ServiceBus Queue - Serialization operation failed due to unsupported type System.Byte[]

Manoj Singh 41 Reputation points
2022-07-05T09:30:49.783+00:00

Hi,

I'm trying to send custom message to Azure ServiceBuz Queue. The custom message has a property which accepts Byte[]. While sending the message to queue using I'm getting "Serialization operation failed due to unsupported type System.Byte[]" error.

Below code is throwing the error:

     var serviceMessage = new ServiceBusMessage(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(replyMessage)))  
                    {  
      
                        MessageId = replyMessage.OriginatorMessageId,  
                        ApplicationProperties =  
                    {  
                        { "ByteArrayResponseContent", replyMessage.ByteArrayContent }  
      
                    }  
                    };  
                    await sender.SendMessageAsync(serviceMessage);  
  
  

If I do not use "await" for "SendMessageAsync" then it doesn't throw any error, but the message does not get delivered to the Queue.

var serviceMessage = new ServiceBusMessage(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(replyMessage)))  
                 {  
      
                     MessageId = replyMessage.OriginatorMessageId,  
                     ApplicationProperties =  
                 {  
                     { "ByteArrayResponseContent", replyMessage.ByteArrayContent }  
      
                 }  
                 };  
                 sender.SendMessageAsync(serviceMessage);  

Please suggest me how to resolve this error?

Thanks in advance!

Azure Service Bus
Azure Service Bus
An Azure service that provides cloud messaging as a service and hybrid integration.
700 questions
Developer technologies | C#
0 comments No comments
{count} votes

Accepted answer
  1. Bruce (SqlWork.com) 77,686 Reputation points Volunteer Moderator
    2022-07-05T15:30:17.827+00:00

    the default serialization is to json and byte arrays are not supported. you can pass an byte stream instead, but the receiver must have a custom de-serializer as GetBody<T> will not work.

    you could define a new message, and pass the byte array as a base64 or hex string


1 additional answer

Sort by: Most helpful
  1. Manoj Singh 41 Reputation points
    2022-07-06T11:07:26.903+00:00

    Hi @Bruce (SqlWork.com) , Thanks for your support.

    I was able to resolve the problem by following steps

    1. Change the ServiceBus plan from "Standard" to "Premium"
    2. I was trying to put large message body(6MB) into the Message Custom property section.
                      var serviceMessage = new ServiceBusMessage(convertMsgBodyToBase64String)  
                          {  
      
                              MessageId = replyMessage.OriginatorMessageId  
                          };  
                          sender.SendMessageAsync(serviceMessage);  
      

    Removing the message body data from message custom property resolved the issue and I could see my messages in ServiceBus Queue.

    Many Thanks!

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.