Skip to content

Commit 698769b

Browse files
authored
Improve gRPC test tools doc (#19902)
1 parent 905252c commit 698769b

4 files changed

Lines changed: 43 additions & 26 deletions

File tree

aspnetcore/grpc/test-tools.md

Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,26 @@
11
---
2-
title: Test services with gRPC tools
2+
title: Test gRPC services with gRPCurl in ASP.NET Core
33
author: jamesnk
44
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'
5+
monikerRange: '>= aspnetcore-3.1'
66
ms.author: jamesnk
77
ms.date: 08/09/2020
88
no-loc: ["ASP.NET Core Identity", cookie, Cookie, Blazor, "Blazor Server", "Blazor WebAssembly", "Identity", "Let's Encrypt", Razor, SignalR]
99
uid: grpc/test-tools
1010
---
11-
# Test services with gRPC tools
11+
# Test gRPC services with gRPCurl in ASP.NET Core
1212

1313
By [James Newton-King](https://twitter.com/jamesnk)
1414

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.
15+
Tooling is available for gRPC that allows developers to test services without building client apps:
16+
17+
* [gRPCurl](https://github.com/fullstorydev/grpcurl) is a command-line tool that provides interaction with gRPC services.
18+
* [gRPCui](https://github.com/fullstorydev/grpcui) builds on top of gRPCurl and adds an interactive web UI for gRPC, similar to tools such as Postman and Swagger UI.
1619

1720
This article discusses how to:
1821

1922
* Download and install gRPCurl and gRPCui.
20-
* Setup gRPC reflection with a gRPC ASP.NET Core app.
23+
* Set up gRPC reflection with a gRPC ASP.NET Core app.
2124
* Discover and test gRPC services with `grpcurl`.
2225
* Interact with gRPC services via a browser using `grpcui`.
2326

@@ -32,21 +35,31 @@ gRPCurl is a command-line tool created by the gRPC community. Its features inclu
3235

3336
For information about downloading and installing `grpcurl`, see the [gRPCurl GitHub homepage](https://github.com/fullstorydev/grpcurl#installation).
3437

35-
## Setup gRPC reflection
38+
![gRPCurl command line](~/grpc/test-tools/static/grpcurl.png)
39+
40+
## Set up gRPC reflection
41+
42+
`grpcurl` must know the Protobuf contract of services before it can call them. There are two ways to do this:
3643

37-
`grpcurl` needs to know the Protobuf contract of services before it can call them. There are two ways to do this:
44+
* Set up [gRPC reflection](https://github.com/grpc/grpc/blob/master/doc/server-reflection.md) on the server. gRPCurl automatically discovers service contracts.
45+
* Specify `.proto` files in command-line arguments to gRPCurl.
3846

39-
* Use gRPC reflection to discover service contracts.
40-
* Specify *.proto* files in command-line arguments.
47+
It's easier to use gRPCurl with gRPC reflection. gRPC reflection adds a new gRPC service to the app that clients can call to discover services.
4148

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:
49+
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:
4350

44-
* Add `Grpc.AspNetCore.Server.Reflection` package reference.
45-
* Register reflection in *Startup.cs*:
51+
* Add a `Grpc.AspNetCore.Server.Reflection` package reference.
52+
* Register reflection in `Startup.cs`:
4653
* `AddGrpcReflection` to register services that enable reflection.
47-
* `MapGrpcReflectionService` to add reflection service endpoint.
54+
* `MapGrpcReflectionService` to add a reflection service endpoint.
4855

49-
[!code-csharp[](~/grpc/test-tools/Startup.cs?name=snippet_1&highlight=4,14)]
56+
[!code-csharp[](~/grpc/test-tools/Startup.cs?name=snippet_1&highlight=4,15-18)]
57+
58+
When gRPC reflection is set up:
59+
60+
* A gRPC reflection service is added to the server app.
61+
* Client apps that support gRPC reflection can call the reflection service to discover services hosted by the server.
62+
* gRPC services are still called from the client. Reflection only enables service discovery and doesn't bypass server-side security. Endpoints protected by [authentication and authorization](xref:grpc/authn-and-authz) require the caller to pass credentials for the endpoint to be called successfully.
5063

5164
## Use `grpcurl`
5265

@@ -75,12 +88,12 @@ service ServerReflection {
7588

7689
The preceding example:
7790

78-
* Runs `describe` verb on server `localhost:5001`.
91+
* Runs the `describe` verb on server `localhost:5001`.
7992
* Prints services and methods returned by gRPC reflection.
8093
* `Greeter` is a service implemented by the app.
8194
* `ServerReflection` is the service added by the `Grpc.AspNetCore.Server.Reflection` package.
8295

83-
Combine `describe` with a service, method or message name to view its detail:
96+
Combine `describe` with a service, method, or message name to view its detail:
8497

8598
```powershell
8699
> grpcurl.exe localhost:5001 describe greet.HelloRequest
@@ -92,7 +105,7 @@ message HelloRequest {
92105

93106
### Call gRPC services
94107

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.
108+
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.
96109

97110
```powershell
98111
> grpcurl.exe -d '{ \"name\": \"World\" }' localhost:5001 greet.Greeter/SayHello
@@ -101,33 +114,33 @@ Call a gRPC service by specifying a service and method name, along with a JSON a
101114
}
102115
```
103116

104-
The preceding example:
117+
In the preceding example:
105118

106-
* `-d` argument specifies a request message with JSON. This argument must come before the server address and method name.
119+
* The `-d` argument specifies a request message with JSON. This argument must come before the server address and method name.
107120
* Calls the `SayHello` method on the `greeter.Greeter` service.
108121
* Prints the response message as JSON.
109122

110123
## About gRPCui
111124

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.
125+
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 such as Postman or Swagger UI.
113126

114127
For information about downloading and installing `grpcui`, see the [gRPCui GitHub homepage](https://github.com/fullstorydev/grpcui#installation).
115128

116129
## Using `grpcui`
117130

118-
Run `grpcui` with the server address to interact with as an argument.
131+
Run `grpcui` with the server address to interact with as an argument:
119132

120133
```powershell
121134
> grpcui.exe localhost:5001
122135
gRPC Web UI available at http://127.0.0.1:55038/
123136
```
124137

125-
The tool will launch a browser window with the interactive web UI. gRPC services are automatically discovered using gRPC reflection.
138+
The tool launches a browser window with the interactive web UI. gRPC services are automatically discovered using gRPC reflection.
126139

127140
![gRPCui web UI](~/grpc/test-tools/static/grpcui.png)
128141

129142
## Additional resources
130143

131144
* [gRPCurl GitHub homepage](https://github.com/fullstorydev/grpcurl)
132145
* [gRPCui GitHub homepage](https://github.com/fullstorydev/grpcui)
133-
* [Grpc.AspNetCore.Server.Reflection](https://www.nuget.org/packages/Grpc.AspNetCore.Server.Reflection)
146+
* [`Grpc.AspNetCore.Server.Reflection`](https://www.nuget.org/packages/Grpc.AspNetCore.Server.Reflection)

aspnetcore/grpc/test-tools/Startup.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,18 @@ public void ConfigureServices(IServiceCollection services)
55
services.AddGrpcReflection();
66
}
77

8-
public void Configure(IApplicationBuilder app)
8+
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
99
{
1010
app.UseRouting();
1111

1212
app.UseEndpoints(endpoints =>
1313
{
1414
endpoints.MapGrpcService<GreeterService>();
15-
endpoints.MapGrpcReflectionService();
15+
16+
if (env.IsDevelopment())
17+
{
18+
endpoints.MapGrpcReflectionService();
19+
}
1620
});
1721
}
1822
#endregion
14.2 KB
Loading

aspnetcore/toc.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -746,7 +746,7 @@
746746
uid: grpc/httpapi
747747
- name: Manage Protobuf references with dotnet-grpc
748748
uid: grpc/dotnet-grpc
749-
- name: Test services with gRPC tools
749+
- name: Test gRPC services with gRPCurl
750750
uid: grpc/test-tools
751751
- name: Migrate gRPC services from C-core
752752
uid: grpc/migration

0 commit comments

Comments
 (0)