.NET7 gRPC JSON转码+OpenAPI( 二 )


.NET7 gRPC JSON转码+OpenAPI

文章插图
在支持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】

经验总结扩展阅读