如何在IronPython中使用标签,回复队列和其他属性创建新的MSMQ消息 [英] How to create a new MSMQ message in IronPython with label, reply queue and other properties
问题描述
我在下面的示例中此处将MS Message Queues与IronPython一起使用. 该示例将创建一个没有任何属性的消息文本字符串.
I'm following this example here to use MS Message Queues with IronPython. The example works to create a message text string without any properties.
import clr
clr.AddReference('System.Messaging')
from System.Messaging import MessageQueue
ourQueue = '.\\private$\\myqueue'
queue = MessageQueue(ourQueue)
queue.Send('Hello from IronPython')
我试图创建一个空消息,然后添加属性(例如标签,回复队列和二进制消息正文),然后发送完整的消息.
I am trying to create an empty message and then add properties (like label, a reply queue and a binary message body) and then send that complete message.
如何在IronPython中做到这一点?
How can I do this in IronPython?
消息类的文档为此处,但显然没有python示例代码.我从来没有将.net代码与python一起使用,而只是安装了IronPython来连接到现有的MSMQ环境,所以我对如何进行操作有些困惑.
The documentation of the message class is here, but obviously has no python sample code. I have never used .net code with python and just installed IronPython to connect to an existing MSMQ environment, so I'm a bit stuck in how to proceed.
有帮助吗?
请参阅下面的答案,我设法猜出了创建消息的系统税. 该解决方案似乎有点骇人听闻,所以我将其开放几天
See answer below, I managed to guess the systax to create a message. The solution seems a bit hacky so I'll leave this open for a few days
推荐答案
我已经通过检查c#示例和 已经解决了该问题的解决方案.以下代码 传递带有用户定义的标签和响应队列以及正文消息的消息.
I've started guessing the syntax by examining c# examples and have hacked a solution to the problem. The following code delivers a message with a user defined label and response queue and a body message.
import clr
from System import Array
from System import Byte
clr.AddReference('System.Messaging')
from System.Messaging import MessageQueue
from System.Messaging import Message
ourQueue = '.\\private$\python_in'
ourOutQueue = '.\\private$\python_out'
if not MessageQueue.Exists(ourQueue):
queue = MessageQueue.Create(ourQueue)
else:
queue = MessageQueue(ourQueue)
if not MessageQueue.Exists(ourOutQueue):
out_queue = MessageQueue.Create(ourOutQueue)
else:
out_queue = MessageQueue(ourOutQueue)
mymessage = Message()
mymessage.Label = 'MyLabel'
mymessage.ResponseQueue = out_queue
mystring = 'hello world'
mybytearray = bytearray(mystring)
# this is very hacky
mymessage.BodyStream.Write(Array[Byte](mybytearray),0,len(mybytearray))
queue.Send(mymessage)
这篇关于如何在IronPython中使用标签,回复队列和其他属性创建新的MSMQ消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!