在VS 2017的预建事件中使用protoc从.proto文件生成C#文件

问题描述:

我尝试在Visual Studio 2017中针对所有.proto文件的预构建事件中使用 google.protobuf.tools nuget包中的 protoc 从.proto文件中生成c#文件特别是文件夹.我用.proto文件创建了.net核心库项目.

I tried to generate c# files from .proto files using protoc from google.protobuf.tools nuget package on pre build event in Visual Studio 2017 for all .proto files in particular folder. I created .net core library project with .proto files.

为此的预构建事件命令行

Pre-build event command line for for that

protoc -I=$(ProjectDir)Messages --csharp_out=$(ProjectDir)Messages $(ProjectDir)*.proto

其中 protoc protoc.exe

但是我遇到了错误

 error MSB3073: The command "protoc -I=C:\Projects\TecAlliance.Core.Messages\TecAlliance.Core.Messages\Messages --csharp_out=C:\Projects\TecAlliance.Core.Messages\TecAlliance.Core.Messages\Messages C:\Projects\TecAlliance.Core.Messages\TecAlliance.Core.Messages\GpsCoodinates.proto" exited with code 9009.
1>Done building project "TecAlliance.Core.Messages.csproj" -- FAILED.

我该如何解决?

这些链接很有帮助:

NugetPackageRoot

MSBuild预构建

我要使用的Pre-build命令:

The Pre-build command I got to work:

$(NugetPackageRoot)google.protobuf.tools\3.13.0\tools\windows_x64\protoc.exe -I=$(ProjectDir)protos --csharp_out=$(ProjectDir)Messages $(ProjectDir)protos\*.proto

我没有手动安装协议.此命令使用的是Nuget软件包google.protobuf.tools随附的命令.

I did not manually install protoc. This command is using the one that came with the Nuget package google.protobuf.tools.

我的项目结构如下:

-项目目录

----消息目录

------ myMessages.cs

------myMessages.cs

---- protos目录

----protos directory

------ myMessages.proto

------myMessages.proto

要回答特定问题:

我收到了同样的错误,这意味着它找不到协议".通过使用上述构建命令,它将能够找到protoc exe.一个明显的缺点是,如果更新protobuf软件包,则还需要更新MSBuild命令中的版本.也许有人知道一种自动化的方法?

I received the same error and it means that it can't find "protoc". By using a build command like above, then it will be able to find the protoc exe. An obvious downside is that if you update the protobuf packages then you will also need to update the version in MSBuild command. Maybe someone knows a way to automate that?