Skip to content

Latest commit

 

History

History
80 lines (63 loc) · 3.09 KB

README.md

File metadata and controls

80 lines (63 loc) · 3.09 KB

Net Core Protocol Buffers

This repository

This repository shows how to define basic Protocol Buffer (*.proto) file, generate source code for serialising and deserializing data messages. The process of working with Protocol Buffer is as follow: define proto file, generate source file native to your language using protocol buffers compiler (protoc), use in code.

Getting started

  1. Download repository
  2. Download .Net SDK (in the moment of writing 3.1.101)
  3. Open in VS 2019
  4. Setup startup project & start debugging

Tools

Generate code from proto files (command line)

  1. Install the protoc compiler,
  1. (Optional) Install additional Google types,
  • Open the include folder located in the package
  • Move the google folder to a folder where *.proto files are defined
  1. Open powershell
  2. Navigate to .\net-core-protobuf\NetCoreProtobuf.Proto\ folder
  3. Execute command
protoc --proto_path=. --csharp_out=.\Generated .\Definitions\address_book.proto
protoc --proto_path=. --csharp_out=.\Generated .\Definitions\person.proto

NOTE: in order to generate code for different language replace --csharp_out argument eg. --java_out NOTE: this example already has included generated files into repository tree, search for result in Generated folder

  • (optional) change parameter --proto_path=.\path\to\imported\proto\files (default's to current directory)

Generate code from proto files (Configuration for VS Code)

Install vscode-proto3 extension in order to get first class experience when working with *.proto files.

Features:

  • code snippets,
  • code completion,
  • vscode commands for compiling files,
  • on-save compilation for *.proto files,

Steps:

  1. Install the protoc compiler if not installed already
  2. Install vscode-proto3 via VS Code Extensions Marketplace
  3. Restart VS Code
  4. Create a folder named .vscode in the main repository folder
  5. Create a configuration file named settings.json inside the newly created folder
{
    "protoc": {
        "compile_on_save": true,
        "options": [
            "--proto_path=.",
            "--csharp_out=out"
        ]
    }
}

NOTE: adjust directories for your setup

  1. Open and save any file with the *.proto extension. The result should appear in the folder named out. In case of compilation errors a popup notification will be displayed.

Sources