You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C#跨项目关联Protobuf文件失败:编译报找不到文件错误求助

跨项目Protobuf文件引用编译错误解决方案

1. 调整项目A的csproj配置

项目A需正确关联项目B的Protobuf文件,并指定编译器的搜索目录。打开项目A的.csproj文件,替换原有Protobuf相关配置:

<ItemGroup>
  <!-- 项目A自身的Protobuf服务定义文件 -->
  <Protobuf Include="Protos\User.Proto" GrpcServices="Server" />
  <!-- 引用项目B的Common.proto,设置文件链接与编译器搜索目录 -->
  <Protobuf Include="..\..\Shared\Protos\Common.Proto" Link="Protos\Common.Proto" GrpcServices="None" AdditionalImportDirs="..\..\Shared\Protos" />
</ItemGroup>

AdditionalImportDirs是核心配置,需填写从项目A根目录到项目B Protos文件夹的相对路径,确保编译器能定位到Common.proto。

2. 修正User.Proto的导入语句

在User.Proto中,直接使用相对于AdditionalImportDirs的路径导入,无需嵌套项目路径:

import "Common.proto";

3. 确认项目B的csproj配置

项目B的.csproj需正确标记Protobuf文件,确保可被其他项目引用:

<ItemGroup>
  <Protobuf Include="Protos\Common.Proto" GrpcServices="None" />
</ItemGroup>

GrpcServices="None"是因为Common.proto仅定义消息,无需生成Grpc服务代码。

4. 验证编译

  • 清理解决方案后重建,确保路径无拼写错误
  • 注意文件名大小写统一(建议统一使用小写.proto后缀,避免编译器匹配问题)

内容的提问来源于stack exchange,提问作者Kevindt12

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.01 12:01:10