怎样为asp.net核心添加protobuf支持

  介绍

这篇文章给大家分享的是有关怎样为asp.net核心添加protobuf支持的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

<强>前言

在一些性能要求很高的应用中,使用协议缓冲区序列化,优于Json。而且协议缓冲区向后兼容的能力比较好。

由于asp.net核心采用了全新中间件的方式,因此使用protobuf序列化,只需要使用Protobuf-net修饰需要序列化的对象,并在MVC初始化的时候增加相应的格式化程序就可以了。

<强>没时间解释了,快上车。

通过NuGet获取Zaabee。AspNetCoreProtobuf

Install-Package  Zaabee.AspNetCoreProtobuf

在启动。cs文件中修改ConfigureServices方法

public  void  ConfigureServices (IServiceCollection 服务)   {   services.AddMvc才能(options =祝辞,{,options.AddProtobufSupport ();,});   }

搞掂……这时候你就可以通过应用程序/x-protobuf的内容类型来让asp.net核心使用protobuf来进行序列化/反序列化。

<强>测试代码

在asp.net核心项目中添加以下DTO

[ProtoContract]   public  class  TestDto   {   [ProtoMember才能(1)),public  Guid  Id {组,得到,,,}   [ProtoMember才能(2)),public  string  Name {组,得到,,,}   [ProtoMember才能(3)],public  DateTime  CreateTime {组,得到,,,}   [ProtoMember才能(4)],public  List, Kids {组,得到,,,}   [ProtoMember才能(5)],public  long  Tag {组,得到,,,}   [ProtoMember才能(6)],public  TestEnum  Enum {组,得到,,,}   }      public  enum  TestEnum   {   苹果,才能   香蕉,才能   ,梨   }

新建一个XUnit项目,通过Nuget引用Microsoft.AspNetCore。TestHost,建立一个测试类

public  class  AspNetCoreProtobufTest   {   private 才能readonly  TestServer  _server;   private 才能readonly  HttpClient  _client;      public 才能;AspNetCoreProtobufTest ()   {才能   ,,,_server =, new  TestServer (   ,,,,,new  WebHostBuilder ()   ,,,,,,,.UseKestrel ()   ,,,,,,,.UseStartup ());   ,,,_client =, _server.CreateClient ();   ,,}      [事实]才能   public 才能;void 测试()   {才能   ,,,//,HTTP  Post  with  Protobuf  Response 身体   ,,,_client.DefaultRequestHeaders.Accept.Add (new  MediaTypeWithQualityHeaderValue(“应用程序/x-protobuf"));      ,,,var  dtos =,让getdto ();   ,,,var  stream =, new  MemoryStream ();   ,,,ProtoBuf.Serializer.Serialize(流,,dto);      ,,,HttpContent  HttpContent =, new  StreamContent(流);      ,,,//,HTTP  POST  with  Protobuf  Request 身体   ,,,var  responseForPost =, _client.PostAsync (“api/Values",, httpContent);      ,,,var  result =, ProtoBuf.Serializer.Deserialize祝辞(   ,,,,,responseForPost.Result.Content.ReadAsStreamAsync () .Result);      ,,,Assert.True (CompareDtos (dto,结果));   ,,}      private 才能static  bool  CompareDtos (List, lstTwo)   {才能   ,,,lstOne =, lstOne  ? ?, new  List ();   ,,,lstTwo =, lstTwo  ? ?, new  List ();      ,,,if  (lstOne.Count  !=, lstTwo.Count), return 假;      ,,,for  (var 小姐:=,0;,小姐:& lt;, lstOne.Count;,我+ +)   ,,,{   ,,,,,var  dtoOne =, lstOne[我];   ,,,,,var  dtoTwo =, lstTwo[我];   ,,,,,if  (dtoOne.Id  !=, dtoTwo.Id  | |, dtoOne.CreateTime  !=, dtoTwo.CreateTime  | |, dtoOne.Enum  !=, dtoTwo.Enum  | |   ,,,,,,,dtoOne.Name  !=, dtoTwo.Name  | |, dtoOne.Tag  !=, dtoTwo.Tag  | |, ! CompareDtos (dtoOne.Kids, dtoTwo.Kids))   ,,,,,,,return 假;   ,,,}      ,,,return 真实;   ,,}      private 才能static  List< TestDto>,让getdto ()   {才能   ,,,return  new  List   ,,,{   ,,,,,new  TestDto   ,,,,,{   ,,,,,,,Id =, Guid.NewGuid (),   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null

怎样为asp.net核心添加protobuf支持