Browse Source

Add new memory serializer

master
Savorboard 5 years ago
parent
commit
3845698f2c
1 changed files with 32 additions and 0 deletions
  1. +32
    -0
      src/DotNetCore.CAP/Serialization/ISerializer.Memory.cs

+ 32
- 0
src/DotNetCore.CAP/Serialization/ISerializer.Memory.cs View File

@@ -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<TransportMessage> 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<Message> 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);
}
}
}
}

Loading…
Cancel
Save