无法从服务总线读取消息。它返回null

时间:2017-09-13 19:43:09

标签: azure azureservicebus servicebus azure-servicebus-queues

我正在尝试读取Azure Service总线上的流。但我得到空字节。正在创建的文件大小与正在发送的字节大小相同,但它包含所有空值。我添加了用于将Stream转换为Byte数组的代码,其函数名称为ReadAllBytes(Stream source)

下面是参考代码:

         static void Main(string[] args)
         {
            MemoryStream largeMessageStream = new MemoryStream();
            #region ReceiveMessage


            var msg = Microsoft.ServiceBus.NamespaceManager.CreateFromConnectionString(ConfigurationSettings.AppSettings["Microsoft.ServiceBus.ConnectionString"].ToString());
            var numofmessages = msg.GetQueue(AccountDetails.QueueName).MessageCountDetails.ActiveMessageCount.ToString();
            if (msg.GetQueue(AccountDetails.QueueName).RequiresSession)
            {
                var queueClient1 = QueueClient.CreateFromConnectionString(ConfigurationSettings.AppSettings["Microsoft.ServiceBus.ConnectionString"].ToString(), AccountDetails.QueueName);
                var session = queueClient1.AcceptMessageSession();

                Console.WriteLine("Message session Id: " + session.SessionId);
                Console.Write("Receiving sub messages");

                while (true)
                {
                    // Receive a sub message
                    BrokeredMessage subMessage = session.Receive(TimeSpan.FromSeconds(5));

                    if (subMessage != null)
                    {
                        // Copy the sub message body to the large message stream.
                        Stream subMessageStream = subMessage.GetBody<Stream>();
                        subMessageStream.CopyTo(largeMessageStream);
                        // Mark the message as complete.
                        subMessage.Complete();
                        Console.Write(".");
                    }
                    else
                    {
                        // The last message in the sequence is our completeness criteria.
                        Console.WriteLine("Done!");
                        break;
                    }
                }

                // Create an aggregated message from the large message stream.
                BrokeredMessage largeMessage = new BrokeredMessage(largeMessageStream, true);

                Console.WriteLine("Received message");
                Console.WriteLine("Message body size: " + largeMessageStream.Length);

                string testFile = @"D:\Dev\csvData1.csv";
                Console.WriteLine("Saving file: " + testFile);

                // Save the message body as a file.
                Stream resultStream = largeMessage.GetBody<Stream>();
                byte[] x = ReadAllBytes(resultStream);

                File.WriteAllBytes(testFile, x);
            }


    public static byte[] ReadAllBytes(Stream source)
    {
        long originalPosition = source.Position;
        source.Position = 0;

        try
        {
            byte[] readBuffer = new byte[source.Length];
            int totalBytesRead = 0;
            int bytesRead;
            while ((bytesRead = source.Read(readBuffer, totalBytesRead, readBuffer.Length - totalBytesRead)) > 0)
            {
                totalBytesRead += bytesRead;
                if (totalBytesRead == readBuffer.Length)
                {
                    int nextByte = source.ReadByte();
                    if (nextByte != -1)
                    {
                        byte[] temp = new byte[readBuffer.Length * 2];
                        Buffer.BlockCopy(readBuffer, 0, temp, 0, readBuffer.Length);
                        Buffer.SetByte(temp, totalBytesRead, (byte)nextByte);
                        readBuffer = temp;
                        totalBytesRead++;
                    }
                }
            }

            byte[] buffer = readBuffer;
            if (readBuffer.Length != totalBytesRead)
            {
                buffer = new byte[totalBytesRead];
                Buffer.BlockCopy(readBuffer, 0, buffer, 0, totalBytesRead);
            }
            return buffer;
        }
        finally
        {
            source.Position = originalPosition;
        }
    }

1 个答案:

答案 0 :(得分:0)

我使用以下代码发送带有流体的消息,然后我收到消息并在我身边测试您的代码,代码适用于我。

SELECT u.id, 
       u.application_id,
       u.login,
       u.phone,
       #...
FROM users as u
WHERE u.application_id = 1234
ORDER BY u.id+0

我的source.txt

using (MemoryStream stream = new MemoryStream(File.ReadAllBytes(@"C:\Users\xxx\Desktop\source.txt")))
{
    client.Send(new BrokeredMessage(stream));
}

字节数组

enter image description here

控制台应用输出

enter image description here

<强> csvData1.csv

enter image description here

如果可能,您可以尝试使用流体发送新消息并执行代码以检查它是否可以正常工作。