dotnet-cqrs/docs/tutorials/ecommerce-example/08-grpc-api.md

1.1 KiB

E-Commerce Example: gRPC API

Expose commands and queries via gRPC services.

Proto File Definition

Create Protos/ecommerce.proto:

syntax = "proto3";

package ecommerce;

service OrderService {
  rpc PlaceOrder(PlaceOrderCommand) returns (PlaceOrderResponse);
  rpc CancelOrder(CancelOrderCommand) returns (google.protobuf.Empty);
  rpc GetOrder(GetOrderQuery) returns (OrderDto);
  rpc ListOrders(ListOrdersQuery) returns (OrderListResponse);
}

message PlaceOrderCommand {
  string customer_id = 1;
  repeated OrderLineDto lines = 2;
  string shipping_address = 3;
}

message PlaceOrderResponse {
  string order_id = 1;
}

message OrderDto {
  string order_id = 1;
  string customer_id = 2;
  repeated OrderLineDto lines = 3;
  double total_amount = 4;
  string status = 5;
  int64 placed_at = 6;
}

gRPC Setup

builder.Services.AddGrpc();

var app = builder.Build();

app.MapGrpcService<OrderServiceImpl>();
app.MapGrpcReflectionService();

app.Run();

See Also