简体   繁体   English

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

[英]Generating c# files from .proto files using protoc on pre build event in VS 2017

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.我尝试使用来自protoc nuget package 的 protoc 从google.protobuf.tools文件生成 c# 文件。 I created .net core library project with.proto files.我用 .proto 文件创建了 .net 核心库项目。

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其中protocprotoc.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?我该如何解决?

These links were helpful:这些链接很有帮助:

NugetPackageRoot NugetPackageRoot

MSBuild Prebuild MSBuild 预构建

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.我没有手动安装protoc。 This command is using the one that came with the Nuget package google.protobuf.tools.此命令使用 Nuget package google.protobuf.tools 附带的命令。

My project structure is as follows:我的项目结构如下:

-Project directory -项目目录

----Messages directory ----留言目录

------myMessages.cs ------myMessages.cs

----protos directory ----protos目录

------myMessages.proto ------myMessages.proto

To answer the specific question:要回答具体问题:

I received the same error and it means that it can't find "protoc".我收到了同样的错误,这意味着它找不到“protoc”。 By using a build command like above, then it will be able to find the protoc exe.通过使用上面的构建命令,它将能够找到 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.一个明显的缺点是,如果您更新 protobuf 包,那么您还需要在 MSBuild 命令中更新版本。 Maybe someone knows a way to automate that?也许有人知道一种自动化的方法?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在VS 2017中添加预构建命令以将文件复制到项目 - Adding pre-build commands to copy files to a project in VS 2017 Visual Studio C#不会生成因预生成事件而更改的文件 - visual studio c# does not build files that were change by pre-build event 使用 Grpc.Tools 和 Protoc 插件生成额外的 C# 文件 - Using Grpc.Tools with Protoc plug-in to generate additional C# files 是否可以使用 protobuf-net 为扩展名为“FieldOptions”的 proto2 文件生成 C# 文件? - Is it possible to generate C# files using protobuf-net for proto2 files with extension of 'FieldOptions'? 是否有本机方式或通过 VS Extension for VS 2017/2019 从 C# POCO 自动生成 typescript .d 文件 - Is there a native way or via VS Extension for VS 2017/2019 to auto generate typescript .d files from a C# POCO 被只能在VS2013上运行的LightSwitch项目占用时,使用C#7.0调试dll并从VS 2017构建 - Debug a dll using C# 7.0 and build from VS 2017 when consumed by a LightSwitch project that can only run on VS2013 C#nant使用.csproj文件构建 - C# nant build using .csproj files 从proto文件生成C#,反之亦然解释自定义选项 - Generate C# from proto files and vice versa interpreting custom options 使用Google Protobuf proto2文件生成C#代码 - C# code generation with Google Protobuf proto2 files 如何管理在VS C#Express中发布和构建的常用文件? - How to manage files common to release and build in VS C# Express?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM