Grpc.Net.Client 2.67.0
Grpc.Net.Client
Grpc.Net.Client
is a gRPC client library for .NET.
Configure gRPC client
gRPC clients are concrete client types that are generated from .proto
files. The concrete gRPC client has methods that translate to the gRPC service in the .proto
file. For example, a service called Greeter
generates a GreeterClient
type with methods to call the service.
A gRPC client is created from a channel. Start by using GrpcChannel.ForAddress
to create a channel, and then use the channel to create a gRPC client:
var channel = GrpcChannel.ForAddress("https://localhost:5001");
var client = new Greet.GreeterClient(channel);
A channel represents a long-lived connection to a gRPC service. When a channel is created, it's configured with options related to calling a service. For example, the HttpClient
used to make calls, the maximum send and receive message size, and logging can be specified on GrpcChannelOptions
and used with GrpcChannel.ForAddress
. For a complete list of options, see client configuration options.
var channel = GrpcChannel.ForAddress("https://localhost:5001");
var greeterClient = new Greet.GreeterClient(channel);
var counterClient = new Count.CounterClient(channel);
// Use clients to call gRPC services
Make gRPC calls
A gRPC call is initiated by calling a method on the client. The gRPC client will handle message serialization and addressing the gRPC call to the correct service.
gRPC has different types of methods. How the client is used to make a gRPC call depends on the type of method called. The gRPC method types are:
- Unary
- Server streaming
- Client streaming
- Bi-directional streaming
Unary call
A unary call starts with the client sending a request message. A response message is returned when the service finishes.
var client = new Greet.GreeterClient(channel);
var response = await client.SayHelloAsync(new HelloRequest { Name = "World" });
Console.WriteLine("Greeting: " + response.Message);
// Greeting: Hello World
Each unary service method in the .proto
file will result in two .NET methods on the concrete gRPC client type for calling the method: an asynchronous method and a blocking method. For example, on GreeterClient
there are two ways of calling SayHello
:
GreeterClient.SayHelloAsync
- callsGreeter.SayHello
service asynchronously. Can be awaited.GreeterClient.SayHello
- callsGreeter.SayHello
service and blocks until complete. Don't use in asynchronous code.
Server streaming call
A server streaming call starts with the client sending a request message. ResponseStream.MoveNext()
reads messages streamed from the service. The server streaming call is complete when ResponseStream.MoveNext()
returns false
.
var client = new Greet.GreeterClient(channel);
using var call = client.SayHellos(new HelloRequest { Name = "World" });
while (await call.ResponseStream.MoveNext())
{
Console.WriteLine("Greeting: " + call.ResponseStream.Current.Message);
// "Greeting: Hello World" is written multiple times
}
When using C# 8 or later, the await foreach
syntax can be used to read messages. The IAsyncStreamReader<T>.ReadAllAsync()
extension method reads all messages from the response stream:
var client = new Greet.GreeterClient(channel);
using var call = client.SayHellos(new HelloRequest { Name = "World" });
await foreach (var response in call.ResponseStream.ReadAllAsync())
{
Console.WriteLine("Greeting: " + response.Message);
// "Greeting: Hello World" is written multiple times
}
Client streaming call
A client streaming call starts without the client sending a message. The client can choose to send messages with RequestStream.WriteAsync
. When the client has finished sending messages, RequestStream.CompleteAsync()
should be called to notify the service. The call is finished when the service returns a response message.
var client = new Counter.CounterClient(channel);
using var call = client.AccumulateCount();
for (var i = 0; i < 3; i++)
{
await call.RequestStream.WriteAsync(new CounterRequest { Count = 1 });
}
await call.RequestStream.CompleteAsync();
var response = await call;
Console.WriteLine($"Count: {response.Count}");
// Count: 3
Bi-directional streaming call
A bi-directional streaming call starts without the client sending a message. The client can choose to send messages with RequestStream.WriteAsync
. Messages streamed from the service are accessible with ResponseStream.MoveNext()
or ResponseStream.ReadAllAsync()
. The bi-directional streaming call is complete when the ResponseStream
has no more messages.
var client = new Echo.EchoClient(channel);
using var call = client.Echo();
Console.WriteLine("Starting background task to receive messages");
var readTask = Task.Run(async () =>
{
await foreach (var response in call.ResponseStream.ReadAllAsync())
{
Console.WriteLine(response.Message);
// Echo messages sent to the service
}
});
Console.WriteLine("Starting to send messages");
Console.WriteLine("Type a message to echo then press enter.");
while (true)
{
var result = Console.ReadLine();
if (string.IsNullOrEmpty(result))
{
break;
}
await call.RequestStream.WriteAsync(new EchoMessage { Message = result });
}
Console.WriteLine("Disconnecting");
await call.RequestStream.CompleteAsync();
await readTask;
For best performance, and to avoid unnecessary errors in the client and service, try to complete bi-directional streaming calls gracefully. A bi-directional call completes gracefully when the server has finished reading the request stream and the client has finished reading the response stream. The preceding sample call is one example of a bi-directional call that ends gracefully. In the call, the client:
- Starts a new bi-directional streaming call by calling
EchoClient.Echo
. - Creates a background task to read messages from the service using
ResponseStream.ReadAllAsync()
. - Sends messages to the server with
RequestStream.WriteAsync
. - Notifies the server it has finished sending messages with
RequestStream.CompleteAsync()
. - Waits until the background task has read all incoming messages.
During a bi-directional streaming call, the client and service can send messages to each other at any time. The best client logic for interacting with a bi-directional call varies depending upon the service logic.
Links
Showing the top 20 packages that depend on Grpc.Net.Client.
Packages | Downloads |
---|---|
Grpc.Net.ClientFactory
HttpClientFactory integration the for gRPC .NET client
|
977 |
Grpc.Net.ClientFactory
HttpClientFactory integration the for gRPC .NET client
|
526 |
Grpc.Net.ClientFactory
HttpClientFactory integration the for gRPC .NET client
|
470 |
SkyAPM.Transport.Grpc.Protocol
SkyAPM gRPC data transmitter.
|
421 |
Grpc.Net.ClientFactory
HttpClientFactory integration the for gRPC .NET client
|
378 |
Grpc.Net.ClientFactory
HttpClientFactory integration the for gRPC .NET client
|
240 |
Grpc.Net.ClientFactory
HttpClientFactory integration the for gRPC .NET client
|
168 |
Grpc.Net.ClientFactory
HttpClientFactory integration the for gRPC .NET client
|
117 |
Grpc.Net.ClientFactory
HttpClientFactory integration the for gRPC .NET client
|
97 |
Grpc.Net.ClientFactory
HttpClientFactory integration the for gRPC .NET client
|
80 |
Grpc.Net.ClientFactory
HttpClientFactory integration the for gRPC .NET client
|
60 |
Grpc.Net.ClientFactory
HttpClientFactory integration the for gRPC .NET client
|
58 |
Grpc.Net.ClientFactory
HttpClientFactory integration the for gRPC .NET client
|
46 |
Grpc.Net.ClientFactory
HttpClientFactory integration the for gRPC .NET client
|
35 |
Grpc.Net.ClientFactory
HttpClientFactory integration the for gRPC .NET client
|
27 |
Grpc.Net.ClientFactory
HttpClientFactory integration the for gRPC .NET client
|
23 |
Grpc.Net.ClientFactory
HttpClientFactory integration the for gRPC .NET client
|
22 |
Grpc.Net.ClientFactory
HttpClientFactory integration the for gRPC .NET client
|
21 |
SkyAPM.Transport.Grpc
SkyAPM gRPC data transmitter.
|
21 |
.NET Framework 4.6.2
- Grpc.Net.Common (>= 2.67.0)
- System.Net.Http.WinHttpHandler (>= 8.0.0)
- System.Diagnostics.DiagnosticSource (>= 6.0.1)
- Microsoft.Extensions.Logging.Abstractions (>= 6.0.0)
.NET Standard 2.1
- System.Diagnostics.DiagnosticSource (>= 6.0.1)
- Microsoft.Extensions.Logging.Abstractions (>= 6.0.0)
- Grpc.Net.Common (>= 2.67.0)
.NET Standard 2.0
- System.Diagnostics.DiagnosticSource (>= 6.0.1)
- Grpc.Net.Common (>= 2.67.0)
- Microsoft.Extensions.Logging.Abstractions (>= 6.0.0)
.NET 8.0
- Microsoft.Extensions.Logging.Abstractions (>= 6.0.0)
- Grpc.Net.Common (>= 2.67.0)
.NET 7.0
- Grpc.Net.Common (>= 2.67.0)
- Microsoft.Extensions.Logging.Abstractions (>= 6.0.0)
.NET 6.0
- Microsoft.Extensions.Logging.Abstractions (>= 6.0.0)
- Grpc.Net.Common (>= 2.67.0)
Version | Downloads | Last updated |
---|---|---|
2.67.0 | 5 | 12/01/2024 |
2.67.0-pre1 | 9 | 10/24/2024 |
2.66.0 | 10 | 09/21/2024 |
2.66.0-pre1 | 7 | 09/11/2024 |
2.65.0 | 10 | 07/27/2024 |
2.65.0-pre1 | 9 | 08/16/2024 |
2.64.0 | 11 | 07/20/2024 |
2.64.0-pre1 | 8 | 08/16/2024 |
2.63.0 | 9 | 05/28/2024 |
2.63.0-pre1 | 11 | 05/13/2024 |
2.62.0 | 13 | 04/01/2024 |
2.62.0-pre1 | 15 | 03/09/2024 |
2.61.0 | 14 | 02/28/2024 |
2.61.0-pre1 | 13 | 02/19/2024 |
2.60.0 | 197 | 01/17/2024 |
2.60.0-pre1 | 13 | 02/10/2024 |
2.59.0 | 929 | 11/15/2023 |
2.59.0-pre1 | 16 | 11/03/2023 |
2.58.0 | 13 | 12/20/2023 |
2.58.0-pre1 | 12 | 10/11/2023 |
2.57.0 | 13 | 11/10/2023 |
2.57.0-pre1 | 15 | 12/18/2023 |
2.56.0 | 14 | 10/30/2023 |
2.56.0-pre2 | 18 | 10/29/2023 |
2.56.0-pre1 | 18 | 08/14/2023 |
2.55.0 | 18 | 09/27/2023 |
2.55.0-pre1 | 14 | 11/09/2023 |
2.54.0 | 18 | 08/12/2023 |
2.54.0-pre1 | 13 | 09/22/2023 |
2.53.0 | 16 | 06/05/2023 |
2.53.0-pre1 | 16 | 07/02/2023 |
2.52.0 | 15 | 09/22/2023 |
2.52.0-pre1 | 18 | 09/27/2023 |
2.51.0 | 15 | 10/19/2023 |
2.51.0-pre1 | 15 | 11/06/2023 |
2.50.0 | 1,380 | 12/14/2022 |
2.50.0-pre1 | 13 | 08/15/2023 |
2.49.0 | 472 | 12/14/2022 |
2.49.0-pre1 | 13 | 01/24/2024 |
2.48.0 | 16 | 06/15/2023 |
2.48.0-pre1 | 19 | 12/03/2023 |
2.47.0 | 268 | 11/24/2023 |
2.47.0-pre1 | 13 | 12/13/2023 |
2.46.0 | 388 | 06/15/2022 |
2.46.0-pre1 | 16 | 06/05/2023 |
2.45.0 | 19 | 09/27/2023 |
2.45.0-pre1 | 18 | 07/17/2023 |
2.44.0 | 22 | 03/20/2022 |
2.44.0-pre1 | 15 | 07/18/2023 |
2.43.0 | 14 | 10/31/2023 |
2.43.0-pre1 | 13 | 10/03/2023 |
2.42.0 | 20 | 07/01/2023 |
2.42.0-pre1 | 14 | 08/25/2023 |
2.41.0 | 16 | 06/04/2023 |
2.41.0-pre1 | 18 | 10/22/2023 |
2.40.0 | 1,918 | 11/12/2021 |
2.40.0-pre1 | 18 | 10/02/2023 |
2.39.0 | 132 | 09/16/2021 |
2.39.0-pre1 | 14 | 11/13/2023 |
2.38.0 | 97 | 07/16/2021 |
2.38.0-pre1 | 19 | 08/16/2023 |
2.37.0 | 85 | 05/24/2021 |
2.37.0-pre1 | 15 | 10/14/2023 |
2.36.0 | 41 | 03/19/2021 |
2.36.0-pre1 | 15 | 07/01/2023 |
2.35.0 | 26 | 03/03/2021 |
2.35.0-pre1 | 16 | 10/07/2023 |
2.34.0 | 706 | 01/13/2021 |
2.34.0-pre1 | 15 | 12/11/2023 |
2.33.1 | 20 | 12/16/2020 |
2.33.1-pre1 | 16 | 10/26/2023 |
2.32.0 | 114 | 01/12/2021 |
2.32.0-pre1 | 13 | 09/12/2023 |
2.31.0 | 18 | 10/06/2023 |
2.31.0-pre2 | 14 | 07/05/2023 |
2.30.0 | 17 | 09/14/2023 |
2.30.0-pre1 | 14 | 11/19/2023 |
2.29.0 | 17 | 09/10/2023 |
2.29.0-pre1 | 20 | 11/13/2023 |
2.28.0 | 15 | 09/03/2023 |
2.28.0-pre2 | 12 | 01/22/2024 |
2.28.0-pre1 | 15 | 11/30/2023 |
2.27.0 | 18 | 09/20/2023 |
2.27.0-pre1 | 15 | 01/23/2024 |
2.26.0 | 12 | 11/15/2023 |
2.26.0-pre1 | 15 | 09/21/2023 |
2.25.0 | 17 | 09/05/2023 |
2.25.0-pre1 | 13 | 03/01/2024 |
2.24.0 | 14 | 11/26/2023 |
2.24.0-pre1 | 17 | 09/28/2023 |
2.23.2 | 17 | 09/27/2023 |
0.2.23-pre2 | 15 | 10/12/2023 |
0.2.23-pre1 | 17 | 12/21/2023 |
0.1.22-pre3 | 14 | 01/25/2024 |
0.1.22-pre2 | 14 | 12/02/2023 |
0.1.22-pre1 | 16 | 11/08/2023 |
0.1.21-pre1 | 14 | 10/15/2023 |