Skip to content

Creating Microservice with grpc

🎬 Creating Microservice via grpc - Discount

The Shopping Basket Service will be responsible for managing the list of events that users can view and search.

🧱 1. Responsibilities of EventCatalogService

Feature Description

⚙️ 2. Tech Stack

Layer Tech
Language C# (.NET 9)
Framework ASP.NET Core Web API
DB SQL Server
API Docs Swagger /Scalar / http / OpenAPI
Container Docker
ORM EF Core

🛠️ 3. Core Models (Example)

public class Coupon
{
    public Guid CouponId { get; set; }
    public string Code { get; set; }
    public int Amount { get; set; }
    public bool AlreadyUsed { get; set; }
}

🔗 4. API Endpoints (OpenAPI Spec)

Method Endpoint Description

BasketLines

GET /api/baskets/{basketId}/basketlines POST /api/baskets/{basketId}/basketlines GET /api/baskets/{basketId}/basketlines/{basketLineId} PUT /api/baskets/{basketId}/basketlines/{basketLineId} DELETE /api/baskets/{basketId}/basketlines/{basketLineId}

Baskets

GET /api/baskets/{basketId} POST /api/baskets

🛠️ Creating the Discount Solution

🧱 2. Create the Discount Web API Project

dotnet new webapi -n EvenTicket.Services.Discount --use-controllers -o src/EvenTicket.Services.Discount
dotnet sln add src/EvenTicket.Services.Discount/EvenTicket.Services.Discount.csproj
dotnet sln list
  <ItemGroup>
    <Protobuf Include="..\discount.proto" GrpcServices="Server">
      <Link>Protos\discount.proto</Link>
    </Protobuf>
  </ItemGroup>
<ItemGroup>
<Protobuf Include="..\discount.proto" GrpcServices="Client">
    <Link>Protos\discount.proto</Link>
</Protobuf>
</ItemGroup>

postman collection https://universal-sunset-705171.postman.co/workspace/Personal~c89459ae-d13f-4ef4-8063-e204380a69bc/collection/686a750e692ca2c981fdb4dd?action=share&creator=11002687

gRPC gRPC (Google Remote Procedure Call) is a high-performance RPC framework developed by Google. It uses Protocol Buffers (Protobuf) for data serialization and supports multiple programming languages. The lowercase “g” signifies that gRPC was developed by Google. It reflects the project’s origins within Google’s infrastructure. RPC stands for Remote Procedure Call, which is a protocol that one program can use to request a service from a program located on another computer in a network. gRPC enables the execution of a function (or procedure) on a remote server, allowing for communication between different systems and services in a network.

alt text

service Discounts {
    rpc GetCoupon (GetCouponByIdRequest) returns (GetCouponByIdResponse) {}
}

message GetCouponByIdRequest {
    string CouponId = 1;
}

message GetCouponByIdResponse {
    Coupon coupon = 1;
}

message Coupon {
    string CouponId = 1;
    string Code = 2;
    int32 Amount = 3;
    bool AlreadyUsed = 4;
}

Service functionality

public class DiscountsService(IMapper mapper, ICouponRepository couponRepository) : Discounts.DiscountsBase
{
    public override async Task<GetCouponByIdResponse> GetCoupon(GetCouponByIdRequest request, ServerCallContext context)
    {
        var response = new GetCouponByIdResponse();

        // ... logic

        return response;
    }
}