【发布时间】:2019-09-07 16:46:51
【问题描述】:
我想使用 MSMQ 传输消息。当我发送消息时,我将枚举 (StudentMessageType) 作为 AppSpecific 传递,这样我就可以识别在目的地应该做什么。但是当我尝试接收消息时,我无法访问 AppSpecific 属性并且我得到了这个异常:
接收消息时未检索到属性 AppSpecific。 确保 PropertyFilter 设置正确。
我应该怎么做才能收到 AppSpecific?
发送
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();
接收
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;
}
【问题讨论】:
-
查看示例。我有两个问题 1) 为什么你有美元符号? 2)您在哪里将消息附加到正文?这是示例:docs.microsoft.com/en-us/dotnet/api/…
-
@jdweng 1. 我不知道为什么! 2.Message构造函数的First Argument(我得到正确的消息,我只是没有得到AppSpecific)
-
我认为您的意思是您收到了回复,但消息为空。好吧,如果你不把任何东西放在身体里,你就不会得到 AppSpecific。
-
@jdweng 我得到一个响应,它不是空的,它是我传递给消息对象(studentName)的第一个参数的字符串。