Skip to main content

C# gRPC Integration Tutorial

gRPC (Google Remote Procedure Call) is a high-performance RPC framework that uses HTTP/2 and Protocol Buffers. This tutorial covers the basics of setting up a gRPC server and client in C# using .NET.


Prerequisites​

  1. .NET SDK: Ensure the .NET SDK (version 5 or higher) is installed on your system.

    dotnet --version
  2. Protobuf Compiler (optional): To define and work with Protocol Buffers (.proto files), install protoc. (Note: Visual Studio and dotnet handle .proto compilation automatically).


1. Setting Up a New gRPC Project​

Step 1: Create a gRPC Server Project​

Run the following command to create a new gRPC server project:

dotnet new grpc -o GrpcServer
cd GrpcServer

The dotnet new grpc command creates a new project with the necessary gRPC dependencies and a default GreeterService.

Step 2: Review the protos Folder​

The template includes a protos folder with a file named greet.proto, which defines a Greeter service.

Example greet.proto File​

syntax = "proto3";

option csharp_namespace = "GrpcServer";

service Greeter {
rpc SayHello (HelloRequest) returns (HelloReply);
}

message HelloRequest {
string name = 1;
}

message HelloReply {
string message = 1;
}

This defines a Greeter service with a SayHello RPC method that takes a HelloRequest and returns a HelloReply.


2. Implementing the gRPC Server​

Step 1: Implement the Service​

Open Services/GreeterService.cs and implement the SayHello method:

using Grpc.Core;
using System.Threading.Tasks;

public class GreeterService : Greeter.GreeterBase
{
public override Task<HelloReply> SayHello(HelloRequest request, ServerCallContext context)
{
return Task.FromResult(new HelloReply
{
Message = $"Hello, {request.Name}"
});
}
}

Step 2: Run the Server​

Run the server to test it:

dotnet run

This starts the gRPC server on localhost:5001 by default.


3. Setting Up the gRPC Client​

Step 1: Create a New Console App for the Client​

In a new directory, create a console app to act as the gRPC client:

dotnet new console -o GrpcClient
cd GrpcClient

Step 2: Add Required gRPC and Protobuf Packages​

Add Grpc.Net.Client and Google.Protobuf packages:

dotnet add package Grpc.Net.Client
dotnet add package Google.Protobuf
dotnet add package Grpc.Tools

Step 3: Add the .proto File​

Copy the greet.proto file from the server’s protos folder into the client project, and update the .csproj file to include it:

<ItemGroup>
<Protobuf Include="protos\greet.proto" GrpcServices="Client" />
</ItemGroup>

Step 4: Implement the Client​

Open Program.cs and add the following code:

using System;
using System.Threading.Tasks;
using Grpc.Net.Client;

class Program
{
static async Task Main(string[] args)
{
using 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);
}
}

This client connects to the gRPC server, sends a HelloRequest, and prints the server's response.

Step 5: Run the Client​

Run the client from the GrpcClient directory:

dotnet run

You should see the server’s response: "Greeting: Hello, World".


Summary​

This tutorial covered the basics of integrating gRPC with C# and .NET, including:

  1. Creating and running a gRPC server with .NET.
  2. Defining services and messages in a .proto file.
  3. Setting up a gRPC client to communicate with the server.

gRPC provides a powerful, efficient way to handle communication between services. Explore other gRPC methods and configurations to enhance your setup.

Content Review​

The content in this repository has been reviewed by chevp. Chevp is dedicated to ensuring that the information provided is accurate, relevant, and up-to-date, helping users to learn and implement programming skills effectively.

About the Reviewer​

For more insights and contributions, visit chevp's GitHub profile: chevp's GitHub Profile.