Extra bytes in message body

Mar 9, 2015 at 4:52 AM
Edited Mar 9, 2015 at 5:05 AM
Hey,

Great work with this library.

I'm seeing a strange issue. I'm able to successfully send a message to Azure Event Hub using the following console application:
using Amqp;
using Amqp.Framing;
using System;
using System.Text;

namespace AmqpSender
{
    class Program
    {
        private static bool sendInProgress = false;
        static void Main(string[] args)
        {
            string connectionString = // removed this for this forum post
            string eventHubName = // removed this for this forum post
            var connection = new Connection(new Address(connectionString));
            var session = new Session(connection);
            var sender = new SenderLink(session, "rp2-sender", eventHubName);
            var data = Encoding.UTF8.GetBytes("Hello World AMQP");
            Message message = new Message(data);
            Console.Write("Sending message...");
            sendInProgress = true;
            sender.Send(message, SendOutcome, null);
            while (sendInProgress)
            {
                System.Threading.Thread.Sleep(50);
            }
            sender.Close();
            session.Close();
            connection.Close();
        }
        public static void SendOutcome(Message message, Outcome outcome, object state)
        {
            sendInProgress = false;
            if (outcome is Accepted)
            {
                Console.WriteLine("done.");
            }
            else
            {
                Console.WriteLine("ERROR!");
            }
        }
    }
}
Note that in the above code, the first three bytes of data are:

72
101
108

Corresponding to "H", "e", "l": http://en.wikipedia.org/wiki/UTF-8

However, after receiving the message from the Event Hub:
Task IEventProcessor.ProcessEventsAsync(PartitionContext context, IEnumerable<EventData> messages)
        {
            string partitionId = context.Lease.PartitionId;
            if (messages != null)
            {
                foreach (var message in messages)
                {
                    byte[] data = message.GetBytes();
there are two extra bytes at the beginning of the message:

160
16
72
101
108

Interestingly, "16" is the length of the string "Hello World AMQP"

Any ideas?

Thanks!
Matt
Editor
Mar 9, 2015 at 7:16 AM
Hi,

see the reply here for the same problem :

https://amqpnetlite.codeplex.com/workitem/27

Paolo.
Mar 9, 2015 at 1:50 PM
Thanks Paolo. I fixed the issue like this:
var encodedTextData = Encoding.UTF8.GetBytes("Hello World AMQP - fixed!");
            Message message = new Message(encodedTextData);
            Data data = new Data();
            data.Binary = encodedTextData;
            message.BodySection = data;
Editor
Mar 9, 2015 at 1:54 PM
You can avoid to use the Message constructor with the parameter ...
In your way the body inside the message is first allocated as AMQP value and then overridden with your "data" ... the first is a useless allocation ...
You can simply use :
Message message = new Message();
followed by your code

However more simple way is :
var encodedTextData = Encoding.UTF8.GetBytes("Hello World AMQP - fixed!");
Message  message = new Message()
                {
                    BodySection = new Data() { Binary = encodedTextData }
                };
Less code :-)

Paolo.
Mar 9, 2015 at 2:28 PM
Hah, thanks Paolo. I meant to replace that with Message message = new Message();. I appreciate the reply. Have a great day.

Matt