我在Azure服务总线队列中有消息,我无法接收。而且我没有得到任何关于问题的指标。我认为这与邮件大小有关。您可以从下面的代码中看到我正在使用OpenFileDialog。我正在选择jpeg图像并将它们发送到队列中。现在,当我发送小于约50KB的小图像时,它们在接收过程中显示正常,但是大于100KB的较大图像只是停留在队列中。 MSDN说消息大小限制是256KB所以我不确定这里发生了什么。
我有两节课。一个是SendToQueue,另一个是RecvFromQueue。这是代码。
using System;
using System.IO;
using System.Windows.Forms;
using Microsoft.ServiceBus;
using Microsoft.ServiceBus.Messaging;
using Microsoft.WindowsAzure;
namespace ServiceBusQueueApp
{
public class SendToQueue
{
private const string c_testqueue = "TestQueue";
[STAThreadAttribute]
static void Main(string[] args)
{
// Configure Queue Settings
QueueDescription qd = new QueueDescription(c_testqueue)
{
MaxSizeInMegabytes = 5120,
DefaultMessageTimeToLive = new TimeSpan(1, 1, 0)
};
// Create a new Queue with custom settings
string connectionString =
CloudConfigurationManager.GetSetting("Microsoft.ServiceBus.ConnectionString");
var namespaceManager =
NamespaceManager.CreateFromConnectionString(connectionString);
if (!namespaceManager.QueueExists(c_testqueue))
{
namespaceManager.CreateQueue(qd);
}
namespaceManager.DeleteQueue(qd.Path);
namespaceManager.CreateQueue(qd);
QueueClient client = QueueClient.CreateFromConnectionString(connectionString, c_testqueue);
double maxSize = Math.Pow(2, 18);
OpenFileDialog openFile = new OpenFileDialog();
while (true)
{
if (openFile.ShowDialog() == DialogResult.Cancel)
{
break;
}
var messageBodyStream = new FileStream(openFile.FileName, System.IO.FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
if (messageBodyStream.Length > maxSize)
{
MessageBox.Show("File is larger than 256KB.");
continue;
}
BrokeredMessage msg =
new BrokeredMessage(messageBodyStream);
msg.Properties["MyProperty"] = "Test Value";
try
{
//send msg to the queue
client.Send(msg);
}
catch (Exception exception)
{
MessageBox.Show(exception.Message);
throw;
}
}
}
}
}
using System;
using System.Diagnostics;
using System.IO;
using System.Threading;
using System.Windows.Forms;
using Microsoft.ServiceBus;
using Microsoft.ServiceBus.Messaging;
using Microsoft.WindowsAzure;
namespace ServiceBusQueueApp
{
class RecvFromQueue
{
private const string c_testqueue = "TestQueue";
static void Main(string[] args)
{
// Create a new Queue with custom settings
string connectionString =
CloudConfigurationManager.GetSetting("Microsoft.ServiceBus.ConnectionString");
var namespaceManager =
NamespaceManager.CreateFromConnectionString(connectionString);
if (!namespaceManager.QueueExists(c_testqueue))
{
MessageBox.Show("Queue does not exist.");
throw new Exception("Queue does not exist.");
}
QueueClient client = QueueClient.CreateFromConnectionString(connectionString, c_testqueue);
while (true)
{
BrokeredMessage message = client.Receive();
if (message == null)
{
continue;
}
try
{
Stream fstream = message.GetBody<Stream>();
byte[] buffer = new byte[fstream.Length];
fstream.Read(buffer, 0, (int)fstream.Length);
File.WriteAllBytes(@"C:\users\roberthar\pictures\testpic.png", buffer);
fstream.Close();
Process paint = new Process();
paint.StartInfo.FileName = @"C:\Windows\System32\mspaint.exe";
paint.StartInfo.Arguments = @"C:\users\roberthar\pictures\testpic.png";
paint.Start();
Thread.Sleep(3000);
paint.Close();
// Remove message from queue
message.Complete();
}
catch (Exception exception)
{
// Indicate a problem, unlock message in queue
message.Abandon();
}
}
}
}
}
答案 0 :(得分:1)
邮件大小限制 256 KB 但这包括标头和正文,其中最大标头大小为 64 KB 。在您的情况下,标题不是问题(小于1 KB)
我运行你的例子,只做了一些小改动:
string filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory,
string.Format("testpic_{0}_{1}_{2}.png", now.Hour, now.Minute, now.Second));
File.WriteAllBytes(filePath, buffer);
fstream.Close();
Process paint = new Process();
paint.StartInfo.FileName = @"C:\Windows\System32\mspaint.exe";
paint.StartInfo.Arguments = filePath;
paint.Start();
我能够使用this 254 KB image
成功发送和接收消息如果您的邮件太大,当您致电client.Send(msg);
时,您会收到MessageSizeExceededException
您可以在队列中运行此测试以查看是否可以接收所有消息,它会传递给我。
[Fact]
public void MaxMessageSize()
{
var sender = CreateClient();
var reciver = CreateClient();
for (int i = 1; i < 255; i++)
{
var size = i*1024;
var buffer = new byte[size];
random.NextBytes(buffer);
BrokeredMessage msg =
new BrokeredMessage(buffer);
msg.Properties["size"] = size;
sender.Send(msg);
var message = reciver.Receive();
Assert.NotNull(message);
Assert.Equal(message.Properties["size"], size);
var bufferReceived = message.GetBody<byte[]>();
Assert.Equal(buffer, bufferReceived);
message.Complete();
}
}
答案 1 :(得分:0)
我偶然发现了帮助同事的这个问题。 (我保证这是另一个开发!)
我们在运行检查queue.MessageCount(将其设置为名称myQMessageCount的变量)的代码时遇到了这个问题,代码有&#34;而(myQMessageCount&gt; 0)&#34;并且他在每个msg.Complete之后重置了queuecount(在同一个while循环中)
结果.MessageCount是&#34;总和&#34;队列中的所有消息,包括Active(您应该能够读取的消息)和死信以及其他消息。
所以(1),修复是让他改变他的代码来检查ActiveMessageCount,而不是.MessageCount
Microsoft.ServiceBus.Messaging.QueueDescription qd = myMicrosoftdotServiceBusdotNamespaceManager.GetQueue(qName);
string deadLetterQueueName = QueueClient.FormatDeadLetterPath(qd.Path);
int activeMessageCount = qd.MessageCountDetails.ActiveMessageCount;
int deadLetterMessageCount = qd.MessageCountDetails.DeadLetterMessageCount;
int scheduledMessageCount = qd.MessageCountDetails.ScheduledMessageCount;
int transferDeadLetterMessageCount = qd.MessageCountDetails.TransferDeadLetterMessageCount;
int transferMessageCount = qd.MessageCountDetails.TransferMessageCount;
和(2),在我们讨论之后,继续检查ActiveMessageCount可能并不明智,只需让返回的null BrokeredMessage检查队列中没有其他消息。
反正。我在这里发布这个答案给未来的读者,他们可能会遇到他们正在编写的一些自定义读取队列代码。