5
Visual Studio 2015 
C#
NuGet Packages : 
Google.Protobuf v3.0.0 
Google.Protobuf.Tools v3.0.0

MessageType Quake

syntax = "proto3";
import "google/protobuf/timestamp.proto";
message Quake {
  google.protobuf.Timestamp _timestamp = 1;
  double magnitude = 2;
}

timestamp.proto included in same folder

protoc 3.0.2 command-line compilation succeeds

But VS right-click proto file and select "Run Custom Tool" fails with error "The custom tool 'ProtoBufTool' failed."

to generate the C# classes from within Visual Studio ?

BaltoStar
  • 8,165
  • 17
  • 59
  • 91

3 Answers3

4

The Protobuf documentation seems short (weak) when it explains how to execute protoc.exe. Here is how I do it in a small test project I am writing. I have installed TWO NuGet packages for my Protobuf project:

Google.Protobuf (C# runtime library for Protocol Buffers)
Google.Protobuf.Tools (protoc.exe)

In Visual Studio I have defined a Pre-build event command line. This is defined in the properties menu for the project:

..\..\..\packages\Google.Protobuf.Tools.3.6.0\tools\windows_x64\protoc -I="$(ProjectDir)."
    --csharp_out=$(ProjectDir)Model $(ProjectDir)MyOwn.proto

The resulting MyOwn.cs file lands in subdirectory "Model". It must be manually included in the project, to be automatically compiled. This is how I do it today. I hope this helps someone.

  • thanks. with little modifications I was able to generate message classes. ..\..\..\packages\Google.Protobuf.Tools.3.10.0\tools\windows_x64\protoc -I=$(ProjectDir) --csharp_out=$(ProjectDir) $(ProjectDir)/addressbook.proto – Asanka Indrajith Oct 06 '19 at 14:26
1

I was able to get it working with Visual Studio 2019 by doing

<ItemGroup>
  <PackageReference Include="Google.Protobuf" Version="3.10.1" />
  <PackageReference Include="Google.Protobuf.Tools" Version="3.10.1" GeneratePathProperty="true" />
</ItemGroup>

<Target Name="PreBuild" BeforeTargets="PreBuildEvent">
  <Exec Command="$(PkgGoogle_Protobuf_Tools)\tools\windows_x64\protoc -I=$(PkgGoogle_Protobuf_Tools)\tools -I=$(ProjectDir)resources\proto --csharp_out=$(ProjectDir) $(ProjectDir)resources\proto\*.proto" />
</Target>

GeneratePathProperty is a newer property that gives you the directory of a NuGet package. From there you can use PreBuild to build the protocol buffers using protoc and any of the Google Well Known Types.

EmDash
  • 389
  • 4
  • 4
-1

Import the Timestamp and use the full namespace:

syntax = "proto3";
import "google/protobuf/timestamp.proto";

message Quake {
  google.protobuf.Timestamp _timestamp = 1;
  double magnitude = 2;
}

To generate the C# code, see their manual: https://developers.google.com/protocol-buffers/docs/csharptutorial#compiling-your-protocol-buffers

Rick Chen
  • 181
  • 7
  • thanks Rick but my issue is running C# compiler within VS integraton via NuGet packages – BaltoStar Sep 20 '16 at 23:24
  • 7
    Did you ever solve this. I am using the Google.Protobuf and Google.Protobuf.Tools NuGet packages that are intstalled. But these packages do not include any .proto files. If I am using protoc from these packages, should I need access to the timestamp.proto file or will the compiler know about this? – Max Palmer Aug 04 '17 at 10:33
  • Using VS2019 this was the very actual info I needed – Andreas H. Feb 18 '21 at 12:14