2025年9月15日月曜日

gRPCやってみる(6)

 

gRPC-Webってのをやってみる。

気を取り直して、Microsoftの公式ページを参考にやってみる。

サーバー

普通に
dotnet new grpc
Grpc.AspNetCore.Web パッケージへの参照を追加、、、するらしい
dotnet add package Grpc.AspNetCore.Web
Program.csを編集("<--"って部分を見てちょ)
  1. using grpcweb_srv.Services;
  2.  
  3. var builder = WebApplication.CreateBuilder(args);
  4.  
  5. // Add services to the container.
  6. builder.Services.AddGrpc();
  7.  
  8. var app = builder.Build();
  9.  
  10. app.UseGrpcWeb();//<-- added
  11. // Configure the HTTP request pipeline.
  12. //app.MapGrpcService<GreeterService>(); //<-- commented out
  13. app.MapGrpcService<GreeterService>().EnableGrpcWeb(); //<-- added
  14. app.MapGet("/", () => "Communication with gRPC endpoints must be made through a gRPC client. To learn how to create a client, visit: https://go.microsoft.com/fwlink/?linkid=2086909");
  15.  
  16. app.Run();
これでサーバーはokらしいので、
dotnet build
dotnet run
してみる
まぁこんなかんじ

クライアント

普通に
dotnet new console
次のパッケージのプロジェクト ファイルにパッケージ参照を追加
Grpc.Net.Client.Web
Grpc.Net.ClientFactory
、、、らしい
dotnet add package Grpc.Net.Client.Web
dotnet add package Grpc.Net.ClientFactory
Protoを使う以上は
dotnet add package Google.Protobuf
は必要なんよね;
これも
dotnet add package Grpc.Tools
Protoファイルをサーバーのプロジェクトフォルダからcopy
mkdir Protos; cp ../grpcweb-srv/Protos/greet.proto Protos
Protos/greet.protoを編集
  1. syntax = "proto3";
  2.  
  3. option csharp_namespace = "grpcweb_cli";//<--changed _srv to _cli
  4.  
  5. package greet;
  6.  
  7. // The greeting service definition.
  8. service Greeter {
  9.   // Sends a greeting
  10.   rpc SayHello (HelloRequest) returns (HelloReply);
  11. }
  12.  
  13. // The request message containing the user's name.
  14. message HelloRequest {
  15.   string name = 1;
  16. }
  17.  
  18. // The response message containing the greetings.
  19. message HelloReply {
  20.   string message = 1;
  21. }
grpcweb-cli.csprojを編集、、、
  •   <ItemGroup>
  •     <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  •   </ItemGroup>
を追加する
  1. <Project Sdk="Microsoft.NET.Sdk">
  2.  
  3.   <PropertyGroup>
  4.     <OutputType>Exe</OutputType>
  5.     <TargetFramework>net8.0</TargetFramework>
  6.     <RootNamespace>grpcweb_cli</RootNamespace>
  7.     <ImplicitUsings>enable</ImplicitUsings>
  8.     <Nullable>enable</Nullable>
  9.   </PropertyGroup>
  10.  
  11.   <ItemGroup>
  12.     <PackageReference Include="Google.Protobuf" Version="3.32.1" />
  13.     <PackageReference Include="Grpc.Net.Client.Web" Version="2.71.0" />
  14.     <PackageReference Include="Grpc.Net.ClientFactory" Version="2.71.0" />
  15.     <PackageReference Include="Grpc.Tools" Version="2.72.0">
  16.       <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
  17.       <PrivateAssets>all</PrivateAssets>
  18.     </PackageReference>
  19.   </ItemGroup>
  20.  
  21.   <ItemGroup>
  22.     <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  23.   </ItemGroup>
  24.  
  25. </Project>
Program.csを編集
  1. using Google.Protobuf;
  2. using Grpc.Net.Client;
  3. using Grpc.Net.Client.Web;
  4. using grpcweb_cli;
  5.  
  6. var channel = GrpcChannel.ForAddress("http://localhost:5056", new GrpcChannelOptions
  7. {
  8.     HttpHandler = new GrpcWebHandler(new HttpClientHandler())
  9. });
  10.  
  11. var client = new Greeter.GreeterClient(channel);
  12. var response = await client.SayHelloAsync(
  13.                   new HelloRequest { Name = "GreeterClient" });
  14. Console.WriteLine("Greeting: " + response.Message);
  15. Console.WriteLine("Press any key to exit...");
  16. Console.ReadKey();
(ポート番号はサーバーを起動して確認する<--固定化する方法をいつか調べないとな)
以前作ったのと比較すると、
using Grpc.Net.Client.Web;
を追加
channelインスタンス生成がなんか複雑になった
、、、そのほかは同じかな
で、dotnet buildからのdotnet run
(サーバーは起動しておこう)
となって、うまくいったがね。
で、HTMLに埋め込んだJavaScriptでクライアントをやってみるってのに進んでいくわけだけど、うまくいくかどうかはまだ不明;
Geminiに奪われた時間を無理やり取り戻してやったぜ

0 件のコメント:

コメントを投稿