Microsoft supports several development environments of the gRPC communication implementation, and provides many documents. I want to introduce Microsoft official document and describe mechanism of HTTP/3 protocol, relation between the gRPC and HTTP/3 and so on. Design of gRPC communication was described at previous article ‘Design of solution with gRPC communication‘, please refer to it.
This article describes two-thirds steps of implementation of the gRPC service, at first, create the gRPC service with localhost, then query simple request use the gRPCurl tool. The last step is in the next article [Deploy the gRPC service to the Azure App Service] that describes how to deploy the service to Azure Web App Service.
And mechanism of HTTP/3 protocol and relation between the gRPC and HTTP/3 is described in the article [HTTP3 and gRPC service in .NET framework].

>>The gRPC service sample project, before deployment to Azure Web Service.

• Implementation of the gRPC service.

This article refers to Microsoft official document ‘Tutorial: Create a gRPC client and server in ASP.NET Core‘, so if lost a step of implementation of the gRPC service, please refer to it too. This article describe steps of the gRPC service with ASP.NET Core, so the service implementation with one of architectures below.

Kestrel
Cross-platform web server
TestServer
In-memory web server for unit test mainly
Internet Information Service
Standard web server of Windows, the constraints are .NET 5 and Windows 11 Build 22000 or Windows Server 2022 Build 20348 or later.
HTTP.sys
The Windows web service for ASP.NET Core includes several extending features that differ from Kestrel; constraints are same as IIS above.

In the step 2, call it from the gRPCurl tool or the Postman, then after confirm runnable of the service, as last step, deploy to any of Azure Services below.

Azure Kubernetes Service (AKS)
Azure Container Apps
Azure App Service (gRPC-Web)

The gRPC service have to includes the gRPC reflection service for providing description of the gRPC service (works like as swagger). The gRPC client communicates with the gRPC reflection service for solving objects of the gRPC service before calls method of the service.

If it needs only communication test, a gRPC service that does not implement the gRPC reflection service can also use the gRPC connection with loading the proto source file to the gRPCurl. Also, the Postman can use as gRPC client as same as the gRPCurl (useable the proto source file).

It is easy that the gRPC reflection service implementation, Open Visual Studio Code, use the “dotnet new” command at target folder and use the “dotnet add package” command for implementation of service on the generic host of ASP.NET Core web app.

1. Press the Ctrl key + @ key for open the terminal pain of Visual Studio Code. Then put the “dotnet new list” command to show all templates of dotnet sdk on development environment. The figure above shows .NET SDK 7.0 environment. There is the ASP.NET Core gRPC Service in the list.

2. So, move to the target folder in the terminal pain, and put the “dotnet new grpc” command to generate project files at target folder.

3. Install reflection server package use nuget dotnet command “dotnet add package Grpc.AspNetCore.Server.Reflection –version 2.51.0”.

4. Next, open the Program.cs file from the EXPLORER pain, and put the code “builder.Services.AddGrpcReflection();” after the “var builder = WebApplication.CreateBuilder(args);” statement.

5. After the “var app = builder.Build();” statement, add the “IWebHostEnvironment env = app.Environment;” code and the “if” section like code below.

using gRPC2.Services;

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddGrpcReflection();
// Additional configuration is required to successfully run gRPC on macOS.
// For instructions on how to configure Kestrel and gRPC clients on macOS, visit https://go.microsoft.com/fwlink/?linkid=2099682

// Add services to the container.
builder.Services.AddGrpc();

var app = builder.Build();
IWebHostEnvironment env = app.Environment;
if (env.IsDevelopment())
{
    app.MapGrpcReflectionService();
}
// Configure the HTTP request pipeline.
app.MapGrpcService();
app.MapGet("/", () => "Communication with gRPC endpoints must be made through a gRPC client. To learn how to create a client, visit: https://go.microsoft.com/fwlink/?linkid=2086909");

app.Run();

6. Put the “dotnet run” command to run gRPC service at local environment, then the “Now listening on: http://localhost:5008” statement displayed on the terminal pain. The HTTPS connection is able to use with the “dotnet dev-certs https” command in the terminal pane.

At first, the Postman is easy to confirm the gRPC service connection than using the gRPCurl because the gRPCurl is command line application. So describes the Postman usage ahead, then the gRPCurl usage follows.

• Call the gRPC service use the gRPCurl and the Postman.

The “New…” menu item of the File menu item that displayed when press the hamburger icon of Postman has the gRPC connection feature, refer below figure.

From the terminal pain of Visual Studio Code, copy the part of the URL (localhost:5008) excluded the protocol part that is listening, and paste at the server address textbox of the gRPC Request tub. Then press the [Try again] link of the last item [Use server reflection] of the method select list that is shown when the method textbox selects. After pressing the [Try again] link, the list is changed to be able to select the method (SayHello).

This “SayHello” method defined at the GreeterService class of the GreeterService.cs file, thus the reflection service create the proto file at the greet.proto file of the Protos folder.

The gRPCurl is command line application for confirming the gRPC service communication. After install Go language, install the gRPCurl (download the “grpcurl_1.8.7_windows_x86_64.zip” file and unzip it) and run the Windows Command Prompt at the gRPCurl install folder.

This figure is result of that acquire information of the gRPC service then call remote procedure.
The describe command responds “greet.Greeter is a service” (package name and service name), and “rpc SayHello …” (method name). The Method is able to call with format “grpcurl -plaintext -d %value(s)% %server address% %package name%.%service name%/%methodname%”.

Definition of argument(s) of the method of the service uses the “-d” command. Thus “grpcurl -plaintext -d %value(s)% localhost:5008 greet.Greeter/SayHello” warks well. Only in Windows development environment, argument(s) wrap with [” (double quate)] and JSON format: wrap key/value with [{} (Brace)] and wrap the key and the value each with [” (double quate)], [” (double quate)] in argument(s) needs escape.

"grpcurl -plaintext -d "{\"name\": \"takao\"}" localhost:5008 greet.Greeter/SayHello"

If there is Go development environment, it is able to connect to gRPC service with browser. If not, go to the page “Download and install” of the Go website and install it. After install it, install gRPC UI use the command “go install github.com/fullstorydev/grpcui/cmd/grpcui@latest” (move to the Go installed path if it needs). It is installed at the GOPATH of the system environment variables that is shown from the System Properties dialog’s [Environment Variables…] button select. The dialog is shown when select the [Advanced system settings] link of the [Related links] of the [System About] window that is displayed by selection the context menu item [System] of Windows icon (right click).
Move to the gRPC UI installed path if it needs, type “grpcui -plaintext localhost:5008”, then the browser is opened and can test it.

Categories:

Tags:

No responses yet

Leave a Reply

Your email address will not be published. Required fields are marked *