Skip to content

Commit f746b7d

Browse files
authored
Add gRPCurl article (#19793)
1 parent 423d138 commit f746b7d

4 files changed

Lines changed: 153 additions & 0 deletions

File tree

aspnetcore/grpc/test-tools.md

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
---
2+
title: Test services with gRPC tools
3+
author: jamesnk
4+
description: Learn how to test services with gRPC tools. gRPCurl a command-line tool for interacting with gRPC services. gRPCui is an interactive web UI.
5+
monikerRange: '>= aspnetcore-3.0'
6+
ms.author: jamesnk
7+
ms.date: 08/09/2020
8+
no-loc: ["ASP.NET Core Identity", cookie, Cookie, Blazor, "Blazor Server", "Blazor WebAssembly", "Identity", "Let's Encrypt", Razor, SignalR]
9+
uid: grpc/test-tools
10+
---
11+
# Test services with gRPC tools
12+
13+
By [James Newton-King](https://twitter.com/jamesnk)
14+
15+
Tooling is available for gRPC that allows developers to test services without building client apps. [gRPCurl](https://github.com/fullstorydev/grpcurl) is a command-line tool that provides interaction with gRPC services. [gRPCui](https://github.com/fullstorydev/grpcui) adds an interactive web UI for gRPC.
16+
17+
This article discusses how to:
18+
19+
* Download and install gRPCurl and gRPCui.
20+
* Setup gRPC reflection with a gRPC ASP.NET Core app.
21+
* Discover and test gRPC services with `grpcurl`.
22+
* Interact with gRPC services via a browser using `grpcui`.
23+
24+
## About gRPCurl
25+
26+
gRPCurl is a command-line tool created by the gRPC community. Its features include:
27+
28+
* Calling gRPC services, including streaming services.
29+
* Service discovery using [gRPC reflection](https://github.com/grpc/grpc/blob/master/doc/server-reflection.md).
30+
* Listing and describing gRPC services.
31+
* Works with secure (TLS) and insecure (plain-text) servers.
32+
33+
For information about downloading and installing `grpcurl`, see the [gRPCurl GitHub homepage](https://github.com/fullstorydev/grpcurl#installation).
34+
35+
## Setup gRPC reflection
36+
37+
`grpcurl` needs to know the Protobuf contract of services before it can call them. There are two ways to do this:
38+
39+
* Use gRPC reflection to discover service contracts.
40+
* Specify *.proto* files in command-line arguments.
41+
42+
It's easier to use gRPCurl with gRPC reflection and service discovery. gRPC ASP.NET Core has built-in support for gRPC reflection with the [Grpc.AspNetCore.Server.Reflection](https://www.nuget.org/packages/Grpc.AspNetCore.Server.Reflection) package. To configure reflection in an app:
43+
44+
* Add `Grpc.AspNetCore.Server.Reflection` package reference.
45+
* Register reflection in *Startup.cs*:
46+
* `AddGrpcReflection` to register services that enable reflection.
47+
* `MapGrpcReflectionService` to add reflection service endpoint.
48+
49+
[!code-csharp[](~/grpc/test-tools/Startup.cs?name=snippet_1&highlight=4,14)]
50+
51+
## Use `grpcurl`
52+
53+
The `-help` argument explains `grpcurl` command-line options:
54+
55+
```powershell
56+
> grpcurl.exe -help
57+
```
58+
59+
### Discover services
60+
61+
Use the `describe` verb to view the services defined by the server:
62+
63+
```powershell
64+
> grpcurl.exe localhost:5001 describe
65+
greet.Greeter is a service:
66+
service Greeter {
67+
rpc SayHello ( .greet.HelloRequest ) returns ( .greet.HelloReply );
68+
rpc SayHellos ( .greet.HelloRequest ) returns ( stream .greet.HelloReply );
69+
}
70+
grpc.reflection.v1alpha.ServerReflection is a service:
71+
service ServerReflection {
72+
rpc ServerReflectionInfo ( stream .grpc.reflection.v1alpha.ServerReflectionRequest ) returns ( stream .grpc.reflection.v1alpha.ServerReflectionResponse );
73+
}
74+
```
75+
76+
The preceding example:
77+
78+
* Runs `describe` verb on server `localhost:5001`.
79+
* Prints services and methods returned by gRPC reflection.
80+
* `Greeter` is a service implemented by the app.
81+
* `ServerReflection` is the service added by the `Grpc.AspNetCore.Server.Reflection` package.
82+
83+
Combine `describe` with a service, method or message name to view its detail:
84+
85+
```powershell
86+
> grpcurl.exe localhost:5001 describe greet.HelloRequest
87+
greet.HelloRequest is a message:
88+
message HelloRequest {
89+
string name = 1;
90+
}
91+
```
92+
93+
### Call gRPC services
94+
95+
Call a gRPC service by specifying a service and method name, along with a JSON argument that represents the request message. The JSON is converted into Protobuf and sent to the service.
96+
97+
```powershell
98+
> grpcurl.exe -d '{ \"name\": \"World\" }' localhost:5001 greet.Greeter/SayHello
99+
{
100+
"message": "Hello World"
101+
}
102+
```
103+
104+
The preceding example:
105+
106+
* `-d` argument specifies a request message with JSON. This argument must come before the server address and method name.
107+
* Calls the `SayHello` method on the `greeter.Greeter` service.
108+
* Prints the response message as JSON.
109+
110+
## About gRPCui
111+
112+
gRPCui is an interactive web UI for gRPC. It builds on top of gRPCurl, and offers a GUI for discovering and testing gRPC services, similar to HTTP tools like Postman.
113+
114+
For information about downloading and installing `grpcui`, see the [gRPCui GitHub homepage](https://github.com/fullstorydev/grpcui#installation).
115+
116+
## Using `grpcui`
117+
118+
Run `grpcui` with the server address to interact with as an argument.
119+
120+
```powershell
121+
> grpcui.exe localhost:5001
122+
gRPC Web UI available at http://127.0.0.1:55038/
123+
```
124+
125+
The tool will launch a browser window with the interactive web UI. gRPC services are automatically discovered using gRPC reflection.
126+
127+
![gRPCui web UI](~/grpc/test-tools/static/grpcui.png)
128+
129+
## Additional resources
130+
131+
* [gRPCurl GitHub homepage](https://github.com/fullstorydev/grpcurl)
132+
* [gRPCui GitHub homepage](https://github.com/fullstorydev/grpcui)
133+
* [Grpc.AspNetCore.Server.Reflection](https://www.nuget.org/packages/Grpc.AspNetCore.Server.Reflection)
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#region snippet_1
2+
public void ConfigureServices(IServiceCollection services)
3+
{
4+
services.AddGrpc();
5+
services.AddGrpcReflection();
6+
}
7+
8+
public void Configure(IApplicationBuilder app)
9+
{
10+
app.UseRouting();
11+
12+
app.UseEndpoints(endpoints =>
13+
{
14+
endpoints.MapGrpcService<GreeterService>();
15+
endpoints.MapGrpcReflectionService();
16+
});
17+
}
18+
#endregion
175 KB
Loading

aspnetcore/toc.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -738,6 +738,8 @@
738738
uid: grpc/versioning
739739
- name: Manage Protobuf references with dotnet-grpc
740740
uid: grpc/dotnet-grpc
741+
- name: Test services with gRPC tools
742+
uid: grpc/test-tools
741743
- name: Migrate gRPC services from C-core
742744
uid: grpc/migration
743745
- name: Why migrate WCF to ASP.NET Core gRPC

0 commit comments

Comments
 (0)