Macnique
Macnique

Reputation: 1034

MSMQ:Get the count of messages from MSMQ queue in remote machine

I am trying to get the count of messages of a particular private queue in a remote machine. I am able to get the count from my local machine.

LocalMachine:

path=@".\Private$\Sample";

RemoteMachine:

Path=@"RemoteMachineName\Private$\Sample";

Full Code:

When i try the remote machine path it throws me the error invalid path .

I would appreciate if some one can guide me to the solution ?

 var path ="FormatName:Direct=OS:RemoteMachineName\\private$\\sample";

 MessageQueue queuename = new MessageQueue(path);
 var mgmt = new MSMQManagement();
 object machine = "RemoteMachineName";
 object queuename = queue.Path;         
 object formatname = "Direct=OS";
 mgmt.Init(ref machine, ref queuename, ref formatname);
 int messageCount = mgmt.MessageCount;
 MessageBox.Show(string.Format("Queue has {0} items", messageCount));

Upvotes: 1

Views: 4029

Answers (4)

Paul Kohler
Paul Kohler

Reputation: 2714

This is a paraphrased snippet that I use for bith remote and local queues. You need to get the format name right though, that can be a pain sometimes.

The arguments below to "msmqManagement.Init" are different for me than your example, try that perhaps.

TIP - post the actual Exception trace, it makes a difference. I don't think you are getting "error invalid path", the stack trace will give you clues, e.g. machine name resolution issues etc.

MessageQueue mq = new MessageQueue(path);
var formatName = mq.FormatName;
var msmqManagement = new MSMQManagement();
msmqManagement.Init(machineName, null, formatName);
var messageCount = msmqManagement.MessageCount;
Marshal.ReleaseComObject(msmqManagement);

Also - state what version of MSMQ are you using....

Upvotes: 0

rhatwar007
rhatwar007

Reputation: 343

      //strHostName is Machine name/IP from which you have to get queue
      //MSMQManagement present in COM library which is present at "C:\Program Files (x86)\Microsoft SDKs\Windows"
      PrivateQueueList =  MessageQueue.GetPrivateQueuesByMachine(strHostName);
      int count = PrivateQueueList.Count();
      MSMQManagement QueueManagement = new MSMQManagement[count];
      MSMQManagement msmq = null;      
      int index = 0;
      foreach(var queue in PrivateQueueList)
      {
           msmq = new MSMQManagement();          
           object machine = queue.MachineName;
           object path = null;
           object formate=queue.FormatName;
           msmq.Init(ref machine, ref path,ref formate);
           QueueManagement[index] = msmq;                                
           index++;
      }
      foreach(var queue in QueueManagement)
      {
           int count= queue.MessageCount();
           Console.WriteLine(queue.QueueName+ " ="+ count);
      }

Here you get all private msmq message count at any machine which is ping able. Let First confirm that machine is active or not by pinging. There is error showing if msmq have zero message in it at .Init method so you can use that statement in try catch and at catch set count =0 and it will be solved (I know it is wrong way but there is no another way to handle that error). For more details you can visit MSDN http://msdn.microsoft.com/en-us/library/ms705997(v=vs.85).aspx

Upvotes: 0

leskovar
leskovar

Reputation: 661

Try this code..

 MSMQManagement _manager = new MSMQManagement();
 _manager.Init("MACHINE", null, @"DIRECT=OS:MACHINE\PRIVATE$\sample");
 Console.WriteLine(_manager.MessageCount);
 Marshal.ReleaseComObject(_manager);

Upvotes: 1

John Breakwell
John Breakwell

Reputation: 4687

Three options are:

  1. Destination queue is not active (that is, has no messages and is not held open by an app)
  2. Function doesn't work on remote queues (although I can't find this specifically called out in MSDN)
  3. The API call uses RPC and the destination machine is blocking the RPC call.

Understanding how MSMQ security blocks RPC traffic

Cheers
John

Upvotes: 1

Related Questions