Grpc.Net.Client
2.64.0
Prefix Reserved
See the version list below for details.
dotnet add package Grpc.Net.Client --version 2.64.0
NuGet\Install-Package Grpc.Net.Client -Version 2.64.0
<PackageReference Include="Grpc.Net.Client" Version="2.64.0" />
paket add Grpc.Net.Client --version 2.64.0
#r "nuget: Grpc.Net.Client, 2.64.0"
// Install Grpc.Net.Client as a Cake Addin #addin nuget:?package=Grpc.Net.Client&version=2.64.0 // Install Grpc.Net.Client as a Cake Tool #tool nuget:?package=Grpc.Net.Client&version=2.64.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
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 is compatible. |
.NET Framework | net461 was computed. net462 is compatible. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETFramework 4.6.2
- Grpc.Net.Common (>= 2.64.0)
- Microsoft.Extensions.Logging.Abstractions (>= 6.0.0)
- System.Diagnostics.DiagnosticSource (>= 6.0.1)
- System.Net.Http.WinHttpHandler (>= 8.0.0)
-
.NETStandard 2.0
- Grpc.Net.Common (>= 2.64.0)
- Microsoft.Extensions.Logging.Abstractions (>= 6.0.0)
- System.Diagnostics.DiagnosticSource (>= 6.0.1)
-
.NETStandard 2.1
- Grpc.Net.Common (>= 2.64.0)
- Microsoft.Extensions.Logging.Abstractions (>= 6.0.0)
- System.Diagnostics.DiagnosticSource (>= 6.0.1)
-
net6.0
- Grpc.Net.Common (>= 2.64.0)
- Microsoft.Extensions.Logging.Abstractions (>= 6.0.0)
-
net7.0
- Grpc.Net.Common (>= 2.64.0)
- Microsoft.Extensions.Logging.Abstractions (>= 6.0.0)
-
net8.0
- Grpc.Net.Common (>= 2.64.0)
- Microsoft.Extensions.Logging.Abstractions (>= 6.0.0)
NuGet packages (743)
Showing the top 5 NuGet packages that depend on Grpc.Net.Client:
Package | Downloads |
---|---|
Grpc.Net.ClientFactory
HttpClientFactory integration the for gRPC .NET client |
|
Google.Api.Gax.Grpc
Additional support classes for Google gRPC API client libraries |
|
OpenTelemetry.Exporter.OpenTelemetryProtocol
OpenTelemetry protocol exporter for OpenTelemetry .NET |
|
Microsoft.Azure.Functions.Worker.Grpc
This library provides gRPC support for Azure Functions .NET Worker communication with the Azure Functions Host. |
|
CouchbaseNetClient
The Official Couchbase .NET SDK. |
GitHub repositories (89)
Showing the top 5 popular GitHub repositories that depend on Grpc.Net.Client:
Repository | Stars |
---|---|
2dust/v2rayN
A GUI client for Windows, support Xray core and v2fly core and others
|
|
microsoft/semantic-kernel
Integrate cutting-edge LLM technology quickly and easily into your apps
|
|
dotnet/runtime
.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
|
|
dotnet/AspNetCore.Docs
Documentation for ASP.NET Core
|
|
dodyg/practical-aspnetcore
Practical samples of ASP.NET Core 9 RC2, 8.0, 7.0, 6.0, 5.0, 3.1, 2.2, and 2.1,projects you can use. Readme contains explanations on all projects.
|
Version | Downloads | Last updated |
---|---|---|
2.67.0-pre1 | 1,964 | 10/22/2024 |
2.66.0 | 1,184,479 | 9/20/2024 |
2.66.0-pre1 | 4,168 | 9/6/2024 |
2.65.0 | 2,551,907 | 7/27/2024 |
2.65.0-pre1 | 880 | 7/20/2024 |
2.64.0 | 291,840 | 7/19/2024 |
2.64.0-pre1 | 1,957 | 7/15/2024 |
2.63.0 | 3,317,734 | 5/24/2024 |
2.63.0-pre1 | 22,510 | 5/8/2024 |
2.62.0 | 4,891,329 | 3/29/2024 |
2.62.0-pre1 | 8,488 | 3/8/2024 |
2.61.0 | 3,125,629 | 2/22/2024 |
2.61.0-pre1 | 13,914 | 2/8/2024 |
2.60.0 | 18,320,767 | 1/3/2024 |
2.60.0-pre1 | 18,179 | 12/14/2023 |
2.59.0 | 4,564,472 | 11/8/2023 |
2.59.0-pre1 | 9,225 | 10/27/2023 |
2.58.0 | 1,477,924 | 10/19/2023 |
2.58.0-pre1 | 33,001 | 10/9/2023 |
2.57.0 | 3,522,814 | 9/6/2023 |
2.57.0-pre1 | 5,514 | 8/31/2023 |
2.56.0 | 942,282 | 8/25/2023 |
2.56.0-pre2 | 7,434 | 8/16/2023 |
2.56.0-pre1 | 22,849 | 8/3/2023 |
2.55.0 | 17,454,025 | 7/4/2023 |
2.55.0-pre1 | 11,104 | 6/23/2023 |
2.54.0 | 1,714,315 | 6/15/2023 |
2.54.0-pre1 | 13,720 | 5/25/2023 |
2.53.0 | 9,606,726 | 5/5/2023 |
2.53.0-pre1 | 25,868 | 4/12/2023 |
2.52.0 | 38,753,718 | 3/15/2023 |
2.52.0-pre1 | 13,776 | 3/3/2023 |
2.51.0 | 15,343,724 | 1/2/2023 |
2.51.0-pre1 | 22,123 | 12/7/2022 |
2.50.0 | 5,712,196 | 11/17/2022 |
2.50.0-pre1 | 37,033 | 11/3/2022 |
2.49.0 | 19,033,886 | 9/26/2022 |
2.49.0-pre1 | 10,834 | 9/1/2022 |
2.48.0 | 2,398,392 | 8/24/2022 |
2.48.0-pre1 | 4,287 | 8/17/2022 |
2.47.0 | 8,812,762 | 7/3/2022 |
2.47.0-pre1 | 5,456 | 6/23/2022 |
2.46.0 | 13,665,578 | 5/13/2022 |
2.46.0-pre1 | 9,613 | 4/28/2022 |
2.45.0 | 2,438,072 | 4/19/2022 |
2.45.0-pre1 | 15,103 | 4/7/2022 |
2.44.0 | 2,239,704 | 3/17/2022 |
2.44.0-pre1 | 22,373 | 3/9/2022 |
2.43.0 | 31,779,608 | 2/25/2022 |
2.43.0-pre1 | 24,314 | 1/28/2022 |
2.42.0 | 4,059,535 | 1/19/2022 |
2.42.0-pre1 | 15,971 | 12/30/2021 |
2.41.0 | 2,253,471 | 12/7/2021 |
2.41.0-pre1 | 40,748 | 11/12/2021 |
2.40.0 | 10,062,038 | 10/5/2021 |
2.40.0-pre1 | 8,629 | 9/9/2021 |
2.39.0 | 2,554,865 | 8/18/2021 |
2.39.0-pre1 | 98,180 | 8/6/2021 |
2.38.0 | 3,021,062 | 6/11/2021 |
2.38.0-pre1 | 3,619 | 6/4/2021 |
2.37.0 | 8,444,040 | 4/20/2021 |
2.37.0-pre1 | 3,454 | 4/14/2021 |
2.36.0 | 1,754,054 | 3/17/2021 |
2.36.0-pre1 | 3,608 | 3/9/2021 |
2.35.0 | 1,619,706 | 2/4/2021 |
2.35.0-pre1 | 4,284 | 1/26/2021 |
2.34.0 | 4,054,429 | 12/11/2020 |
2.34.0-pre1 | 5,820 | 12/1/2020 |
2.33.1 | 2,191,121 | 10/28/2020 |
2.33.1-pre1 | 2,961 | 10/22/2020 |
2.32.0 | 12,818,464 | 10/5/2020 |
2.32.0-pre1 | 16,680 | 9/8/2020 |
2.31.0 | 1,324,258 | 8/14/2020 |
2.31.0-pre2 | 3,188 | 8/3/2020 |
2.30.0 | 642,215 | 7/16/2020 |
2.30.0-pre1 | 17,205 | 6/17/2020 |
2.29.0 | 1,233,500 | 5/27/2020 |
2.29.0-pre1 | 7,692 | 5/15/2020 |
2.28.0 | 914,966 | 4/9/2020 |
2.28.0-pre2 | 20,953 | 3/11/2020 |
2.28.0-pre1 | 10,134 | 3/3/2020 |
2.27.0 | 1,971,948 | 2/7/2020 |
2.27.0-pre1 | 27,930 | 1/24/2020 |
2.26.0 | 1,618,651 | 12/19/2019 |
2.26.0-pre1 | 3,037 | 12/10/2019 |
2.25.0 | 1,076,081 | 11/7/2019 |
2.25.0-pre1 | 2,916 | 11/1/2019 |
2.24.0 | 519,930 | 10/21/2019 |
2.24.0-pre1 | 4,181 | 10/9/2019 |
2.23.2 | 589,025 | 9/20/2019 |
0.2.23-pre2 | 14,212 | 9/6/2019 |
0.2.23-pre1 | 14,526 | 8/21/2019 |
0.1.22-pre3 | 9,565 | 7/30/2019 |
0.1.22-pre2 | 16,920 | 7/2/2019 |
0.1.22-pre1 | 5,797 | 6/17/2019 |
0.1.21-pre1 | 3,130 | 6/4/2019 |