问题
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
where protoc
is environment variable for precompiled version of protoc.exe
But I got error
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.
How can I resolve it?
回答1:
These links were helpful:
NugetPackageRoot
MSBuild Prebuild
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
I did not manually install protoc. This command is using the one that came with the Nuget package google.protobuf.tools.
My project structure is as follows:
-Project directory
----Messages directory
------myMessages.cs
----protos directory
------myMessages.proto
To answer the specific question:
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?
来源:https://stackoverflow.com/questions/58221315/generating-c-sharp-files-from-proto-files-using-protoc-on-pre-build-event-in-vs