文章插图
在支持http方式的请求的情况下,同时支持grpc请求 。
实现Client端新增控制台项目,添加以下nuget
<ItemGroup><PackageReference Include="Google.Protobuf" Version="3.21.9" /><PackageReference Include="Grpc.Net.Client" Version="2.49.0" /><PackageReference Include="Microsoft.AspNetCore.Grpc.JsonTranscoding" Version="7.0.0" /><PackageReference Include="Grpc.Tools" Version="2.50.0"><PrivateAssets>all</PrivateAssets><IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets></PackageReference></ItemGroup>包含与server端相同的proto文件
<ItemGroup><Protobuf Include="Protos\greet.proto" GrpcServices="Client" /></ItemGroup>尝试请求Server
static async Task Main(string[] args){using var channel = GrpcChannel.ForAddress("https://localhost:7167");var client = new Greeter.GreeterClient(channel);var reply = await client.SayHelloAsync(new HelloRequest { Name = "GreeterClient" });Console.WriteLine("Greeting: " + reply.Message);var replyPost = await client.SayHelloPostAsync(new HelloRequestPost { Data = https://www.huyubaike.com/biancheng/new HelloRequestPostData { Name ="111" } });Console.WriteLine("Greeting: " + replyPost.Message);Console.WriteLine("Press any key to exit...");Console.ReadKey();}source code: https://github.com/cysnet/grpc-json-transcoding
【.NET7 gRPC JSON转码+OpenAPI】
经验总结扩展阅读
- 试试将.NET7编译为WASM并在Docker上运行
- .net 温故知新:【8】.NET 中的配置从xml转向json
- JMETER与它的组件们
- apijson 初探
- .NET性能系列文章二:Newtonsoft.Json vs. System.Text.Json
- Dapr实现.Net Grpc服务之间的发布和订阅,并采用WebApi类似的事件订阅方式
- 是什么让.NET7的Min和Max方法性能暴增了45倍?
- .NET性能系列文章一:.NET7的性能改进
- Springboot JSON 转换:Jackson篇
- 2022年实时最新省市区县乡镇街道geojson行政边界数据获取方法