【发布时间】: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