Dogodek
Power BI DataViz Svetovno prvenstvo
14. feb., 16h - 31. mar., 16h
S 4 možnosti za vstop, bi lahko zmagal konferenčni paket in da bi bilo v ŽIVO Grand Finale v Las Vegasu
Več informacijTa brskalnik ni več podprt.
Izvedite nadgradnjo na Microsoft Edge, če želite izkoristiti vse prednosti najnovejših funkcij, varnostnih posodobitev in tehnične podpore.
Opomba
This isn't the latest version of this article. For the current release, see the .NET 9 version of this article.
Opozorilo
This version of ASP.NET Core is no longer supported. For more information, see the .NET and .NET Core Support Policy. For the current release, see the .NET 9 version of this article.
Pomembno
This information relates to a pre-release product that may be substantially modified before it's commercially released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
For the current release, see the .NET 9 version of this article.
Tooling is available for gRPC that allows developers to test services without building client apps:
This article discusses how to:
grpcurl
.grpcui
.Opomba
To learn how to unit test gRPC services, see Test gRPC services in ASP.NET Core.
Tooling must know the Protobuf contract of services before it can call them. There are two ways to do this:
.proto
files to the tool manually.It's easier to use gRPC reflection. gRPC reflection adds a new gRPC service to the app that clients can call to discover services.
gRPC ASP.NET Core has built-in support for gRPC reflection with the Grpc.AspNetCore.Server.Reflection
package. To configure reflection in an app:
Grpc.AspNetCore.Server.Reflection
package reference.Program.cs
:
AddGrpcReflection
to register services that enable reflection.MapGrpcReflectionService
to add a reflection service endpoint.builder.Services.AddGrpc();
builder.Services.AddGrpcReflection();
var app = builder.Build();
app.MapGrpcService<GreeterService>();
IWebHostEnvironment env = app.Environment;
if (env.IsDevelopment())
{
app.MapGrpcReflectionService();
}
When gRPC reflection is set up:
gRPC reflection returns a list of available APIs, which could contain sensitive information. Care should be taken to limit access to the gRPC reflection service.
gRPC reflection is usually only required in a local development environment. For local development, the reflection service should only be mapped when IsDevelopment returns true:
if (env.IsDevelopment())
{
app.MapGrpcReflectionService();
}
Access to the service can be controlled through standard ASP.NET Core authorization extension methods, such as AllowAnonymous
and RequireAuthorization
.
For example, if an app has been configured to require authorization by default, configuration the gRPC reflection endpoint with AllowAnonymous
to skip authentication and authorization.
if (env.IsDevelopment())
{
app.MapGrpcReflectionService().AllowAnonymous();
}
gRPCurl is a command-line tool created by the gRPC community. Its features include:
For information about downloading and installing grpcurl
, see the gRPCurl GitHub homepage.
The -help
argument explains grpcurl
command-line options:
$ grpcurl -help
Use the describe
verb to view the services defined by the server. Specify <port>
as the localhost port number of the gRPC server. The port number is randomly assigned when the project is created and set in Properties/launchSettings.json
:
$ grpcurl localhost:<port> describe
greet.Greeter is a service:
service Greeter {
rpc SayHello ( .greet.HelloRequest ) returns ( .greet.HelloReply );
rpc SayHellos ( .greet.HelloRequest ) returns ( stream .greet.HelloReply );
}
grpc.reflection.v1alpha.ServerReflection is a service:
service ServerReflection {
rpc ServerReflectionInfo ( stream .grpc.reflection.v1alpha.ServerReflectionRequest ) returns ( stream .grpc.reflection.v1alpha.ServerReflectionResponse );
}
The preceding example:
describe
verb on server localhost:<port>
. Where <port>
is randomly assigned when the gRPC server project is created and set in Properties/launchSettings.json
Greeter
is a service implemented by the app.ServerReflection
is the service added by the Grpc.AspNetCore.Server.Reflection
package.Combine describe
with a service, method, or message name to view its detail:
$ grpcurl localhost:<port> describe greet.HelloRequest
greet.HelloRequest is a message:
message HelloRequest {
string name = 1;
}
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.
$ grpcurl -d '{ \"name\": \"World\" }' localhost:<port> greet.Greeter/SayHello
{
"message": "Hello World"
}
In the preceding example:
-d
argument specifies a request message with JSON. This argument must come before the server address and method name.SayHello
method on the greeter.Greeter
service.<port>
is randomly assigned when the gRPC server project is created and set in Properties/launchSettings.json
The preceding example uses \
to escape the "
character. Escaping "
is required in a PowerShell console but must not be used in some consoles. For example, the previous command for a macOS console:
$ grpcurl -d '{ "name": "World" }' localhost:<port> greet.Greeter/SayHello
{
"message": "Hello World"
}
gRPCui is an interactive web UI for gRPC. gRPCui builds on top of gRPCurl. gRPCui offers a GUI for discovering and testing gRPC services, similar to HTTP tools such as Swagger UI.
For information about downloading and installing grpcui
, see the gRPCui GitHub homepage.
Run grpcui
with the server address to interact with as an argument:
$ grpcui localhost:<port>
gRPC Web UI available at http://127.0.0.1:55038/
In the preceding example, specify <port>
as the localhost port number of the gRPC server. The port number is randomly assigned when the project is created and set in Properties/launchSettings.json
The tool launches a browser window with the interactive web UI. gRPC services are automatically discovered using gRPC reflection.
Tooling is available for gRPC that allows developers to test services without building client apps:
This article discusses how to:
grpcurl
.grpcui
.Opomba
To learn how to unit test gRPC services, see Test gRPC services in ASP.NET Core.
Tooling must know the Protobuf contract of services before it can call them. There are two ways to do this:
.proto
files to the tool manually.It's easier to use gRPC reflection. gRPC reflection adds a new gRPC service to the app that clients can call to discover services.
gRPC ASP.NET Core has built-in support for gRPC reflection with the Grpc.AspNetCore.Server.Reflection
package. To configure reflection in an app:
Grpc.AspNetCore.Server.Reflection
package reference.Startup.cs
:
AddGrpcReflection
to register services that enable reflection.MapGrpcReflectionService
to add a reflection service endpoint.public void ConfigureServices(IServiceCollection services)
{
services.AddGrpc();
services.AddGrpcReflection();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapGrpcService<GreeterService>();
if (env.IsDevelopment())
{
endpoints.MapGrpcReflectionService();
}
});
}
When gRPC reflection is set up:
gRPCurl is a command-line tool created by the gRPC community. Its features include:
For information about downloading and installing grpcurl
, see the gRPCurl GitHub homepage.
The -help
argument explains grpcurl
command-line options:
$ grpcurl -help
Use the describe
verb to view the services defined by the server:
$ grpcurl localhost:5001 describe
greet.Greeter is a service:
service Greeter {
rpc SayHello ( .greet.HelloRequest ) returns ( .greet.HelloReply );
rpc SayHellos ( .greet.HelloRequest ) returns ( stream .greet.HelloReply );
}
grpc.reflection.v1alpha.ServerReflection is a service:
service ServerReflection {
rpc ServerReflectionInfo ( stream .grpc.reflection.v1alpha.ServerReflectionRequest ) returns ( stream .grpc.reflection.v1alpha.ServerReflectionResponse );
}
The preceding example:
describe
verb on server localhost:5001
.Greeter
is a service implemented by the app.ServerReflection
is the service added by the Grpc.AspNetCore.Server.Reflection
package.Combine describe
with a service, method, or message name to view its detail:
$ grpcurl localhost:5001 describe greet.HelloRequest
greet.HelloRequest is a message:
message HelloRequest {
string name = 1;
}
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.
$ grpcurl -d '{ \"name\": \"World\" }' localhost:5001 greet.Greeter/SayHello
{
"message": "Hello World"
}
In the preceding example:
-d
argument specifies a request message with JSON. This argument must come before the server address and method name.SayHello
method on the greeter.Greeter
service.The preceding example uses \
to escape the "
character. Escaping "
is required in a PowerShell console but must not be used in some consoles. For example, the previous command for a macOS console:
$ grpcurl -d '{ "name": "World" }' localhost:5001 greet.Greeter/SayHello
{
"message": "Hello World"
}
gRPCui is an interactive web UI for gRPC. gRPCui builds on top of gRPCurl. gRPCui offers a GUI for discovering and testing gRPC services, similar to HTTP tools such as Swagger UI.
For information about downloading and installing grpcui
, see the gRPCui GitHub homepage.
Run grpcui
with the server address to interact with as an argument:
$ grpcui localhost:5001
gRPC Web UI available at http://127.0.0.1:55038/
The tool launches a browser window with the interactive web UI. gRPC services are automatically discovered using gRPC reflection.
Povratne informacije o izdelku ASP.NET Core
ASP.NET Core je odprtokodni projekt. Izberite povezavo za pošiljanje povratnih informacij:
Dogodek
Power BI DataViz Svetovno prvenstvo
14. feb., 16h - 31. mar., 16h
S 4 možnosti za vstop, bi lahko zmagal konferenčni paket in da bi bilo v ŽIVO Grand Finale v Las Vegasu
Več informacijUsposabljanje
Modul
Improve the developer experience of an API with Swagger documentation - Training
Learn how to document an existing API, written in C#/ASP.NET Core, using Swashbuckle, Swagger/OpenAPI, and Swagger UI.