瀏覽代碼

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);
}
}
}
}

Loading…
取消
儲存