How do I save IBytesMessage to a file in IBM MQ using IBM.XMS for .NET Client

Viewed 153

I am fetching messages from an IBM MQ using .Net C# IBM.XMS library. I can receive the messages (IMessage) and I am able to get the message and save the file when its sent as text. I use the .Text property. However I don't know how to save get the message if its sent in say zip. I try to see if I can get the byte[] body and save it as a file. Any suggestions on how to do it?


 message = consumer.Receive(TIMEOUTTIME);
            if (message != null)
            {
                _log.LogInformation("Message received.");
                    var fileName = _fileUtil.GenerateFileName(_queueName);

                    int receivedBytes = 0;
                    if (message is ITextMessage)
                    {
                        var msg = (ITextMessage)message;
                        var result = msg.Text;

                    }
                    else if (message is IBytesMessage)
                    {
                        var msg = (IBytesMessage)message; 
                        byte[] result = msg...
                        (how to get the bodys byte array)
                        ...
                        File.WriteAllBytes(filepath, result);   
                  
                    }

1 Answers

You can use ReadBytes method to read message payload.

Int32   ReadBytes(Byte[] array, Int32 length);  

Read an array of bytes from the bytes message stream starting from the current position of the cursor. The method returns the number of bytes read into the buffer you pass.

Related