Skip to content

Instantly share code, notes, and snippets.

@guitarrapc
Created November 9, 2020 07:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save guitarrapc/0ebd76e92b33d4678ae0348da9d015f3 to your computer and use it in GitHub Desktop.
Save guitarrapc/0ebd76e92b33d4678ae0348da9d015f3 to your computer and use it in GitHub Desktop.
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="MagicOnion" Version="4.0.0-preview.1" />
<PackageReference Include="MagicOnion.Abstractions" Version="4.0.0-preview.1" />
</ItemGroup>
</Project>
using ConsoleAppFramework;
using EchoGrpcMagicOnion.Shared;
using Grpc.Core;
using Grpc.Net.Client;
using MagicOnion.Client;
using Microsoft.Extensions.Hosting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace EchoGrpcMagicOnion.Client
{
class Program : ConsoleAppBase, IMyHubReceiver
{
static async Task Main(string[] args)
{
await Host.CreateDefaultBuilder()
.RunConsoleAppFrameworkAsync<Program>(args);
}
[Command("Echo")]
public async Task Echo(string hostPort, string message, [Option("H")]string headers, [Option("v")]bool verbose = false)
{
// under .NET Core 3.x: https://docs.microsoft.com/ja-jp/aspnet/core/grpc/troubleshoot?view=aspnetcore-3.1#call-insecure-grpc-services-with-net-core-client
AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
await Task.Delay(TimeSpan.FromSeconds(3));
var channel = GrpcChannel.ForAddress(hostPort);
var client = MagicOnionClient.Create<IEchoService>(channel, new IClientFilter[]
{
new AppendHeaderFilter(headers),
new ResponseHandlingFilter(verbose),
});
try
{
var res = await client.EchoAsync(message);
Console.WriteLine(res);
}
catch (Exception ex)
{
Console.WriteLine($"{ex.Message} {ex.StackTrace}");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment