gRPC-Webってのをやってみる。
サーバー
普通にdotnet new grpc
Grpc.AspNetCore.Web パッケージへの参照を追加、、、するらしい
dotnet add package Grpc.AspNetCore.Web
Program.csを編集("<--"って部分を見てちょ)
- using grpcweb_srv.Services;
-
- var builder = WebApplication.CreateBuilder(args);
-
- // Add services to the container.
- builder.Services.AddGrpc();
-
- var app = builder.Build();
-
- app.UseGrpcWeb();//<-- added
- // Configure the HTTP request pipeline.
- //app.MapGrpcService<GreeterService>(); //<-- commented out
- app.MapGrpcService<GreeterService>().EnableGrpcWeb(); //<-- added
- 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");
-
- 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を編集
- syntax = "proto3";
-
- option csharp_namespace = "grpcweb_cli";//<--changed _srv to _cli
-
- package greet;
-
- // The greeting service definition.
- service Greeter {
- // Sends a greeting
- rpc SayHello (HelloRequest) returns (HelloReply);
- }
-
- // The request message containing the user's name.
- message HelloRequest {
- string name = 1;
- }
-
- // The response message containing the greetings.
- message HelloReply {
- string message = 1;
- }
grpcweb-cli.csprojを編集、、、
- <ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>
を追加する
- <Project Sdk="Microsoft.NET.Sdk">
-
- <PropertyGroup>
- <OutputType>Exe</OutputType>
- <TargetFramework>net8.0</TargetFramework>
- <RootNamespace>grpcweb_cli</RootNamespace>
- <ImplicitUsings>enable</ImplicitUsings>
- <Nullable>enable</Nullable>
- </PropertyGroup>
-
- <ItemGroup>
- <PackageReference Include="Google.Protobuf" Version="3.32.1" />
- <PackageReference Include="Grpc.Net.Client.Web" Version="2.71.0" />
- <PackageReference Include="Grpc.Net.ClientFactory" Version="2.71.0" />
- <PackageReference Include="Grpc.Tools" Version="2.72.0">
- <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
- <PrivateAssets>all</PrivateAssets>
- </PackageReference>
- </ItemGroup>
-
- <ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>
-
- </Project>
Program.csを編集
- using Google.Protobuf;
- using Grpc.Net.Client;
- using Grpc.Net.Client.Web;
- using grpcweb_cli;
-
- var channel = GrpcChannel.ForAddress("http://localhost:5056", new GrpcChannelOptions
- {
- HttpHandler = new GrpcWebHandler(new HttpClientHandler())
- });
-
- var client = new Greeter.GreeterClient(channel);
- var response = await client.SayHelloAsync(
- new HelloRequest { Name = "GreeterClient" });
- Console.WriteLine("Greeting: " + response.Message);
- Console.WriteLine("Press any key to exit...");
- Console.ReadKey();
(ポート番号はサーバーを起動して確認する<--固定化する方法をいつか調べないとな)以前作ったのと比較すると、
using Grpc.Net.Client.Web;
を追加
channelインスタンス生成がなんか複雑になった
、、、そのほかは同じかな
で、dotnet buildからのdotnet run
(サーバーは起動しておこう)
となって、うまくいったがね。
で、HTMLに埋め込んだJavaScriptでクライアントをやってみるってのに進んでいくわけだけど、うまくいくかどうかはまだ不明;
Geminiに奪われた時間を無理やり取り戻してやったぜ
0 件のコメント:
コメントを投稿