Beispiel #1
0
        public void SendReceiveXmlMessage()
        {
            using (IConnection connection = CreateConnection(TEST_CLIENT_ID))
            {
                connection.Start();
                using (ISession session = connection.CreateSession(AcknowledgementMode.AutoAcknowledge))
                {
                    IDestination destination = SessionUtil.GetDestination(session, DESTINATION_NAME);
                    using (IMessageConsumer consumer = session.CreateConsumer(destination))
                        using (IMessageProducer producer = session.CreateProducer(destination))
                        {
                            producer.RequestTimeout = receiveTimeout;

                            NMSTestXmlType1 srcIntObject = new NMSTestXmlType1();
                            srcIntObject.crcCheck  = 0xbadf00d;
                            srcIntObject.checkType = CheckType.command;
                            producer.Send(NMSConvert.ToXmlMessage(session, srcIntObject));

                            NMSTestXmlType2 srcStringObject = new NMSTestXmlType2();
                            srcStringObject.stringCheck = "BadFood";
                            producer.Send(NMSConvert.ToXmlMessage(session, srcStringObject));

                            // Demonstrate the ability to generically handle multiple object types
                            // sent to the same consumer.  If only one object type is ever sent to
                            // the destination, then a simple inline cast is all that is necessary
                            // when calling the NMSConvert.FromXmlMessage() function.

                            for (int index = 0; index < 2; index++)
                            {
                                object receivedObject = NMSConvert.FromXmlMessage(consumer.Receive(receiveTimeout));
                                Assert.IsNotNull(receivedObject, "Failed to retrieve XML message object.");

                                if (receivedObject is NMSTestXmlType1)
                                {
                                    NMSTestXmlType1 destObject = (NMSTestXmlType1)receivedObject;
                                    Assert.AreEqual(srcIntObject.crcCheck, destObject.crcCheck, "CRC integer mis-match.");
                                    Assert.AreEqual(srcIntObject.checkType, destObject.checkType, "Check type mis-match.");
                                }
                                else if (receivedObject is NMSTestXmlType2)
                                {
                                    NMSTestXmlType2 destObject = (NMSTestXmlType2)receivedObject;
                                    Assert.AreEqual(srcStringObject.stringCheck, destObject.stringCheck, "CRC string mis-match.");
                                }
                                else
                                {
                                    Assert.Fail("Invalid object type.");
                                }
                            }
                        }
                }
            }
        }
Beispiel #2
0
        /// <summary>
        /// Deserializes the object from Xml, and returns it.
        /// </summary>
        public static T ToObject <T>(this IMessage message) where T : class
        {
            try
            {
                if (null != message)
                {
                    return((T)NMSConvert.DeserializeObjFromMessage(message));
                }
            }
            catch (Exception ex)
            {
                Tracer.ErrorFormat("Error converting message to object: {0}", ex.Message);
            }

            return(null);
        }
Beispiel #3
0
 /// <summary>
 /// Extension function to create a text message from an object.  The object must be serializable to XML.
 /// </summary>
 public static ITextMessage CreateXmlMessage(this IMessageProducer producer, object obj, Encoding encoding)
 {
     return(NMSConvert.SerializeObjToMessage(producer.CreateTextMessage(), obj, encoding));
 }
 /// <summary>
 /// Extension function to create a text message from an object.  The object must be serializable to XML.
 /// </summary>
 public static ITextMessage CreateXmlMessage(this ISession session, object obj)
 {
     return(NMSConvert.SerializeObjToMessage(session.CreateTextMessage(), obj));
 }