views:

91

answers:

2

New to MSMQ... My local queue never receives my test message. I have verified that Message Queuing and Message Queuing Triggers are running in my Services dialog (Vista Ultimate). Thoughts?

class ConsoleApplication
{
    private const string Path = @".\private$\SomeQueue";

    static void Main(string[] args)
    {
        var queue = !MessageQueue.Exists(Path) 
         ? MessageQueue.Create(Path) 
         : new MessageQueue(Path) { Formatter = new BinaryMessageFormatter() };

        queue.ReceiveCompleted += queue_ReceiveCompleted;
        queue.BeginReceive();
        queue.Send("test message");
        Console.ReadLine();
        queue.Close();
    }

    static void queue_ReceiveCompleted(object sender, 
                                        ReceiveCompletedEventArgs e)
    {
        Console.WriteLine("Received message...");
        var queue = (MessageQueue)sender;
        try
        {
            var message = queue.EndReceive(e.AsyncResult);
            Console.WriteLine("Processing message...");
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
        finally
        {
            queue.BeginReceive();
        }
    }
}

EDIT: Just had a thought, try deleting and re-creating the queue with MessageQueue.Delete(Path). And it worked. I guess my queue was hosed somehow.

A: 

Hi

I doubt that the queue itself was broken. More likely a timing issue between creating the queue and accessing it. MSMQ has to perform various operations, such as create a config file in the LQS directory, before the private queue is available.

Cheers
John Breakwell

John Breakwell
A: 

You can't access the console concurrently. Try doing a thread.Sleep() and set a boolean in your callback. Then you can do a readline after the boolean is set to read your output.

while (!received)
{
    System.Threading.Thread.Sleep(1000);
}
Console.ReadLine();

Perhaps you might want to use a breakpoint and the debug.WriteLine() to read output written into the visual studio output window.

Spence