Преглед на файлове

Add new memory serializer

master
Savorboard преди 5 години
родител
ревизия
3845698f2c
променени са 1 файла, в които са добавени 32 реда и са изтрити 0 реда
  1. +32
    -0
      src/DotNetCore.CAP/Serialization/ISerializer.Memory.cs

+ 32
- 0
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<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);
}
}
}
}

Зареждане…
Отказ
Запис