0
votes

I am trying to send messages to service bus queue. For that I write the below lines of code in my program.cs

try
        {
            var connectionString = "Endpoint=sb://Xx-ns.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=XXXXXXXXXXXx";
            var queueName = "queue";
            for (int i = 0; i < 10; i++)
            {
                //Send messages to the queue
                var client = QueueClient.CreateFromConnectionString(connectionString, queueName);
                var message = new BrokeredMessage("This is a test message!");

                 client.Send(message);

                Console.WriteLine("Message successfully sent! Press ENTER to exit program");
            }
        }catch(Exception ex)
        {
            Console.WriteLine(ex.ToString());
        }
        Console.ReadLine();

In the above code I am sending xml data to service bus queue.While I run above code, for first iteration successfully sent message to queue but for second iteration, and when my debug point hits Client.Send (message) then I am getting the following error:

Failed to serialize the message because its body stream has been partially consumed. at Microsoft.ServiceBus.Messaging.BrokeredMessage.SerializeBodyStream(BrokeredMessage message, XmlWriter writer) at Microsoft.ServiceBus.Messaging.BrokeredMessage.System.Xml.Serialization.IXmlSerializable.WriteXml(XmlWriter writer) at System.Runtime.Serialization.XmlObjectSerializerWriteContext.WriteIXmlSerializable(XmlWriterDelegator xmlWriter, Object obj, XmlSerializableWriter xmlSerializableWriter) at System.Runtime.Serialization.XmlObjectSerializerWriteContext.WriteIXmlSerializable(XmlWriterDelegator xmlWriter, Object obj) at System.Runtime.Serialization.XmlDataContract.WriteXmlValue(XmlWriterDelegator xmlWriter, Object obj, XmlObjectSerializerWriteContext context) at System.Runtime.Serialization.XmlObjectSerializerWriteContext.WriteDataContractValue(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle declaredTypeHandle) at System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeWithoutXsiType(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle declaredTypeHandle) at System.Runtime.Serialization.XmlObjectSerializerWriteContext.InternalSerialize(XmlWriterDelegator xmlWriter, Object obj, Boolean isDeclaredType, Boolean writeXsiType, Int32 declaredTypeID, RuntimeTypeHandle declaredTypeHandle) at WritemessagesToXml(XmlWriterDelegator , Object , XmlObjectSerializerWriteContext , CollectionDataContract ) at System.Runtime.Serialization.CollectionDataContract.WriteXmlValue(XmlWriterDelegator xmlWriter, Object obj, XmlObjectSerializerWriteContext context)

Can you please tell me how to resolve the above error as soon as possible?

1

1 Answers

0
votes

You are not allowed to call message.GetBody() for the message-to-be-sent. I'm not exactly sure why it doesn't fail during the first iteration, but overall GetBody should only be called for received messages (and only once).