Пример #1
0
        private static async Task AddPhotoRequestAsync(EmployeeService.EmployeeServiceClient client)
        {
            var md = new Metadata();

            md.Add("badgenumber", "1");
            var fs = File.OpenRead(@"emp.jpg");

            using (var call = client.AddPhoto(md))
            {
                var stream = call.RequestStream;
                while (true)
                {
                    var buffer  = new byte[64 * 1024];
                    int numRead = await fs.ReadAsync(buffer, 0, buffer.Length);

                    if (numRead == 0)
                    {
                        break;
                    }
                    if (numRead < buffer.Length)
                    {
                        Array.Resize(ref buffer, numRead);
                    }
                    await stream.WriteAsync(new AddPhotoRequest { Data = ByteString.CopyFrom(buffer) });
                }
                await stream.CompleteAsync();

                var res = await call.ResponseAsync;
                Console.WriteLine(res.IsOk);
            }
        }
Пример #2
0
        public static async Task AddPhotoAsync(EmployeeService.EmployeeServiceClient client)
        {
            try
            {
                if (!NeedToken() || await GetTokenAsync(client))
                {
                    var headers = new Metadata
                    {
                        { "Authorization", $"Bearer {_token}" }
                    };

                    var fs = File.OpenRead("logo.jpg");
                    using var call = client.AddPhoto(headers);

                    var stream = call.RequestStream;

                    while (true)
                    {
                        var buffer     = new byte[1024];
                        var numberRead = await fs.ReadAsync(buffer, 0, buffer.Length);

                        if (numberRead == 0)
                        {
                            break;
                        }
                        if (numberRead < buffer.Length)
                        {
                            Array.Resize(ref buffer, numberRead);
                        }

                        await stream.WriteAsync(new AddPhotoRequest
                        {
                            Data = ByteString.CopyFrom(buffer)
                        });
                    }

                    await stream.CompleteAsync();

                    var response = await call.ResponseAsync;

                    Console.WriteLine(response.IsOk);
                }
            }
            catch (RpcException e)
            {
                Log.Logger.Error($"{JsonConvert.SerializeObject(e.Trailers)}");
                Log.Logger.Error(e.Message);
            }
        }
Пример #3
0
        public static async Task AddPhotoAsync(EmployeeService.EmployeeServiceClient client)
        {
            //这里也可以传输元数据
            var md = new Metadata
            {
                { "username", "dave" },
                { "role", "administrator" }
            };

            //首先读取数据
            FileStream fs   = File.OpenRead("logo.png");
            var        call = client.AddPhoto(md);
            //创建请求数据流
            var stream = call.RequestStream;

            while (true)
            {
                byte[] buffer = new byte[1024];
                //读取数据长度
                int numRead = await fs.ReadAsync(buffer, 0, buffer.Length);

                //判断是否已经读取完
                if (numRead == 0)
                {
                    break;
                }

                //若读取到长度小于buffer,则将buffer重置为numRead大小
                if (numRead < buffer.Length)
                {
                    Array.Resize(ref buffer, numRead);
                }

                //将图片读写到流中
                await stream.WriteAsync(new AddPhotoRequest { Data = ByteString.CopyFrom(buffer) });
            }

            //告知服务器上传完成
            await stream.CompleteAsync();

            //接收服务器返回的消息
            var res = await call.ResponseAsync;

            Console.WriteLine(res);
        }
Пример #4
0
        public async Task AddPhoto()
        {
            //传入元数据
            var md = new Metadata
            {
                { "username", "jack" },
                { "role", "admin" }
            };

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

            var s = Path.Combine(hostEnvironment.ContentRootPath, "doc", "abc.png");

            FileStream fs = System.IO.File.OpenRead(s);

            using var call = client.AddPhoto(md);
            var stream = call.RequestStream;

            while (true)
            {
                byte[] buffer  = new byte[1024];
                int    numRead = await fs.ReadAsync(buffer, 0, buffer.Length);

                if (numRead == 0)
                {
                    break;
                }
                if (numRead < buffer.Length)
                {
                    Array.Resize(ref buffer, numRead);
                }

                await stream.WriteAsync(new AddPhotoRequest
                {
                    Data = ByteString.CopyFrom(buffer)
                });
            }

            await stream.CompleteAsync();

            var res = await call.ResponseAsync;

            Console.WriteLine(res.IsOk);
        }
Пример #5
0
        /// <summary>
        /// client streaming
        /// </summary>
        /// <param name="client"></param>
        /// <returns></returns>
        public static async Task AddPhotoAsync(EmployeeService.EmployeeServiceClient client)
        {
            var md = new Metadata()
            {
                { "username", "kael.tian" },
                { "age", "18" },
            };
            FileStream fs = File.OpenRead("JOJO.jpg");

            //可以先传metadata.
            using var cal = client.AddPhoto(md);
            var stream = cal.RequestStream;

            while (true)
            {
                byte[] buffer  = new byte[1024 * 2];
                int    numRead = await fs.ReadAsync(buffer, 0, buffer.Length);

                if (numRead == 0)
                {
                    break;
                }

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

                await stream.WriteAsync(new AddPhotoRequest
                {
                    Data = ByteString.CopyFrom(buffer)
                });
            }

            //告诉server端数据已全部传输完毕.
            await stream.CompleteAsync();

            //获取响应
            var res  = await cal.ResponseAsync;
            var isOk = res.IsOk ? "Success" : "Failed";

            Console.WriteLine($"Response:{isOk}");
        }
Пример #6
0
        public static async Task AddPhotoAsync(EmployeeService.EmployeeServiceClient client)
        {
            var md = new Metadata
            {
                { "username", "admin" },
                { "role", "administrator" }
            };

            FileStream fs = File.OpenRead("asd.jpg");

            using var call = client.AddPhoto(md);

            var stream = call.RequestStream;

            while (true)
            {
                byte[] buffer = new byte[1024];

                int numRead = await fs.ReadAsync(buffer, 0, buffer.Length);

                if (numRead == 0)
                {
                    break;
                }

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

                await stream.WriteAsync(new AddPhotoRequest
                {
                    Data = ByteString.CopyFrom(buffer)
                });
            }

            await stream.CompleteAsync();

            var response = await call.ResponseAsync;

            Console.WriteLine(response.IsOk);
        }
Пример #7
0
        //client stream
        static async Task AddPhoto(EmployeeService.EmployeeServiceClient client)
        {
            var md = new Metadata
            {
                new Metadata.Entry("badgenumber", "2080")
            };
            //credits: Photo by Daniel Norris on Unsplash
            var imagePath = Path.Combine(Environment.CurrentDirectory, "Data", "daniel-norris-405371-unsplash.jpg");

            using (var fs = File.OpenRead(imagePath))
                using (var call = client.AddPhoto(headers: md))
                {
                    var stream = call.RequestStream;
                    while (true)
                    {
                        var buffer  = new byte[64 * 1024];// 64kb
                        var numRead = await fs.ReadAsync(buffer, 0, buffer.Length);

                        if (numRead == 0)
                        {
                            break;
                        }

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

                        await stream.WriteAsync(new AddPhotoRequest
                        {
                            Data = Google.Protobuf.ByteString.CopyFrom(buffer)
                        });
                    }

                    await stream.CompleteAsync();

                    var res = await call.ResponseAsync;
                    Console.WriteLine($"Finished uploading, IsOk: {res.IsOk}");
                }
        }
Пример #8
0
        protected async void AddPhoto()
        {
            Response3         = string.Empty; //清空前台显示
            using var channel = GrpcChannel.ForAddress(ServerAdderss);
            var client = new EmployeeService.EmployeeServiceClient(channel);

            // 调用这个存根方法得到的是“AsyncClientStreamingCall类型”
            using var clientStreamingCall = client.AddPhoto();
            // 拿到“请求流”
            var requestStream = clientStreamingCall.RequestStream;

            //向“请求流”中写数据
            await using var fs = File.OpenRead(Request3);
            while (true)
            {
                var buffer = new byte[1024];                               //模拟多次传递,将缓存设置小一点
                var length = await fs.ReadAsync(buffer, 0, buffer.Length); //将数据读取到buffer中

                if (length == 0)                                           //读取完毕
                {
                    break;                                                 //跳出循环
                }
                else if (length < buffer.Length)                           //最后一次读取长度无法填满buffer的长度
                {
                    Array.Resize(ref buffer, length);                      //改变buffer数组的长度
                }
                var streamData = ByteString.CopyFrom(buffer);              //将byte数组数据转成传递时需要的ByteString类型
                                                                           //将ByteString数据写入“请求流”中
                await requestStream.WriteAsync(new AddPhotoRequest { Photo = streamData });
            }

            await requestStream.CompleteAsync();  //告知服务端数据传递完毕

            var response = await clientStreamingCall.ResponseAsync;

            Response3 = response.IsOK ? "congratulations" : "ah oh"; // 将消息显示到前端
        }