public void TestMethodBinaryMessageSerializationComplexPayloadWithResponse()
        {
            BinaryMessageSerializer msgSerializer = new BinaryMessageSerializer();

            var testItem = new TestItem()
            {
                ID = 5, Name = "Test it"
            };

            GenericMessage msg = new GenericMessage();

            msg.Type      = MessageType.MethodInvokeRequest;
            msg.Target    = typeof(ITestService).FullName;
            msg.Name      = "CallTest(BSAG.IOCTalk.Serialization.Binary.Test.Model.ITestItem)";
            msg.RequestId = 1;
            msg.Payload   = new object[] { testItem };

            var bytes = msgSerializer.SerializeToBytes(msg, null);

            BinaryMessageSerializer msgSerializerNewDeserialize = new BinaryMessageSerializer();

            GenericMessage deserializedMsg = (GenericMessage)msgSerializerNewDeserialize.DeserializeFromBytes(bytes, null);

            object[] payloadArr = (object[])deserializedMsg.Payload;

            ITestItem testItemDeserialized = (ITestItem)payloadArr.First();

            Assert.Equal(testItem.ID, testItemDeserialized.ID);
            Assert.Equal(testItem.Name, testItemDeserialized.Name);

            // response test
            GenericMessage msg2 = new GenericMessage();

            msg2.Type      = MessageType.MethodInvokeResponse;
            msg2.Target    = typeof(ITestService).FullName;
            msg2.Name      = "GetTestItem()";
            msg2.RequestId = 2;
            msg2.Payload   = testItem;

            var bytesMsg2 = msgSerializer.SerializeToBytes(msg2, null);

            GenericMessage deserializedMsg2 = (GenericMessage)msgSerializerNewDeserialize.DeserializeFromBytes(bytesMsg2, null);

            ITestItem deserializedTest2 = (ITestItem)deserializedMsg2.Payload;

            Assert.Equal(testItem.ID, deserializedTest2.ID);
            Assert.Equal(testItem.Name, deserializedTest2.Name);
        }
Example #2
0
        private static IGenericMessage ParseMessage(string textMsg, RawMessageFormat format)
        {
            IGenericMessage message;

            if (format == RawMessageFormat.Binary)
            {
                byte[] binaryData = Convert.FromBase64String(textMsg);

                if (binarySerializer == null)
                {
                    binarySerializer = new BinaryMessageSerializer();
                }

                message = binarySerializer.DeserializeFromBytes(binaryData, null);
            }
            else
            {
                //{"Type":10,"RequestId":2,"Target":"BSAG.Xitaro.XBCI.Requester.Interface.Services.IRouterService","Name":"GetLatestSequenceNumbers()","Payload":[]}

                message           = new GenericMessage();
                message.Type      = (MessageType)short.Parse(GetJsonSimpleStringValue(textMsg, "Type"));
                message.RequestId = long.Parse(GetJsonSimpleStringValue(textMsg, "RequestId"));
                message.Name      = GetJsonSimpleStringValue(textMsg, "Name");
                message.Target    = GetJsonSimpleStringValue(textMsg, "Target");
            }

            return(message);
        }
        private static void DeserializeAndCheck(BinaryMessageSerializer msgSerializer, GenericMessage orgMsg, byte[] bytes)
        {
            GenericMessage deserializedMsg = (GenericMessage)msgSerializer.DeserializeFromBytes(bytes, null);

            Assert.Equal(orgMsg.Type, deserializedMsg.Type);
            Assert.Equal(orgMsg.Target, deserializedMsg.Target);
            Assert.Equal(orgMsg.Name, deserializedMsg.Name);
            Assert.Equal(orgMsg.RequestId, deserializedMsg.RequestId);
            Assert.True(orgMsg.Payload is object[] arr && arr.Length == 4);
        }