0

I am using nodejs, msmq and .net as technology stack in my project. I am collecting data from hardware using nodejs script and writing into MSMQ. I want to read this MSMQ in .net but I am getting below error. In addition I am not able to receive the message as well.

Error System.Xml.XmlException: Data at the root level is invalid. Line 1, position at System.Xml.XmlTextReaderImpl.Throw(Exception e) at System.Xml.XmlTextReaderImpl.Throw(String res, String arg) 'objMessage.ConnectorType' threw an exception of type 'System.InvalidOperationException' 'objMessage.Body' threw an exception of type 'System.Xml.XmlException'

//Nodejs script
const msmq = require('updated-node-msmq');
const queue = msmq.openOrCreateQueue('.\\Private$\\EEG');
queue.send(records);   
// C# code
if (MessageQueue.Exists(@".\Private$\EEG")){
    objMessageQueue = new MessageQueue(@".\Private$\EEG");
}
objMessage = objMessageQueue.Receive();
objMessage.Formatter = new XmlMessageFormatter(new Type[] 
     {typeof(Payload) });
var message = (Payload)objMessage.Body;
Pretasoc
  • 1,116
  • 12
  • 22

1 Answers1

0

I am able to solve the issue. Instead of using message.Body I have used message.BodyStream and converted that data into binary format. here's my solution.

   using System;
using System.Messaging;
using System.Data.SqlClient;
using System.Data;
using Newtonsoft.Json;
using System.IO;
using System.Text;

namespace NeuroskyListener
{
    class Program
    {
        public static bool isRunning;
        private static MessageQueue messageQueue;

        public static void Main(string[] args)
        {
            InitializeQueue();
            Console.ReadLine();
        }

        private static void InitializeQueue()
        {
            string queuePath = @".\Private$\eegNew";

            if (!MessageQueue.Exists(queuePath))
            {
                messageQueue = MessageQueue.Create(queuePath);
            }
            else
            {
                messageQueue = new MessageQueue(queuePath);
            }
            isRunning = true;
            messageQueue.Formatter = new XmlMessageFormatter(new Type[] {typeof(string) });
            messageQueue.ReceiveCompleted += OnReceiveCompleted;
            messageQueue.BeginReceive();
        }

        private static void OnReceiveCompleted(object source, ReceiveCompletedEventArgs asyncResult)
        {
            try
            {   
                MessageQueue mq =  (MessageQueue)source;

                if (mq != null)
                {
                    try
                    {
                        System.Messaging.Message message = null;
                        try
                        {
                            message = mq.EndReceive(asyncResult.AsyncResult);
                            BinaryReader reader = new BinaryReader(message.BodyStream);

                            int count = (int)message.BodyStream.Length;

                            byte[] bytes = reader.ReadBytes(count);


                           string bodyString = Encoding.UTF8.GetString(bytes);
                            eegPayload lst = JsonConvert.DeserializeObject<eegPayload>(bodyString);

                        }
                        catch (Exception ex)
                        {
                           // LogMessage(ex.Message);
                        }
                        if (message != null)
                        {
                            //Payload payload = message.Body as Payload;

                            Console.WriteLine(message.Body);

                            //if (payload != null)
                            //{
                            //    receivedCounter++;
                            //    if ((receivedCounter % 10000) == 0)
                            //    {
                            //        string messageText = string.Format("Received {0} messages", receivedCounter);
                            //        LogMessage(messageText);
                            //    }
                            //}
                        }
                    }
                    finally
                    {
                        if (isRunning)
                        {
                            mq.BeginReceive();
                        }
                    }
                }
                return;
            }
            catch (Exception exc)
            {
                //LogMessage(exc.Message);
            }
        }
    }
}