Introduction To gRPC in C# and ASP.NET Core
Building a gRPC Service in C# and ASP.NET Core – Part 1

gRPC (gRPC Remote Procedure Call) is a modern open-source RPC framework that can run in any environment. It uses HTTP/2 for transport, Protocol Buffers (Protobuf) for defining APIs, and provides features like bi-directional streaming, client-server communication, and more. It’s an excellent choice for microservices architecture due to its efficiency and speed.
In this blog, we’ll build a simple gRPC service using C# and ASP.NET Core.
Prerequisites
.NET SDK installed (v6.0 or later recommended)
Visual Studio or any other IDE of your choice
Basic understanding of C# and ASP.NET Core
Step 1: Create the Project
Open your terminal or IDE and create a new gRPC project:
dotnet new grpc -o GrpcDemo cd GrpcDemoThis command creates a new gRPC project with a predefined structure.
Step 2: Define the gRPC Service
gRPC services are defined using Protocol Buffers (Protobuf).
Open the
Protosfolder in your project.Replace the content of
greet.protowith the following:syntax = "proto3"; option csharp_namespace = "GrpcDemo"; service Greeter { rpc SayHello (HelloRequest) returns (HelloReply); } message HelloRequest { string name = 1; } message HelloReply { string message = 1; }Save the file. The
protoccompiler generates the necessary C# code for gRPC communication.
Step 3: Implement the Service
Open
Services/GreeterService.cs.Replace the code with the following implementation:
using Grpc.Core; using GrpcDemo; public class GreeterService : Greeter.GreeterBase { public override Task<HelloReply> SayHello(HelloRequest request, ServerCallContext context) { return Task.FromResult(new HelloReply { Message = $"Hello, {request.Name}!" }); } }This code defines the server-side logic for handling the
SayHelloRPC call.
Step 4: Configure the ASP.NET Core App
Open
Program.csand ensure the gRPC service is added:var builder = WebApplication.CreateBuilder(args); var app = builder.Build(); builder.Services.AddGrpc(); app.MapGrpcService<GreeterService>(); app.MapGet("/", () => "Use a gRPC client to communicate with this service."); app.Run();This code registers the
GreeterServiceand sets up the HTTP/2 endpoint for gRPC.
Step 5: Create a gRPC Client
Create a new console application:
dotnet new console -o GrpcClient cd GrpcClientAdd the gRPC client package:
dotnet add package Grpc.Net.Client dotnet add package Google.Protobuf dotnet add package Grpc.ToolsCopy the
greet.protofile from the server project to the client project.Edit the
GrpcClient.csprojfile to include:<ItemGroup> <Protobuf Include="Protos\greet.proto" GrpcServices="Client" /> </ItemGroup>Update the
Program.csfile in the client project:using Grpc.Net.Client; using GrpcDemo; var channel = GrpcChannel.ForAddress("https://localhost:5001"); var client = new Greeter.GreeterClient(channel); var reply = await client.SayHelloAsync(new HelloRequest { Name = "World" }); Console.WriteLine("Greeting: " + reply.Message);
Step 6: Run the Application
Run the server:
dotnet run --project GrpcDemoRun the client:
dotnet run --project GrpcClient
You should see the client output:
Greeting: Hello, World!
Key Features of gRPC in ASP.NET Core
High Performance: Built on HTTP/2 for efficient communication.
Cross-Language Support: Use the same
.protofile for clients in other languages.Streaming: Supports client, server, and bi-directional streaming.
Built-in Code Generation: Protobuf generates boilerplate code, reducing development time.
Conclusion
In this tutorial, we created a simple gRPC service and client using C# and ASP.NET Core. This foundational setup can be extended for more complex microservices architectures or to include advanced features like authentication, load balancing, and streaming.
Experiment with gRPC to see how it can boost your application's performance and scalability!



