--- lang: ja-jp breaks: true --- # gRPC MagicOnion を使用する 2021-09-14 > Cysharp/MagicOnion > https://github.com/Cysharp/MagicOnion > MagicOnion Deployment Guide > https://cysharp.github.io/MagicOnion/articles/deployment/guide.html ## サーバ側プロジェクトの作成 Visual Studio または .NET CLI ツール内からgRPC サービスプロジェクトを作成する必要があります。MagicOnion サーバーは、ASP.NET コアと gRPC の上に構築されているので、サーバー プロジェクトは ASP.NET コア プロジェクトである必要があります。 `Protos` と `Services` フォルダおよび中身は不要なので削除する。 ### NuGet パッケージのインストール ```shell= Install-Package MagicOnion.Server ``` ### Startup.cs ```csharp= public void ConfigureServices(IServiceCollection services) { services.AddGrpc(); services.AddMagicOnion(); } ``` ```csharp= public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseRouting(); app.UseEndpoints(endpoints => { endpoints.MapMagicOnionService(); endpoints.MapGet("/", async context => { await context.Response.WriteAsync("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"); }); }); } ``` ### インターフェイス ```csharp= namespace Grpc_Interface { // Defines .NET interface as a Server/Client IDL. // The interface is shared between server and client. public interface IMyFirstService : IService<IMyFirstService> { // The return type must be `UnaryResult<T>`. UnaryResult<int> SumAsync(int x, int y); } } ``` ### サービス実装 ```csharp= namespace Grpc_Service { // Implements RPC service in the server project. // The implementation class must inherit `ServiceBase<IMyFirstService>` and `IMyFirstService` public class MyFirstService : ServiceBase<IMyFirstService>, IMyFirstService { IOptions<MyFirstService> config; ILogger<MyFirstService> logger; public MyFirstService(IOptions<MyFirstService> config, ILogger<MyFirstService> logger) { Console.WriteLine("[" + Thread.CurrentThread.ManagedThreadId + "] MyFirstService"); this.config = config; this.logger = logger; } // `UnaryResult<T>` allows the method to be treated as `async` method. public async UnaryResult<int> SumAsync(int x, int y) { Console.WriteLine($"Received:{x}, {y}"); return x + y; } } } ``` ## クライアント側プロジェクトの作成 ### コンソールアプリケーションのプロジェクトを作成 ### NuGet パッケージのインストール ```csharp= Install-Package MagicOnion.Client ``` ### Program.cs ```csharp= class Program { static async Task Main(string[] args) { Console.WriteLine("Hello World!"); // Connect to the server using gRPC channel. var channel = GrpcChannel.ForAddress("https://localhost:5001"); // NOTE: If your project targets non-.NET Standard 2.1, use `Grpc.Core.Channel` class instead. // var channel = new Channel("localhost", 5001, new SslCredentials()); // Create a proxy to call the server transparently. var client = MagicOnionClient.Create<IMyFirstService>(channel); // Call the server-side method using the proxy. var result = await client.SumAsync(123, 456); Console.WriteLine($"Result: {result}"); } } ``` ###### tags: `MagicOnion` `gRPC` `ASP.NET Core`