Building gRPC Solutions Using Visual Studio 2019

Introduction

 
In this article we will look at gRPC and how to implement gRPC solutions using Visual Studio 2019. gRPC is a high performance, open-source and easy to use RPC framework developed by Google. It has gained lots of popularity as it can be used across platforms and has lots of features for easy documentation. Visual Studio 2019 includes a template for building gRPC server applications, and we will look at building a simple gRPC server application. We will also build a client application to consume the API that is available on the server.

Difference between gRPC and Web API

 
One question that might come to mind immediately is, what is the difference between Web API of the .NET world and gRPC of the Google world. Although both are cross platform and open source the main difference is that requests for Web API are in a text format which are human readable whereas gRPC messages are encoded by Protobuf by default. Protobuf or Protocol buffers is a very efficient way to transfer information and the binary format is not human readable.
 

Building the gRPC server

 
We will be using Visual Studio 2019 to build the gRPC server. Create a new project and follow the steps below,
 
Building gRPC Solutions Using Visual Studio 2019
 
Building gRPC Solutions Using Visual Studio 2019
 
You can delete the existing “greet.proto” file and the service associated with it. Then add the following employee.proto file to the Protos folder,
  1. syntax = "proto3";  
  2.   
  3. option csharp_namespace = "GrpcEmployee";  
  4.   
  5. package employee;  
  6.   
  7. // The greeting service definition.  
  8. service Employee {  
  9.   
  10.   rpc GetEmployeeInfo (EmployeeInfoRequest) returns (EmployeeInfoResponse);  
  11. }  
  12.   
  13. // The request message containing the employee ID.  
  14. message EmployeeInfoRequest {  
  15.   int32 ID = 1;  
  16. }  
  17.   
  18. // The response message containing the employee data.  
  19. message EmployeeInfoResponse {  
  20.   int32 ID = 1;  
  21.   string FirstName = 2;  
  22.   string LastName = 3;  
  23.   string Address = 4;  
  24.   string PhoneNumber = 5;  
  25.   string EmailAddress = 6;  
  26. }  
Ensure that the property settings of this file are as below,
 
Building gRPC Solutions Using Visual Studio 2019
 
Also, add a class “EmployeeService” under services as below,
  1. using System.Threading.Tasks;  
  2. using Grpc.Core;  
  3. using Microsoft.Extensions.Logging;  
  4.   
  5.   
  6. namespace GrpcEmployee  
  7. {  
  8.     public class EmployeeService : Employee.EmployeeBase  
  9.     {  
  10.         private readonly ILogger<EmployeeService> _logger;  
  11.         public EmployeeService(ILogger<EmployeeService> logger)  
  12.         {  
  13.             _logger = logger;  
  14.         }  
  15.   
  16.         public override Task<EmployeeInfoResponse> GetEmployeeInfo(EmployeeInfoRequest request, ServerCallContext context)  
  17.         {  
  18.             return Task.FromResult(new EmployeeInfoResponse  
  19.             {  
  20.                 ID = 100,  
  21.                 FirstName = "John",  
  22.                 LastName = "Doe",  
  23.                 Address = "Toronto",  
  24.                 PhoneNumber = "123-123-1234",  
  25.                 EmailAddress = "[email protected]"  
  26.             });  
  27.         }  
  28.   
  29.     }  
  30. }  
Ensure the project file matches the below,
  1. <Project Sdk="Microsoft.NET.Sdk.Web">  
  2.   
  3.   <PropertyGroup>  
  4.     <TargetFramework>netcoreapp3.1</TargetFramework>  
  5.   </PropertyGroup>  
  6.   
  7.   <ItemGroup>  
  8.     <Protobuf Include="Protos\employee.proto" GrpcServices="Server" />  
  9.   </ItemGroup>  
  10.   
  11.   <ItemGroup>  
  12.     <PackageReference Include="Grpc.AspNetCore" Version="2.27.0" />  
  13.   </ItemGroup>  
  14.   
  15. </Project>  
You can now build and run the application and you will see the below,
 
Building gRPC Solutions Using Visual Studio 2019
 

Building the gRPC client

 
We will now build the client application which will call the API on the gRPC server. This will be a simple .NET Core 3.1 console application.
 
Create a folder called “Protos” and add the below employee.proto file to it as below,
  1. syntax = "proto3";  
  2.   
  3. option csharp_namespace = "GrpcEmployee";  
  4.   
  5. package employee;  
  6.   
  7. // The greeting service definition.  
  8. service Employee {  
  9.   
  10.   rpc GetEmployeeInfo (EmployeeInfoRequest) returns (EmployeeInfoResponse);  
  11. }  
  12.   
  13. // The request message containing the employee ID.  
  14. message EmployeeInfoRequest {  
  15.   int32 ID = 1;  
  16. }  
  17.   
  18. // The response message containing the employee data.  
  19. message EmployeeInfoResponse {  
  20.   int32 ID = 1;  
  21.   string FirstName = 2;  
  22.   string LastName = 3;  
  23.   string Address = 4;  
  24.   string PhoneNumber = 5;  
  25.   string EmailAddress = 6;  
  26. }  
The properties of this file are as below,
 
Building gRPC Solutions Using Visual Studio 2019
 
Add the below code to the Main function,
  1. using System;  
  2. using System.Threading.Tasks;  
  3. using GrpcEmployee;  
  4. using Grpc.Net.Client;  
  5.   
  6. namespace GrpcClient  
  7. {  
  8.     class Program  
  9.     {  
  10.         static async Task Main(string[] args)  
  11.         {  
  12.             // The port number(5001) must match the port of the gRPC server.  
  13.             using var channel = GrpcChannel.ForAddress("https://localhost:5001");  
  14.   
  15.             var client = new Employee.EmployeeClient(channel);  
  16.             var reply = await client.GetEmployeeInfoAsync(  
  17.                               new EmployeeInfoRequest { ID = 100 });  
  18.   
  19.             Console.WriteLine(reply);  
  20.   
  21.             Console.WriteLine("Press any key to exit...");  
  22.             Console.ReadKey();  
  23.         }  
  24.     }  
  25. }  
Also, ensure the project file matches the below,
  1. <Project Sdk="Microsoft.NET.Sdk">  
  2.   
  3.   <PropertyGroup>  
  4.     <OutputType>Exe</OutputType>  
  5.     <TargetFramework>netcoreapp3.1</TargetFramework>  
  6.   </PropertyGroup>  
  7.   
  8.   <ItemGroup>  
  9.     <PackageReference Include="Google.Protobuf" Version="3.11.4" />  
  10.     <PackageReference Include="Grpc.Net.Client" Version="2.28.0" />  
  11.     <PackageReference Include="Grpc.Tools" Version="2.28.1">  
  12.       <PrivateAssets>all</PrivateAssets>  
  13.       <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>  
  14.     </PackageReference>  
  15.   </ItemGroup>  
  16.   
  17.   <ItemGroup>  
  18.     <Protobuf Include="Protos\employee.proto" GrpcServices="Client" />  
  19.   </ItemGroup>  
  20.   
  21. </Project>  
Here we see that three NuGet packages have been added.
 
We can now run the client application and we see that it communicates with the gRPC server and returns the data.
 
Building gRPC Solutions Using Visual Studio 2019
 

Summary

 
In this article, we have looked at how to create a gRPC server and then create a client application to consume the API from this server. This is a useful API to understand in addition to the Web API from the Microsoft world. It will give you more options when you are in the process of developing a RPC framework.


Similar Articles