
In this article, we will see how to create a gRPC service using the ASP.NET Core template. We will also create a client application using .NET Core Console to call the gRPC service server method directly to send and receive the messages from the client application.
gRPC
gRPC is fast, efficient, and lightweight Remote Procedure Call which is widely used in microservices. gRPC was initially developed by Google and now it’s opensource.
In ASP.NET Core 3, we can see a new template has been included as gRPC Service. Yes, using this template, we can create our gRPC Service and create client application as .NET Core Console, WinForms, or ASP.ENT Core for data serialization for sending and receiving data from client to server. gRPC uses HTTP/2 for transport. gRPC uses protocol buffer aka Protobuf as the data serialization format which is used to send and receive from the client to the server.
Protobuf File
gRPC uses protobuf as the default data serialization to send and receive data from the client and server. In other words, the protobuf is used as the Interface Design Language. In order to work with gRPC service, we need to understand the protobuf file as well. Protobuf files have 2 parts - one is for the defining the gRPC Service and the other part is to define the message sent between the client to the server. Here, we will see each section of the protobuf file
In the first line of the protobuf file, we need to declare the syntax to mention the version we are using of protocol buffer language. Here, in our example, we are using the proto3
syntax = "proto3";
Next, we give our C# Application namespace name that is our solution name
option csharp_namespace = "GrpcGreeter";
Next is the service part and here, we have created the service named as the Greeter. When we create the gRPC service application, the proto file has been added by default and this below service has been created automatically.
In this Greeter Service, we have created 2 calls as SayHello and Servermessage. In this call, each call sends HelloRequest as a message and receives the HelloReply message.
- // The greeting service definition.
- service Greeter {
- // Sends a greeting
- rpc SayHello (HelloRequest) returns (HelloReply) {}
- rpc Servermessage (HelloRequest) returns (HelloReply) {}
- }
Prerequisites
Make sure you have installed all the prerequisites on your computer. If not, then download and install them all, one by one.
Make sure you have installed all the prerequisites on your computer. If not, then download and install them all, one by one.
Prerequisites
Code Part
Step 1 - Create a gRPC Service application
After installing all the prerequisites listed above click Start >> Programs >> Visual Studio 2019 >> Visual Studio 2019 on your desktop. >> Click New Project.

Click on ASP.NET Core Web Application and click "Next".

Enter your project name and click the "Create" button.

Now, we can see that ASP.NET Core 3.0 has been listed. We Select the gRPC Service and click create to create our gRPC service application.

gRPC project Structure

Once we created the gRPC Service project we can see as by default the project will contain the Protos folder with protobuf file as an extension with “.proto” file. Also, we can see the Services folder with our Service class file. In this article beginning, we have seen in detail about what protobuf file is and what the protobuf file contains. In our gRPC Service project, we can see the proto file has been by default created with sayHello method call to send and receive the request and replay between client and the server.

We add one more method call
rpc Servermessage (HelloRequest) returns (HelloReply) {}
Here, we are adding one more method call for our example and let’s see how to use this in our service and client application as well. Here is the complete code of greet.proto file,
- syntax = "proto3";
- option csharp_namespace = "shanugRPC";
- package Greet;
- // The greeting service definition.
- service Greeter {
- // Sends a greeting
- rpc SayHello (HelloRequest) returns (HelloReply) {}
- rpc Servermessage (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;
- }
The service folder has a service class as default with GreeterService. This class has SayHello method which was created by default. Like this we can add our own method to send response.

Now we also need to add one more Override method as ServerMessage and from this service we can send method message to return to client applicaiotn. Like this you can create any number of methods and call it from the client application for message requests and replies from the server.
Here is the complete code of our Service.
- public class GreeterService : Greeter.GreeterBase
- {
- public override Task<HelloReply> SayHello(HelloRequest request, ServerCallContext context)
- {
- return Task.FromResult(new HelloReply
- {
- Message = "Hello " + request.Name
- });
- }
- public override Task<HelloReply> Servermessage(HelloRequest request, ServerCallContext context)
- {
- return Task.FromResult(new HelloReply
- {
- Message = "Your Friend Name is : " + request.Name
- });
- }
- }
appsetting.json file will contain the protocols used for the service. Here we can see Http2 protocols have been used for the gRPC service.

Build and Run the application
When we build and run the application we can see the service is running successfully and also we can see our gRPC service is listening on http://localhost:50051.

Note that as gRPC is template is configured to use the TSL and the clients need to use the HTTPS to call the server. But here, we are using the HTTP. In order to use the HTTP in our client application, we need to use this below code to connect between server and client
AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
We will see this in detail while creating the client application.
Step 2 - Creating Client Console .Net Core application
After installing all the prerequisites listed above, click Start >> Programs >> Visual Studio 2019 >> Visual Studio 2019 on your desktop. >> Click New Project.

Click on Console App(.NET Core) and click "Next".

Enter your project name and click the "Create" button.
Now our console application has been created.
Add required Packages
In order to send and receive messages between client and the gRPC Service we need to add Grpc.Net.Client , Google.Protobuf and Grpc.Tools packages to our project.
Grpc.Net.Client packages is used for the .NET Core client, Google.Protobuf packages contains the protobuf message API’s to be used for the C# language. Grpc.Tool contains the tooling support for the protobuf files.












Keertthi mettuPosted Mar 18, 2021, 4:38 PM
Hey, I'm getting errors in the following line of code even after using the necessary namespaces.
Ano MepaniPosted Oct 27, 2019, 6:11 AM
I see for just hello world app we need to do lots of stuff and have other knowledge as well. So, What is purpose of Grpc? Where it will be used in real world use case?
Chittaranjan SwainPosted Oct 3, 2019, 9:44 AM
Nice article....