LoginSignup
19
15

More than 5 years have passed since last update.

gRPC C#環境を作成する

Last updated at Posted at 2018-03-26

はじめに

gRPCのC#環境を作成するためのインストール手順です。
Goで作成する場合に比べて手間がかかるので公開いたしました。

作成手順

  1. C#プロジェクトを作成
  2. 各種GRPCのパッケージをインストール
  3. Protocol Buffers生成

GRPCのパッケージ

C#のプロジェクト上で使用するには下記のパッケージが必要です

  • Grpc.Core

    • Grpcを使用するためのコアライブラリ
  • Grpc.Tools

    • 各種ツールライブラリが格納されている
    • protocol Buffersのprotoファイル作成
    • パフォーマンステスト
  • Google.Cloud.Language

Grpc.CoreとGoogle.Cloud.Languageのインストール

Windows, Mac共に
Visual Studio上のNugetパッケージ管理で行うのが一番楽かと思います。

  • プロジェクトを選択してNugetパッケージを追加
    VisualSturio1.png

  • Grpcを選択してパッケージ追加
    VisualSturio2.png

  • ソリューションエクスプローラー上でパッケージ追加がされていることを確認
    VisualSturio4.png

  • Google.Cloud.Languageのインストール
    GoogleCloundLanguage.png

  • (注意)Grpc.Toolsはここではインストールしない
    caution.png

Grpc.Toolsのインストール

結論から申し上げますと
Grpc.ToolsもVisual Studio上のNugetパッケージ管理で落として来ることは可能ですが、
何故かProtocol Buffers、Grpc用各種クラスの生成ができません。(Window, Mac共に)
よって、下記の方法で取得・実行を行いました。

.protoファイル

user.proto
syntax = "proto3";

package proto.user;

service Users{
    rpc Get (Request) returns (Response) {}
}

message User {
    int32 id = 1;
    string name = 2;
}

message Request {
    int32 id = 1;
}

message Response {
    User user = 1;
}

Protocol Buffers生成(Macの場合)

  • HomeBrewを使用してインストール
brew tap grpc/grpc
brew install --with-plugins grpc
  • Protocol Buffers、Grpc用各種クラスの生成
    • HomeBrewで入れた/user/local/bin配下のprotocとgrpc_csharp_pluginを使用します
# --csharp_out: cshapスクリプト出力場所
# --grpc_out: grpc cshapスクリプト出力場所
# --plugin: protoc-gen-grpc指定をする

/usr/local/bin/protoc -I ./protos --csharp_out=./GrpcSample/Proto --grpc_out=./GrpcSample/Proto --plugin=protoc-gen-grpc=/usr/local/bin/grpc_csharp_plugin ./protos/user.proto

Protocol Buffers生成(Windowsの場合)

  • nuget.exeの取得
    こちらからnuget.exeをダウンロード

  • Grpc.Toolsを取得

nuget.exe install Grpc.Tools
  • Protocol Buffers、Grpc用各種クラスの生成 インストールしたGrpc.Tools配下のprotocとgrpc_csharp_pluginを使用します
# --csharp_out: cshapスクリプト出力場所
# --grpc_out: grpc cshapスクリプト出力場所
# --plugin: protoc-gen-grpc指定をする

C:\Users\xxxxx\Grpc.Tools.1.10.0\tools\windows_x64\protoc.exe -I protos --csharp_out GrpcSample\Proto --grpc_out GrpcSample\Proto --plugin=protoc-gen-grpc=C:\Users\xxxxx\Grpc.Tools.1.10.0\tools\windows_x64\grpc_csharp_plugin.exe protos\user.proto

生成されたクラス

Protoフォルダ配下に下記のクラスが生成されていることを確認

  • User.cs
    • メッセージで使用するUser, Request, Responseのクラス
  • UserGrpc.cs
    • メーッセージ送受信を行うUsers, UsersClientクラス
19
15
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
19
15