I am sending a csv file to an Azure Service Bus Queue from a .NET 3.5 based C# solution. Since the Service Bus nuget packages aren't available in .NET 3.5, I am using the Rest API.
byte[] file = File.ReadAllBytes(@"VehicleContacts.csv");
string url = baseAddress + queueName + "/messages" + "?timeout=60&api-version=2013-08 ";
WebClient webClient = new WebClient();
webClient.Proxy = proxy;
webClient.Headers[HttpRequestHeader.Authorization] = token;
// Add Broker Properties
webClient.Headers.Add("BrokerProperties", "{ \"Label\":\"VehicleContactsSync\" }");
// Add Custom Properties
webClient.Headers.Add("FileName", "VehicleContactsSyncFile");
webClient.UploadData(url, "POST", file);
The queue receives the file properly. On the receiving side, I can use .NET 4.5. So, I try to get the message body using the following code:
BrokeredMessage message = queueClient.Receive(TimeSpan.FromSeconds(5));
if (message != null)
{
var contentType = message.ContentType;
var body = message.GetBody<byte[]>();
}
Here I get the contentType as byte[] as expected. But when I try to get the Body, I get the following error:
System.Runtime.Serialization.SerializationException occurred
HResult=0x8013150C
Message=There was an error deserializing the object of type System.Byte[].
The input source is not correctly formatted.
Source=System.Runtime.Serialization
StackTrace:
at System.Runtime.Serialization.XmlObjectSerializer.ReadObjectHandleExceptions(XmlReaderDelegator reader, Boolean verifyObjectName, DataContractResolver dataContractResolver)
at System.Runtime.Serialization.DataContractSerializer.ReadObject(XmlDictionaryReader reader, Boolean verifyObjectName)
at System.Runtime.Serialization.XmlObjectSerializer.ReadObjectHandleExceptions(XmlReaderDelegator reader, Boolean verifyObjectName, DataContractResolver dataContractResolver)
at System.Runtime.Serialization.XmlObjectSerializer.ReadObject(XmlDictionaryReader reader)
at Microsoft.ServiceBus.Messaging.BrokeredMessage.GetBody[T](XmlObjectSerializer serializer)
Inner Exception 1:
XmlException: The input source is not correctly formatted.
What am I doing wrong and how can I fix it?