Enabling gRPC server reflection

Enabling grpc serv er reflection by Irina Scurtu

Enabling gRPC server reflection will allow you to easily call your gRPC services from Postman without actually loading the protofile .

PostMan will help you test your gRPC services, by giving you the same look and feel as a you would have with a traditional HTTP API.

gRPC uses binary serialization, which means that is not human readable as JSON is. Is still possible to use grpcurl or similar tools, but you will not be able to decipher binary. Having the right tools will help you understand gRPC faster, because it has a slightly different vocabulary.

When you open PostMan and click the New button , you can choose to issue gRPC requests. Thankfully, this is not in beta anymore.

A possum parent and two possum kids hanging from the iconic red balloon

PostMan New menu options

To issue a gRPC request, you will need to specify the URL of the server (as you would do with a regular RESTful API), and then give PostMan access to the proto file that is implemented on the server. In this case, you need to help the tool a bit, so you can gain access to available functionality.

To do that you can simply import the proto file or let PostMan discover what is available.

In this case we will use the second option, because we would want our service operations to be somehow discoverable.

A possum parent and two possum kids hanging from the iconic red balloon

Different ways of 'accessing' the proto file contract

Now, if we try to click the highlighted option, we will get a big red error. This is as if our server throws an Unimplemented exception from a method.

A possum parent and two possum kids hanging from the iconic red balloon

In reality, if we look at the logs, it actually does throw that kind of exception. :)

info: Grpc.AspNetCore.Server.Internal.ServerCallHandlerFactory[1]
      Service 'grpc.reflection.v1alpha.ServerReflection' is unimplemented.

Enabling gRPC server reflection requires a few code tweaks because we actually need to an implementation that allows reflection over our grPC services.

First, install Grpc.AspNetCore.Server.Reflection NuGet package. This will contain the ServerReflection implementation that will we need, that allows external services and tools to discover existing methods

Next, we need to add the middleware and then map the incoming gRPC requests to the Reflection Service.

builder.Services.AddGrpcReflection();

IWebHostEnvironment env = app.Environment;
if (env.IsDevelopment())
{
   app.MapGrpcReflectionService();
}

Once you did this, PostMan will successfully discover the available gRPC methods, and you will be able to call invoke these from the UI

A possum parent and two possum kids hanging from the iconic red balloon

A possum parent and two possum kids hanging from the iconic red balloon

gRPC methods

That's it! A package and a few lines of code and you will enable gRPC server reflection.