コード例 #1
0
        public IActionResult Index()
        {
            var          client   = new EmployeeService.EmployeeServiceClient(channel);
            EmployeeList employee = client.GetAll(new Empty());

            return(View(employee));
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: Christopher-Shi/Chris.Grpc
        public static async Task GetAllAsync(EmployeeService.EmployeeServiceClient client)
        {
            try
            {
                if (!NeedToken() || await GetTokenAsync(client))
                {
                    var headers = new Metadata
                    {
                        { "Authorization", $"Bearer {_token}" }
                    };

                    using var call = client.GetAll(new GetAllRequest(), headers);
                    var responseStream = call.ResponseStream;
                    while (await responseStream.MoveNext())
                    {
                        Console.WriteLine(responseStream.Current.Employee);
                    }
                }
            }
            catch (RpcException e)
            {
                Log.Logger.Error($"{JsonConvert.SerializeObject(e.Trailers)}");
                Log.Logger.Error(e.Message);
            }
        }
コード例 #3
0
ファイル: Program.cs プロジェクト: WuHaaaaa/.NETCoreDemo
        private static async Task GetAllAsync(EmployeeService.EmployeeServiceClient client)
        {
            using var call = client.GetAll(new GetAllRequest());
            var responseStream = call.ResponseStream;

            while (await responseStream.MoveNext())
            {
                Console.WriteLine(responseStream.Current.Employee);
            }
        }
コード例 #4
0
ファイル: Program.cs プロジェクト: yangdengfeng/grpc
        public static async Task GetAllAsync(EmployeeService.EmployeeServiceClient client)
        {
            using var call = client.GetAll(new GetAllRequest());

            var responseSteam = call.ResponseStream;

            while (await responseSteam.MoveNext())
            {
                Log.Debug($"response message : {responseSteam.Current.Employee}");
                Console.WriteLine($"response message : {responseSteam.Current.Employee}");
            }
        }
コード例 #5
0
        // server stream
        static async Task GetAll(EmployeeService.EmployeeServiceClient client)
        {
            Console.WriteLine($"GetAll - Server streaming sample");

            using (var call = client.GetAll(new GetAllRequest()))
            {
                var responserStream = call.ResponseStream;
                while (await responserStream.MoveNext())
                {
                    Console.WriteLine(responserStream.Current.Employee);
                }
            }
        }
コード例 #6
0
        public async Task <List <Models.Employee> > GetAllAsync()
        {
            var result = new List <Models.Employee>();

            using (var call = _employeeServiceClient.GetAll(new ProtocolBuffers.GetAllRequest()))
            {
                while (await call.ResponseStream.MoveNext())
                {
                    result.Add(_employeeConverter.ToModel(call.ResponseStream.Current.Employee));
                }
            }

            return(result);
        }
コード例 #7
0
 static async Task GetAllAsync(EmployeeService.EmployeeServiceClient client)
 {
     if (!NeedToken() || await CreateTokenAsync(client))
     {
         var header = new Metadata
         {
             { "Authorization", $"Bearer {_token}" }
         };
         using var call = client.GetAll(new GetAllRequest(), header);
         var responseSream = call.ResponseStream;
         while (await responseSream.MoveNext())
         {
             Console.WriteLine(responseSream.Current.Employee);
         }
     }
 }
コード例 #8
0
        public async Task <List <Employee> > GetAll()
        {
            //传入元数据

            using var channel = GrpcChannel.ForAddress("http://localhost:6001");
            var client = new EmployeeService.EmployeeServiceClient(channel);

            List <Employee> employees = new List <Employee>();

            using var call = client.GetAll(new GetAllRequest());
            var responseStream = call.ResponseStream;

            while (await responseStream.MoveNext())
            {
                employees.Add(responseStream.Current.Employee);
            }

            return(employees);
        }
コード例 #9
0
        static async Task Main(string[] args)
        {
            var metaDatas = new Metadata
            {
                { "username", "Leon" },
                { "role", "administrator" },
            };

            using var channel = GrpcChannel.ForAddress("https://localhost:5001");
            var client = new EmployeeService.EmployeeServiceClient(channel);

            var response = await client.GetByNoAsync(new GetByNoRequest()
            {
                No = 102
            }, metaDatas);

            Console.WriteLine(response.Employee.FirstName);

            using var getAllCall = client.GetAll(new GetAllRequest());
            var responseStream = getAllCall.ResponseStream;

            while (await responseStream.MoveNext())
            {
                Console.WriteLine(responseStream.Current.Employee.FirstName);
            }

            FileStream fileStream = File.OpenRead("Dream_x280.jpg");

            using var uploadPhotoCall = client.UploadPhoto();
            while (fileStream.Position < fileStream.Length)
            {
                var buffer = new byte[1024];
                int length = await fileStream.ReadAsync(buffer, 0, buffer.Length);

                if (length < buffer.Length)
                {
                    Array.Resize(ref buffer, length);
                }

                await uploadPhotoCall.RequestStream.WriteAsync(
                    new UploadPhotoRequest()
                {
                    Data = ByteString.CopyFrom(buffer)
                });
            }
            await uploadPhotoCall.RequestStream.CompleteAsync();


            using var saveAllCall = client.SaveAll();
            for (int index = 0; index < 5; index++)
            {
                await saveAllCall.RequestStream.WriteAsync(new EmployeeRequest()
                {
                    Employee = new Employee()
                    {
                        Id        = 10 + index,
                        FirstName = "Employee",
                        LastName  = index.ToString(),
                        No        = 110 + index,
                        Salary    = 10000 + index * 1000,
                    }
                });
            }
            await saveAllCall.RequestStream.CompleteAsync();

            while (await saveAllCall.ResponseStream.MoveNext())
            {
                Console.WriteLine(saveAllCall.ResponseStream.Current.Employee.Id);
            }

            Console.ReadLine();
        }