I have messages in an Azure Service Bus Queue that I can't receive. And I'm not getting any indicator as to what the problem is. I think it has something to do with message size. You can see from the code below that I'm using OpenFileDialog. I'm selecting jpeg images and they are getting sent to the queue. Now, when I send small images less than about 50KB, they are getting displayed fine by the receiving process, but larger ones over 100KB are just staying in the queue. MSDN says that message size limit is 256KB so I'm not sure what is going on here.
I have two classes. One is SendToQueue and the other is RecvFromQueue. Here is the code.
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();
}
}
}
}
}
Message size limit is 256 KB but this includes both the headers and the body, where the maximum header size is 64 KB. In your case header is not an issue (less than 1 KB)
I run your example with few minor changes:
And I was able to successfully send and receive message with this 254 KB image
If your message would be too large you would get MessageSizeExceededException when you call
client.Send(msg);
You can run this test on your queue to see if you can receive all messages, it passes for me.
Full gist here