Wednesday, 22 March 2017

Azure Service Bus: Serialization operation failed due to unsupported type

When trying to add an object property to my Service Bus Queue, I encountered the following error:

"Serialization operation failed due to unsupported type RichardLeeman.Model.MyObject."

So I added the  [Serializable] annotation to the class, but to no avail. The issue seems to be that the property bag only likes simple types like int or string.

So, my simple fix was to convert the class to JSON and put that in the bus. I used System.Web.Script.Serialization.JavaScriptSerializer to convert the object and placed the resulting string in the property bag.

var client = QueueClient.CreateFromConnectionString(SERVICE_BUS_CNN, "myqueue");
var json = new JavaScriptSerializer().Serialize(myObjectInstance);
var message = new BrokeredMessage("my test message");
message.Properties.Add(new KeyValuePair<string, object>("MyObjectProperty",json));
client.Send(message);





No comments:

Post a comment