Exemplo n.º 1
0
        public static void Run()
        {
            Server server = new Server();

            server.Ports.Add("127.0.0.1", 50001, ServerCredentials.Insecure);
            server.Services.Add(StatsService.BindService(new ServerImpl()));
            server.Start();

            Thread.Sleep(1000);

            var client = new StatsService.StatsServiceClient(new Channel("127.0.0.1", 50001, ChannelCredentials.Insecure));

            // Three different ways of computing the average of [1,2,3,4]. We're pretty sure it's 2.5 at this point.

            // Unary operations take one request and send one response.
            // Our operation includes the whole array of data.
            UnaryOperation(client).GetAwaiter().GetResult();

            // Client streaming operations let the client send many requests and the server sends one response.
            // The client streams the individual numbers it wants averaged, and the server sends the aggregated response at the end.
            ClientStreamingOperation(client).GetAwaiter().GetResult();

            // Duplex streaming operations let the client send a stream of requests and the server send a stream of responses.
            // We send the cumulative average after each request in this example.
            DuplexStreamingOperation(client).GetAwaiter().GetResult();

            // Server streaming operations are the opposite of client streaming. One client request results in a stream of server responses.
            // However, this is left as an exercise to the reader.

            Thread.Sleep(1000);

            server.ShutdownAsync().GetAwaiter().GetResult();
        }