Introduction
Internal ShopNest services (Inventory ↔ Warehouse) use gRPC for fast binary RPC with strongly typed .proto contracts — REST stays for public APIs; gRPC wins service-to-service.
After this article you will
- Define services in .proto files
- Implement gRPC server in ASP.NET Core
- Call from .NET client with generated stubs
- Use unary and streaming patterns
- Choose gRPC vs REST appropriately
Prerequisites
- Article 52 — Message Queues with RabbitMQ
- ShopNest API, EF Core, and DI from prior modules
Concept deep-dive
syntax = "proto3";
option csharp_namespace = "ShopNest.Inventory.Grpc";
service InventoryService {
rpc GetStock (GetStockRequest) returns (StockReply);
rpc WatchStock (WatchStockRequest) returns (stream StockReply);
}
message GetStockRequest { int32 product_id = 1; }
message StockReply { int32 product_id = 1; int32 quantity = 2; }
// Server Program.cs
builder.Services.AddGrpc();
app.MapGrpcService<InventoryGrpcService>();
// Client
var channel = GrpcChannel.ForAddress("https://inventory:5001");
var client = new InventoryService.InventoryServiceClient(channel);
var reply = await client.GetStockAsync(new GetStockRequest { ProductId = 42 });
gRPC-Web allows browser clients via proxy. Auth: JWT in metadata headers.
Hands-on — ShopNest High-Performance Internal Service Communication
- Add Grpc.AspNetCore; create inventory.proto.
- InventoryGrpcService implements GetStock from EF.
- Order service client calls gRPC instead of REST for stock check.
- Compare latency REST vs gRPC in local benchmark.
Common errors & best practices
- gRPC for public browser API without gRPC-Web — stick to REST/OpenAPI.
- Proto breaking changes without versioning — coordinate deployments.
Interview questions
Q: gRPC vs REST?
A: gRPC: HTTP/2, binary, contract-first, faster internal; REST: universal, human-readable, public APIs.
Q: Streaming types?
A: Server, client, bidirectional — for live stock feeds.
Summary
- Proto files define cross-service contracts
- gRPC ideal for internal ShopNest service mesh
- REST remains for external partners and mobile BFF
- GrpcChannel + generated client for callers
Previous: Message Queues with RabbitMQ
Next: Unit Testing with xUnit and Moq
FAQ
gRPC through API Gateway?
YARP and some gateways support gRPC proxying.
TLS required?
Yes in production — Http2 + TLS on port 443 or dedicated port.