Link to home
Start Free TrialLog in
Avatar of HPFE455
HPFE455Flag for United States of America

asked on

How to send message to MSMQ public queue?

I have created a public queue in MSMQ. When I am trying to write/read messages to the queue, my application is throwing the below error.

  The queue does not exist or you do not have sufficient permissions to perform the operation.

   at System.Messaging.MessageQueue.ResolveFormatNameFromQueuePath(String queuePath, Boolean throwException)
   at System.Messaging.MessageQueue.get_FormatName()
   at System.Messaging.MessageQueue.SendInternal(Object obj, MessageQueueTransaction internalTransaction, MessageQueueTransactionType transactionType)
   at System.Messaging.MessageQueue.Send(Object obj)

My code is:
	private static string queueName = @".\test_queue";   
	static void Main(string[] args)
	{
		try
            {
                MessageQueue msgQ = new MessageQueue(queueName);
                string sMessage = "Test MSMQ Message 2";
                msgQ.Send(sMessage.Trim());
                System.Messaging.Message[] MSMQMessages = msgQ.GetAllMessages();

                foreach (System.Messaging.Message msg in MSMQMessages)
                {
                    byte[] data = new byte[1024];
                    msg.BodyStream.Read(data, 0, 1024);
                    string strMessage = ASCIIEncoding.ASCII.GetString(data);
                    Console.WriteLine(strMessage.Trim());
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

Open in new window


Thanks for your help
Avatar of it_saige
it_saige
Flag of United States of America image

My first inclination is that the queue does not exist.

Verify that you have a Public Queue named test_queue in Computer Management -> Services and Applications -> Message Queuing -> Public Queues
User generated imageYour code -
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Messaging;
using System.IO;

namespace EE_Q28586575
{
	class Program
	{
		private static string queueName = @".\test_queue";
		static void Main(string[] args)
		{
			try
			{
				MessageQueue msgQ = new MessageQueue(queueName);
				string sMessage = "Test MSMQ Message 2";
				msgQ.Send(sMessage.Trim());
				System.Messaging.Message[] MSMQMessages = msgQ.GetAllMessages();

				foreach (System.Messaging.Message msg in MSMQMessages)
				{
					byte[] data = new byte[1024];
					msg.BodyStream.Read(data, 0, 1024);
					string strMessage = ASCIIEncoding.ASCII.GetString(data);
					Console.WriteLine(strMessage.Trim());
				}
			}
			catch (Exception ex)
			{
				Console.WriteLine(ex.Message);
			}
			Console.ReadLine();
		}
	}
}

Open in new window

Produces the following output for me -User generated image-saige-
Avatar of HPFE455

ASKER

Thanks saige. I have checked the queue and it exists. (Actually I have created the  queue). Now I think my login may not have enough privileges to read and write messages to the queue.
ASKER CERTIFIED SOLUTION
Avatar of it_saige
it_saige
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial