我正在从基于 .NET 3.5 的 C# 解决方案向 Azure 服务总线队列发送 csv 文件。由于服务总线 nuget 包在 .NET 3.5 中不可用,因此我使用的是 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);
队列正确接收文件。在接收端,我可以使用 .NET 4.5。因此,我尝试使用以下代码获取消息正文:
BrokeredMessage message = queueClient.Receive(TimeSpan.FromSeconds(5));
if (message != null)
{
var contentType = message.ContentType;
var body = message.GetBody<byte[]>();
}
在这里,我按预期将 contentType 设置为 byte[] 。但是当我尝试获取 Body 时,出现以下错误:
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.
我做错了什么,我该如何解决?