C# 通过 http 传输序列化 Protobuff 数据的最佳方式



我正在使用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传输更好/更快吗?

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

我能做得更好吗?

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而不是ReadProtobuffAsyncDictionary<int, decimal>.我会重新评论完全放弃Dictionary并使用有意义的 protobuf 定义和相应的类,因为这是 Protobuf 的真正强项。

问题和答案完全基于"最佳"的定义是主观的。我确实看到很多不必要的"额外"代码。我建议你直接使用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));
}
}

最新更新