简体   繁体   English

如何从HttpResponseMessage反序列化protobuf内容

[英]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.

相关问题 反序列化 HttpResponseMessage - Deserialize HttpResponseMessage 当来自CreateErrorResponse时,HttpResponseMessage.Content.ReadAsStringAsync不反序列化JSON - HttpResponseMessage.Content.ReadAsStringAsync don't deserialize JSON when it come from CreateErrorResponse 从 HttpResponseMessage 获取内容/消息 - Getting content/message from HttpResponseMessage 我如何从控制台应用程序中提取 HTTPResponseMessage 内容 - How do i Extract HTTPResponseMessage content from Console App 如何从 HttpResponseMessage 读取应用程序/pdf 内容类型并将其转换为 stream - How to read application/pdf content type from HttpResponseMessage and convert it into stream 如何从另一个HttpResponseMessage返回PDF内容给浏览器? - How to return PDF content from another HttpResponseMessage to the browser? 如何将 HttpResponseMessage 内容读取为文本 - How to read HttpResponseMessage content as text 如何将文件放入HttpResponseMessage内容? - How to put a file in the HttpResponseMessage Content? 从HttpResponseMessage.Content读取流式内容 - Reading streamed content from HttpResponseMessage.Content 无法将 HttpResponseMessage 反序列化为 object - Unable to deserialize HttpResponseMessage to an object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM