[英]How to deserialize protobuf content from HttpResponseMessage
I'm trying to deserialize the response message from my database in protobuf format. 我正在尝试以protobuf格式反序列化来自数据库的响应消息。 It has this schema: 它具有以下架构:
syntax = "proto3";
message Person {
uint64 id = 1;
string name = 2;
string surname = 3;
uint32 age = 4;
};
and I created this class to deserialize it: 我创建了该类以反序列化它:
[ProtoContract]
public class Person
{
[ProtoMember(1)]
public long Id { get; set; }
[ProtoMember(2)]
public string Name { get; set; }
[ProtoMember(3)]
public string Surname { get; set; }
[ProtoMember(4)]
public int Age { get; set; }
}
Next, I tried to do this: 接下来,我尝试执行以下操作:
var response = await client.PostAsync("", new StringContent(request));
using (var responseStream = await response.Content.ReadAsStreamAsync())
{
var person = Serializer.Deserialize<Person>(responseStream);
}
But I cathed a ProtoBuf.ProtoException: Invalid wire-type;
但是我加上了一个ProtoBuf.ProtoException: Invalid wire-type;
. 。
Then I decided to see at byte array: 然后我决定看一下字节数组:
{25, 8, 1, 18, 6, 82, 111, 98, 101, 114, 116, 26, 11, 79, 112, 112, 101, 110, 104, 101, 105, 109, 101, 114, 32, 38 }
And I created by hand an entry, that must be returned by my database, and serialize it: 我手动创建了一个条目,该条目必须由我的数据库返回并序列化:
{8, 1, 18, 6, 82, 111, 98, 101, 114, 116, 26, 11, 79, 112, 112, 101, 110, 104, 101, 105, 109, 101, 114, 32, 38 }
As you can see, they are almost the same, but my db send 25
at the start. 如您所见,它们几乎相同,但是我的数据库在开始时发送25
。
Could you help me, what could be the error? 您能帮我吗,可能是什么错误? Thanks! 谢谢!
I realized, that it is due to streaming multiple messages
. 我意识到,这是由于streaming multiple messages
。 And in this case, I've rewritten my method to: 在这种情况下,我将方法重写为:
List<Person> persons;
using (var responseStream = await response.Content.ReadAsStreamAsync())
{
persons = Serializer.DeserializeItems<Person>(responseStream, PrefixStyle.Base128, 0).ToList();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.