Code Monkey home page Code Monkey logo

protobuf-net-sample's Introduction

Protobuf-Net-Sample

Use Protobuf in Asp.Net Core WebApi(.net core 3.1)

1.Inport Nuget

protobuf-net

WebApiContrib.Core.Formatter.Protobuf

2.Edit Startup.cs

services.AddControllers()
                .AddProtobufFormatters();

3.Model

[ProtoContract]
    public class Article
    {
        [ProtoMember(1)]
        public int Id { get; set; }

        [ProtoMember(2)]
        public string Title { get; set; }

        [ProtoMember(3)]
        public string Content { get; set; }
    }
    
    
[ProtoContract]
    public class Book
    {
        [ProtoMember(1)]
        public int Id { get; set; }

        [ProtoMember(2)]
        public string Name { get; set; }

        [ProtoMember(3)]
        public Article Article { get; set; }
    }

4.Controller Action Return Protobuf

[HttpGet]
        //添加此配置项,仅返回application/x-protobuf格式的数据,若未添加默认返回json,或客户端请求时指定Accept=application/x-protobuf
        //[Produces("application/x-protobuf")]
        public Book GetBook()
        {
            var request = HttpContext.Request;
            _logger.LogInformation(request.Host.Value);

            var book = new Book
            {
                Id = 12345,
                Name = "Fred",
                Article = new Article
                {
                    Id = 1,
                    Title = ".Net Core Protobuf",
                    Content = "Protobuf is great"
                }
            };
            return book;

        }

5.Client Call Rest API And Deserialize Form Protobuf

HttpClient client = new HttpClient();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/x-protobuf"));
            var response = await client.GetAsync("http://"+HttpContext.Request.Host.Value+"/book/getBook");
            MemoryStream stream = new MemoryStream();
            await response.Content.CopyToAsync(stream);
            stream.Position = 0;
            var book = Serializer.Deserialize<Book>(stream);

protobuf-net-sample's People

Contributors

uchiha-peng avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.