Skip to main content

Command Palette

Search for a command to run...

Introduction To gRPC in C# and ASP.NET Core

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

Updated
3 min read
Introduction To gRPC in C# and ASP.NET Core

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

  1. Open your terminal or IDE and create a new gRPC project:

     dotnet new grpc -o GrpcDemo
     cd GrpcDemo
    

    This command creates a new gRPC project with a predefined structure.


Step 2: Define the gRPC Service

gRPC services are defined using Protocol Buffers (Protobuf).

  1. Open the Protos folder in your project.

  2. Replace the content of greet.proto with 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;
     }
    
  3. Save the file. The protoc compiler generates the necessary C# code for gRPC communication.


Step 3: Implement the Service

  1. Open Services/GreeterService.cs.

  2. 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 SayHello RPC call.


Step 4: Configure the ASP.NET Core App

  1. Open Program.cs and 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 GreeterService and sets up the HTTP/2 endpoint for gRPC.


Step 5: Create a gRPC Client

  1. Create a new console application:

     dotnet new console -o GrpcClient
     cd GrpcClient
    
  2. Add the gRPC client package:

     dotnet add package Grpc.Net.Client
     dotnet add package Google.Protobuf
     dotnet add package Grpc.Tools
    
  3. Copy the greet.proto file from the server project to the client project.

  4. Edit the GrpcClient.csproj file to include:

     <ItemGroup>
         <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
     </ItemGroup>
    
  5. Update the Program.cs file 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

  1. Run the server:

     dotnet run --project GrpcDemo
    
  2. Run 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 .proto file 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!

Mastering gRPC with C# and ASP.NET Core

Part 2 of 2

Explore gRPC, a high-performance RPC framework, in this series. Build scalable microservices with C# and ASP.NET Core, covering Protobuf and cross-language communication. Perfect for mastering modern APIs and distributed systems!

Start from the beginning

Building a Low-Latency gRPC Service for Real-Time Inter-Microservice Communication in C# and ASP.NET Core

Building a gRPC Service in C# and ASP.NET Core – Part 2