Advertisement
Intermediate ASP.NET Core, gRPC Services

How do you generate and use a strongly-typed gRPC client in a .NET application to call a gRPC service?

Short answer

The Grpc.Net.Client package combined with the same .proto file (with GrpcServices="Client" in the .csproj) generates a strongly-typed client class; instantiate a GrpcChannel pointing at the service's address, construct the generated client class with that channel, and call its…

Save ASP.NET Core mock interview
Advertisement
AModel answer
The Grpc.Net.Client package combined with the same .proto file (with GrpcServices="Client" in the .csproj) generates a strongly-typed client class; instantiate a GrpcChannel pointing at the service's address, construct the generated client class with that channel, and call its strongly-typed methods directly -- ASP.NET Core's typed HttpClient integration (AddGrpcClient<T>()) further simplifies this by handling channel lifetime and DI registration automatically.
// Program.cs -- DI-based typed client registration
builder.Services.AddGrpcClient<Greeter.GreeterClient>(options => {
    options.Address = new Uri("https://localhost:5001");
});

// Injected and used in a service
public class MyService(Greeter.GreeterClient client) {
    public async Task<string> Greet(string name) {
        var reply = await client.SayHelloAsync(new HelloRequest { Name = name });
        return reply.Message;
    }
}
Real-world example
An order-processing service registers a typed gRPC client for the inventory service via AddGrpcClient, getting automatic HttpClient lifetime management and DI integration the same way a typed HttpClient for a REST API would be registered.
Likely follow-ups
  • How does connection pooling/channel reuse work under the hood with GrpcChannel?
  • How would you configure retry policies for a gRPC client using the built-in resilience handlers?
Related topics