【问题标题】:Unable to read message from Service bus. it is returning null无法从服务总线读取消息。它返回 null
【发布时间】:2017-09-13 19:43:09
【问题描述】:

我正在尝试读取 Azure 服务总线上的流。但我得到空字节。正在创建的文件大小与正在发送的字节大小相同,但它包含所有空值。我添加了用于将 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;
        }
    }

【问题讨论】:

  • 能否添加ReadAllBytes 实现并删除注释掉的代码?
  • 感谢您的回复。我已经更新了代码。请检查一下。
  • 谢谢。还尝试将代码减少到最小大小以重现问题,例如试着找出你在什么时候得到空值。
  • 当我尝试从 brokerMessage 获取字节时,我在 byte[] x = ReadAllBytes(resultStream);
  • 原始消息是如何生成的?是你的代码你可以share吗?您是否尝试使用其中一种工具(ServiceBus Explorer、ServiceBus360 或 Cerebrata Cerulean)查看消息以确认消息有效?

标签: azure azureservicebus servicebus azure-servicebus-queues


【解决方案1】:

我使用以下代码发送带有流正文的消息,然后我收到消息并在我这边测试您的代码,该代码对我有用。

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

我的来源.txt

"ID", "Age", "Rich", "timestamp"
1, "50", "Y", "2017-06-06 14:19:21.77"
2, "22", "N", "2017-06-06 14:19:21.77"

字节数组

控制台应用输出

csvData1.csv

如果可能,您可以尝试发送一条带有流正文的新消息并执行您的代码以检查它是否可以正常工作。

【讨论】:

  • 嗨@user3458700,有什么更新吗?如果您发送带有流正文的新消息,您的代码可以正常工作吗?
猜你喜欢
  • 2022-07-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-11
  • 2015-05-15
  • 2020-02-20
  • 1970-01-01
相关资源
最近更新 更多