public static async Task ExecuteOperation(Greeter.GreeterClient greeterClient, int i) { Stopwatch stopwatch = Stopwatch.StartNew(); var r = await greeterClient.SayHelloAsync(new HelloRequest() { Name = "Test" + i }).ResponseAsync; stopwatch.Stop(); OperationCount++; Console.WriteLine($"Ms: {stopwatch.ElapsedMilliseconds} Elapsed: {stopwatch.Elapsed}"); }
static async Task Main(string[] args) { var input = new HelloRequest { Name = "test" }; var channel = GrpcChannel.ForAddress("https://localhost:5001/"); var client = new Greeter.GreeterClient(channel); var reply = await client.SayHelloAsync(input); Console.WriteLine(reply.Message); Console.ReadLine(); }
static async Task Main(string[] args) { var channel = GrpcChannel.ForAddress("https://localhost:5001"); var connection = new Greeter.GreeterClient(channel); var connection2 = new Client.ClientClient(channel); do { Console.WriteLine(" 1 - Per Salutare \n 2 - Per ricevere \n 3 - Per inviare"); switch (Console.ReadLine()) { case "1": var input1 = new HelloRequest { Name = "Step" }; var reply = await connection.SayHelloAsync(input1); Console.ForegroundColor = ConsoleColor.Cyan; Console.WriteLine(reply.Message); break; case "2": for (int i = 0; i < 5; i++) { var clientRequested = new ClientLookupModel { Id = i }; var reply2 = await connection2.GetClientInfoAsync(clientRequested); Console.ForegroundColor = ConsoleColor.Cyan; Console.WriteLine($"Age:{reply2.Age}\n FirstName:{reply2.FirstName}\n LastName:{reply2.LastName}\n IsItWarner?:{reply2.IsIt}"); } break; case "3": var input3 = new ClientModel { Age = 10, FirstName = "Dot", LastName = "Princess Angelina Contessa Louisa Francesca Banana Fanna Bo Besca III", IsIt = true }; var reply3 = await connection2.SaveClientInfoAsync(input3); Console.ForegroundColor = ConsoleColor.Cyan; Console.WriteLine(reply3.Success); break; } Console.ResetColor(); Console.WriteLine("\n\n"); }while (true); }
public static async Task Main(string[] args) { var channel = GrpcChannel.ForAddress("https://localhost:5001"); Greeter.GreeterClient client = new Greeter.GreeterClient(channel); for (int i = 0; i < 2; i++) { await ExecuteParallel(client); await Task.Delay(1000); } Console.WriteLine(OperationCount); }
static void Main(string[] args) { //1、建立连接 //GrpcChannel grpcChannel = GrpcChannel.ForAddress("https://localhost:5002"); //// 2、客户端创建 //GreeterClient greeterClient = new Greeter.GreeterClient(grpcChannel); ////// 3、开始调用 //HelloReply helloReply = greeterClient.SayHello(new HelloRequest() //{ // Name = "grpc客户端" //}); //// 3、开始调用 //GetUserListResp resp = greeterClient.GetUserList(new GetUserListReq() //{ // Name = "GetUserList" //}); //// 4、打印 //Console.WriteLine($"返回值打印:1:{helloReply.Message};2:{resp.UserListInfo}"); //grpcChannel.Dispose(); using (GrpcChannel grpcChannel = GrpcChannel.ForAddress("https://localhost:5002")) { // 2、客户端创建 GreeterClient greeterClient = new Greeter.GreeterClient(grpcChannel); //// 3、开始调用 HelloReply helloReply = greeterClient.SayHello(new HelloRequest() { Name = "grpc客户端" }); // 3、开始调用 GetUserListResp resp = greeterClient.GetUserList(new GetUserListReq() { Name = "GetUserList" }); // 4、打印 Console.WriteLine($"返回值打印:1:{helloReply.Message};2:{resp.UserListInfo}"); } }
static async Task Main(string[] args) { var input = new HelloRequest { Name = "Fred" }; var channel = GrpcChannel.ForAddress("https://localhost:5001"); var client = new Greeter.GreeterClient(channel); var reply = await client.SayHelloAsync(input); Console.WriteLine(reply.Message); channel = GrpcChannel.ForAddress("https://localhost:5001"); var customerClient = new Customer.CustomerClient(channel); var clientRequested = new CustomerLookupModel { UserId = 2 }; var customer = await customerClient.GetCustomerInfoAsync(clientRequested); Console.WriteLine($"{customer.FirstName} { customer.LastName }"); Console.WriteLine(); Console.WriteLine("New Customer list"); Console.WriteLine(); using (var call = customerClient.GetNewCustomers(new NewCustomerRequest())) { while (await call.ResponseStream.MoveNext()) { var currentCustomer = call.ResponseStream.Current; Console.WriteLine($"{currentCustomer.FirstName} { currentCustomer.LastName } {currentCustomer.EmailAdress}"); } } Console.ReadLine(); }
private static async Task Main(string[] args) { Console.WriteLine("Hello World!"); var channel = GrpcChannel.ForAddress("https://localhost:5001"); var client = new Greeter.GreeterClient(channel); var reply = await client.SayHelloAsync(new HelloRequest { Name = "悟空" }); Console.WriteLine("调用Greeter服务 : " + reply.Message); var ss = new MyMath.MyMathClient(channel); var ssReply = await ss.AddAsync(new AddRquest { X = 10, Y = 11 }); Console.WriteLine("调用MyMath服务 : " + ssReply.Sum); Console.ReadLine(); }
static async Task Main(string[] args) { var handler = new HttpClientHandler(); var certificate = new X509Certificate2("clientl4.pfx", "1234"); handler.ClientCertificates.Add(certificate); var channel = GrpcChannel.ForAddress("https://localhost:44379", new GrpcChannelOptions { HttpClient = new HttpClient(handler) }); var client = new Greeter.GreeterClient(channel); var reply = await client.SayHelloAsync(new HelloRequest { Name = "GreeterClient" }); Console.WriteLine("Greeting: " + reply.Message); Console.WriteLine("Press any key to exit..."); Console.ReadKey(); }
static async Task Main(string[] args) { var personGenerator = new PersonGenerator(); var person = personGenerator.GetNewPerson(); var channel = GrpcChannel.ForAddress("https://localhost:5001"); var client = new Greeter.GreeterClient(channel); Console.WriteLine("Hello from client"); var input = new HelloRequest { Name = person.FullName }; var reply = await client.SayHelloAsync(input); Console.WriteLine(reply.Message); var customerService = new Customer.CustomerClient(channel); var customerId = new CustomerLookupModel { UserId = new Random().Next(1, 100) }; var userFound = customerService.GetCustomerInfo(customerId); Console.WriteLine($"Name: {userFound.Fullname}\nEmailAddress:{userFound.EmailAddress}\nGender:{userFound.Gender}\nId:{userFound.UserId}"); //var input2 = new CrowdLookupModel { UserIds = new Google.Protobuf.Collections.RepeatedField<CustomerLookupModel> { customerId, customerId } }; var cancel = new CancellationToken(); using (var call = customerService.GetCrowdInfoFromId(customerId)) { while (await call.ResponseStream.MoveNext(cancel)) { var current = call.ResponseStream.Current; Console.WriteLine($"Name: {current.Fullname}"); } } Console.ReadLine(); }
static async Task RunAsync(string endpoint, string prefix) { // The port number(5001) must match the port of the gRPC server. using var channel = GrpcChannel.ForAddress(endpoint); var client = new Greeter.GreeterClient(channel); var reply = await client.SayHelloAsync(new HelloRequest { Name = $"{prefix} GreeterClient" }); Console.WriteLine("Greeting: " + reply.Message); // duplex var requestHeaders = new Metadata { { "x-host-port", "10-0-0-10" }, }; using var streaming = client.StreamingBothWays(requestHeaders); var readTask = Task.Run(async() => { await foreach (var response in streaming.ResponseStream.ReadAllAsync()) { Console.WriteLine(response.Message); } }); var i = 0; while (i++ < 100) { await streaming.RequestStream.WriteAsync(new HelloRequest { Name = $"{prefix} {i}", }); await Task.Delay(TimeSpan.FromSeconds(1)); } await streaming.RequestStream.CompleteAsync(); await readTask; }