我该怎么做才能使用C#ASP .NET Web API发送Protobuf序列化数据? [英] What do I need to do to send out Protobuf serialized Data using C# ASP .NET web api?

查看:68
本文介绍了我该怎么做才能使用C#ASP .NET Web API发送Protobuf序列化数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是protobuf的新手,并且是.net的新手.所以我可能需要帮助.我的PersonsController有以下代码段:

I'm new to protobuf and fairly new to asp .net. so I might need help on this. I have this code snippet for my PersonsController:

public class PersonController : ApiController
{
    [ProtoContract]
    public class Person
    {
        [ProtoMember(1)]
        public int ID { get; set; }
        [ProtoMember(2)]
        public string First { get; set; }
        [ProtoMember(3)]
        public string Last { get; set; }
    }
    // GET api/values
    public IEnumerable<Person> Get()
    {
        List<Person> myList = new List<Person> { 
            new Person { ID = 0, First = "Judy", Last  = "Lee" },
            new Person { ID = 1, First = "John", Last  = "Doe" },
            new Person { ID = 2, First = "George", Last  = "Poole" },
        };

        return myList;
    }
}

,我想知道是否足以发送出protobuf数据并被其他应用程序使用?

and I'm wondering if that's enough to be able to send out the protobuf data and be consumed by other applications?

我正在尝试直接在Google chrome中访问它,而我所得到的只是数据的XML格式.

I'm trying to access it directly in google chrome and all I'm getting is an XML format of the data.

<ArrayOfPersonController.Person xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/SampleSerialize.Controllers">
  <PersonController.Person>
    <First>Judy</First>
    <ID>0</ID>
    <Last>Lee</Last>
  </PersonController.Person>
  <PersonController.Person>
    <First>John</First>
    <ID>1</ID>
    <Last>Doe</Last>
  </PersonController.Person>
  <PersonController.Person>
    <First>George</First>
    <ID>2</ID>
    <Last>Poole</Last>
  </PersonController.Person>
</ArrayOfPersonController.Person>

我怎么知道我是否能够发送序列化的数据?

How do I know if I'm able to send out the serialized data?

推荐答案

您需要做几件事:

  1. 您需要一个Web客户端,该客户端实际上在请求的 Accept 高级REST客户端,可让您使用REST API从浏览器中,并根据需要设置请求标头.

  1. You need a web client that actually asks for protobuf-serialized content in the request's Accept header. Chrome doesn't do this--it only asks for things like text/html, image/*, etc... stuff you'd expect a web browser to ask for. There isn't a standard content type for protobuf, so you can just define your own--many people use application/x-protobuf. There are Chrome developer tools like Advanced REST client that let you exercise REST APIs from a browser and set your request headers however you'd like.

在Web API方面,您需要创建并注册自己的媒体格式化程序.有一个很好的演练 BufferedMediaTypeFormatter 派生进行原始(de/)序列化,您将需要配置此类以处理 application/x-protobuf 请求.然后,您需要在Web API管道中注册它.

On the Web API side, you need to create and register your own media formatter. There's a good walkthrough here. You'll probably derive from BufferedMediaTypeFormatter to do the protobuf (de/)serialization, and you'll want to configure this class to handle application/x-protobuf requests. Then you'll need to register it with the Web API pipeline.

这篇关于我该怎么做才能使用C#ASP .NET Web API发送Protobuf序列化数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆