private static async Task SendFile(UploadFileService.UploadFileServiceClient client, IFormFile filePath, string postId) { byte[] buffer; var fileStream = new MemoryStream(); await filePath.CopyToAsync(fileStream); fileStream.Position = 0; try { int length = (int)fileStream.Length; buffer = new byte[length]; int count; int sum = 0; while ((count = await fileStream.ReadAsync(buffer, sum, length - sum)) > 0) { sum += count; } } finally { fileStream.Close(); } var result = await client.SendFileAsync(new Chunk { PostId = postId, Content = ByteString.CopyFrom(buffer) }); Console.WriteLine(result.Success); }
public async Task <IActionResult> OnPost() { Post.Id = Guid.NewGuid(); Post.UserId = User.GetUserId(); var polly = Polly.Policy.Handle <Exception>() .CircuitBreakerAsync(2, TimeSpan.FromSeconds(20)); await polly.ExecuteAsync(async() => { using var channel = GrpcChannel.ForAddress("https://localhost:5303"); var uploadFileClient = new UploadFileService.UploadFileServiceClient(channel); await SendFile(uploadFileClient, Post.File, Post.Id.ToString()); }); var result = await _postClient.Create(Post); if (result == false) { return(Page()); } return(RedirectToPage("./index")); }