C#通过http传输Serialized Protobuff数据的最佳方式

时间:2018-02-23 12:44:05

标签: c# .net protobuf-net data-transfer-objects

我正在使用Marc Gravell的Protobub-Net来序列化和反序列化对象。

我正在寻找通过http://请求传输数据的最有效方式。

这是我到目前为止所做的:

public string ProtobuffToString()
{
    Dictionary<int, decimal> List1 = new Dictionary<int, decimal>();
    List1.Add(2018, 1.2m);
    List1.Add(2017, 1.4m);
    List1.Add(2016, 1.9m);

    using (var stream = new MemoryStream())
    {
         ProtoBuf.Serializer.Serialize(stream, List1);
        return Convert.ToBase64String(stream.ToArray());
    }
}

public async System.Threading.Tasks.Task<string> ReadProtobuffAsync()
{
    using (System.Net.Http.HttpClient client = new System.Net.Http.HttpClient())
    using (System.Net.Http.HttpResponseMessage response = await client.GetAsync("http://localhost:53204/ProtobuffToString")) //<= Will call ProtobuffToString() above
    using (System.Net.Http.HttpContent content = response.Content)
    {
        string result = await content.ReadAsStringAsync();
        byte[] resultByte = Convert.FromBase64String(result);

        using (var stream = new MemoryStream(resultByte))
        {
            return JsonConvert.SerializeObject(ProtoBuf.Serializer.Deserialize<Dictionary<int, decimal>>(stream));
        }
    }
}

我可以做得更好吗?

它比从json / Json.Net传输更好/更快吗?

也许是的,因为数据传输会更小,序列化/反序列化更快。

2 个答案:

答案 0 :(得分:2)

  

我可以做得更好吗?

Protobuf是一种二进制序列化格式,针对(反)序列化和低占用率传输进行了优化。通过将数据转换为Base64,您可以添加另一个转换层并增加传输的占用空间。

  

它比从json / Json.Net传输更好/更快吗?

没有合理的论据支持这种说法。重申我之前的陈述,Protobuf是高度优化的,而JSON是某种权衡,序列化效率不高,也不是很好读。

话虽如此,举个小例子,如何通过HTTP发送你的Protobuf

HttpClient client = new HttpClient();
using (var stream = new MemoryStream())
{
    // serialize to stream
    ProtoBuf.Serializer.Serialize(stream, List1);
    stream.Seek(0, SeekOrigin.Begin);

    // send data via HTTP
    StreamContent streamContent = new StreamContent(stream);
    streamContent.Headers.Add("Content-Type", "application/octet-stream");
    var response = client.PostAsync("http://what.ever/api/upload", streamContent);
}

如果您想接收Protobuf,可以像

一样修改您的代码段
using (System.Net.Http.HttpClient client = new System.Net.Http.HttpClient())
using (System.Net.Http.HttpResponseMessage response = await client.GetAsync("http://localhost:53204/ProtobuffToString")) //<= Will call ProtobuffToString() above
using (System.Net.Http.HttpContent content = response.Content)
using (var memoryStream = new MemoryStream())
{
    await content.CopyToAsync(memoryStream);
    memoryStream.Seek(0, SeekOrigin.Begin); // reset stream

    return ProtoBuf.Serializer.Deserialize<Dictionary<int, decimal>>(memoryStream); // assuming that you return the Dictionary, not a string
}

实际上,我不明白为什么你要从string返回Dictionary<int, decimal>而不是ReadProtobuffAsync。我建议完全放弃Dictionary并使用有意义的protobuf定义和相应的类,因为这是Protobuf真正的优点。

答案 1 :(得分:2)

问题和答案完全基于“最佳”的定义是主观的。我确实看到了许多不必要的“额外”代码。我建议您直接使用Streams,不需要先转到内存流或转换为base64或转换为json。如果确实需要字符串表示,请在另一个实用程序方法中执行,而不是将其包装在此代码中。

编写代码

// depending on where you want to write to pass an appropriate Stream like a stream to write directly to an HttpPost or File on disk,
// there is no need to write to memory
public void SerializeToStream(Stream destinationStream)
{
    Dictionary<int, decimal> List1 = new Dictionary<int, decimal>();
    List1.Add(2018, 1.2m);
    List1.Add(2017, 1.4m);
    List1.Add(2016, 1.9m);

    ProtoBuf.Serializer.Serialize(destinationStream, List1);
}

阅读代码

public async System.Threading.Tasks.Task<Dictionary<int, decimal>> ReadProtobuffAsync()
{
    using (System.Net.Http.HttpClient client = new System.Net.Http.HttpClient())
    using (System.IO.Stream responseStream = await client.GetStreamAsync("http://localhost:53204/ProtobuffToString"))
    {
        // deserialize directly from the response stream
        return ProtoBuf.Serializer.Deserialize<Dictionary<int, decimal>>(responseStream));
    }
}