gRPC with C# and .Net Core – Part 1

Net Core 3.0 has been launched last month and one of the major features of it is gRPC. Let’s have a detailed discussion on how to get started with gRPC using C# and .Net Core. Let’s divide our article into the following sections, Agenda

  1. What is GRPC
  3. Creating GRPC Service
  4. Creating GRPC Client
  5. WCF to GRPC
  6. Conclusion

What is gRPC

 According to official blog “GRPC is a open source high performance RPC framework that can run in any environment, it can efficiently connect services across data centers with support of load balancing, tracing and authentication.” Mostly gRPC is used for connecting polyglot services in microservices architecture, connecting mobile devices, browser clients to the services Grpc has two parts the — gRPC protocol and data serialization. By default gRPC uses Protobuf for the serialization and protocol is based on Http2 which provides a lot of advantages over traditional Http1.x 


 REST has been one of the concrete pillars of the web programming recently, but the emergence of the gRPC has put some challenge before REST. The below tables explain the differences between them,

Browser SupportYesNo
Data ExchangeResources and VerbsMessages
Request-Response ModelOnly supports Request Response as based on Http/1.1Supports All Types Of Streaming as based on Http/2
Payload Exchange FormatSerialization like JSON/XMLStrong Typing

Creating GRPC Service

 As we are good with the basics, for now, let’s move and see how we can create the Service, and later  let’s see how we can consume that service into the client. To get started with the service we need to have a few initial things as follows

  1. Visual Studio 2019
  2. .NET Core SDK 3.1

Once you have this we can follow the following steps to create our first service 

Add New Project and Select the GRPC Service as a new project

project structre

In our case, we are adding a Project with name ToDoGrpcService, when we will add the Project we will have the following Structure,

Project Structure

Our project will be added with the default Greet service and One Default greet. Proto File. In this Project, we are going to create our first To-do. Grpc Service for that first we need to add the proto file like below. Right Click on Protos Folder -> Add New Item -> Select ProtoBuff File and name it “ToDo” like below,

Proto File

Creating ProtoBuff Service Contract and Schema

 Grpc uses Proto File to define the service contracts and it also contains the schema for the data which will be sent across between the services. Our ToDo service will have the following code,

syntax = "proto3";  
option csharp_namespace = "ToDoProtos";  
service ToDoService{  
rpc getToDoItem(google.protobuf.Empty) returns (ToDoModel);  
message ToDoModel{  
    string Title=1;;  
    string Description=2;  
    bool status=3;  

Code Explanation

  1. The first line specifies the version of Protobuff used in the programs; i.e. Proto3
  2. The second line specifies the Namespace under which this service will be created
  3. The next section specifies the actual service name and the Method into the Service. In our case our service is ToDoService and we have getToDoItem Method with empty parameter and returns the ToDoModel
  4. The last section is the actual data which will be returned from the service

Creating Server

 Once we are done with adding the proto buff file we are ready to create the server. In this server we will use the automatically generated code from the protobuff file. We can stub out the functionality over the code which handles the communication and serialization. To generate the code from the protobuf file we need to make the changes in the .csproj file of our project and add lines like below,

Stub Changes

Implementing the Service

The next step in creating a service is to create a class that will be inherited from the above-generated class and implement the service contract that we have defined in the protobuff file above. Once implemented it will look like below

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Threading.Tasks;  
using Google.Protobuf.WellKnownTypes;  
using Grpc.Core;  
using ToDoProtos;  
namespace SampleGrpcService.Services  
    public class ToDoDataService: ToDoProtos.ToDoService.ToDoServiceBase  
        public override Task<ToDoModel> getToDoItem(Empty request, ServerCallContext context)  
            return Task.FromResult(GetToDoData());  
        public ToDoModel GetToDoData()  
            return new ToDoModel()  
                Status = true,  
                Description = "First Grpc Service",  
                Title = "Grpc Service"  

This will complete our steps for the creation of our first grpc service. Now it’s time to create a client which will consume this service and show us the output. Before that let’s run the application using console using command dotnet Run

Startup and Configure Services changes

 After making changes in the startup section the code will look like below. In this we have to map our endpoints to the services which we have generated and we have to tell the application that we are going to use the grpc as a service in the application. 

public void ConfigureServices(IServiceCollection services)  
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)  
    if (env.IsDevelopment())  
    app.UseEndpoints(endpoints =>  
        endpoints.MapGrpcService< SampleGrpcService.Services.ToDoDataService>();  
        endpoints.MapGet("/", async context =>  
            await context.Response.WriteAsync("Communication with gRPC endpoints must be made through a gRPC client. To learn how to create a client, visit:");  

Creating Client

 To create a client let’s add the console application like usual. Once the application is added let’s add following three packages in the project,

  1. Google.Protobuf
  2. Grpc.Net.Client
  3. Grpc.Tools

Next thing that we need to do in the client project is to add the Proto file that we have generated already. Again we have to modify .csproj file to generate a stub for the client code that we want to use in the application like below

<Protobuf Include="Protos\ToDo.proto" GrpcServices="Client" />  

Next step is to add the client code which will call the service. The code will be like below in which we create a channel pointing to an address where our service will be hosted and next we will have a client-generated from the proto file. It will have a Method which will be called just like we call a class Method.

using System;  
namespace TodoGrpcClient  
    class Program  
        static void Main(string[] args)  
            var channel = Grpc.Net.Client.GrpcChannel.ForAddress("https://localhost:5001/");  
            var client = new ToDoProtos.ToDoService.ToDoServiceClient(channel);  
            var request = new Google.Protobuf.WellKnownTypes.Empty();  
            var result = client.getToDoItem(request);  

Just set the client project as a startup project and run the application and we can see the output as below,

This was all about creating the client for the Grpc Service 


 One of the alternatives as recommended by Microsoft is to use the Grpc instead of the WCF services in the future projects. In this section, let us see how we can relate our understanding of the WCF to the Grpc concepts that we have right now,

ServiceContractService in ProtoFile
OperationContractRPC Method in ProtoFile
DataContractMessage in the ProtoFile
FaultContractRicher error Model
Request-ReplyUnary Streaming
DuplexBidirectional Streaming


 This was all about creating your first Grpc Service using C# and core. You can find the code attached to the article for you

.  References