Create the gRPC cliet in a .NET console app
Create a console app
dotnet new console -o GrpcGreeterClient
Add gRPC packages to the project
dotnet add GrpcGreeterClient.csproj package Grpc.Net.Client
dotnet add GrpcGreeterClient.csproj package Google.Protobuf
dotnet add GrpcGreeterClient.csproj package Grpc.Tools
Add gree.proto
- Create a
Protofolder in the project. - Copy the
Greet.protofile from gRPC server to here.(gRPC server and clinet use a same proto file) - add namespace to the file
option csharp_namespace = "GrpcGreeterClient"
Add <Protobuf> element to <ItemGroup> in the .csproj file.
<ItemGroup>
<Protobuf Include="Protos/greet.proto" GrpcService="Client">
<ItemGroup>
Create the Greeter client
-
Build the client project to create the types in the
GrpcGreeterClientnamespace. -
Update the gRPC client
Program.csfile.
var GrpcServerAddress = "https://localhost:7042"
// the port number must match the port of hte gRPC server
using var channel = GrpcChannel.ForAddress(GrpcServerAddress)
var client = new Greeter.GreeterClient(channel);
var reply = await client.SayHelloAsync(
new HelloRequest { Name = "GreeterClient" });
Console.WriteLine("Greeting" + reply.message);