Exemplo n.º 1
0
        static async Task Main(string[] args)
        {
            using var channel = GrpcChannel.ForAddress("https://localhost:5001");
            var client = new Greeter.GreeterClient(channel);

            using var call = client.SayHelloStream();

            var readTask = Task.Run(async() =>
            {
                await foreach (var responce in call.ResponseStream.ReadAllAsync())
                {
                    Console.WriteLine(responce.Message);
                }
            });

            while (true)
            {
                var result = Console.ReadLine();
                if (string.IsNullOrEmpty(result))
                {
                    break;
                }

                await call.RequestStream.WriteAsync(new HelloRequest { Name = result });
            }

            await call.RequestStream.CompleteAsync();

            await readTask;

            Console.ReadKey();
        }
Exemplo n.º 2
0
        public static void Main(string[] args)
        {
            Thread.Sleep(2000); //just wait for server to start

            var server = "127.0.0.1:50051";

            if (args.Length > 0)
            {
                server = args[0];
            }

            Channel channel = new Channel(server, ChannelCredentials.Insecure);

            var client = new Greeter.GreeterClient(channel);

            Console.WriteLine("Executing synchronously, 10 000 calls");
            var sw = Stopwatch.StartNew();

            for (int i = 0; i < 10000; i++)
            {
                client.SayHello(new HelloRequest {
                    Name = "Roger"
                });
            }
            sw.Stop();
            Console.WriteLine(sw.Elapsed);

            Console.WriteLine("Executing asynchronously, 50 000 calls");
            var l = new List <Task>();

            sw = Stopwatch.StartNew();
            for (int i = 0; i < 50000; i++)
            {
                var t = Task.Run(async() => await client.SayHelloAsync(new HelloRequest {
                    Name = "Roger"
                }));
                l.Add(t);
            }

            Task.WaitAll(l.ToArray());
            sw.Stop();
            Console.WriteLine(sw.Elapsed);


            Console.WriteLine("Executing stream, 50 000 calls");
            sw = Stopwatch.StartNew();
            var x     = client.SayHelloStream();
            var batch = Enumerable.Repeat(new HelloRequest {
                Name = "Roger"
            }, 50000);

            x.RequestStream.WriteAllAsync(batch).Wait();
            x.ResponseAsync.Wait();
            sw.Stop();
            Console.WriteLine(sw.Elapsed);

            channel.ShutdownAsync().Wait();
            Console.WriteLine("Done.");
            //Console.ReadKey();
        }
Exemplo n.º 3
0
        private static async Task StreamCall(Greeter.GreeterClient client)
        {
            using var call = client.SayHelloStream();

            var readTask = Task.Run(async() =>
            {
                await foreach (var response in call.ResponseStream.ReadAllAsync())
                {
                    Console.WriteLine(response.Message);
                    Console.WriteLine();
                }
            });

            while (true)
            {
                var result = Console.ReadLine();
                if (string.IsNullOrWhiteSpace(result))
                {
                    break;
                }

                await call.RequestStream.WriteAsync(new HelloRequest()
                {
                    Name = result
                });
            }

            await call.RequestStream.CompleteAsync();

            await readTask;
        }
Exemplo n.º 4
0
        static async Task Main(string[] args)
        {
            // A channel represents a connection from the client to the server
            var channel = GrpcChannel.ForAddress("http://localhost:5000");
            var client  = new Greeter.GreeterClient(channel);

            /*
             * var response = await client.SayHelloAsync(new HelloRequest
             * {
             *  Name = "Niam Bisa"
             * });
             *
             * Console.WriteLine($"From server: {response.Message}");*/

            var call = client.SayHelloStream(new HelloRequest
            {
                Name = "Niam Bisa"
            });

            await foreach (var item in call.ResponseStream.ReadAllAsync())
            {
                Console.WriteLine($"From server: {item.Message}");
            }
        }
Exemplo n.º 5
0
        static async Task run1()
        {
            ////http1方式要加下面这个段,不然有异常
            //AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);

            var channel     = GrpcChannel.ForAddress("http://localhost:50051");
            var helloClient = new Greeter.GreeterClient(channel);

            Console.WriteLine("一元调用开始-------");
            //一元调用(同步方法)
            Console.WriteLine("发送:一元同步调用");
            var reply = helloClient.SayHello(new HelloRequest {
                Name = "一元同步调用"
            });

            Console.WriteLine($"{reply.Message}");
            //一元调用(异步方法)
            Console.WriteLine("发送:一元异步调用");
            var reply2 = helloClient.SayHelloAsync(new HelloRequest {
                Name = "一元异步调用"
            }).GetAwaiter().GetResult();

            Console.WriteLine($"{reply2.Message}");
            Console.WriteLine("一元调用结束-------");
            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine("服务端流开始-------");
            //服务端流
            Console.WriteLine("发送:服务端流");
            var reply3 = helloClient.SayHelloServerStream(new HelloRequest {
                Name = "服务端流"
            });

            while (await reply3.ResponseStream.MoveNext())
            {
                Console.WriteLine(reply3.ResponseStream.Current.Message);
            }
            Console.WriteLine("服务端流结束-------");
            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine("客户端流开始-------");
            //客户端流
            using (var call = helloClient.SayHelloClientStream())
            {
                for (var i = 0; i < 3; i++)
                {
                    Console.WriteLine("发送:" + "客户端流" + i.ToString());
                    await call.RequestStream.WriteAsync(new HelloRequest { Name = "客户端流" + i.ToString() });
                }
                await call.RequestStream.CompleteAsync();

                var reply4 = await call;
                Console.WriteLine($"{reply4.Message}");
            }
            Console.WriteLine("客户端流结束-------");
            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine("双向流开始-------");
            //双向流
            using (var call = helloClient.SayHelloStream())
            {
                //Console.WriteLine("启动后台任务以接收消息:输入quit退出");
                //接收
                var readTask = Task.Run(async() =>
                {
                    await foreach (var response in call.ResponseStream.ReadAllAsync())
                    {
                        Console.WriteLine(response.Message);
                    }
                });

                ////连续发送
                //Console.WriteLine("启动后台任务以接收消息");
                //for (var i = 0; i < 3; i++)
                //{
                //    Thread.Sleep(1000);//等待1秒,等服务器回复
                //    Console.WriteLine("发送:" + "双向流" + i.ToString());
                //    await call.RequestStream.WriteAsync(new HelloRequest { Name = "双向流" + i.ToString() });
                //}

                //手动输入发送
                Console.WriteLine("启动后台任务以接收消息:输入quit退出");
                while (true)
                {
                    string input = Console.ReadLine();
                    if (input == "quit")
                    {
                        break;
                    }
                    await call.RequestStream.WriteAsync(new HelloRequest { Name = input });
                }

                await call.RequestStream.CompleteAsync();

                await readTask;
            }
            Console.WriteLine("双向流结束-------");



            Console.ReadKey();
        }