public MultiDeserializer(Configure config, Conventions conventions, IMessageSerializer defaultSerializer, IMessageMapper mapper, LogicalMessageFactory logicalMessageFactory, MessageMetadataRegistry metadataRegistry) { _defaultSerializer = defaultSerializer; _logicalMessageFactory = logicalMessageFactory; _metadataRegistry = metadataRegistry; var json = new JsonMessageSerializer(mapper); serializers.Add(json.ContentType, json); var bson = new BsonMessageSerializer(mapper); serializers.Add(bson.ContentType, bson); var binary = new BinaryMessageSerializer(); serializers.Add(binary.ContentType, binary); var xml = new XmlMessageSerializer(mapper, conventions); List <Type> messageTypes = config.TypesToScan.Where(conventions.IsMessageType).ToList(); xml.Initialize(messageTypes); serializers.Add(xml.ContentType, xml); if (!serializers.ContainsKey(_defaultSerializer.ContentType)) { serializers.Add(_defaultSerializer.ContentType, _defaultSerializer); } }
public MessageSerializerAdapter() { var mapper = new MessageMapper(); var json = new JsonMessageSerializer(mapper); _serializers.Add(json.ContentType, json); var bson = new BsonMessageSerializer(mapper); _serializers.Add(bson.ContentType, bson); var binary = new BinaryMessageSerializer(); _serializers.Add(binary.ContentType, binary); IList <Type> messageTypes = Configure.TypesToScan.Where(MessageConventionExtensions.IsMessageType).ToList(); var xml = new XmlMessageSerializer(mapper); xml.Initialize(messageTypes); _serializers.Add(xml.ContentType, xml); _defaultSerializer = xml; if (!_serializers.ContainsKey(_defaultSerializer.ContentType)) { _serializers.Add(_defaultSerializer.ContentType, _defaultSerializer); } }
public void TestMethodBinaryMessageSerializationComplexPayload() { 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 msgSerializerNew = new BinaryMessageSerializer(); GenericMessage deserializedMsg = (GenericMessage)msgSerializerNew.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); }
public void TestMethodBinaryMessageSerialization() { BinaryMessageSerializer msgSerializer = new BinaryMessageSerializer(); GenericMessage msg = new GenericMessage(); msg.Type = MessageType.MethodInvokeRequest; msg.Target = typeof(TypeDescription).FullName; msg.Name = "CallThisMethod(int)"; msg.RequestId = 1; msg.Payload = new object[] { 1, 2, 3, 4 }; var bytes = msgSerializer.SerializeToBytes(msg, null); // 425 DeserializeAndCheck(msgSerializer, msg, bytes); var bytes2 = msgSerializer.SerializeToBytes(msg, null); // 77 DeserializeAndCheck(msgSerializer, msg, bytes2); var bytes3 = msgSerializer.SerializeToBytes(msg, null); // 77 DeserializeAndCheck(msgSerializer, msg, bytes3); }
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); }
public static async Task <TMessage> GetDeserializedMessageAsync <TMessage>(this Stream stream, CancellationToken cancellationToken) where TMessage : class { var message = await stream.ReadMessageAsync(cancellationToken).ConfigureAwait(false); return(BinaryMessageSerializer.Deserialize <TMessage>(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); }
public void ShouldRegisterSerializableTypesFromAssembly() { BinaryMessageSerializer.RegsterSerializableFrom(GetType().Assembly); var bytes1 = _subject.Serialize(new ResponseMessage("1", new Greeting1 { Hello = "hello" })); var bytes2 = _subject.Serialize(new ResponseMessage("1", new Greeting2 { Hello = "hello" })); Assert.That(bytes1.Length, Is.LessThan(bytes2.Length)); }
public static async Task SendMessageAsync(this Stream stream, object messageObject, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); var serializedMessage = BinaryMessageSerializer.Serialize(messageObject); var sizeBytes = serializedMessage.Length.ToHexBytes(); using (var memoryStream = new MemoryStream()) { memoryStream.Write(sizeBytes, 0, sizeBytes.Length); memoryStream.Write(ControlSeq, 0, ControlSeq.Length); memoryStream.Write(serializedMessage, 0, serializedMessage.Length); memoryStream.Seek(0, SeekOrigin.Begin); await memoryStream.CopyToAsync(stream, BufferSize, cancellationToken).ConfigureAwait(false); } }
protected virtual Task OnFailedAsync(string actionName, object payload, Exception exception, CancellationToken cancellationToken) { var dict = new Dictionary <string, object> { { "ServiceUri", ServiceUri }, { "Payload", "UNABLE_TO_PARSE" }, { "MethodName", "NOT_YET_PARSED" }, { "ActionName", actionName }, { "Actor", CurrentActor }, { "OperationId", CurrentFlowInstanceId.Id }, { "FlowName", CurrentFlowInstanceId.FlowName }, { "ApplicationName", ApplicationName }, { "Resendable", false } }; try { if (payload is SerializableMethodInfo serializableMethodInfo) { foreach (var ar in serializableMethodInfo.Arguments) { dict.Add(ar.ArgumentName, BinaryMessageSerializer.ToJson(ar.Value)); } dict["MethodName"] = serializableMethodInfo.MethodName; } } catch (Exception ex) { //failed to parse payload for logging dict["Payload"] = "UNABLE_TO_PARSE" + $" {ex.Message}"; } if (exception is ActorMessageValidationException) { Logger.LogError(exception, $"{CurrentActor} failed to validate the message by {actionName}"); } else { LogPayload(payload, actionName, dict, exception); } return(Task.CompletedTask); }
public void The_binary_formatter_should_make_mouths_happy() { byte[] serializedMessageData; IMessageSerializer serializer = new BinaryMessageSerializer(); using (MemoryStream output = new MemoryStream()) { serializer.Serialize(output, _message); serializedMessageData = output.ToArray(); } using (MemoryStream input = new MemoryStream(serializedMessageData)) { SerializationTestMessage receivedMessage = serializer.Deserialize(input) as SerializationTestMessage; Assert.AreEqual(_message, receivedMessage); } }
public SerializationMapper(IMessageMapper mapper) { jsonSerializer = new JsonMessageSerializer(mapper); binarySerializer = new BinaryMessageSerializer(); }
public void SetUp() { serializer = new BinaryMessageSerializer(); }
public void The_binary_formatter_should_make_mouths_happy() { byte[] serializedMessageData; IMessageSerializer serializer = new BinaryMessageSerializer(); using (MemoryStream output = new MemoryStream()) { serializer.Serialize(output, _message); serializedMessageData = output.ToArray(); } using (MemoryStream input = new MemoryStream(serializedMessageData)) { SerializationTestMessage receivedMessage = serializer.Deserialize(input) as SerializationTestMessage; Assert.AreEqual(_message, receivedMessage); } }