grpc asp.net core 集成时一些配置的说明

      一  什么是grpc                                                                                                                                    

              google出了一款分布式通讯框架:grpc。我想这也不是新的东西了,在13年的一个项目中,用在了数据层和业务端之间的通讯上,当时并没有觉得怎么样,因为wcf很轻松的也可以可以实现哪些功能。但是想在想一想,确实在某些场合,grpc比wcf要更好一点。第一就是比wcf要轻量级的多,第二,跨平台,他出了一款协议Protocol Buffers,来定义接口,然后使用工具去生成不同 的语言代码。而wcf我们也知道,跨平台使用soap协议,基于http,坏处就是慢。而基于tcp或者命名管道,又不跨平台。所以grpc非常适合某些特殊的场景中使用。尤其是他的消息交换是json,比wcf的xml轻量级很多。

      二  配置说明                                                                                                                                          

               vs2017简单的安装几个nuget包:

                                                                                                                             

                为项目添加proto文件,查看工程文件

          

 <ItemGroup> <Protobuf Include="proto\order.proto" CompileOutputs="false" /> </ItemGroup>

        CompileOutputs:这个配置属性意思就是不要编译进程序集

 <ItemGroup> <Protobuf Include="**/*.proto" OutputDir="%(RelativePath)" CompileOutputs="false" /> </ItemGroup>
OutputDir :这个属性意思就很明显了,输出目录
 <ItemGroup> <Protobuf Include="**/*.proto" OutputDir="%(RelativePath)" CompileOutputs="false" GrpcServices="None" /> <Protobuf Update="**/hello/*.proto;**/bye/*.proto" GrpcServices="Both" /> </ItemGroup>
<Protobuf Update="**/hello/*.proto;**/bye/*.proto" GrpcServices="Both" /> 告诉插件只有这个节点配置的proto才进行更新,这是针对大一点的项目,不想让它生成整个项目的proto
GrpcServices是指生成客户端,还是服务端,还是两部分都生成。
 <ItemGroup> <Protobuf Include="**/*.proto" OutputDir="%(RelativeDir)" CompileOutputs="false" GrpcServices="None" /> </ItemGroup>

GrpcServices="None" 只生成message不生成services

 <ItemGroup> <Protobuf Include="../**/*.proto" ProtoRoot=".." OutputDir="%(RelativeDir)" CompileOutputs="false" /> </ItemGroup>

ProtoRoot 指定生成的代码导出的地方。

相关文章