Proto File
Location:src/Services/Discount/Discount.Grpc/Protos/discount.proto
Service Definition
DiscountProtoService
The main service definition with four RPC methods:Message Definitions
GetDiscountRequest
productName(string, field 1): The name of the product to look up
CouponModel
id(int32, field 1): Unique identifier for the couponproductName(string, field 2): Name of the product this discount applies todescription(string, field 3): Human-readable description of the discountamount(int32, field 4): Discount amount (in currency units or percentage)
CreateDiscountRequest
coupon(CouponModel, field 1): The coupon data to create (id is usually 0 or omitted)
UpdateDiscountRequest
coupon(CouponModel, field 1): The complete coupon data including the id
DeleteDiscountRequest
productName(string, field 1): The product name to identify which discount to delete
DeleteDiscountResponse
success(bool, field 1): True if deletion was successful, false otherwise
Proto File Configuration
Syntax Declaration
C# Namespace
Discount.Grpc namespace, matching the project structure.
Package Name
discount, used for service resolution.
Code Generation
Server-Side (.csproj configuration)
DiscountProtoService- Base service classDiscountProtoService.DiscountProtoServiceBase- Abstract base class to inheritGetDiscountRequest,CouponModel, etc. - Message classes
Client-Side (.csproj configuration)
DiscountProtoService.DiscountProtoServiceClient- Client class for calling the service- All message classes (same as server)
Field Numbering
In Protocol Buffers, each field has a unique number:- Field numbers 1-15 take 1 byte to encode (use for frequently used fields)
- Field numbers 16-2047 take 2 bytes
- Field numbers must be unique within a message
- Cannot reuse field numbers (breaks backward compatibility)
- Numbers 19000-19999 are reserved for Protocol Buffers
Data Types
Proto3 to C# Type Mapping
| Proto3 Type | C# Type | Notes |
|---|---|---|
string | string | UTF-8 encoded |
int32 | int | 32-bit signed integer |
bool | bool | Boolean value |
message | Class | Custom message type |
Default Values
In proto3, fields have default values when not set:string: empty string""int32:0bool:falsemessage:null
Backward Compatibility
Protocol Buffers are designed for evolution: Safe Changes:- Adding new fields (old clients ignore them)
- Adding new RPC methods
- Renaming fields (only field numbers matter)
- Changing field numbers
- Changing field types
- Removing required fields
- Removing or renaming RPC methods
Service Contract Benefits
Type Safety
- Compile-time type checking
- No runtime serialization errors
- IntelliSense support in IDEs
Performance
- Binary serialization (smaller than JSON)
- Fast encoding/decoding
- Efficient network usage
Cross-Language Support
- Same proto file works for C#, Java, Python, Go, etc.
- Consistent API across services
- Easy polyglot microservices
Documentation
- Proto file serves as API contract
- Self-documenting service definition
- Easy to understand service capabilities
Testing Proto Messages
Unit Test Example
Advanced Proto Features
Repeated Fields (Arrays)
If you needed to return multiple coupons:Enums
Timestamps
Resources
Next Steps
- gRPC Service Implementation - How the service implements this proto
- Overview - Service architecture and integration
