The most steps to create a gRPC project are the same as other asp.net core projects
Here is the step Use Visual Studio to create an example project
1. Create a gRPC service
Open Visual Studio 2022 with the ASP.NET and web development workload.
Start Visual Studio 2022 and select New Project.
In the Create a new project dialog, search for gRPC. Select ASP.NET Core gRPC Service and select Next.
In the Configure your new project dialog, enter GrpcGreeter for Project name. It's important to name the project GrpcGreeter so the namespaces match when you copy and paste code.
Select Next.
In the Additional information dialog, select .NET 9.0 (Standard Term Support) and then select Create.
Run the service
from visual studio
Press Ctrl+F5 to run without the debugger.
Select Yes if you trust the IIS Express SSL certificate.
Select Yes if you agree to trust the development certificate.
from Visual Studio:
Starts Kestrel server.
Launches a browser.
Navigates to http://localhost:port, such as http://localhost:7042.
port: A randomly assigned port number for the app.
localhost: The standard hostname for the local computer. Localhost only serves web requests from the local computer.
Examine the project files
GrpcGreeter project files:
Protos/greet.proto: defines the Greeter gRPC and is used to generate the gRPC server assets. The below data is an example proto file.
syntax = "proto3";
option csharp_namespace = "GrpcGreeterClient";
package greet;
// The greeting service definition.
service Greeter {
// Sends a greeting
rpc SayHello (HelloRequest) returns (HelloReply);
}
// The request message containing the user's name.
message HelloRequest {
string name = 1;
}
// The response message containing the greetings.
message HelloReply {
string message = 1;
}
appSettings.json: Contains configuration data such as the protocol used by Kestrel.
Program.cs, which contains:The entry point for the gRPC service.
Also, you can use other tools to test this gPRC service site, such as use Postman
When you finish your test you can use Web Deploy to publish your site to our server
2. Create the gRPC client in a .NET console app
visual studio
Open a second instance of Visual Studio and select New Project.
In the Create a new project dialog, select Console App, and select Next.
In the Project name text box, enter GrpcGreeterClient and select Next.
In the Additional information dialog, select .NET 9.0 (Standard Term Support) and then select Create.
Add required NuGet packages
From Visual Studio, select Tools > NuGet Package Manager > Package Manager Console
From the Package Manager Console window, run cd GrpcGreeterClient to change directories to the folder containing the GrpcGreeterClient.csproj files.
Run the following commands:
Install-Package Grpc.Net.Client
Install-Package Google.Protobuf
Install-Package Grpc.Tools
Add greet.proto
Create a Protos folder in the gRPC client project.
Copy the Protos\greet.proto file from the gRPC Greeter service to the Protos folder in the gRPC client project.
Update the namespace inside the greet.proto file to the project's namespace:
option csharp_namespace = "GrpcGreeterClient";
Edit the GrpcGreeterClient.csproj project file:
Visual studio:
Add an item group with a <Protobuf> element that refers to the greet.proto file:
<ItemGroup>
<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
</ItemGroup>
Create the Greeter client
Build the client project to create the types in the GrpcGreeterClient namespace.
Update the gRPC client Program.cs file with the following code
using Grpc.Net.Client;
using GrpcGreeterClient;
// The port number must match the port of the gRPC server.
using var channel = GrpcChannel.ForAddress("https://localhost:7042");
var client = new Greeter.GreeterClient(channel);
var reply = await client.SayHelloAsync(
new HelloRequest { Name = "GreeterClient" });
Console.WriteLine("Greeting: " + reply.Message);
Console.WriteLine("Press any key to exit...");
Console.ReadKey();
In the preceding highlighted code, replace the localhost port number 7042 with the HTTPS port number specified in Properties/launchSettings.json within the GrpcGreeter service project.
Program.cs contains the entry point and logic for the gRPC client.
The Greeter client is created by:
Instantiating a GrpcChannel containing the information for creating the connection to the gRPC service.
Using the GrpcChannel to construct the Greeter client:
// The port number must match the port of the gRPC server.
using var channel = GrpcChannel.ForAddress("https://localhost:7042");
var client = new Greeter.GreeterClient(channel);
var reply = await client.SayHelloAsync(
new HelloRequest { Name = "GreeterClient" });
Console.WriteLine("Greeting: " + reply.Message);
Console.WriteLine("Press any key to exit...");
Console.ReadKey();
The Greeter client calls the asynchronous SayHello method. The result of the SayHello call is displayed:
// The port number must match the port of the gRPC server.
using var channel = GrpcChannel.ForAddress("https://localhost:7042");
var client = new Greeter.GreeterClient(channel);
var reply = await client.SayHelloAsync(
new HelloRequest { Name = "GreeterClient" });
Console.WriteLine("Greeting: " + reply.Message);
Console.WriteLine("Press any key to exit...");
Console.ReadKey();
Test the gRPC client with the gRPC Greeter service
Update the appsettings.Development.json file by adding the following highlighted lines:
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning",
"Microsoft.AspNetCore.Hosting": "Information",
"Microsoft.AspNetCore.Routing.EndpointMiddleware": "Information"
}
}
}
Then you can run both sites and check the console output info.
For more info please refer to the below Microsoft learn page.