【发布时间】:2021-08-13 08:34:26
【问题描述】:
我有两个项目:
- 项目托管 gRPC 服务器的代码以及 .proto 文件。
- 项目充当客户端,但还具有许多其他不同的功能
我希望能够清理和构建我的客户端项目,这取决于自动生成的 .cs 文件。使用 Google.Protobuf 和 Grpc.Net.ClientFactory 从 .proto 文件生成 .cs 文件。
到目前为止,我已经完成了: I added a service reference to my 2. project. 我的 2.project.csproj 看起来像(对于 Example.proto):
<ItemGroup Condition=" '$(TargetFramework)' == 'net48' ">
<PackageReference Include="Grpc.Core" Version="2.32.0" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'netcoreapp3.1'">
<PackageReference Include="Grpc.AspNetCore" Version="2.32.0" />
<PackageReference Include="Grpc.Net.ClientFactory" Version="2.32.0" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net5.0-windows'">
<PackageReference Include="Grpc.AspNetCore" Version="2.32.0" />
<PackageReference Include="Grpc.Net.ClientFactory" Version="2.32.0" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Google.Protobuf" Version="3.13.0" />
<PackageReference Include="Grpc.Tools" Version="2.32.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<Protobuf Include="..\Project_A\Protos\Example.proto" GrpcServices="Client">
<Link>Protos\Example.proto</Link>
</Protobuf>
</ItemGroup>
当我现在更改 .protofile 时,客户端 .cs 文件会在路径中生成:
...\2.project\obj\$(ConfigurationName)\$(FrameworkName)\
简单地构建第二个项目时这一切都很好。但是,每当第二个项目进行干净的重建时,.cs 文件就不会再次生成,这让我缺少依赖项。
我猜想在某个地方可以选择在构建开始之前触发 .proto 文件的生成? 或者我应该从清理中排除生成的文件吗? 还是有其他我没有看到的方法来实现这一点?
更新:
我现在将 proto 文件移动到一个单独的项目中,并使用以下语句导入它们:
<ItemGroup>
<Protobuf Include="..\Proto_Project\Protos\Example.proto" GrpcServices="Client" OutputDir="Protos" CompileOutputs="false">
<Link>Protos\Example.proto</Link>
</Protobuf>
</ItemGroup>
这种方式是这样工作的,每次我更改 proto 时,它都会在 .cs 中重新转换。 但这是我当前(以及以前,据我所知)问题的根源。每次我重建解决方案时,原型文件都会重新转换为 .cs。问题是这发生在项目编译之后。这会导致缺少依赖项,因为文件在编译时不存在。简单地构建解决方案时,不会重新翻译文件并且一切正常。
如何在重建时阻止 protobuf 编译器运行,或者让他提前运行?
【问题讨论】:
标签: c# visual-studio-2019 protocol-buffers grpc