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

Search for a command to run...
Part 1 - Create your first gRPC method in .NET Core server

No comments yet. Be the first to comment.
Run through of the idea I did a last minute registration, couldn't find anyone to join me, with a bit of hesitation ran solo π Most of the themes were focused towards company's product use cases. I ended up picking the General theme, because I was n...

Combine token sources to get the best out of cancellation model

Why you should start using them in your backend services

Developers = Crazy individuals with mild OCD. Individuals who are particular about their operating system, the editor they use, the way they use language constructs. In this post, I am going to talk about how this craziness make code reviews even c...

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.
Search for Grpc.AspNetCore and add the latest stable version to your project.
.proto fileWe 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;
}

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}"});
}
}
public void ConfigureServices(IServiceCollection services)
{
services.AddGrpc();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapGrpcService<EchoService>();
});
}
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

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
HTTP/2, additionally it also serializes the data over network reducing data size againlow-bandwidth systems, since the size of data over network is reducedProgramming 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 languageBidirectional 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. 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