示例#1
0
        static async Task Main(string[] args)
        {
            var httpClient = new HttpClient(
                new HttpClientHandler()
            {
                ServerCertificateCustomValidationCallback =
                    HttpClientHandler.DangerousAcceptAnyServerCertificateValidator
            });

            var channel = GrpcChannel.ForAddress("https://localhost:5001",
                                                 new GrpcChannelOptions {
                HttpClient = httpClient
            });

            var client = new CustomerService.CustomerServiceClient(channel);

            var request = new CustomerIdRequest()
            {
                CustId = 1
            };
            var reply = await client.GetCustomerByIdAsync(request);

            Console.WriteLine(reply.FullName);

            Console.ReadLine();
        }
示例#2
0
        private static async Task Main(string[] args)
        {
            Channel channel        = new Channel("localhost:5000", ChannelCredentials.Insecure);
            var     healthClient   = new Health.HealthClient(channel);
            var     healthResponse = await healthClient.CheckAsync(new HealthCheckRequest());

            Console.WriteLine($"Server is: {healthResponse.Status}");

            healthResponse = await healthClient.CheckAsync(new HealthCheckRequest { Service = CustomerService.Descriptor.Name });

            Console.WriteLine($"CustomerService is: {healthResponse.Status}");

            var customerClient   = new CustomerService.CustomerServiceClient(channel);
            var customerResponse = await customerClient.GetCustomerByIdAsync(
                new GetCustomerByIdRequest { Id = 1 },
                new CallOptions(new Metadata {
                { "correlation-id", Guid.NewGuid().ToString() }
            })).ResponseAsync.ConfigureAwait(false);

            Console.WriteLine($"Customer: {customerResponse.Customer.Id} retrieved.");

            customerResponse = await customerClient.GetCustomerById2Async(
                new GetCustomerByIdRequest { Id = 1 },
                new CallOptions(new Metadata {
                { "correlation-id", Guid.NewGuid().ToString() }
            })).ResponseAsync.ConfigureAwait(false);

            Console.WriteLine($"Customer: {customerResponse.Customer.Id} retrieved.");
            var customerResponse2 = customerClient.DeleteCustomerById(new DeleteCustomerByIdRequest {
                Id = 1
            });

            var customerResponse3 = customerClient.ListCustomers(new CustomerSearch {
                FirstName = "test"
            });

            while (await customerResponse3.ResponseStream.MoveNext(CancellationToken.None))
            {
                var response = customerResponse3.ResponseStream.Current;
            }

            await channel.ShutdownAsync().ConfigureAwait(false);

            Console.ReadKey();
        }
示例#3
0
        public async Task ShouldIntercept()
        {
            var channel = new Channel("localhost", 5001, ChannelCredentials.Insecure);

            var options = Options.Create(new CallInterceptorOptions
            {
                ServiceName         = "google.protobuf.CustomerService",
                ResponseType        = "GetCustomerByIdResponse",
                JsonResponseContent = JsonConvert.SerializeObject(new GetCustomerByIdResponse {
                    Customer = new Customer {
                        Id = 1, FirstName = "Ed", LastName = "Torsten"
                    }
                })
            });

            var interceptor = new CallInterceptor(options, channel);

            var client = new CustomerService.CustomerServiceClient(interceptor);
            GetCustomerByIdResponse response = await client.GetCustomerByIdAsync(new GetCustomerByIdRequest());

            Assert.Equal(1, response.Customer.Id);
            Assert.Equal("Ed", response.Customer.FirstName);
            Assert.Equal("Torsten", response.Customer.LastName);
        }