public void Can_roundtrip_with_datetime_on_non_english_culture(string cultureName)
        {
            var oldCulture = Thread.CurrentThread.CurrentCulture;

            Thread.CurrentThread.CurrentCulture = new CultureInfo(cultureName);

            try
            {
                var serializer = new XmlMessageSerializer(new DefaultReflection(),
                                                          new CastleServiceLocator(new WindsorContainer()));

                var stream = new MemoryStream();
                var date   = DateTime.Now;
                serializer.Serialize(new object[] { new Bar {
                                                        Date = date
                                                    } }, stream);

                stream.Position = 0;

                var bar = (Bar)serializer.Deserialize(stream)[0];
                Assert.Equal(date, bar.Date);
            }
            finally
            {
                Thread.CurrentThread.CurrentCulture = oldCulture;
            }
        }
        public void Can_handle_dictionaries_where_values_are_objects()
        {
            var serializer = new XmlMessageSerializer(new DefaultReflection(),
                                                      new CastleServiceLocator(new WindsorContainer()));

            var stream = new MemoryStream();

            serializer.Serialize(new object[] { new ItemWithObjectDictionary()
                                                {
                                                    Name = "abc", Arguments = new Dictionary <string, object>
                                                    {
                                                        { "abc", "cdef" },
                                                        { "def", 1 },
                                                        { "123", new Dog {
                                                              Name = "Oscar"
                                                          } }
                                                    }
                                                } }, stream);

            stream.Position = 0;

            stream.Position = 0;


            var foo = (ItemWithObjectDictionary)serializer.Deserialize(stream)[0];

            Assert.Equal("abc", foo.Name);
            Assert.Equal("cdef", foo.Arguments["abc"]);
            Assert.Equal(1, (int)foo.Arguments["def"]);
            Assert.Equal("Oscar", ((Dog)foo.Arguments["123"]).Name);
        }
        public void The_xml_message_serializer_should_rock_the_house()
        {
            byte[] serializedMessageData;

            var serializer = new XmlMessageSerializer();

            OutboundMessage.Set(x =>
            {
                x.SetSourceAddress("msmq://localhost/queue_name");
                x.SetDestinationAddress("msmq://remotehost/queue_name");
                x.SetResponseAddress("msmq://localhost/response_queue");
                x.SetFaultAddress("msmq://localhost/fault_queue");
                x.SetRetryCount(7);
            });

            using (MemoryStream output = new MemoryStream())
            {
                serializer.Serialize(output, _message);

                serializedMessageData = output.ToArray();

                Trace.WriteLine(Encoding.UTF8.GetString(serializedMessageData));
            }

            using (MemoryStream input = new MemoryStream(serializedMessageData))
            {
                SerializationTestMessage receivedMessage = serializer.Deserialize(input) as SerializationTestMessage;

                Assert.AreEqual(_message, receivedMessage);
            }
        }
Exemple #4
0
        private void VerifyMessageHeaderIsPassed(Action <IOutboundMessage> setHeaderAction, Action <IInboundMessageHeaders> checkHeaderAction)
        {
            byte[] data;
            var    serializer = new XmlMessageSerializer();

            OutboundMessage.Set(setHeaderAction);

            var message = new PingMessage();

            using (MemoryStream output = new MemoryStream())
            {
                serializer.Serialize(output, message);

                data = output.ToArray();
            }

            Trace.WriteLine(OutboundMessage.Headers.MessageType);

            Trace.WriteLine(Encoding.UTF8.GetString(data));

            using (MemoryStream input = new MemoryStream(data))
            {
                serializer.Deserialize(input);

                checkHeaderAction(CurrentMessage.Headers);
            }
        }
Exemple #5
0
        public void MoveErrorMessageToOriginQueue(QueueItem itm)
        {
            if (string.IsNullOrEmpty(itm.Id))
            {
                throw new ArgumentException("MessageId can not be null or empty");
            }

            if (itm.Queue.Type != QueueType.Error)
            {
                throw new ArgumentException("Queue is not of type Error, " + itm.Queue.Type);
            }

            var mgr = new ErrorManager();

            // TODO:
            // Check if Clustered Queue, due if Clustered && NonTransactional, then Error

            //var deserializer = new XmlMessageSerializer();

            XmlMessageSerializer _serializer = new XmlMessageSerializer();

            using (var stream = new MemoryStream(UTF8Encoding.Default.GetBytes(itm.Content ?? "")))
            {
                var rcx = ReceiveContext.FromBodyStream(stream);
                _serializer.Deserialize(rcx);

                //var query = rcx.DestinationAddress.Query;

                //var errorQueue = rcx.DestinationAddress.OriginalString.Replace(query, "") + "_error";

                //mgr.InputQueue = new EndpointAddress(errorQueue);

                mgr.ReturnMessageToSourceQueue(itm.Id, rcx);
            }
        }
        public void Will_move_poision_message_to_error_queue()
        {
            transport.MessageArrived += information =>
            {
                throw new InvalidOperationException("problem");
            };

            using (var tx = new TransactionScope())
            {
                transport.Send(transport.Endpoint, new object[] { "test" });
                tx.Complete();
            }

            using (var tx = new TransactionScope())
            {
                var message = transport.Queue.Receive(SubQueue.Errors.ToString());

                Assert.Equal("test", messageSerializer.Deserialize(new MemoryStream(message.Data))[0]);
                var errMsg = transport.Queue.Receive(SubQueue.Errors.ToString());
                Assert.True(Encoding.Unicode.GetString(errMsg.Data).StartsWith("System.InvalidOperationException: problem"));

                Assert.Equal(message.Id.ToString(), errMsg.Headers["correlation-id"]);
                Assert.Equal("5", errMsg.Headers["retries"]);

                tx.Complete();
            }
        }
Exemple #7
0
        void VerifyMessageHeaderIsPassed(Action <ISendContext <PingMessage> > setHeaderAction,
                                         Action <IConsumeContext> checkHeaderAction)
        {
            byte[] data;
            var    serializer = new XmlMessageSerializer();

            var message = new PingMessage();

            using (var output = new MemoryStream())
            {
                var sendContext = new SendContext <PingMessage>(message);
                setHeaderAction(sendContext);

                serializer.Serialize(output, sendContext);

                data = output.ToArray();
            }

            //Trace.WriteLine(Encoding.UTF8.GetString(data));

            using (var input = new MemoryStream(data))
            {
                var receiveContext = ReceiveContext.FromBodyStream(input);
                serializer.Deserialize(receiveContext);

                checkHeaderAction(receiveContext);
            }
        }
Exemple #8
0
		private void VerifyMessageHeaderIsPassed(Action<IOutboundMessage> setHeaderAction, Action<IInboundMessageHeaders> checkHeaderAction)
		{
			byte[] data;
			var serializer = new XmlMessageSerializer();

			OutboundMessage.Set(setHeaderAction);

			var message = new PingMessage();

			using (MemoryStream output = new MemoryStream())
			{
				serializer.Serialize(output, message);

				data = output.ToArray();
			}

			Trace.WriteLine(OutboundMessage.Headers.MessageType);

			Trace.WriteLine(Encoding.UTF8.GetString(data));

			using (MemoryStream input = new MemoryStream(data))
			{
				serializer.Deserialize(input);

				checkHeaderAction(CurrentMessage.Headers);
			}
		}
        public void Deserialize_BytesMessage_Exception()
        {
            var serializer = new XmlMessageSerializer <BytesMessage>();
            var message    = new TextMessage {
                Body = MessageText
            };

            Assert.Throws <InvalidOperationException>(() => serializer.Deserialize(message));
        }
Exemple #10
0
		public void Should_deserialize_into_the_proxy_object()
		{
			XmlMessageSerializer serializer = new XmlMessageSerializer();
			using (var bodyStream = new MemoryStream(Encoding.UTF8.GetBytes(InterfaceBasedMessageXml)))
			{
				object obj = serializer.Deserialize(bodyStream);

				Assert.IsNotNull(obj);
			}
		}
        public void can_deserialize_rhino_esb_message()
        {
            var writer = new StreamWriter(_stream);
            writer.Write(XmlInput);
            writer.Flush();
            _stream.Position = 0;

            var result = (TestRhinoToFtSerialization) _serializer.Deserialize(_stream);
            result.Id.ShouldEqual(1);
        }
Exemple #12
0
        public void Deserialize_TextMessage_CorrectResult()
        {
            var serializer = new XmlMessageSerializer <TextMessage>();

            var result = serializer.Deserialize(new TextMessage {
                Body = MessageText
            });

            Assert.That(result.Body == "testMessage");
        }
        public void Can_serialize_and_deserialize_byte_array()
        {
            var serializer = new XmlMessageSerializer(new DefaultReflection(), new DefaultKernel());
            var stream     = new MemoryStream();

            serializer.Serialize(new object[] { new byte[] { 1, 2, 3, 4 } }, stream);
            stream.Position = 0;
            var actual = (byte[])serializer.Deserialize(stream)[0];

            Assert.Equal(new byte[] { 1, 2, 3, 4 }, actual);
        }
Exemple #14
0
        public void Should_deserialize_into_the_proxy_object()
        {
            XmlMessageSerializer serializer = new XmlMessageSerializer();

            using (var bodyStream = new MemoryStream(Encoding.UTF8.GetBytes(InterfaceBasedMessageXml)))
            {
                object obj = serializer.Deserialize(bodyStream);

                Assert.IsNotNull(obj);
            }
        }
        public void Deserialize_BytesMessage_CastResult()
        {
            var serializer = new XmlMessageSerializer <TextMessage>();
            var message    = new BytesMessage {
                Body = Encoding.UTF8.GetBytes(MessageText)
            };

            var result = serializer.Deserialize(message);

            Assert.That(result.Body == "testMessage");
        }
Exemple #16
0
        public void Can_serialize_and_deserialize_byte_array()
        {
            var serializer = new XmlMessageSerializer();
            var stream     = new MemoryStream();

            serializer.Serialize(new object[] { new byte[] { 1, 2, 3, 4 } }, stream);
            stream.Position = 0;
            var actual = (byte[])serializer.Deserialize(stream).As <byte[]>();

            new byte[] { 1, 2, 3, 4 }.ShouldEqual(actual);
        }
		public void Should_not_cause_fatal_explosions_of_the_fiery_death_kind()
		{
			var serializer = new XmlMessageSerializer();
			using (var bodyStream = new MemoryStream(Encoding.UTF8.GetBytes(Version4Message)))
			{
				object obj = serializer.Deserialize(bodyStream);

				Assert.IsNotNull(obj);
				Assert.IsInstanceOf<ComplaintAdded>(obj);
			}
		}
		public void Should_handle_the_uri_type()
		{
			var serializer = new XmlMessageSerializer();
			using (var bodyStream = new MemoryStream(Encoding.UTF8.GetBytes(AnotherVersion4Message)))
			{
				object obj = serializer.Deserialize(bodyStream);

				Assert.IsNotNull(obj);
				Assert.IsInstanceOf<ComplaintAdded>(obj);
			}
		}
        public void Can_serialize_and_deserialize_primitive()
        {
            long ticks      = DateTime.Now.Ticks;
            var  serializer = new XmlMessageSerializer(new DefaultReflection(), new DefaultKernel());
            var  stream     = new MemoryStream();

            serializer.Serialize(new object[] { ticks }, stream);
            stream.Position = 0;
            var actual = (long)serializer.Deserialize(stream)[0];

            Assert.Equal(ticks, actual);
        }
        public void Can_serialize_and_deserialize_DateTimeOffset()
        {
            var value      = DateTimeOffset.Now;
            var serializer = new XmlMessageSerializer(new DefaultReflection(), new DefaultKernel());
            var stream     = new MemoryStream();

            serializer.Serialize(new object[] { value }, stream);
            stream.Position = 0;
            var actual = (DateTimeOffset)serializer.Deserialize(stream)[0];

            Assert.Equal(value, actual);
        }
Exemple #21
0
        public void Should_handle_the_uri_type()
        {
            var serializer = new XmlMessageSerializer();

            using (var bodyStream = new MemoryStream(Encoding.UTF8.GetBytes(AnotherVersion4Message)))
            {
                object obj = serializer.Deserialize(bodyStream);

                Assert.IsNotNull(obj);
                Assert.IsInstanceOf <ComplaintAdded>(obj);
            }
        }
Exemple #22
0
        public void Can_serialize_and_deserialize_float()
        {
            float aFloat     = 1.12f;
            var   serializer = new XmlMessageSerializer();
            var   stream     = new MemoryStream();

            serializer.Serialize(new object[] { aFloat }, stream);
            stream.Position = 0;
            var actual = (float)serializer.Deserialize(stream).As <float>();

            aFloat.ShouldEqual(actual);
        }
Exemple #23
0
        public void Can_serialize_and_deserialize_double()
        {
            double aDouble    = 1.12;
            var    serializer = new XmlMessageSerializer();
            var    stream     = new MemoryStream();

            serializer.Serialize(new object[] { aDouble }, stream);
            stream.Position = 0;
            var actual = (double)serializer.Deserialize(stream).As <double>();

            aDouble.ShouldEqual(actual);
        }
Exemple #24
0
        public void Can_serialize_and_deserialize_primitive()
        {
            long ticks      = DateTime.Now.Ticks;
            var  serializer = new XmlMessageSerializer();
            var  stream     = new MemoryStream();

            serializer.Serialize(new object[] { ticks }, stream);
            stream.Position = 0;
            var actual = (long)serializer.Deserialize(stream).As <long>();

            ticks.ShouldEqual(actual);
        }
        public void Can_serialize_and_deserialize_float()
        {
            float aFloat     = 1.12f;
            var   serializer = new XmlMessageSerializer(new DefaultReflection(), new CastleServiceLocator(new WindsorContainer()));
            var   stream     = new MemoryStream();

            serializer.Serialize(new object[] { aFloat }, stream);
            stream.Position = 0;
            var actual = (float)serializer.Deserialize(stream)[0];

            Assert.Equal(aFloat, actual);
        }
Exemple #26
0
        public void Should_not_cause_fatal_explosions_of_the_fiery_death_kind()
        {
            var serializer = new XmlMessageSerializer();

            using (var bodyStream = new MemoryStream(Encoding.UTF8.GetBytes(Version4Message)))
            {
                object obj = serializer.Deserialize(bodyStream);

                Assert.IsNotNull(obj);
                Assert.IsInstanceOf <ComplaintAdded>(obj);
            }
        }
Exemple #27
0
        public void Can_serialize_and_deserialize_DateTimeOffset()
        {
            var value      = DateTimeOffset.Now;
            var serializer = new XmlMessageSerializer();
            var stream     = new MemoryStream();

            serializer.Serialize(new object[] { value }, stream);
            stream.Position = 0;
            var actual = (DateTimeOffset)serializer.Deserialize(stream).As <DateTimeOffset>();

            value.ShouldEqual(actual);
        }
        public void can_serialize_and_deserialize_icpc_monitor()
        {
            var    ms         = new MemoryStream();
            var    serializer = new XmlMessageSerializer(new DefaultReflection(), new DefaultKernel());
            var    submission = S("testuser A AC 11");
            var    builder    = new IcpcMonitorBuilder(contest);
            object monitor    = builder.BuildMonitor(new[] { submission });

            serializer.Serialize(new[] { monitor }, ms);
            ms.Position = 0;
            serializer.Deserialize(ms);
        }
        public void Can_serialize_and_deserialize_double()
        {
            double aDouble    = 1.12;
            var    serializer = new XmlMessageSerializer(new DefaultReflection(), new CastleServiceLocator(new WindsorContainer()));
            var    stream     = new MemoryStream();

            serializer.Serialize(new object[] { aDouble }, stream);
            stream.Position = 0;
            var actual = (double)serializer.Deserialize(stream)[0];

            Assert.Equal(aDouble, actual);
        }
Exemple #30
0
        public void can_round_trip_single_message()
        {
            var serializer = new XmlMessageSerializer();
            var stream     = new MemoryStream();

            serializer.Serialize(sample, stream);

            stream.Position = 0;

            var actual = serializer.Deserialize(stream).ShouldBeOfType <Order>();

            actual.OrderId.ShouldEqual(sample.OrderId);
        }
Exemple #31
0
        static IConsumeContext <RoutingSlip> TranslateXmlBody(IReceiveContext context)
        {
            var serializer = new XmlMessageSerializer();

            serializer.Deserialize(context);

            IConsumeContext <RoutingSlip> routingSlipContext;

            if (context.TryGetContext(out routingSlipContext))
            {
                return(routingSlipContext);
            }

            throw new InvalidOperationException("Unable to reprocess message as RoutingSlip");
        }
Exemple #32
0
        private void OnTransportMessageReceived(object sender, TransportMessageReceivedEventArgs e)
        {
            dynamic response = new ExpandoObject();

            XmlMessageSerializer serializer = new XmlMessageSerializer(new MessageMapper());
            var message = serializer.Deserialize(new MemoryStream(e.Message.Body)).First();

            response.Endpoint = bus.InputAddress.Queue;
            response.Time     = DateTime.UtcNow;
            response.type     = message.GetType().ToString();
            response.body     = message;
            response.headers  = e.Message.Headers;


            _monitor.Invoke("MessageReceived", response);
        }
Exemple #33
0
        protected void TestSerialization <T>(T message)
        {
            byte[] data;
            var    serializer = new XmlMessageSerializer();

            _sourceUri      = new Uri("loopback://localhost/source");
            _responseUri    = new Uri("loopback://localhost/response");
            _faultUri       = new Uri("loopback://localhost/fault");
            _destinationUri = new Uri("loopback://localhost/destination");
            _retryCount     = 69;

            OutboundMessage.Set(x =>
            {
                x.SetSourceAddress(_sourceUri);
                x.SendResponseTo(_responseUri);
                x.SendFaultTo(_faultUri);
                x.SetDestinationAddress(_destinationUri);
                x.SetRetryCount(_retryCount);
            });

            using (MemoryStream output = new MemoryStream())
            {
                serializer.Serialize(output, message);

                data = output.ToArray();
            }

            Trace.WriteLine(OutboundMessage.Headers.MessageType);

            Trace.WriteLine(Encoding.UTF8.GetString(data));

            using (MemoryStream input = new MemoryStream(data))
            {
                object receivedMessage = serializer.Deserialize(input);

                Assert.AreEqual(message, receivedMessage);
                Assert.AreNotSame(message, receivedMessage);

                Assert.AreEqual(_retryCount, CurrentMessage.Headers.RetryCount);
                Assert.AreEqual(_sourceUri, CurrentMessage.Headers.SourceAddress);
                Assert.AreEqual(_responseUri, CurrentMessage.Headers.ResponseAddress);
                Assert.AreEqual(_faultUri, CurrentMessage.Headers.FaultAddress);
                Assert.AreEqual(_destinationUri, CurrentMessage.Headers.DestinationAddress);
                //			Assert.AreEqual(message.GetType().ToMessageName(), CurrentMessage.Headers.MessageType);
            }
        }
        public void Can_roundtrip()
        {
            var serializer = new XmlMessageSerializer(new DefaultReflection(), new DefaultKernel());

            var stream = new MemoryStream();

            serializer.Serialize(new object[] { new Foo {
                                                    Name = "abc"
                                                } }, stream);

            stream.Position = 0;

            var foo = (Foo)serializer.Deserialize(stream)[0];

            Assert.Equal("abc", foo.Name);
            Assert.Equal("ABC", foo.UName);
        }
        public void MoveErrorMessageToOriginQueue(QueueItem itm)
        {
            if (string.IsNullOrEmpty(itm.Id))
                throw new ArgumentException("MessageId can not be null or empty");

            if (itm.Queue.Type != QueueType.Error)
                throw new ArgumentException("Queue is not of type Error, " + itm.Queue.Type);

            var mgr = new ErrorManager();

            // TODO:
            // Check if Clustered Queue, due if Clustered && NonTransactional, then Error

            //var deserializer = new XmlMessageSerializer();

            XmlMessageSerializer _serializer = new XmlMessageSerializer();

            using (var stream = new MemoryStream(UTF8Encoding.Default.GetBytes(itm.Content ?? "")))
            {
                var rcx = ReceiveContext.FromBodyStream(stream);
                _serializer.Deserialize(rcx);

                //var query = rcx.DestinationAddress.Query;

                //var errorQueue = rcx.DestinationAddress.OriginalString.Replace(query, "") + "_error";

                //mgr.InputQueue = new EndpointAddress(errorQueue);

                mgr.ReturnMessageToSourceQueue(itm.Id, rcx);
            }
        }
        private void OnTransportMessageReceived(object sender, TransportMessageReceivedEventArgs e)
        {
            dynamic response = new ExpandoObject();

            XmlMessageSerializer serializer = new XmlMessageSerializer(new MessageMapper());
            var message = serializer.Deserialize(new MemoryStream(e.Message.Body)).First();

            response.Endpoint = bus.InputAddress.Queue;
            response.Time = DateTime.UtcNow;
            response.type = message.GetType().ToString();
            response.body = message;
            response.headers = e.Message.Headers;


            _monitor.Invoke("MessageReceived", response);
        }
        public void The_xml_message_serializer_should_rock_the_house()
        {
            byte[] serializedMessageData;

            var serializer = new XmlMessageSerializer();

			OutboundMessage.Set(x =>
				{
					x.SetSourceAddress("msmq://localhost/queue_name");
					x.SetDestinationAddress("msmq://remotehost/queue_name");
					x.SetResponseAddress("msmq://localhost/response_queue");
					x.SetFaultAddress("msmq://localhost/fault_queue");
					x.SetRetryCount(7);
				});

            using (MemoryStream output = new MemoryStream())
            {
                serializer.Serialize(output, _message);

                serializedMessageData = output.ToArray();

                Trace.WriteLine(Encoding.UTF8.GetString(serializedMessageData));
            }

            using (MemoryStream input = new MemoryStream(serializedMessageData))
            {
                SerializationTestMessage receivedMessage = serializer.Deserialize(input) as SerializationTestMessage;

                Assert.AreEqual(_message, receivedMessage);
            }
        }
Exemple #38
0
		public void Just_how_fast_is_the_custom_xml_serializer()
		{
			var message = new SerializationTestMessage
				{
					DecimalValue = 123.45m,
					LongValue = 098123213,
					BoolValue = true,
					ByteValue = 127,
					IntValue = 123,
					DateTimeValue = new DateTime(2008, 9, 8, 7, 6, 5, 4),
					TimeSpanValue = 30.Seconds(),
					GuidValue = Guid.NewGuid(),
					StringValue = "Chris's Sample Code",
					DoubleValue = 1823.172,
				};

//			var message = new PingMessage
//				{
//					CorrelationId = CombGuid.Generate()
//				};

			var serializer = new XmlMessageSerializer();

			for (int i = 0; i < 10; i++)
			{
				byte[] data;
				using (MemoryStream output = new MemoryStream())
				{
					serializer.Serialize(output, message);
					data = output.ToArray();
				}
				using (MemoryStream input = new MemoryStream(data))
				{
					//		serializer.Deserialize(input);
				}
			}

			Stopwatch timer = Stopwatch.StartNew();

			const int iterations = 50000;

			for (int i = 0; i < iterations; i++)
			{
				using (MemoryStream output = new MemoryStream())
				{
					serializer.Serialize(output, message);
				}
			}

			timer.Stop();

			long perSecond = iterations*1000/timer.ElapsedMilliseconds;

			var msg = string.Format("Serialize: {0}ms, Rate: {1} m/s", timer.ElapsedMilliseconds, perSecond);
			Trace.WriteLine(msg);

			byte[] sample;
			using (MemoryStream output = new MemoryStream())
			{
				serializer.Serialize(output, message);
				sample = output.ToArray();
			}

			timer = Stopwatch.StartNew();

			for (int i = 0; i < 50000; i++)
			{
				using (MemoryStream input = new MemoryStream(sample))
				{
					serializer.Deserialize(input);
				}
			}

			timer.Stop();

			perSecond = iterations*1000/timer.ElapsedMilliseconds;

			msg = string.Format("Deserialize: {0}ms, Rate: {1} m/s", timer.ElapsedMilliseconds, perSecond);
			Trace.WriteLine(msg);
		}