Table of Contents
1. Introduction
2. Develop gRPC client in .NET 7
3. Test the client
4. Conclusion
Introduction
gRPC (Google Remote Procedure Call) is an open-source high-performance framework developed by Google for building distributed systems.
In my previous post we talked about creating a gRPC service [Develop gRPC service in .NET 7.0]. This post will talk about developing a gRPC client in .NET 7 which will consume the gRPC service that was previously created.
Develop gRPC client in .NET 7
Create a new project in Visual Studio by using "Console App" template.
Give a name for the service and choose a location to save the project.
From the dropdown in the next screen choose .NET 7.0 and leave all other options with default value. Click on create button to create the project.
Add the following NuGet packages to the project.
Right click on the "health.proto" file in the solution and open properties window. Set the value for 'Build Action' as 'Protobuf compiler' and value for 'gRPC Stub Classes' as 'Client only'.
<ItemGroup>
<Protobuf Include="Protos\health.proto" GrpcServices="Client" />
</ItemGroup>
Now build the project [Ctrl+Shift+B] which will generate C# code for the .proto file which can be used to implement your service.
Update the Program.cs file with the following code.
using GrpcClient;
using var channel = GrpcChannel.ForAddress("http://localhost:4000");
var client = new HealthCheck.HealthCheckClient(channel);
var reply = await client.GetHealthAsync(new HealthRequest { });
Console.WriteLine("Response received : " + reply.Message);
Console.ReadKey();
Comments
Post a Comment