缓冲文件以将文件流发送到后端

Buffer files to send a filestream to a backend

我正在为 UiPath RPA 工作流编写我的第一个自定义 Activity,在该工作流中我需要将文件流异步发送到后端。这是我想出的,但我只是觉得这不太管用:

 class SendFiles : AsyncCodeActivity<string>
    {
        private readonly HttpClient client = new HttpClient();
        private readonly string url = "";

        [Category("Input")]
        [RequiredArgument]
        public InArgument<List<string>> Files { get; set; }

        [Category("Input")]
        [RequiredArgument]
        public string BearerToken { get; set; }

        [Category("Output")]
        public OutArgument JsonResult { get; set; }

        protected override IAsyncResult BeginExecute(AsyncCodeActivityContext context, AsyncCallback callback, object state)
        {

            foreach (var filePath in Files.Get(context))
            {
                try
                {
                    using (FileStream fs = File.Open(filePath, FileMode.Open, FileAccess.Read))
                    {
                        HttpContent content = new StreamContent(fs);
                        client.PostAsync(url, content);
                    }
                }
                catch (IOException e)
                {
                    Console.WriteLine(e.Message);
                    throw;
                }
            }
            return null;
        }

        protected override string EndExecute(AsyncCodeActivityContext context, IAsyncResult result)
        {
            throw new NotImplementedException();
        }
    }

发送整批后,我想等待后端处理所有这些文件的结果。我怎样才能做到这一点?

// make outer content
MultipartFormDataContent formdata = new MultipartFormDataContent();

...

// in foreach add every filestream to outer content
FileStream fs = File.Open(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
HttpContent content = new StreamContent(fs);
string name = GetFileName(filePath);
content.Headers.Add("Content-Type", GetFileType(name));
formdata.Add(content, "files", name);

...

// after foreach, send whole outer content in one go
var resultPost = client.PostAsync(url, formdata).Result;
response = resultPost.Content.ReadAsStringAsync().Result;
succesfullRequest = resultPost.IsSuccessStatusCode;