i wnat full implementation of grpcs
Loading
i wnat full implementation of grpcs
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Naimish MakwanaRank #324 · 7.7K reputationPosted Aug 11, 2025, 8:43 AM EDT
Got it — I’ll give you a full working example of a gRPC with TLS/SSL (gRPCs) implementation in .NET 8 so you can run it end-to-end.
This will include:
Proto file (service contract)
Server implementation with TLS enabled (
Kestrel)Client implementation connecting securely
Certificate generation for local testing
1. Create the
.protofileProtos/Greeter.protosyntax = "proto3"; option csharp_namespace = "GrpcDemo"; package greet; service Greeter { rpc SayHello (HelloRequest) returns (HelloReply); } message HelloRequest { string name = 1; } message HelloReply { string message = 1; }2. Generate certificates for gRPCs
Run these commands in PowerShell or Bash:
# Create server certificate dotnet dev-certs https --clean dotnet dev-certs https -ep ./server.pfx -p password dotnet dev-certs https --trust # Extract .crt for client trust (optional in dev) openssl pkcs12 -in server.pfx -clcerts -nokeys -out server.crt -passin pass:password3. Server implementation
Server/Program.csusing GrpcDemo; using GrpcDemo.Services; var builder = WebApplication.CreateBuilder(args); // Enable gRPC builder.Services.AddGrpc(); // Configure Kestrel for gRPC over TLS builder.WebHost.ConfigureKestrel(options => { options.ListenAnyIP(5001, listenOptions => { listenOptions.Protocols = Microsoft.AspNetCore.Server.Kestrel.Core.HttpProtocols.Http2; listenOptions.UseHttps("server.pfx", "password"); }); }); var app = builder.Build(); app.MapGrpcService(); app.MapGet("/", () => "gRPC Server running securely."); app.Run();Server/Services/GreeterService.csusing Grpc.Core; using greet; namespace GrpcDemo.Services { public class GreeterService : Greeter.GreeterBase { public override Task SayHello(HelloRequest request, ServerCallContext context) { return Task.FromResult(new HelloReply { Message = $"Hello, {request.Name} (secure gRPC)!" }); } } }4. Client implementation
Client/Program.csusing Grpc.Net.Client; using greet; using System.Net.Http; using System.Security.Cryptography.X509Certificates; // Load server certificate for trust (optional in dev) var cert = new X509Certificate2("server.crt"); var handler = new HttpClientHandler(); handler.ClientCertificates.Add(cert); handler.ServerCertificateCustomValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator; // Connect to gRPC server over HTTPS using var channel = GrpcChannel.ForAddress("https://localhost:5001", new GrpcChannelOptions { HttpHandler = handler }); var client = new Greeter.GreeterClient(channel); var reply = await client.SayHelloAsync(new HelloRequest { Name = "Naimish" }); Console.WriteLine(reply.Message);5. Project setup steps
For the server:
dotnet new grpc -n Server # Replace Program.cs and add Greeter.proto dotnet add package Grpc.AspNetCore dotnet runFor the client:
dotnet new console -n Client dotnet add package Grpc.Net.Client dotnet add package Google.Protobuf dotnet add package Grpc.Tools # Add Greeter.proto and configure csproj to compile itClient
.csprojsnippet for.proto:? How it works
The server listens on port 5001 using HTTP/2 over TLS (gRPCs).
The client connects using the server’s TLS cert.
The proto file defines the contract;
Grpc.Toolsgenerates the C# types.If you want, I can extend this example with bidirectional streaming, unary calls, and deadline handling so it covers all gRPC patterns in secure mode.
Do you want me to make that advanced version next?