读取通过 Rest API 发送的数据时,Azure 服务总线 BrokeredMessage GetBody 方法抛出 SerializationException

标签 azure serialization azureservicebus azure-servicebus-queues brokeredmessage

我正在从基于 .NET 3.5 的 C# 解决方案将 csv 文件发送到 Azure 服务总线队列。由于服务总线 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[]>();
}

这里我按预期得到了 byte[] 形式的 contentType。但是当我尝试获取 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.

我做错了什么以及如何解决它?

最佳答案

XmlException: The input source is not correctly formatted.

根据您的描述,我们可以知道正文格式不正确。 就您而言,您似乎使用了 CSV 格式文件。

来自document ,我们可以知道BrokeredMessage.GetBody<T>使用 System.Runtime.Serialization.DataContractSerializer 将代理的消息正文反序列化为指定类型的对象的方法与二进制 System.Xml.XmlDictionaryReader .

What am I doing wrong and how can I fix it?

根据Azure service bus send message API ,我们知道我们需要使用Xml格式的消息体。

所以请尝试将文件更改为xml格式即可解决。

编辑:

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

 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);

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

1.创建控制台项目

2.创建发送消息的token

 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);
        }

3.添加创建队列功能

 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);
}

4.添加接收队列消息功能

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();
        }

5.添加http发送消息功能

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);

        }

6.在main函数中测试

  private static QueueClient queueClient;


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

7.在发送消息时,我使用fiddler来捕获http请求,详细信息请参见屏幕截图。我们可以看到发送的消息已被格式化

enter image description here

  • 接收消息。
  • enter image description here

    Package.config 文件

    <?xml version="1.0" encoding="utf-8"?>
    
        <packages>
          <package id="WindowsAzure.ServiceBus" version="4.0.0" targetFramework="net452" />
        </packages>
    

    关于读取通过 Rest API 发送的数据时,Azure 服务总线 BrokeredMessage GetBody 方法抛出 SerializationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43597390/

    相关文章:

    azure - 如何让 Azure Active Directory B2C 与 Bot Framework 配合使用?

    java - 通过 Java JDBC 从 Azure VM 连接 Azure SQL 数据库的问题

    c++ - IplImage 结构的 boost 序列化问题

    java - XStream-不包含某些值

    c# - Azure服务总线消息重新出现超时

    c# - 在 OnMessageOptions 中使用 ExceptionReceived

    c# - 使用适用于 .Net 的 Azure 管理库创建具有 Azure 访问权限的 SQL Server

    azure - 如何嵌套Azure函数应用程序(如何调用另一个函数应用程序)

    php - 在 PHP 中插入序列化数组

    c# - Azure 服务总线 - 多个主题?