Getting Started With gRPC Client And Server Using ASP.NET Core

gRPC Client And Server Using ASP.NET Core
 
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. 
  1. // The greeting service definition.  
  2. service Greeter {  
  3.   // Sends a greeting  
  4.   rpc SayHello (HelloRequest) returns (HelloReply) {}  
  5.   
  6.   rpc Servermessage (HelloRequest) returns (HelloReply) {}  
  7. }  
 
Prerequisites

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.
 
gRPC Client And Server Using ASP.NET Core
 
Click on ASP.NET Core Web Application and click "Next".
 
gRPC Client And Server Using ASP.NET Core
 
Enter your project name and click the "Create" button.
 
gRPC Client And Server Using ASP.NET Core
 
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 Client And Server Using ASP.NET Core
 

gRPC project Structure

 
gRPC Client And Server Using ASP.NET Core
 
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.
 
gRPC Client And Server Using ASP.NET Core
 

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, 
  1. syntax = "proto3";  
  2. option csharp_namespace = "shanugRPC";  
  3. package Greet;  
  4.   
  5. // The greeting service definition.  
  6. service Greeter {  
  7.   // Sends a greeting  
  8.   rpc SayHello (HelloRequest) returns (HelloReply) {}  
  9.   
  10.    rpc Servermessage (HelloRequest) returns (HelloReply) {}  
  11. }  
  12.   
  13. // The request message containing the user's name.  
  14. message HelloRequest {  
  15.   string name = 1;  
  16. }  
  17.   
  18. // The response message containing the greetings.  
  19. message HelloReply {  
  20.   string message = 1;  
  21. }  
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.
 
gRPC Client And Server Using ASP.NET Core

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. 
  1. public class GreeterService : Greeter.GreeterBase  
  2.   {  
  3.       public override Task<HelloReply> SayHello(HelloRequest request, ServerCallContext context)  
  4.       {  
  5.           return Task.FromResult(new HelloReply  
  6.           {  
  7.               Message = "Hello " + request.Name  
  8.           });  
  9.       }  
  10.   
  11.   
  12.       public override Task<HelloReply> Servermessage(HelloRequest request, ServerCallContext context)  
  13.       {  
  14.           return Task.FromResult(new HelloReply  
  15.           {  
  16.               Message = "Your Friend Name is :   " + request.Name  
  17.           });  
  18.       }  
  19.   }  
appsetting.json file will contain the protocols used for the service. Here we can see Http2 protocols have been used for the gRPC service.
 
gRPC Client And Server Using ASP.NET Core
 

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.
 
gRPC Client And Server Using ASP.NET Core
 
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.
 
gRPC Client And Server Using ASP.NET Core
 
Click on Console App(.NET Core) and click "Next".
 
gRPC Client And Server Using ASP.NET Core
 
Enter your project name and click the "Create" button.
 
gRPC Client And Server Using ASP.NET Core 
 
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.
 
We can install this package by NuGet Package Manager or by Manage NuGet Packages.
 
Installing the packages by Manage NuGet Packages
 
Right Click your gRPC client project and click on Manage NuGet Packages.
 
gRPC Client And Server Using ASP.NET Core
 
Select the Browse tab, enter “Grpc.net.Client” search, and Install the package to our client project.
 
gRPC Client And Server Using ASP.NET Core
 
Install the “Google.Protobuf” search and Install the package to our client project.
 
gRPC Client And Server Using ASP.NET Core
 
Install the “Grpc.Tools” search and Install the package to our client project.
 
gRPC Client And Server Using ASP.NET Core
 

Adding Protobuf file to client application

 
In our client application we need to add the same protobuf file for sending and receiving data from the client to server. First, we create the Protos folder in our client project.
 
gRPC Client And Server Using ASP.NET Core 
 
Now we need to add the protobuf which we used in Service project to the Protos folder.
 
Right Click the Protos folder and click on Add Existing Item.
 
gRPC Client And Server Using ASP.NET Core
 
Select and add the greet.proto file which we used in our Service project .
 
gRPC Client And Server Using ASP.NET Core
 
Now in order to use the greet.proto file in our project we need to add the itemGroup with Protobuf to our project.
 
Right Click our Project and click Edit Project file,
 
gRPC Client And Server Using ASP.NET Core
 
Add the below code to the project file. 
  1.   <ItemGroup>  
  2.     <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />  
  3. </ItemGroup>  
And your project file will look like this.
 
gRPC Client And Server Using ASP.NET Core
 
Build the application in order to use our Service in client application.
 

Working with Client Program.cs file

 
Now it's time for us to create the client program to send and receive the message from client to our gRPC Service.
 
For this we open the program.cs file and add the below namespace. Here shanugRPC is our service project namespace. 
  1. using shanugRPC ;  
  2. using Grpc.Net.Client;  
  3. using Grpc.Core;  
Now add the below code in the Main method of yourprogram.cs file,
  1. static async Task Main(string[] args)  
  2.       {  
  3.           var httpClient = new HttpClient();   
  4.           httpClient.BaseAddress = new Uri("https://localhost:50051");  
  5.           var client = GrpcClient.Create<Greeter.GreeterClient>(httpClient);  
  6.   
  7.           var reply = await client.SayHelloAsync(  
  8.                     new HelloRequest { Name = "GreeterClient" });  
  9.           Console.WriteLine("Greeting: " + reply.Message);  
  10.           Console.WriteLine("Press any key to exit...");  
  11.           Console.ReadKey();  
  12.       }  

Build and run the applicatoin

 
Note
First run our Service application and then run our client application for the client and server communication. Always make sure the service is running before running the client.
 
When we run the application, we might get the error as,
 
gRPC Client And Server Using ASP.NET Core
 
This means here our Service is listening to http://localhost:50051 but the above code will work for HTTPS and in our example, we need to use the https://localhost:50051. So, we change the code like below. 
  1. static async Task Main(string[] args)  
  2.         {  
  3.   
  4.             //----------------  
  5.             AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport"true);  
  6.             var httpClient = new HttpClient();   
  7.             httpClient.BaseAddress = new Uri("http://localhost:50051");  
  8.             var client = GrpcClient.Create<Greeter.GreeterClient>(httpClient);  
  9.   
  10.             var reply = await client.SayHelloAsync(  
  11.                       new HelloRequest { Name = "Shanu" });  
  12.             Console.WriteLine("Greeting: " + reply.Message);  
  13.             Console.WriteLine("Press any key to exit...");  
  14.             Console.ReadKey();  
  15.   
  16.         }  

Code part explanation

 
For using HTTP instead of HTTPS, first, set the below code part. 
  1. AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport"true);  
  2. var httpClient = new HttpClient();   
Next, we create the client object with url,
  1. var httpClient = new HttpClient();   
  2. httpClient.BaseAddress = new Uri("http://localhost:50051");  
  3. var client = GrpcClient.Create<Greeter.GreeterClient>(httpClient);   
Next, we send and get the replay from the service by calling the service method and pass the request and get the reply message.
  1. var reply = await client.SayHelloAsync(  
  2.           new HelloRequest { Name = "Shanu" });  
  3. Console.WriteLine("Greeting: " + reply.Message);  

Run the application

 
When we run the application we can see the result, as we send name as Shanu to the service from the client and we get the reply from the service as “Hello Shanu”
 
gRPC Client And Server Using ASP.NET Core
 
Now we change the code to get our name and our friend's name to send to the service for diriment method call and display the results .
 
Here is the complete code. We have used a similar code and asked the user to enter their name and friend's name and called service method SayHello and Servermessage method to send and receive a message from the client and server. 
  1. static async Task Main(string[] args)  
  2.       {  
  3.   
  4.           ////var httpClient = new HttpClient();  
  5.           ////// The port number(5001) must match the port of the gRPC server.  
  6.           ////httpClient.BaseAddress = new Uri("https://localhost:50051");  
  7.           ////var client = GrpcClient.Create<Greeter.GreeterClient>(httpClient);  
  8.   
  9.           //----------------  
  10.           AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport"true);  
  11.           var httpClient = new HttpClient();  
  12.           // The port number(5001) must match the port of the gRPC server.  
  13.           httpClient.BaseAddress = new Uri("http://localhost:50051");  
  14.           var client = GrpcClient.Create<Greeter.GreeterClient>(httpClient);   
  15.   
  16.           //-----------------------   
  17.   
  18.           Boolean isexit = true;  
  19.           while (isexit)  
  20.           {  
  21.               Console.WriteLine("Enter Your Name to send request to the server : ");  
  22.               String myName = Console.ReadLine();  
  23.   
  24.               var reply = await client.SayHelloAsync(  
  25.                                 new HelloRequest { Name = myName });  
  26.               Console.WriteLine("Hello wrold : " + reply.Message);  
  27.   
  28.   
  29.               Console.WriteLine("Enter YourFriend  Name  : ");  
  30.               String friendName = Console.ReadLine();  
  31.   
  32.               var serverreply = await client.ServermessageAsync(  
  33.                                 new HelloRequest { Name = friendName });  
  34.               Console.WriteLine("Message from Server -> " + serverreply.Message);  
  35.   
  36.   
  37.               Console.WriteLine("Do you want to continue say Y or N");  
  38.               string YN = Console.ReadLine();  
  39.               if (YN.ToLower() == "y")  
  40.               {  
  41.                   isexit = true;  
  42.               }  
  43.               else  
  44.               {  
  45.                   isexit = false;  
  46.               }  
  47.               Console.WriteLine("==========================  ============");  
  48.               Console.WriteLine("");  
  49.           }  
  50.   
  51.           Console.WriteLine("Press any key to exit...");  
  52.           Console.ReadKey();  
  53.   
  54.       }  
gRPC Client And Server Using ASP.NET Core