zoukankan      html  css  js  c++  java
  • 在Web Api中集成protobuf

    1. 安装WebApiContrib.Formatting.ProtoBuf

      1. Install-Package WebApiContrib.Formatting.ProtoBuf
    2. 注册ProtoBufFormatter

      1. public static class WebApiConfig
            {
                public static void Register(HttpConfiguration config)
                {
                    config.Formatters.Add(new ProtoBufFormatter());
                    // Web API configuration and services
        
                    // Web API routes
                    config.MapHttpAttributeRoutes();
        
                    config.Routes.MapHttpRoute(
                        name: "DefaultApi",
                        routeTemplate: "api/{controller}/{id}",
                        defaults: new { id = RouteParameter.Optional }
                    );
                }
            }
        
    3. 设置DTO

      1. 注意这里的Order是必须的
      2.  [DataContract]
            public class Item
            {
                [DataMember(Order = 1)]
                public int Id { get; set; }
                [DataMember(Order = 2)]
                public string Name { get; set; }
                [DataMember(Order = 3)]
                public long Value { get; set; }
            }
        
    4. 客户端调用

          static  void Main(string[] args)
            {
          
                var httpClient = new HttpClient();
             
                httpClient.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/x-protobuf"));
    
                var response = httpClient.GetAsync("http://localhost:60339/api/Default/GetItem").Result;
                //把 ProtoBuf Stream 反序列化成 集合
                var  obj = (RuntimeTypeModel.Default).Deserialize(response.Content.ReadAsStreamAsync().Result, null, typeof(List<Item>)) as List<Item>;
                
                Console.WriteLine(obj.Count);
    
                //设置请求头
                var content = new ObjectContent<List<Item>>(obj, new ProtoBufFormatter());
                content.Headers.ContentType = new MediaTypeHeaderValue("application/x-protobuf");
    
                var postResponse1=  httpClient.PostAsync("http://localhost:60339/api/Default/PostItem", content).Result;
                var postResult = postResponse1.Content.ReadAsStreamAsync().Result;
                var intValue = (RuntimeTypeModel.Default).Deserialize(postResult, null, typeof(int));
    
                Console.WriteLine(intValue);
                Console.ReadKey();
            }
    

     使用Fiddler发送Accept 分别为Json,Xml,Protobuf三种格式来请求数据

    git

     https://github.com/xlb378917466/WebApi_protobuf

  • 相关阅读:
    Webform中Repeater控件--绑定嵌入C#代码四种方式
    webform中listbox运用,2个相互传值练习1:
    webform基础介绍及页面传值(session,cookie)、跳转页面
    Webform 常用控件
    使用VS建立Web网站及IIS
    linq to object
    linq to sql用partial扩展属性,创建一个部分类(用于多表连接)
    LINQ 图解
    Linq to sql介绍及增、删、改、查
    数据库操作
  • 原文地址:https://www.cnblogs.com/LittleFeiHu/p/6249231.html
Copyright © 2011-2022 走看看