SHARE:

Exposing proto files in a gRPC service over a frameworkless and lightweight API

Introduction

A few days ago I introduced the notion of nano service in ASP.NET Core or rather how to implement a REST API without any particular framework (https://anthonygiretti.com/2020/06/29/nano-services-with-asp-net-core-or-how-to-build-a-light-api/). Since then, I have challenged myself regularly on the possibilities of relevant applications of this idea. Well, I found a very interesting use case: exposing the protobuffs of a gRPC service without using a framework and having to manage collisions with the gRPC framework of ASP.NET Core. This article will show you how to do it in the simplest way possible.

Implementing a ProtoService

Well, in order to make a clearer code, I have implemented a service (then unit testable) instead of writing the whole implementation in the Startup.cs file. The way I implement it (fetching files in the constructor) it’s because I will use Singleton lifetime of ASP.NET Core DI system and let it manage the my service instance, I could have implemented myself a thread safe constructor. Implementation of the ProtoService:

Once done we can instantiate the service in the Starup.cs file and expose endpoints.

Exposing endpoints

I’m expising here 2 endpoints:

  • GET /protos that provides a json collection of protobuf files grouped version with the WriteAsync extension.
  • GET /protos/v{version:int}/{protoName} that provides the protobuf file in text format with the SendFileAsync extension.

After route parameters conversion to their right type, the Startup file looks like this:

Demo:


Written by

anthonygiretti

Anthony is a specialist in Web technologies (14 years of experience), in particular Microsoft .NET and learns the Cloud Azure platform. He has received twice the Microsoft MVP award and he is also certified Microsoft MCSD and Azure Fundamentals.
%d bloggers like this: