Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am first time working with both windows service and MSMQ. I am trying to read messages from queue. When I start my windows service, i am receiving only first message and next message not able to read, service is still running. if I restart the service It is reading first message from the queue. Please let me know how to fix this issue.

What I have tried:

MessageQueue msMq = null;

protected override void OnStart(string[] args)
    {
        
        JobModel j = new JobModel();
        msMq = new MessageQueue(queueRequestName);
        msMq.ReceiveCompleted += MsMq_ReceiveCompleted1;
        try
        {            
            if (msMq != null)
            {                    
                msMq.Formatter = new XmlMessageFormatter(new Type[] { typeof(JobModel) });
                var message = (JobModel)msMq.BeginReceive();        

            }
        }      
        catch (Exception eee)
        {
            Console.Write(eee.ToString());
        }
        
    }
   private void MsMq_ReceiveCompleted(object sender, ReceiveCompletedEventArgs e)
        {
            var msMq = (MessageQueue)sender;
            var msg = msMq.EndReceive(e.AsyncResult); 
            //ToDo: process message          
            msMq.BeginReceive();
        }
Posted
Updated 28-Mar-18 22:05pm
v3

1 solution

Use something that keeps on listening. The Receive method on a MessageQueue will do that.

For example:
C#
var mq = new System.Messaging.MessageQueue(@".\Private$\SomeQueue");
while (mq.CanRead) // or any other loop condition
{
    var msg = mq.Receive();
    //do something with your message here
}

The Receive method will block untill a message is received at which point it'll remove the message from the queue and return it.

HTH Christiaan
 
Share this answer
 
Comments
Christiaan van Bergen 4-Apr-18 15:53pm    
Have a while-loop around it, otherwise it will still not continue listening after the message has been processed.
Member 11837253 4-Apr-18 16:25pm    
I added but no luck. I modified code
public partial class Service1 : ServiceBase
{
private const string MqName = @".\private$\RequestQueue";
private static MessageQueue _mq;

private static MessageQueue mq
{
get
{
if (_mq == null)
{
if (!MessageQueue.Exists(MqName))
MessageQueue.Create(MqName);
_mq = new MessageQueue(MqName, QueueAccessMode.ReceiveAndAdmin);
_mq.Formatter = new BinaryMessageFormatter();
}
return _mq;
}
}
public Service1()
{
InitializeComponent();
mq.ReceiveCompleted += new ReceiveCompletedEventHandler(mq_ReceiveCompleted);
}

private void mq_ReceiveCompleted(object sender, ReceiveCompletedEventArgs e)
{

MessageQueue cmq = (MessageQueue)sender;
try
{
Message msg = cmq.EndReceive(e.AsyncResult);
}
catch
{
}
cmq.Refresh();
cmq.BeginReceive();
}


protected override void OnStart(string[] args)
{
while(mq != null)
mq.BeginReceive();
}
private void MsMq_ReceiveCompleted(object sender, ReceiveCompletedEventArgs e)
{
var msMq = (MessageQueue)sender;
var msg = msMq.EndReceive(e.AsyncResult);
msMq.BeginReceive();
}
protected override void OnStop()
{
if (mq != null)
mq.Close();
return;

}
}
Member 11837253 4-Apr-18 16:27pm    
Windows service picking messages only when i start it and after that it stops listening
Christiaan van Bergen 4-Apr-18 16:34pm    
You are not implementing the code I showed you. In your OnStart method use mq.Receive(), I also explained why you should use that method. Try it, if it still doesn't work, we'll take it from there.
Christiaan van Bergen 4-Apr-18 17:13pm    
No, that's right. Because you need to do something with the actual message you're getting. Again, look at the example. It says: var msg = mq.Receive();
So do something with the message.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900