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
C#Windows Server 2003

Avatar of undefined
Last Comment
it_saige
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
HPFE455
Flag of United States of America image

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

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Windows Server 2003
Windows Server 2003

Windows Server 2003 was based on Windows XP and was released in four editions: Web, Standard, Enterprise and Datacenter. It also had derivative versions for clusters, storage and Microsoft’s Small Business Server. Important upgrades included integrating Internet Information Services (IIS), improvements to Active Directory (AD) and Group Policy (GP), and the migration to Automated System Recovery (ASR).

129K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo