From 3845698f2cbc4829b9a9bb84f087db25051e42d4 Mon Sep 17 00:00:00 2001 From: Savorboard Date: Thu, 26 Sep 2019 09:48:36 +0800 Subject: [PATCH] Add new memory serializer --- .../Serialization/ISerializer.Memory.cs | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 src/DotNetCore.CAP/Serialization/ISerializer.Memory.cs diff --git a/src/DotNetCore.CAP/Serialization/ISerializer.Memory.cs b/src/DotNetCore.CAP/Serialization/ISerializer.Memory.cs new file mode 100644 index 0000000..4b1e050 --- /dev/null +++ b/src/DotNetCore.CAP/Serialization/ISerializer.Memory.cs @@ -0,0 +1,32 @@ +using System.IO; +using System.Threading.Tasks; +using DotNetCore.CAP.Messages; +using System.Runtime.Serialization.Formatters.Binary; + +namespace DotNetCore.CAP.Serialization +{ + public class MemorySerializer : ISerializer + { + public Task SerializeAsync(Message message) + { + var bf = new BinaryFormatter(); + using (var ms = new MemoryStream()) + { + bf.Serialize(ms, message.Value); + return Task.FromResult(new TransportMessage(message.Headers, ms.ToArray())); + } + } + + public async Task DeserializeAsync(TransportMessage transportMessage) + { + using (var memStream = new MemoryStream()) + { + var binForm = new BinaryFormatter(); + await memStream.WriteAsync(transportMessage.Body, 0, transportMessage.Body.Length); + memStream.Seek(0, SeekOrigin.Begin); + var obj = binForm.Deserialize(memStream); + return new Message(transportMessage.Headers, obj); + } + } + } +}