gRPC with .NET Core

gRPC with .NET Core

Part 1 - Create your first gRPC method in .NET Core server

ยท

4 min read

Reading about REST vs RPC few years back I came across this neat RPC framework called gRPC. Since then the framework has gained a lot of traction, companies like Netflix, Lyft use it at its core today.

Most of us, know by now that the g in gRPC does not stand for Google, it literally stands for nothing. I mean why not give Google the accolades since the project was created and is maintained by them ๐Ÿ˜ƒ

There are areas where it shines and then of course there are limitations, we will take a look at those at the end of this article. In this article we will take a look at how we can create a simple .NET Core gRPC server, expose a unary rpc method and consume it using BloomRPC client.

Create the gRPC server

Add nuget package

Search for Grpc.AspNetCore and add the latest stable version to your project.

Create your .proto file

We are defining a service called Echo, which would have an rpc method called EchoMessage. Additionally we will also define the data transfer objects EchoRequest and EchoResponse.

syntax = "proto3"; // protocol buffer version
option csharp_namespace = "GrpcTrial";
package echo;
service Echo { // the service
  rpc EchoMessage (EchoRequest) returns (EchoResponse); // the rpc method
}
message EchoRequest {
  string message = 1; // index at which the property is available
}
message EchoResponse {
  string message = 1;
}

Set the proto file to build the server stub classes

image.png

As soon as we build our project, few base classes will be autogenerated for us, classes gRPC understands, all we have to do now is override the methods for our logic.

public class EchoService : Echo.EchoBase // Inherit from the auto generated base class
{
    // override the method and add our own logic
    public override Task<EchoResponse> EchoMessage(EchoRequest request, ServerCallContext context)
    {
         return Task.FromResult(new EchoResponse {  Message = $"Did you just say, {request.Message}"});
    }
}

Add the gRPC service to service container

public void ConfigureServices(IServiceCollection services)
{
      services.AddGrpc(); 
}

Add the gRPC middleware

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
      app.UseRouting();
      app.UseEndpoints(endpoints =>
      {
             endpoints.MapGrpcService<EchoService>();
      });
}

Call the RPC method from a client

For this article we will use BloomRPC, an open source GUI client for gRPC services.

Simply add the proto file inside BloomRPC app, enter the base uri of server, modify the payload, and send a request

final_62026d026b89e200619ed6e2_895774.gif

Pretty neat! ๐Ÿ‘Œ

Though very promising, this is not a replacement for REST APIs or SignalR. It shines at places and has its own issues.

Pros

  • High performance since it works on top of HTTP/2, additionally it also serializes the data over network reducing data size again
  • Works for low-bandwidth systems, since the size of data over network is reduced
  • Programming language agnostic, once we have a proto file the same can be used by almost all languages, imagine a microservice architecture where each service is built using a different programming language
  • Bidirectional streaming capabilities, client can stream data to the server, server can stream data to the client, or both can happen at the same time.

Cons

  • HTTP/2 and greater only, which might be a headache for clients/services on older versions of HTTP.
  • There is a learning curve, getting used to protocol buffer, bidirectional streaming etc may take sometime.
  • Not enough tooling is available, ๐Ÿ›‘ I just couldn't make use of https using BloomRPC, from a console app it works seamlessly.

I have used gRPC to stream log messages from one component to another. Few of our backend jobs take time(even days) and logs are continuously written to flat files inside a VM, a gRPC connection is established between the VM and a service, logs are then pushed as soon as they arrive, giving the user visibility of what's happening behind the scenes with the process. ๐ŸŒŸ But more about streaming in my next articles.

I hope this post gave you some insights on gRPC and will help you get started. Thank you for reading. Cheers!

Resources