public static async Task Main(string[] args) { #if NETCOREAPP3_1 ServiceModel.Grpc.GrpcChannelExtensions.Http2UnencryptedSupport = true; #endif // 'grpc-internal-encoding-request' is a special metadata value that tells the client to compress the request. // This metadata is only used in the client is not sent as a header to the server. // The client sends 'Grpc-Encoding: gzip' header to the server var metadata = new Metadata { { "grpc-internal-encoding-request", "gzip" } }; var clientFactory = new ClientFactory(new ServiceModelGrpcClientOptions { // ask ServiceModel.Grpc to apply this metadata for any call from any client, created by this ClientFactory DefaultCallOptionsFactory = () => new CallOptions(metadata) }); using var channel = GrpcChannel.ForAddress("http://localhost:5000"); var client = clientFactory.CreateClient <IGreeterService>(channel); await UnaryCallExample(client); await ServerStreamingCallExample(client); Console.WriteLine("Press any key to exit..."); Console.ReadLine(); }
private static async Task CallAsDemoUser(Channel channel) { // create token var authenticationToken = ResolveJwtToken("demo-user"); var clientFactory = new ClientFactory(new ServiceModelGrpcClientOptions { // setup authorization header for all calls, by all proxies created by this factory DefaultCallOptionsFactory = () => new CallOptions(new Metadata { { "Authorization", "Bearer " + authenticationToken } }) }); var proxy = clientFactory.CreateClient <IDemoService>(channel); Console.WriteLine("Invoke GetCurrentUserNameAsync"); var userName = await proxy.GetCurrentUserNameAsync(); Console.WriteLine(userName); Console.WriteLine("Invoke PingAsync"); var response = await proxy.PingAsync(); Console.WriteLine(response); }
public RemoteRepository(string host, int port) { var channel = new Channel(host, port, ChannelCredentials.Insecure); var proxy = ClientFactory.CreateClient <IQueryService>(channel); _dataProvider = async expression => await proxy.ExecuteQueryAsync(expression); }
public static async Task CallCalculator(Uri calculatorLocation, CancellationToken token) { var clientFactory = new ClientFactory(); using (var channel = GrpcChannel.ForAddress(calculatorLocation)) { var calculator = clientFactory.CreateClient <ICalculator>(channel); await CallSumAsync(calculator, token).ConfigureAwait(false); await CallDivideByAsync(calculator, token).ConfigureAwait(false); await CallMultiplyByAsync(calculator, token).ConfigureAwait(false); } }
public static async Task Main(string[] args) { var clientFactory = new ClientFactory(); using var channel = GrpcChannel.ForAddress("http://localhost:5000", new GrpcChannelOptions()); var invoker = channel.Intercept(new ClientLoggerInterceptor()); var client = clientFactory.CreateClient <IGreeterService>(invoker); await UnaryCallExample(client); await ServerStreamingCallExample(client); Console.WriteLine("Press any key to exit..."); Console.ReadLine(); }
public static async Task Main(string[] args) { ServiceModel.Grpc.GrpcChannelExtensions.Http2UnencryptedSupport = true; var channel = new Channel("localhost", 5001, ChannelCredentials.Insecure); var clientFactory = new ClientFactory(); var figureServiceProxy = clientFactory.CreateClient <IFigureService>(channel); var rectangle = figureServiceProxy.CreateRectangle(new Point(0, 0), 10, 20); var triangle = await figureServiceProxy.CreateTriangle(new Point(0, 0), new Point(2, 2), new Point(2, 0)); // NotSupportedException: Point is not a 2d figure. try { await figureServiceProxy.CreatePoint(1, 1); } catch (RpcException ex) when(ex.StatusCode == StatusCode.Unknown) { Console.WriteLine(ex.Status.Detail); } Console.WriteLine("Rectangle area: {0}", figureServiceProxy.CalculateArea(rectangle)); Console.WriteLine("Triangle area: {0}", figureServiceProxy.CalculateArea(triangle)); var randomFigures = figureServiceProxy.CreateRandomFigures(3, default); await foreach (var figure in randomFigures) { Console.WriteLine("Created random {0}", figure.GetType().Name); } var smallest = await figureServiceProxy.FindSmallestFigure(AsAsyncEnumerable(rectangle, triangle)); Console.WriteLine("Smallest one is {0}", smallest.GetType().Name); var areas = figureServiceProxy.CalculateAreas(AsAsyncEnumerable(rectangle, triangle)); await foreach (var area in areas) { Console.WriteLine("Area is {0}", area); } Console.WriteLine("..."); Console.ReadLine(); }
public static async Task Run(int serverPort) { var clientFactory = new ClientFactory(new ServiceModelGrpcClientOptions { // set JsonMarshallerFactory as default Marshaller MarshallerFactory = JsonMarshallerFactory.Default }); var channel = new Channel("localhost", serverPort, ChannelCredentials.Insecure); var personService = clientFactory.CreateClient <IPersonService>(channel); Console.WriteLine("Invoke CreatePerson"); var person = await personService.CreatePerson("John X", DateTime.Today.AddYears(-20)); Console.WriteLine(" Name: {0}", person.Name); Console.WriteLine(" BirthDay: {0}", person.BirthDay); }