Exemple #1
0
        //客户端流
        public override async Task <HelloReply> SayHelloClientStream(IAsyncStreamReader <HelloRequest> requestStream, ServerCallContext context)
        {
            string msg = string.Empty;

            //var current = new HelloRequest();
            while (await requestStream.MoveNext())
            {
                msg += requestStream.Current.Name;
            }

            var task = new Task <HelloReply>(() =>
            {
                var reply = new HelloReply()
                {
                    Message = "回复:" + msg//current.Name
                };
                return(reply);
            });

            task.Start();

            var result = await task;

            return(result);
        }
        static async Task Main(string[] args)
        {
            GrpcGreeter.HelloReply reply = null;
            // This switch must be set before creating the GrpcChannel/HttpClient.
            AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);


            var insecureChannel = GrpcChannel.ForAddress("http://localhost:4700", new GrpcChannelOptions
            {
                Credentials = ChannelCredentials.Insecure,
                HttpClient  = new System.Net.Http.HttpClient()
                {
                    BaseAddress = new Uri("http://localhost:4700")
                }
            });
            var serverReflectionClient = new ServerReflectionClient(insecureChannel);
            var response = await SingleRequestAsync(serverReflectionClient, new ServerReflectionRequest
            {
                ListServices = "" // Get all services
            });

            Console.WriteLine("Services:");
            foreach (var item in response.ListServicesResponse.Service)
            {
                Console.WriteLine("- " + item.Name);
            }

            var insecureclient = new Greeter.GreeterClient(insecureChannel);

            try
            {
                reply = await insecureclient.SayHelloAsync(
                    new HelloRequest { Name = "GreeterClient" });
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception: " + ex.Message);
            }

            var secureChannel = GrpcChannel.ForAddress("https://localhost:4701");
            var secureClient  = new Greeter.GreeterClient(secureChannel);

            reply = await secureClient.SayHelloAsync(
                new HelloRequest { Name = "GreeterClient" });

            Console.WriteLine("Greeting: " + reply.Message);

            try
            {
                reply = await secureClient.SayHelloThrowAsync(
                    new HelloRequest { Name = "GreeterClient" });
            }
            catch (Grpc.Core.RpcException ex)
            {
                Console.WriteLine("Exception: " + ex.Message);
            }

            Console.WriteLine("Press any key to exit...");
            Console.ReadKey();
        }
Exemple #3
0
        public override Task <HelloReply> SayHello(HelloRequest request, ServerCallContext context)
        {
            var reply = new HelloReply
            {
                Message = "Hello " + request.Name
            };
            var str = request.Name;

            Console.WriteLine(str);
            return(Task.FromResult(reply));
        }
Exemple #4
0
        public override Task <HelloReply> SayHello(HelloRequest request, ServerCallContext context)
        {
            HelloReply helloReply = new HelloReply();

            //string
            helloReply.Message = "Hello " + request.Name;
            //google.protobuf.Timestamp
            helloReply.Start = Timestamp.FromDateTime(DateTime.Now.ToUniversalTime());
            ////google.protobuf.Int32Value
            //helloReply.Age = null;
            ////bytes
            //helloReply.SmallPicture = ByteString.CopyFrom(1);
            ////IList<T>
            //helloReply.Roles.Add("123");
            ////IDictionary<TKey, TValue>
            //helloReply.Attributes.Add("key", "value");

            //--- 无结构的条件消息 ---

            //helloReply.Detail=Any.Pack()

            return(Task.FromResult(helloReply));
        }
Exemple #5
0
        public override async Task SayHelloServerStream(
            HelloRequest request,
            IServerStreamWriter <HelloReply> responseStream,
            ServerCallContext context)
        {
            double i = 0;

            while (!context.CancellationToken.IsCancellationRequested)
            {
                await Task.Delay(500); // Gotta look busy

                HelloReply forecast = new HelloReply
                {
                    Message = "Hello " + request.Name +
                              " Date: " + DateTime.Now +
                              " Response No: " + i++
                };

                _logger.LogInformation("Sending WeatherData response");

                await responseStream.WriteAsync(forecast);
            }
        }