liveBook · Manning

Protocol buffer

As explained in Building Web APIs with ASP.NET Core

Protocol Buffers is a language-neutral serialization format and interface-definition system for structured messages and service contracts.

Protocol Buffers (protobuf) is a language-neutral format for serializing structured messages and defining service interfaces. In a gRPC board-game API, a .proto file becomes the shared contract between client and server, replacing JSON with compact binary messages. section 10.3 section 10.3.4

A practical ASP.NET Core implementation follows this sequence:

  • Create a /gRPC/ directory and add grpc.proto using proto3 syntax. section 10.3.4
  • Describe services, methods, request messages, and response messages in the protobuf contract. section 10.3.4
  • Reference the file in the project file so the .NET compiler generates client and server base classes. section 10.3.4
  • Extend the generated server base class and implement methods against application data, such as EF Core. section 10.3.4
  • Register gRPC services and map the implementation into the ASP.NET Core HTTP pipeline. section 10.3.4

Board-game lookup contract

A grpc.proto contract can declare a Grpc service with a GetBoardGame method, accepting BoardGameRequest and returning BoardGameResponse. The request carries an integer id; the response carries id, name, and year fields. These protobuf messages serve a role similar to C# DTOs while generating strongly typed service accessors. section 10.3.4

Listing 10.7 /gRPC/grpc.proto file
syntax = "proto3";
 
option csharp_namespace = "MyBGList.gRPC";
 
package Grpc;
 
service Grpc {
  rpc GetBoardGame (BoardGameRequest) returns (BoardGameResponse);   #1
}
 
message BoardGameRequest {                                           #2
  int32 id = 1;
}
 
message BoardGameResponse {                                          #3
  int32 id = 1;
  string name = 2;
  int32 year = 3;
}

Inspect the complete grpc.proto example to see proto3 syntax, the service declaration, RPC method, message types, and field numbering together. section 10.3.4

How Building Web APIs with ASP.NET Core covers it

How deep it goes
Defines proto3 services and messages, then defers detailed proto3 syntax.
Examples
Uses grpc.proto with GetBoardGame, BoardGameRequest, and BoardGameResponse.
What you can do after
Build it from scratch by defining a proto3 contract and wiring generated gRPC server types into ASP.NET Core.
How it connects
Connects gRPC with HTTP/2, generated stubs, DTOs, and EF Core-backed services.

Also explained in

Compare how 10 books explain protocol buffer →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Building Web APIs with ASP.NET Core. The section links point to the parts of the book it draws on.