我在Azure服務總線隊列中收到我無法接收的郵件。我沒有得到任何問題的指標。我認爲這與郵件大小有關。你可以從下面的代碼中看到我正在使用OpenFileDialog。我正在選擇jpeg圖像,並將它們發送到隊列中。現在,當我發送小於約50KB的小圖片時,它們在接收過程中顯示得很好,但超過100KB的較大圖片只停留在隊列中。 MSDN表示郵件大小限制爲256KB,因此我不確定這裏發生了什麼。Azure服務總線隊列 - QueueClient.Receive()當郵件在隊列中時返回null BrokeredMessage
我有兩個類。一個是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();
}
}
}
}
}
對不起,我意識到這是很晚,但我會建議你在接收代碼中添加某種日誌記錄。此代碼捕獲任何異常(與隊列相關或與文件相關),然後放棄該消息而不記錄。除非記錄異常詳細信息,否則不會看到實際的錯誤(因此可以修復它)。 – Joon 2017-04-04 16:09:44