iT邦幫忙

2025 iThome 鐵人賽

DAY 19
1
Software Development

30 天的 .Net gRPC 迷途系列 第 19

Day19 gRPC 進階屬性格式(跨專案)

  • 分享至 

  • xImage
  •  

當 gRPC 專案規模擴大、Server 與 Client 分屬不同團隊開發時,.proto 檔的共用管理就變得非常重要。在 .NET 生態中,最推薦且最常見的做法是「獨立到 .csproj 內,編譯出 NuGet 套件供跨團隊共用」

參考前一天的目錄結構

MyGrpc.Shares
├── Protos/
│   ├── common/              # 共用層級
│   │   └── common_types.proto
│   └── greet.proto          # 服務層級

並安裝以下最基本的套件

<ItemGroup>
    //...
    <!-- 以下三個套件按照需要的 .Net 版本安裝 -->
    <PackageReference Include="Google.Protobuf" Version="3.32.0" />
    <PackageReference Include="Grpc.Core" Version="2.46.6" />
    <PackageReference Include="Grpc.Tools" Version="2.72.0" />
</ItemGroup>

// 省略個別設定 GrpcServices
<ItemGroup>
	<!-- 設定所有 .proto 都要被處理 -->
	<Protobuf Include="Protos\*.proto" GrpcServices="Both" />
	<Protobuf Include="Protos\**\*.proto" GrpcServices="Both" />
</ItemGroup>

Server 端

GreeterService.cs 把新專案的命名空間引用進來即可

using MyGrpc.Shares;

上一篇
Day18 gRPC 進階屬性格式(跨檔案)
下一篇
Day20 gRPC-Web 的使用
系列文
30 天的 .Net gRPC 迷途20
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言