1

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.

How can I do this in IronPython?

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.

Any help?

update

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

Community
  • 1
  • 1
576i
  • 7,579
  • 12
  • 55
  • 92

2 Answers2

0

I don't think that this works with IronPython classes, because serialize and deserialize them does not work like it does for c#/.net classes.

The only thing to get this work, will be to get IronPython classes serialize-able and deserialize-able. I think deserialization will be the hard part. But you may proof me wrong.

BendEg
  • 20,098
  • 17
  • 57
  • 131
  • I already successfully ran the other examples from the tutorial (reading and writing to several MSMQs, accessing the message body) so I'm pretty sure I will succeed.My final goal is to use a message queue that is currently fed by a c# program and throw an extra messages into that queue that look exactly like what the c# program produces. If no-one of SO knows within the next days, I'll try guessing the syntax and report the results.... – 576i Jan 16 '16 at 20:08
  • @BendEg It is better to write this kind of comment at the comment section of the post, instead of writing a separate answer (which actually not answering the original question). – Daniel Baktiar Mar 17 '17 at 03:28
0

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)
576i
  • 7,579
  • 12
  • 55
  • 92