0

I want to transfer messages using MSMQ. When I'm sending a message, I pass an enum (StudentMessageType) as AppSpecific so I can recognize what should I do in the destination. But when I try to receive the message, I can't access AppSpecific property and I get this exception:

Property AppSpecific was not retrieved when receiving the message. Ensure that the PropertyFilter is set correctly.

What should I do to receive AppSpecific?

Send

var requestQueue = new MessageQueue(@".\private$\req");
requestQueue.MessageReadPropertyFilter.AppSpecific = true;
requestQueue.Formatter = new BinaryMessageFormatter();
Message studentNameMessage = new Message(studentName, new BinaryMessageFormatter());
studentNameMessage.AppSpecific = (int) Student.StudentMessageType.AddStudent;
requestQueue.Send(studentNameMessage, MessageQueueTransactionType.Single);
requestQueue.Close();

Receive

requestQueue = new MessageQueue(@".\private$\req");
requestQueue.Formatter = new BinaryMessageFormatter();
MessageQueueTransaction transaction = new MessageQueueTransaction();
    try
        {
        transaction.Begin();
                Message message = requestQueue.Receive(transaction);
                output = (string)message.Body;
                output += "\t" + ((Student.StudentMessageType) message.AppSpecific).ToString();
                transaction.Commit();
        }
    catch (Exception e)
        {
                Console.WriteLine(e);
                throw;
        }
Chainsw
  • 37
  • 7
  • See the example. I have two questions 1)Why do you have a dollar sign? 2)Where did you attach message to body? Here is example : https://learn.microsoft.com/en-us/dotnet/api/system.messaging.binarymessageformatter?view=netframework-4.8 – jdweng Sep 07 '19 at 07:26
  • @jdweng 1. I don't know why! As long as it works, it's fine 2.First Argument of Message constructor (I get the message correctly, I just don't get AppSpecific) – Chainsw Sep 07 '19 at 07:44
  • I think you mean you get a response and the message is empty. Well if you do not put anything in the body you will not get the AppSpecific. – jdweng Sep 07 '19 at 08:02
  • @jdweng I get a response and it's not empty, it's the string that I passed to the first argument of Message Object (studentName). – Chainsw Sep 07 '19 at 08:25

1 Answers1

1

omg, I just forget this line in receive method:

requestQueue.MessageReadPropertyFilter.AppSpecific = true;
Chainsw
  • 37
  • 7