Azure Service Bus BrokeredMessage GetBody方法在读取通过Rest API发送的数据时抛出SerializationException

时间:2017-04-24 20:44:19

标签: azure serialization azureservicebus azure-servicebus-queues brokeredmessage

我正在从基于.NET 3.5的C#解决方案向Azure Service Bus Queue发送csv文件。由于.NET 3.5中没有Service Bus nuget包,我使用的是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.

我做错了什么,如何解决?

1 个答案:

答案 0 :(得分:1)

  

XmlException:输入源格式不正确。

根据您的描述,我们可以知道格式化的身体不正确。 在您的情况下,您似乎使用CSV格式文件。

document,我们可以知道BrokeredMessage.GetBody<T>方法,使用带有二进制System.Runtime.Serialization.DataContractSerializer的{​​{1}}将代理邮件正文反序列化为指定类型的对象

  

我做错了什么,如何解决?

根据Azure service bus send message API,我们知道我们需要使用Xml格式的邮件正文。

因此,请尝试将文件更改为xml格式,然后才能解决。

修改

在发送数据之前,请尝试 DataContractSerializer

System.Xml.XmlDictionaryReader

我为它做了一个演示,它在我身边正常工作。

1.创建一个控制台项目

2.创建发送消息的令牌

 MemoryStream ms = new MemoryStream();
 DataContractSerializer serializer = new DataContractSerializer(typeof(byte[]));
 serializer.WriteObject(ms, file);
 byte[] body = ms.ToArray();
 WebClient webClient = new WebClient { Headers = { [HttpRequestHeader.Authorization] = token } };
 // Add Broker Properties
 webClient.Headers.Add("BrokerProperties", "{ \"Label\":\"VehicleContactsSync\" }");
 // Add Custom Properties 
 webClient.Headers.Add("FileName", "VehicleContactsSyncFile");
 webClient.UploadData(queueUrl, "POST", body);

3.add创建队列功能

 public static string GetSasToken(string resourceUri, string keyName, string key, TimeSpan ttl)
        {
            var expiry = GetExpiry(ttl);
            string stringToSign = HttpUtility.UrlEncode(resourceUri) + "\n" + expiry;
            HMACSHA256 hmac = new HMACSHA256(Encoding.UTF8.GetBytes(key));

            var signature = Convert.ToBase64String(hmac.ComputeHash(Encoding.UTF8.GetBytes(stringToSign)));
            var sasToken = String.Format(CultureInfo.InvariantCulture, "SharedAccessSignature sr={0}&sig={1}&se={2}&skn={3}",
                HttpUtility.UrlEncode(resourceUri), HttpUtility.UrlEncode(signature), expiry, keyName);
            return sasToken;
        }

        private static string GetExpiry(TimeSpan ttl)
        {
            TimeSpan expirySinceEpoch = DateTime.UtcNow - new DateTime(1970, 1, 1) + ttl;
            return Convert.ToString((int)expirySinceEpoch.TotalSeconds);
        }

4.add接收队列消息功能

 public static void CreateQueue()
        {
            var cs = "connection string";
            var queueName = "queuename";
            var namespaceManager = NamespaceManager.CreateFromConnectionString(cs);
            var messagingFactory = MessagingFactory.CreateFromConnectionString(cs);
            var ver = namespaceManager.GetVersionInfo();

            if (namespaceManager.QueueExists(queueName))
            {
                namespaceManager.DeleteQueue(queueName);
            }

            namespaceManager.CreateQueue(queueName);
}

5。添加带http功能的发送消息

public static void ReceiveMessage()
        {
            BrokeredMessage message = queueClient.Receive();
            if (message != null)
            {
                var body = message.GetBody<byte[]>(new DataContractSerializer(typeof(byte[])));
                Console.WriteLine(body);
            }
            Console.ReadKey();
        }

6.在主要功能中测试

public static void SendHttpMessage()
        {
            byte[] file = File.ReadAllBytes(@"C:\tom\test.txt");
            string queueUrl = "https://servicebusname.servicebus.windows.net/" + "SampleQueue" + "/messages?timeout=60&api-version=2013-08";
            string token = GetSasToken(queueUrl,
                "Key name", "Key", TimeSpan.FromDays(1));
            MemoryStream ms = new MemoryStream();
            DataContractSerializer serializer = new DataContractSerializer(typeof(byte[]));
            serializer.WriteObject(ms, file);
            byte[] body = ms.ToArray();
            WebClient webClient = new WebClient { Headers = { [HttpRequestHeader.Authorization] = token } };
            // Add Broker Properties
            webClient.Headers.Add("BrokerProperties", "{ \"Label\":\"VehicleContactsSync\" }");
            // Add Custom Properties 
            webClient.Headers.Add("FileName", "VehicleContactsSyncFile");

            webClient.UploadData(queueUrl, "POST", body);

        }

7.在发送消息时我使用fiddler来捕获http请求,详情请参考截图。我们可以看到已发送邮件已格式化

enter image description here

  1. 收到消息。
  2. enter image description here

    Package.config文件

      private static QueueClient queueClient;
    
    
            static void Main(string[] args)
            {
               CreateQueue();
               SendHttpMessage();
               ReceiveMessage();
            }