Introduction to Implementing gRPC in .NET: Creating a gRPC Server and Client
kaboura souhail
?? Administrateur & Développeur ERP chez COFAT Group | Full-Stack Developer (.NET | Angular | Flutter) | Solutions Tech Innovantes ?? | Passionné par l'Optimisation des Processus ??
Part 1: gRPC Server
Creating a gRPC Project
Open a terminal and execute the following command to create a new gRPC project:
dotnet new grpc -o GrpcServer
This will create a new folder GrpcServer with an initial gRPC project.
Defining the gRPC Service
In the Protos folder, open the greet.proto file. You can modify it to define your own service. For example:
syntax = "proto3";
option csharp_namespace = "GrpcServer";
package greet;
// Le service Greeter
service Greeter {
// Envoie une salutation
rpc SayHello (HelloRequest) returns (HelloReply);
}
// Le message de demande contenant le nom de l'utilisateur.
message HelloRequest {
string name = 1;
}
// Le message de réponse contenant la salutation.
message HelloReply {
string message = 1;
}
Implementing the Service
In the Services folder, create a new file GreeterService.cs and implement the service:
using Grpc.Core;
using GrpcServer;
public class GreeterService : Greeter.GreeterBase
{
public override Task<HelloReply> SayHello(HelloRequest request, ServerCallContext context)
{
return Task.FromResult(new HelloReply
{
Message = "Hello " + request.Name
});
}
}
Configuring the Server
In the Program.cs file, ensure the service is added to the gRPC pipeline:
public class Program
{
public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddGrpc();
var app = builder.Build();
app.MapGrpcService<GreeterService>();
app.MapGet("/", async context =>
{
await context.Response.WriteAsync("Communication via gRPC doit utiliser le client gRPC.");
});
app.Run();
}
}
Running the Server
Run the server using
领英推荐
dotnet run --project GrpcServer
Part 2: gRPC Client
Creating a Console Client Project
In a new folder, create a Console client project:
dotnet new console -o GrpcClient
cd GrpcClient
Adding the Dependency and Proto File
Add the gRPC package to the client
dotnet add package Grpc.Net.Client
dotnet add package Google.Protobuf
dotnet add package Grpc.Tools
Copy the greet.proto file from the server into a Protos folder in the client and ensure the client project recognizes it by modifying the .csproj file:
<ItemGroup>
<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
</ItemGroup>
Implementing the Client
In the Program.cs file, implement the client:
using Grpc.Net.Client;
using GrpcServer; // Assurez-vous que l'espace de noms correspond au namespace défini dans greet.proto
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);
Running the Client
Run the client to send a request to the server:
dotnet run
This basic approach shows you how to set up simple communication between a client and a server using gRPC in .NET. You can extend this base by adding more services and methods as needed.