You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
description: Learn how to call gRPC services with the .NET gRPC client.
5
5
monikerRange: '>= aspnetcore-3.0'
6
6
ms.author: jamesnk
7
-
ms.date: 08/21/2019
7
+
ms.date: 04/21/2020
8
8
uid: grpc/client
9
9
---
10
10
# Call gRPC services with the .NET client
@@ -44,7 +44,7 @@ Channel and client performance and usage:
44
44
* A channel and clients created from the channel can safely be used by multiple threads.
45
45
* Clients created from the channel can make multiple simultaneous calls.
46
46
47
-
`GrpcChannel.ForAddress` isn't the only option for creating a gRPC client. If you're calling gRPC services from an ASP.NET Core app, consider [gRPC client factory integration](xref:grpc/clientfactory). gRPC integration with `HttpClientFactory` offers a centralized alternative to creating gRPC clients.
47
+
`GrpcChannel.ForAddress` isn't the only option for creating a gRPC client. If calling gRPC services from an ASP.NET Core app, consider [gRPC client factory integration](xref:grpc/clientfactory). gRPC integration with `HttpClientFactory` offers a centralized alternative to creating gRPC clients.
48
48
49
49
> [!NOTE]
50
50
> Additional configuration is required to [call insecure gRPC services with the .NET client](xref:grpc/troubleshoot#call-insecure-grpc-services-with-net-core-client).
@@ -56,7 +56,7 @@ Channel and client performance and usage:
56
56
57
57
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.
58
58
59
-
gRPC has different types of methods. How you use the client to make a gRPC call depends on the type of method you are calling. The gRPC method types are:
59
+
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:
60
60
61
61
* Unary
62
62
* Server streaming
@@ -96,78 +96,136 @@ using (var call = client.SayHellos(new HelloRequest { Name = "World" }))
96
96
}
97
97
```
98
98
99
-
If you are 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:
99
+
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:
100
100
101
101
```csharp
102
102
varclient=newGreet.GreeterClient(channel);
103
-
using (varcall=client.SayHellos(newHelloRequest { Name="World" }))
// "Greeting: Hello World" is written multiple times
109
-
}
107
+
Console.WriteLine("Greeting: "+response.Message);
108
+
// "Greeting: Hello World" is written multiple times
110
109
}
111
110
```
112
111
113
112
### Client streaming call
114
113
115
-
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.
114
+
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.
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.
136
134
137
135
```csharp
138
-
using (varcall=client.Echo())
136
+
varclient=newEcho.EchoClient(channel);
137
+
usingvarcall=client.Echo();
138
+
139
+
Console.WriteLine("Starting background task to receive messages");
140
+
varreadTask=Task.Run(async () =>
139
141
{
140
-
Console.WriteLine("Starting background task to receive 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.
170
168
169
+
## Access gRPC trailers
170
+
171
+
gRPC calls may return gRPC trailers. gRPC trailers are used to provide name/value metadata about a call. Trailers provide similar functionality to HTTP headers, but are received at the end of the call.
172
+
173
+
gRPC trailers are accessible using `GetTrailers()`, which returns a collection of metadata. Trailers are returned after the response is complete, therefore, you must await all response messages before accessing the trailers.
174
+
175
+
Unary and client streaming calls must await `ResponseAsync` before calling `GetTrailers()`:
gRPC trailers are also accessible from `RpcException`. A service may return trailers together with a non-OK gRPC status. In this situation the trailers are retrieved from the exception thrown by the gRPC client:
0 commit comments